Skip to content

Commit

Permalink
eclipse-archived#190 In progress TqCL - interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
rtotaro committed Jan 18, 2017
1 parent c7e6956 commit e0c60f7
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
*******************************************************************************/
package org.eclipse.triquetrum.commands.api.services;

public interface ModelBuilderService<CompositeActor,Actor,Port,Parameter,Director> {
import java.util.Map;

public interface ModelBuilderService<CompositeActor,Actor> {

public Class<?> getSupportedModelClass();

public Actor insertActor(CompositeActor parent,String actorclass) throws TcQLException;
public CompositeActor createNewModel(String modelName);

public Director insertDirector(CompositeActor actor,String directorClass) throws TcQLException;
public boolean insertActor(CompositeActor parent,String actorName,String actorclass,Map<String,String> parameters);

public Port insertPort(Actor actor, String portClass) throws TcQLException;
public boolean insertDirector(CompositeActor actor,String directorClass, String entityClass, Map<String, String> params);

public Port insertParameter(Actor actor, String parameterClass) throws TcQLException;
public boolean insertPort(Actor actor, String portClass, String entityClass, Map<String, String> params);

//TODO: add other needed methods
public boolean insertParameter(Actor actor, String parameterClass, String entityClass, Map<String, String> params);

public CompositeActor getParent(Actor actor);

public Actor getChild(String name);

public boolean connect(Object currentActor, String from, String to);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void launch(ISelection selection, String mode) {
IFile file = (IFile) firstElement;
java.net.URI rawLocationURI = file.getRawLocationURI();
TqclInterpreter interpreter = new TqclInterpreter();
interpreter.interpret(file.getContents(), rawLocationURI);
interpreter.interpret(file.getName(),file.getContents(), rawLocationURI);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.eclipse.triquetrum.commands.interpreter;

import java.util.Arrays;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.tqcl.Connect;
import org.eclipse.triquetrum.commands.tqcl.ConnectionPort;
import org.eclipse.triquetrum.commands.tqcl.TqclPackage;

public class ConnectInterpreterComponent implements TqclInterpreterComponent {

@Override
public void interpret(EObject element, InterpretContext context) {
if (element instanceof Connect) {
Connect connect = (Connect) element;
EList<ConnectionPort> from = connect.getFrom();
EList<ConnectionPort> to = connect.getTo();
for (ConnectionPort connectionPortFrom : from) {
for (ConnectionPort connectionPortTo : to) {
context.getModelBuilderService().connect(context.getCurrentActor(),createPortPath(connectionPortFrom),createPortPath(connectionPortTo));
}
}

}

}

private String createPortPath(ConnectionPort port) {
return port.getActor().getName()+"."+port.getPort();
}

@Override
public List<EClass> intepretedEClasses() {
return Arrays.asList(TqclPackage.Literals.CONNECT);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.eclipse.triquetrum.commands.interpreter;

import java.util.Arrays;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.tqcl.Go;
import org.eclipse.triquetrum.commands.tqcl.Insert;
import org.eclipse.triquetrum.commands.tqcl.TqclPackage;

public class GoInterpreterComponent implements TqclInterpreterComponent {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void interpret(EObject element, InterpretContext context) {
if (element instanceof Go) {
Go go = (Go) element;
String direction = go.getDirection();

switch (direction) {
case "into":
Insert actor = go.getActor();
String name = actor.getName();
context.setCurrentActor(context.getModelBuilderService().getChild(name));
break;
case "out":
context.setCurrentActor(context.getModelBuilderService().getParent(context.getCurrentActor()));
break;

default:
break;
}
}
}

@Override
public List<EClass> intepretedEClasses() {
return Arrays.asList(TqclPackage.Literals.GO);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.eclipse.triquetrum.commands.interpreter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.api.services.TcQLException;
import org.eclipse.triquetrum.commands.tqcl.Category;
import org.eclipse.triquetrum.commands.tqcl.Insert;
import org.eclipse.triquetrum.commands.tqcl.Parameter;
import org.eclipse.triquetrum.commands.tqcl.TqclPackage;
import org.eclipse.triquetrum.commands.validation.TqCLUtils;

public class InsertInterpreterComponent implements TqclInterpreterComponent {

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void interpret(EObject element, InterpretContext context) {
if (context instanceof Insert) {
Insert insert = (Insert) context;
Category category = insert.getCategory();
String entityClass = insert.getEntityClass();
String name = insert.getName();
Map<String, String> params = extractParameterMap(insert);
switch (category) {
case ACTOR:
context.getModelBuilderService().insertActor(context.getCurrentActor(), name, entityClass, params);
break;
case DIRECTOR:
context.getModelBuilderService().insertDirector(context.getCurrentActor(), name, entityClass, params);
break;
case PARAMETER:
context.getModelBuilderService().insertParameter(context.getCurrentActor(), name, entityClass, params);
break;
case PORT:
context.getModelBuilderService().insertPort(context.getCurrentActor(), name, entityClass, params);
break;
}

}
}

private Map<String, String> extractParameterMap(Insert insert) {
Map<String, String> params = new HashMap<>();
for (Parameter parameter : insert.getParameters()) {
params.put(TqCLUtils.cleanParameterName(parameter.getId()), parameter.getValue());
}
return params;
}

@Override
public List<EClass> intepretedEClasses() {
return Arrays.asList(TqclPackage.Literals.INSERT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.eclipse.triquetrum.commands.interpreter;

import org.eclipse.triquetrum.commands.api.services.ModelBuilderService;

public class InterpretContext<CompositeActor,Actor> {

private ModelBuilderService<CompositeActor,Actor> modelBuilderService;

private TqclInterpreter<CompositeActor,Actor> interpreter;

private CompositeActor model;

private Actor currentActor;

public InterpretContext(TqclInterpreter<CompositeActor,Actor> interpreter,CompositeActor model,ModelBuilderService<CompositeActor,Actor> modelBuilderService) {
super();
this.interpreter = interpreter;
this.model = model;
this.modelBuilderService = modelBuilderService;
}

public TqclInterpreter<CompositeActor,Actor> getInterpreter() {
return interpreter;
}

public ModelBuilderService<CompositeActor,?> getModelBuilderService() {
return modelBuilderService;
}
public CompositeActor getModel() {
return model;
}

public Actor getCurrentActor() {
return currentActor;
}

public void setCurrentActor(Actor currentActor) {
this.currentActor = currentActor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,60 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.TqclStandaloneSetup;
import org.eclipse.triquetrum.commands.api.services.ModelBuilderService;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.XtextResource;

import com.google.inject.Injector;

public class TqclInterpreter {
public class TqclInterpreter<CompositeActor,Actor> {

public void interpret(InputStream script, java.net.URI uri) {
private static Map<EClass, TqclInterpreterComponent> components = new HashMap<>();

static
{
addComponent(new TriquetrumScriptInterpreterComponent());
addComponent(new InsertInterpreterComponent());
addComponent(new GoInterpreterComponent());
addComponent(new ConnectInterpreterComponent());
}

private static void addComponent(TqclInterpreterComponent component){
for (EClass eClass : component.intepretedEClasses()) {
components.put(eClass, component);
}
}

public void interpret(String modelName,InputStream script, java.net.URI uri) {
try {
Injector injector = new TqclStandaloneSetup().createInjectorAndDoEMFRegistration();
IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class);

XtextResource resource = (XtextResource) resourceFactory.createResource(URI.createURI(uri.getPath()));
resource.load(script, null);
IParseResult parseResult = resource.getParseResult();
EObject rootASTElement = parseResult.getRootASTElement();
System.out.println("");
EObject root = parseResult.getRootASTElement();
//TODO:get model builder
ModelBuilderService<CompositeActor,Actor> modelBuilderService = null;
CompositeActor model = modelBuilderService.createNewModel(modelName);
interpret(root, new InterpretContext<CompositeActor,Actor>(this,model,modelBuilderService));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

void interpret(EObject element, InterpretContext<CompositeActor,Actor> context)
{
components.get(element.eClass()).interpret(element, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.eclipse.triquetrum.commands.interpreter;

import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;

public interface TqclInterpreterComponent {

void interpret(EObject element, InterpretContext context);

List<EClass> intepretedEClasses();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.eclipse.triquetrum.commands.interpreter;

import java.util.Arrays;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.tqcl.Command;
import org.eclipse.triquetrum.commands.tqcl.TqclPackage;
import org.eclipse.triquetrum.commands.tqcl.TriquetrumScript;

public class TriquetrumScriptInterpreterComponent implements TqclInterpreterComponent {

@Override
public void interpret(EObject element, InterpretContext context) {
if (element instanceof TriquetrumScript) {
TriquetrumScript script = (TriquetrumScript) element;
EList<Command> commands = script.getCommands();

for (Command command : commands) {
context.getInterpreter().interpret(command, context);
}
}
}

@Override
public List<EClass> intepretedEClasses() {
return Arrays.asList(TqclPackage.Literals.TRIQUETRUM_SCRIPT);
}

}

0 comments on commit e0c60f7

Please sign in to comment.