Przeglądaj źródła

Adopted ExpressionBuilder to Xtext parser

Andreas Mülder 14 lat temu
rodzic
commit
65286a87bf
17 zmienionych plików z 1087 dodań i 719 usunięć
  1. 2 1
      plugins/org.yakindu.sct.simulation.runtime/META-INF/MANIFEST.MF
  2. 5 5
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/GuardExpression.java
  3. 0 224
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/Statechart.java
  4. 5 5
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/TimeEventExpression.java
  5. 63 8
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/builder/SGraphBuilder.java
  6. 4 4
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Assign.java
  7. 9 9
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/BinaryOperation.java
  8. 11 11
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Conditional.java
  9. 1 1
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Constant.java
  10. 1 1
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Expression.java
  11. 7 7
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Trigger.java
  12. 5 5
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/UnaryOperation.java
  13. 1 1
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/VariableRef.java
  14. 242 219
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/builder/ExpressionBuilder.java
  15. 421 0
      plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/builder/STextBuilder.java
  16. 3 6
      test-plugins/org.yakindu.sct.simulation.runtime.test/src/org/yakindu/sct/simulation/runtime/sgraph/builder/test/StatechartBuilderTest.java
  17. 307 212
      test-plugins/org.yakindu.sct.simulation.runtime.test/src/org/yakindu/sct/simulation/runtime/stext/builder/test/BuilderTest.java

+ 2 - 1
plugins/org.yakindu.sct.simulation.runtime/META-INF/MANIFEST.MF

@@ -7,7 +7,8 @@ Bundle-Vendor: YAKINDU.org
 Require-Bundle: org.yakindu.sct.model.sgraph;bundle-version="1.0.0",
  org.eclipse.xtext;bundle-version="2.0.0",
  org.eclipse.emf.mwe.core;bundle-version="1.1.0",
- org.yakindu.sct.model.stext;bundle-version="1.0.0"
+ org.yakindu.sct.model.stext;bundle-version="1.0.0",
+ org.yakindu.sct.model.stext.ui;bundle-version="1.0.0"
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-ActivationPolicy: lazy
 Export-Package: org.yakindu.sct.simulation.runtime,

+ 5 - 5
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/GuardExpression.java

@@ -12,19 +12,19 @@ package org.yakindu.sct.simulation.runtime.sgraph;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 import org.yakindu.sct.simulation.runtime.sgraph.RTGuard;
-import org.yakindu.sct.simulation.runtime.stext.Expression;
+import org.yakindu.sct.simulation.runtime.stext.RTExpression;
 
 
 public class GuardExpression extends RTGuard {
 
-	private Expression expression;
+	private RTExpression expression;
 	private ExecutionScope scope;
 	
 	public GuardExpression() {
 		super();
 	}
 
-	public GuardExpression(Expression expression, ExecutionScope scope) {
+	public GuardExpression(RTExpression expression, ExecutionScope scope) {
 		super();
 		this.expression = expression;
 		this.scope = scope;
@@ -32,11 +32,11 @@ public class GuardExpression extends RTGuard {
 
 
 	
-	public Expression getExpression() {
+	public RTExpression getExpression() {
 		return expression;
 	}
 
-	public void setExpression(Expression expression) {
+	public void setExpression(RTExpression expression) {
 		this.expression = expression;
 	}
 

+ 0 - 224
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/Statechart.java

@@ -1,224 +0,0 @@
-/**
- * Copyright (c) 2010 committers of YAKINDU and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     committers of YAKINDU - initial API and implementation
- */
-package org.yakindu.sct.simulation.runtime.sgraph;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.yakindu.sct.simulation.runtime.ExecutionScope;
-import org.yakindu.sct.simulation.runtime.sgraph.AbstractStatechart;
-import org.yakindu.sct.simulation.runtime.sgraph.CompoundState;
-import org.yakindu.sct.simulation.runtime.sgraph.Event;
-import org.yakindu.sct.simulation.runtime.sgraph.Node;
-import org.yakindu.sct.simulation.runtime.sgraph.Region;
-import org.yakindu.sct.simulation.runtime.sgraph.SignalEvent;
-import org.yakindu.sct.simulation.runtime.sgraph.State;
-import org.yakindu.sct.simulation.runtime.sgraph.TimingService;
-import org.yakindu.sct.simulation.runtime.sgraph.Transition;
-import org.yakindu.sct.simulation.runtime.stext.Variable;
-
-
-public class Statechart extends AbstractStatechart implements ExecutionScope {
-
-
-	protected Map<Object, Object> aliasToElementMap = new HashMap<Object, Object>();
-	protected Map<Object, Object> elementToAliasMap = new HashMap<Object, Object>();
-
-	protected List<SignalEvent> events = new ArrayList<SignalEvent>(); 
-	protected Map<String, SignalEvent> eventMap = new HashMap<String, SignalEvent>();
-
-	protected List<Variable> variables = new ArrayList<Variable>(); 
-	protected Map<String, Variable> variableMap = new HashMap<String, Variable>();
-		
-	protected Set<State> currentStateConfigurartion = new HashSet<State>();
-	
-	protected StatechartListener listener;
-	
-	
-	public Statechart(String id) {
-		super(id);
-	}
-
-	public Statechart(String id, TimingService timingService) {
-		super(id, timingService);
-	}
-
-	
-	public TimingService getTimingService() {
-		return timingService;
-	}
-	
-	public void setTimingService(TimingService ts) {
-		timingService = ts;
-	}
-	
-	
-	public StatechartListener getListener() {
-		return listener;
-	}
-
-	public void setListener(StatechartListener listener) {
-		this.listener = listener;
-	}
-
-	/**
-	 * Adds a signal event definition to the interface of the statechart. 
-	 * only those events, that are registered should be used during operation.
-	 *  
-	 * @param event
-	 */
-	public void addSignalEvent(SignalEvent event) {
-		events.add(event);
-		eventMap.put(event.getId(), event);
-	}
-	
-	/**
-	 * Gets a signal event by its id.
-	 * 
-	 * @param id
-	 * @return the signal event
-	 */
-	public SignalEvent getSignalEvent(String id) {
-		return eventMap.get(id);
-	}
-	
-	public Set<Event> getRaisedEvents() {
-		return Collections.unmodifiableSet(raisedEvents);
-	}
-
-	/**
-	 * @return The list of all defined signal events of this statechart.
-	 */
-	public List<SignalEvent> getSignalEvents() {
-		return Collections.unmodifiableList(events);
-	}
-	
-	
-	public void addVariable(Variable var) {
-		variables.add(var);
-		variableMap.put(var.getName(), var);
-	}
-
-	
-	public Object getValue(String varName) {
-		Variable var = getVariable(varName);
-		return (var != null) ? var.getValue() : null ;		
-	}
-
-	
-	public Variable getVariable(String varName) {
-		return variableMap.get(varName);
-	}
-	
-	public List<Variable>  getVariables() {
-		return Collections.unmodifiableList(variables);
-	}
-
-	
-	protected void stateLeft(State state) {
-		currentStateConfigurartion.remove(state);
-		if (listener != null) listener.stateLeft(state);
-	}
-	
-	protected void stateEntered(State state) {
-		currentStateConfigurartion.add(state);
-		if (listener != null) listener.stateEntered(state);
-	}
-	
-	protected void transitionFired(Transition trans) {
-		if (listener != null) listener.transitionFired(trans);
-	}
-	
-	public Set<State> getCurrentStateConfiguration() {
-		return Collections.unmodifiableSet(currentStateConfigurartion);
-	}
-	
-	
-	public void raise(String signal) {
-		setEvent(eventMap.get(signal));
-	}
-
-	public void reset(String signal) {
-		raisedEvents.remove(eventMap.get(signal));
-	}
-
-	
-	public void call(String procedureId) {
-		// TODO Auto-generated method stub		
-	}
-
-	
-	/**
-	 * If a node is registered with a key then it can be accessed by the key using this method.
-	 * Before a node can be accessed this way it must be registered using {@link #registerNode}. 
-	 * @param alias
-	 * @return the node object that is registered for the given key.
-	 */
-	public Object getElementByAlias(Object alias) {
-		return aliasToElementMap.get(alias);
-	}
-	
-	/**
-	 * If a node is registered with a key then it can be accessed by the key using this method.
-	 * Before a node can be accessed this way it must be registered using {@link #registerNode}. 
-	 * @param element
-	 * @return the node object that is registered for the given key.
-	 */
-	public Object getAliasByElement(Object element) {
-		return elementToAliasMap.get(element);
-	}
-	
-	/**
-	 * Nodes can optionally be regitered with a key for later lookup.
-	 * 
-	 * @param key the key object
-	 * @param element the node
-	 */
-	public void defineAlias(Object alias, Object element) {
-		aliasToElementMap.put(alias, element);
-		elementToAliasMap.put(element, alias);
-	}
-
-	
-	public List<Region> getAllRegions() {
-		List<Region> allRegions = new ArrayList<Region>();
-		
-		allRegions.addAll(getRegions());
-		for (Region region : getRegions()) {
-			addAllRegions(allRegions, region);
-		}
-
-		
-		return allRegions;
-	}
-
-	
-	protected void addAllRegions(List<Region> regions, Region region) {
-		for (Node node : region.getNodes()) {
-			if (node instanceof CompoundState) addAllRegions(regions, (CompoundState) node);
-		}
-	}
-	
-	protected void addAllRegions(List<Region> regions, CompoundState cState) {
-		regions.addAll(cState.getRegions());
-		
-		for (Region region : cState.getRegions()) {
-			addAllRegions(regions, region);
-		}
-	}
-	
-	
-}

+ 5 - 5
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/TimeEventExpression.java

@@ -12,14 +12,14 @@ package org.yakindu.sct.simulation.runtime.sgraph;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 import org.yakindu.sct.simulation.runtime.sgraph.RTTimeEvent;
-import org.yakindu.sct.simulation.runtime.stext.Trigger;
+import org.yakindu.sct.simulation.runtime.stext.RTTrigger;
 
 public class TimeEventExpression extends org.yakindu.sct.simulation.runtime.sgraph.RTTimeEvent {
 
-	protected Trigger.TimeEvent timeEvent;
+	protected RTTrigger.TimeEvent timeEvent;
 	protected ExecutionScope scope;
 
-	public TimeEventExpression(String id, Trigger.TimeEvent timeEvent, ExecutionScope scope) {
+	public TimeEventExpression(String id, RTTrigger.TimeEvent timeEvent, ExecutionScope scope) {
 		super(id);
 		this.timeEvent = timeEvent;
 		this.scope = scope;
@@ -27,13 +27,13 @@ public class TimeEventExpression extends org.yakindu.sct.simulation.runtime.sgra
 
 
 		
-	public Trigger.TimeEvent getTimeEvent() {
+	public RTTrigger.TimeEvent getTimeEvent() {
 			return timeEvent;
 		}
 
 
 
-	public void setTimeEvent(Trigger.TimeEvent timeEvent) {
+	public void setTimeEvent(RTTrigger.TimeEvent timeEvent) {
 			this.timeEvent = timeEvent;
 		}
 

+ 63 - 8
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/sgraph/builder/SGraphBuilder.java

@@ -12,12 +12,16 @@ package org.yakindu.sct.simulation.runtime.sgraph.builder;
 
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 import org.eclipse.emf.common.util.BasicEList;
 import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.xtext.EcoreUtil2;
 import org.yakindu.sct.model.sgraph.Choice;
+import org.yakindu.sct.model.sgraph.Effect;
 import org.yakindu.sct.model.sgraph.Entry;
 import org.yakindu.sct.model.sgraph.Event;
 import org.yakindu.sct.model.sgraph.FinalState;
@@ -26,11 +30,21 @@ import org.yakindu.sct.model.sgraph.Scope;
 import org.yakindu.sct.model.sgraph.State;
 import org.yakindu.sct.model.sgraph.Statechart;
 import org.yakindu.sct.model.sgraph.Transition;
+import org.yakindu.sct.model.sgraph.Trigger;
+import org.yakindu.sct.model.stext.stext.EventSpec;
+import org.yakindu.sct.model.stext.stext.Expression;
+import org.yakindu.sct.model.stext.stext.ReactionTrigger;
+import org.yakindu.sct.model.stext.stext.RegularEventSpec;
+import org.yakindu.sct.model.stext.stext.TimeEventSpec;
 import org.yakindu.sct.model.stext.stext.Type;
 import org.yakindu.sct.model.stext.stext.VariableDefinition;
+import org.yakindu.sct.simulation.runtime.sgraph.ActionStatement;
+import org.yakindu.sct.simulation.runtime.sgraph.GuardExpression;
 import org.yakindu.sct.simulation.runtime.sgraph.PseudostateKind;
+import org.yakindu.sct.simulation.runtime.sgraph.RTAction;
 import org.yakindu.sct.simulation.runtime.sgraph.RTCompoundState;
 import org.yakindu.sct.simulation.runtime.sgraph.RTFinalState;
+import org.yakindu.sct.simulation.runtime.sgraph.RTGuard;
 import org.yakindu.sct.simulation.runtime.sgraph.RTNode;
 import org.yakindu.sct.simulation.runtime.sgraph.RTPseudostate;
 import org.yakindu.sct.simulation.runtime.sgraph.RTRegion;
@@ -38,10 +52,14 @@ import org.yakindu.sct.simulation.runtime.sgraph.RTSignalEvent;
 import org.yakindu.sct.simulation.runtime.sgraph.RTSimpleState;
 import org.yakindu.sct.simulation.runtime.sgraph.RTState;
 import org.yakindu.sct.simulation.runtime.sgraph.RTStatechart;
+import org.yakindu.sct.simulation.runtime.sgraph.RTTimeEvent;
 import org.yakindu.sct.simulation.runtime.sgraph.RTTransition;
+import org.yakindu.sct.simulation.runtime.sgraph.TimeEventExpression;
 import org.yakindu.sct.simulation.runtime.stext.Function;
 import org.yakindu.sct.simulation.runtime.stext.FunctionMethod;
+import org.yakindu.sct.simulation.runtime.stext.RTTrigger.SignalEvent;
 import org.yakindu.sct.simulation.runtime.stext.Variable;
+import org.yakindu.sct.simulation.runtime.stext.builder.STextBuilder;
 
 /**
  * 
@@ -51,6 +69,9 @@ import org.yakindu.sct.simulation.runtime.stext.Variable;
  */
 public class SGraphBuilder extends Function {
 
+	// TODO: Extract interface
+	private STextBuilder sTextBuilder;
+
 	protected static Comparator<RTRegion> regionComparator = new Comparator<RTRegion>() {
 		public int compare(RTRegion o1, RTRegion o2) {
 			return o1.getPriority() - o2.getPriority();
@@ -185,14 +206,48 @@ public class SGraphBuilder extends Function {
 	}
 
 	@FunctionMethod("")
-	public Object build(RTStatechart tParent, Transition sTrans) {
-		RTNode fromNode = (RTNode) tParent
-				.getElementByAlias(sTrans.getSource());
-		RTNode toNode = (RTNode) tParent.getElementByAlias(sTrans.getTarget());
-		// TODO: Trigger, Actions, etc.
-		RTTransition tTrans = new RTTransition("t@", sTrans.getPriority(),
-				null, null, null, null, fromNode, toNode);
-		tParent.defineAlias(sTrans, tTrans);
+	public Object build(RTStatechart tParent, Transition transition) {
+		RTNode fromNode = (RTNode) tParent.getElementByAlias(transition
+				.getSource());
+		RTNode toNode = (RTNode) tParent.getElementByAlias(transition
+				.getTarget());
+
+		RTTimeEvent timeTrigger = null;
+		Set<RTSignalEvent> signalTriggers = new HashSet<RTSignalEvent>();
+		RTGuard guard = null;
+		RTAction action = null;
+
+		Trigger trigger = transition.getTrigger();
+		// TODO: Das muzss hier raus:
+		if (trigger instanceof ReactionTrigger) {
+			EList<EventSpec> triggers = ((ReactionTrigger) trigger)
+					.getTriggers();
+			for (EventSpec eventSpec : triggers) {
+				if (eventSpec instanceof RegularEventSpec) {
+					String name = ((RegularEventSpec) eventSpec).getEvent()
+							.getName();
+					RTSignalEvent signalEvent = tParent
+							.getSignalEvent(name);
+					if (signalEvent != null) {
+						signalTriggers.add(signalEvent);
+					}
+				}
+				// TODO: TimeEvent
+			}
+
+			Expression guardExpression = ((ReactionTrigger) trigger)
+					.getGuardExpression();
+			guard = new GuardExpression(
+					sTextBuilder.buildGuardExpression(guardExpression), tParent);
+		}
+
+		Effect effect = transition.getEffect();
+		action = new ActionStatement(
+				sTextBuilder.buildActionExpression(effect), tParent);
+
+		RTTransition tTrans = new RTTransition("t@", transition.getPriority(),
+				null, signalTriggers, guard, action, fromNode, toNode);
+		tParent.defineAlias(transition, tTrans);
 		return tTrans;
 	}
 

+ 4 - 4
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Assign.java

@@ -20,14 +20,14 @@ import org.yakindu.sct.simulation.runtime.ExecutionScope;
 public class Assign implements Statement {
 	
 	protected VariableRef varRef;
-	protected Expression expression;
+	protected RTExpression expression;
 	
 	
 	public Assign() {
 		super();
 	}
 
-	public Assign(VariableRef varRef, Expression expression) {
+	public Assign(VariableRef varRef, RTExpression expression) {
 		this();
 		this.varRef = varRef;
 		this.expression = expression;
@@ -41,11 +41,11 @@ public class Assign implements Statement {
 		this.varRef = varRef;
 	}
 
-	public Expression getExpression() {
+	public RTExpression getExpression() {
 		return expression;
 	}
 
-	public void setExpression(Expression expression) {
+	public void setExpression(RTExpression expression) {
 		this.expression = expression;
 	}
 

+ 9 - 9
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/BinaryOperation.java

@@ -13,40 +13,40 @@ package org.yakindu.sct.simulation.runtime.stext;
 import org.yakindu.sct.simulation.runtime.EvaluationException;
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public class BinaryOperation implements Expression {
+public class BinaryOperation implements RTExpression {
 
 	
-	protected Expression expr1;
-	protected Expression expr2;
+	protected RTExpression expr1;
+	protected RTExpression expr2;
 	protected Operator   operator;
 	
 	public BinaryOperation() {
 	}
 	
-	public BinaryOperation(Operator operator, Expression expr1, Expression expr2) {
+	public BinaryOperation(Operator operator, RTExpression expr1, RTExpression expr2) {
 		super();
 		this.expr1 = expr1;
 		this.expr2 = expr2;
 		this.operator = operator;
 	}
 
-	public BinaryOperation(String operator, Expression expr1, Expression expr2) {
+	public BinaryOperation(String operator, RTExpression expr1, RTExpression expr2) {
 		this(new Operator(operator), expr1, expr2);
 	}
 
-	public Expression getExpr1() {
+	public RTExpression getExpr1() {
 		return expr1;
 	}
 
-	public void setExpr1(Expression expr1) {
+	public void setExpr1(RTExpression expr1) {
 		this.expr1 = expr1;
 	}
 
-	public Expression getExpr2() {
+	public RTExpression getExpr2() {
 		return expr2;
 	}
 
-	public void setExpr2(Expression expr2) {
+	public void setExpr2(RTExpression expr2) {
 		this.expr2 = expr2;
 	}
 

+ 11 - 11
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Conditional.java

@@ -12,18 +12,18 @@ package org.yakindu.sct.simulation.runtime.stext;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public class Conditional implements Expression {
+public class Conditional implements RTExpression {
 
-	protected Expression condition;
-	protected Expression trueValue;
-	protected Expression falseValue;
+	protected RTExpression condition;
+	protected RTExpression trueValue;
+	protected RTExpression falseValue;
 	
 	
 	public Conditional() {
 	}
 	
 	
-	public Conditional(Expression condition, Expression trueValue, Expression falseValue) {
+	public Conditional(RTExpression condition, RTExpression trueValue, RTExpression falseValue) {
 		super();
 		this.condition = condition;
 		this.trueValue = trueValue;
@@ -32,32 +32,32 @@ public class Conditional implements Expression {
 
 
 	
-	public Expression getCondition() {
+	public RTExpression getCondition() {
 		return condition;
 	}
 
 
-	public void setCondition(Expression condition) {
+	public void setCondition(RTExpression condition) {
 		this.condition = condition;
 	}
 
 
-	public Expression getTrueValue() {
+	public RTExpression getTrueValue() {
 		return trueValue;
 	}
 
 
-	public void setTrueValue(Expression trueValue) {
+	public void setTrueValue(RTExpression trueValue) {
 		this.trueValue = trueValue;
 	}
 
 
-	public Expression getFalseValue() {
+	public RTExpression getFalseValue() {
 		return falseValue;
 	}
 
 
-	public void setFalseValue(Expression falseValue) {
+	public void setFalseValue(RTExpression falseValue) {
 		this.falseValue = falseValue;
 	}
 

+ 1 - 1
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Constant.java

@@ -17,7 +17,7 @@ import org.yakindu.sct.simulation.runtime.ExecutionScope;
  * @author terfloth@itemis.de
  *
  */
-public class Constant implements Expression {
+public class Constant implements RTExpression {
 	
 	protected Object value;
 	

+ 1 - 1
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Expression.java

@@ -12,7 +12,7 @@ package org.yakindu.sct.simulation.runtime.stext;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public interface Expression {
+public interface RTExpression {
 	
 	Object execute(ExecutionScope scope);
 

+ 7 - 7
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/Trigger.java

@@ -12,9 +12,9 @@ package org.yakindu.sct.simulation.runtime.stext;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public class Trigger {
+public class RTTrigger {
 	
-	public static class SignalEvent extends Trigger {
+	public static class SignalEvent extends RTTrigger {
 		protected String signal;
 
 		public SignalEvent(String signal) {
@@ -32,20 +32,20 @@ public class Trigger {
 		
 	}
 	
-	public static class TimeEvent extends Trigger implements Expression {
-		protected Expression durationExp;
+	public static class TimeEvent extends RTTrigger implements RTExpression {
+		protected RTExpression durationExp;
 
 		
-		public TimeEvent(Expression timeValueExp) {
+		public TimeEvent(RTExpression timeValueExp) {
 			super();
 			this.durationExp = timeValueExp;
 		}
 
-		public Expression getDurationExp() {
+		public RTExpression getDurationExp() {
 			return durationExp;
 		}
 
-		public void setDurationExp(Expression timeValueExp) {
+		public void setDurationExp(RTExpression timeValueExp) {
 			this.durationExp = timeValueExp;
 		}
 

+ 5 - 5
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/UnaryOperation.java

@@ -13,26 +13,26 @@ package org.yakindu.sct.simulation.runtime.stext;
 import org.yakindu.sct.simulation.runtime.EvaluationException;
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public class UnaryOperation implements Expression {
+public class UnaryOperation implements RTExpression {
 
 	
-	protected Expression expression;
+	protected RTExpression expression;
 	protected String operator;
 	
 	public UnaryOperation() {
 	}
 	
-	public UnaryOperation(String operator, Expression expr) {
+	public UnaryOperation(String operator, RTExpression expr) {
 		super();
 		this.expression = expr;
 		this.operator = operator;
 	}
 
-	public Expression getExpression() {
+	public RTExpression getExpression() {
 		return expression;
 	}
 
-	public void setExpression(Expression expr) {
+	public void setExpression(RTExpression expr) {
 		this.expression = expr;
 	}
 

+ 1 - 1
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/VariableRef.java

@@ -12,7 +12,7 @@ package org.yakindu.sct.simulation.runtime.stext;
 
 import org.yakindu.sct.simulation.runtime.ExecutionScope;
 
-public class VariableRef implements Expression {
+public class VariableRef implements RTExpression {
 
 	protected String name;
 	

+ 242 - 219
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/builder/ExpressionBuilder.java

@@ -10,8 +10,11 @@
  */
 package org.yakindu.sct.simulation.runtime.stext.builder;
 
-import static org.yakindu.sct.simulation.runtime.stext.CoreFunction.*;
+import static org.yakindu.sct.simulation.runtime.stext.CoreFunction.BIT_AND;
+import static org.yakindu.sct.simulation.runtime.stext.CoreFunction.BIT_OR;
+import static org.yakindu.sct.simulation.runtime.stext.CoreFunction.BIT_XOR;
 
+import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -19,479 +22,499 @@ import java.util.Map;
 
 import org.eclipse.emf.common.util.Enumerator;
 import org.eclipse.emf.ecore.EAttribute;
-import org.eclipse.emf.ecore.EEnumLiteral;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.EReference;
 import org.eclipse.emf.ecore.EStructuralFeature;
-
-import org.yakindu.sct.simulation.runtime.stext.*;
+import org.eclipse.xtext.ParserRule;
+import org.eclipse.xtext.XtextFactory;
+import org.eclipse.xtext.nodemodel.INode;
+import org.eclipse.xtext.parser.IParseResult;
+import org.eclipse.xtext.parser.IParser;
+import org.yakindu.sct.model.stext.ui.internal.STextActivator;
+import org.yakindu.sct.simulation.runtime.stext.Assign;
+import org.yakindu.sct.simulation.runtime.stext.BinaryOperation;
+import org.yakindu.sct.simulation.runtime.stext.Conditional;
+import org.yakindu.sct.simulation.runtime.stext.Constant;
+import org.yakindu.sct.simulation.runtime.stext.Function;
+import org.yakindu.sct.simulation.runtime.stext.FunctionMethod;
+import org.yakindu.sct.simulation.runtime.stext.ProcedureCall;
+import org.yakindu.sct.simulation.runtime.stext.RTExpression;
+import org.yakindu.sct.simulation.runtime.stext.RTTrigger;
+import org.yakindu.sct.simulation.runtime.stext.Raise;
+import org.yakindu.sct.simulation.runtime.stext.Statement;
+import org.yakindu.sct.simulation.runtime.stext.StatementSequence;
+import org.yakindu.sct.simulation.runtime.stext.UnaryOperation;
+import org.yakindu.sct.simulation.runtime.stext.VariableRef;
+
+import com.google.inject.Inject;
 
 /**
- * The ExpressionBuilder creates an executable structure from the textual expression definition 
- * or the corresponding EMF based abstract syntax tree. This AST is defined by the expression grammer 
- * of this plugin.<br>
+ * The ExpressionBuilder creates an executable structure from the textual
+ * expression definition or the corresponding EMF based abstract syntax tree.
+ * This AST is defined by the expression grammer of this plugin.<br>
  * <br>
- * Expression builder instances are state-less so a single instance can be used to build 
- * expression runtime structures multiple times and in parallel.<br>
+ * Expression builder instances are state-less so a single instance can be used
+ * to build expression runtime structures multiple times and in parallel.<br>
  * <br>
- * The current implementation mainly consist of a set of builder functions with a name prefix 'build'. 
- * This class defines a build function for each grammar rule. So when changing the grammer it will be necessary
- * to change, add or remove build methods for the affected grammar rules. The implementation strongly relies on dynamic dispatch of build function calls and therefore
+ * The current implementation mainly consist of a set of builder functions with
+ * a name prefix 'build'. This class defines a build function for each grammar
+ * rule. So when changing the grammer it will be necessary to change, add or
+ * remove build methods for the affected grammar rules. The implementation
+ * strongly relies on dynamic dispatch of build function calls and therefore
  * makes use of the 'pluggable adapter' pattern and Java reflection. <br>
  * <br>
- * Example:
- * <code>
+ * Example: <code>
  * (new ExpressionBuilder()).build("a = 2*b; a <<= 2;");
  * </code>
  * 
  * @author terfloth@itemis.de
- *
+ * 
  */
 public class ExpressionBuilder extends Function {
 
-	/** Builder functions always*/
-	protected static Class<?>[] paramTypes = new Class[]{EObject.class};
+	@Inject
+	private IParser parser;
+
+	/** Builder functions always */
+	protected static Class<?>[] paramTypes = new Class[] { EObject.class };
 
 	protected static Map<String, String> assignFunctionMap = new HashMap<String, String>();
 
 	static {
-		assignFunctionMap.put("multAssign", "*"); 
-		assignFunctionMap.put("divAssign", "/"); 
+		assignFunctionMap.put("multAssign", "*");
+		assignFunctionMap.put("divAssign", "/");
 		assignFunctionMap.put("modAssign", "%");
-		assignFunctionMap.put("addAssign", "+"); 
-		assignFunctionMap.put("subAssign", "-"); 
-		assignFunctionMap.put("leftShiftAssign", "<<"); 
-		assignFunctionMap.put("rightShiftAssign", ">>"); 
-		assignFunctionMap.put("andAssign", "&"); 
-		assignFunctionMap.put("xorAssign", "^"); 
+		assignFunctionMap.put("addAssign", "+");
+		assignFunctionMap.put("subAssign", "-");
+		assignFunctionMap.put("leftShiftAssign", "<<");
+		assignFunctionMap.put("rightShiftAssign", ">>");
+		assignFunctionMap.put("andAssign", "&");
+		assignFunctionMap.put("xorAssign", "^");
 		assignFunctionMap.put("orAssign", "|");
 	}
-	
-	
-	
+
+	public ExpressionBuilder() {
+		parser = STextActivator.getInstance()
+				.getInjector("org.yakindu.sct.model.stext.SText")
+				.getInstance(IParser.class);
+	}
+
 	/**
-	 * Static helper method that creates a executable expression structure from a expression string.
+	 * Static helper method that creates a executable expression structure from
+	 * a expression string.
 	 * 
 	 * @param actionExpression
 	 * @return an executable statement object.
 	 */
 	public static Statement buildAction(String actionExpression) {
 		ExpressionBuilder builder = new ExpressionBuilder();
-		return builder.buildActionExpression(actionExpression);
+		return builder.buildEffect(actionExpression);
 	}
 
-	
 	/**
-	 * Static helper method that creates a executable expression structure from a expression string.
+	 * Static helper method that creates a executable expression structure from
+	 * a expression string.
 	 * 
 	 * @param guardExpression
 	 * @return an executable statement object.
 	 */
-	public static Expression buildGuard(String guardExpression) {
+	public static RTExpression buildGuard(String guardExpression) {
 		ExpressionBuilder builder = new ExpressionBuilder();
 		return builder.buildGuardExpression(guardExpression);
 	}
 
-	
 	/**
-	 * Static helper method that creates a executable expression structure from a expression string.
+	 * Static helper method that creates a executable expression structure from
+	 * a expression string.
 	 * 
 	 * @param guardExpression
 	 * @return an executable statement object.
 	 */
-	public static List<Trigger> buildTriggers(String guardExpression) {
+	public static List<RTTrigger> buildTriggers(String guardExpression) {
 		ExpressionBuilder builder = new ExpressionBuilder();
 		return builder.buildTriggerExpression(guardExpression);
 	}
 
-
-	
 	/**
-	 * The primary builder method that creates a executable statement structure from a expression string.
+	 * The primary builder method that creates a executable statement structure
+	 * from a expression string.
 	 * 
 	 * @param actionExpression
 	 * @return an executable statement object.
 	 */
-	public Statement buildActionExpression(String expr) {
-		EObject root = JavaExtensions.parseActionExpressionObject(expr);
-		return (Statement) build(root);		
-	}
-	
-	
-	
-	
+	public Statement buildEffect(String expr) {
+		ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
+		parserRule.setName("Statement");
+		IParseResult result = parser.parse(parserRule, new StringReader(expr));
+		if(result.hasSyntaxErrors()){
+			StringBuilder errorMessages = new StringBuilder();
+			Iterable<INode> syntaxErrors = result.getSyntaxErrors();
+			for (INode iNode : syntaxErrors) {
+				errorMessages.append(iNode.getSyntaxErrorMessage());
+				errorMessages.append("\n");
+			}
+			throw new RuntimeException("Could not parse expression, syntax errors: " + errorMessages);
+		}
+		EObject rootASTElement = result.getRootASTElement();
+		return (Statement) build(rootASTElement);
+	}
+
 	/**
-	 * The primary builder method that creates a executable guard expression structure from an expression string.
+	 * The primary builder method that creates a executable guard expression
+	 * structure from an expression string.
 	 * 
 	 * @param actionExpression
 	 * @return an executable statement object.
 	 */
-	public Expression buildGuardExpression(String expr) {
-		EObject root = JavaExtensions.parseGuardExpressionObject(expr);
-		return (Expression) build(root);		
+	public RTExpression buildGuardExpression(String expr) {
+		EObject root = parser.parse(new StringReader(expr)).getRootASTElement();
+		return (RTExpression) build(root);
 	}
 
-	
 	/**
-	 * The primary builder method that creates a executable guard expression structure from an expression string.
+	 * The primary builder method that creates a executable guard expression
+	 * structure from an expression string.
 	 * 
 	 * @param actionExpression
 	 * @return an executable statement object.
 	 */
-	public List<Trigger> buildTriggerExpression(String expr) {
-		EObject root = JavaExtensions.parseTriggerExpressionObject(expr);
-		return (List<Trigger>) build(root);		
+	public List<RTTrigger> buildTriggerExpression(String expr) {
+		EObject root = parser.parse(new StringReader(expr)).getRootASTElement();
+		return (List<RTTrigger>) build(root);
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public List<Trigger> buildTriggerExpression(EObject obj) {
-		List<Trigger> triggers = new ArrayList<Trigger>();
+	public List<RTTrigger> buildTriggerExpression(EObject obj) {
+		List<RTTrigger> triggers = new ArrayList<RTTrigger>();
 
 		for (EObject child : obj.eContents()) {
-			Trigger trigger = (Trigger) build(child);
-			if(trigger != null) triggers.add(trigger);
+			RTTrigger trigger = (RTTrigger) build(child);
+			if (trigger != null)
+				triggers.add(trigger);
 		}
 
 		return triggers;
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Trigger buildTrigger(EObject obj) {
-		return (Trigger) build(getToOne(obj, "event"));
+	public RTTrigger buildTrigger(EObject obj) {
+		return (RTTrigger) build(getToOne(obj, "event"));
 	}
-	
+
 	@FunctionMethod("build")
-	public Trigger buildSignalEvent(EObject obj) {
-		return new Trigger.SignalEvent(getString(obj, "identifier"));
+	public RTTrigger buildSignalEvent(EObject obj) {
+		return new RTTrigger.SignalEvent(getString(obj, "identifier"));
 	}
-	
+
 	@FunctionMethod("build")
-	public Trigger buildTimeEvent(EObject obj) {
-		return new Trigger.TimeEvent(buildRefExpression(obj, "duration"));
+	public RTTrigger buildTimeEvent(EObject obj) {
+		return new RTTrigger.TimeEvent(buildRefExpression(obj, "duration"));
 	}
-	
+
 	@FunctionMethod("build")
-	public Expression buildTimeConstant(EObject obj) {
+	public RTExpression buildTimeConstant(EObject obj) {
 		int duration = getInteger(obj, "value");
 		String unit = getLiteral(obj, "unit");
-		
+
 		if (unit != null) {
-			if ("second".equals(unit)) duration *= 1000;
-			else if ("nanosecond".equals(unit)) duration /= 1000;
+			if ("second".equals(unit))
+				duration *= 1000;
+			else if ("nanosecond".equals(unit))
+				duration /= 1000;
 		}
-		
+
 		return new Constant(duration);
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Expression buildGuardExpression(EObject obj) {
+	public RTExpression buildGuardExpression(EObject obj) {
 		return buildRefExpression(obj, "expression");
 	}
-	
-	
+
 	@FunctionMethod("build")
 	public Statement buildActionExpression(EObject obj) {
 		StatementSequence seq = new StatementSequence();
-		
+
 		for (EObject child : obj.eContents()) {
 			Statement stmt = (Statement) build(child);
-			if(stmt != null) seq.add(stmt);
+			if (stmt != null)
+				seq.add(stmt);
 		}
-		
+
 		return seq;
 	}
-	
-	
+
 	@FunctionMethod("build")
 	public Statement buildEventRaising(EObject obj) {
-		
-		EObject event = getToOne(obj, "event");		
+
+		EObject event = getToOne(obj, "event");
 		return new Raise(getString(event, "identifier"));
 	}
-	
 
 	@FunctionMethod("build")
 	public Statement buildProcedureCall(EObject obj) {
-		
-		EObject event = getToOne(obj, "procedure");	
+
+		EObject event = getToOne(obj, "procedure");
 		return new ProcedureCall(getString(event, "identifier"));
 	}
-	
 
 	@FunctionMethod("build")
-	public Statement buildVariableAssignment(EObject obj) {
-		
+	public Statement buildAssignment(EObject obj) {
+
 		VariableRef var = (VariableRef) build(getToOne(obj, "variableReference"));
 		String operator = getLiteral(obj, "operator");
-		Expression exp = (Expression) build(getToOne(obj, "value"));
+		RTExpression exp = (RTExpression) build(getToOne(obj, "expression"));
 
-		if (! "assign".equals(operator)) {
+		if (!"assign".equals(operator)) {
 			String mappedOperator = assignFunctionMap.get(operator);
-			if (mappedOperator == null) 
-				throw new BuilderException("Assignment operator '" + operator + "' can not be resolved to a function.");
-			
+			if (mappedOperator == null)
+				throw new BuilderException("Assignment operator '" + operator
+						+ "' can not be resolved to a function.");
+
 			exp = new BinaryOperation(mappedOperator, var, exp);
 		}
-		
+
 		return new Assign(var, exp);
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildConditionalExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
-		Expression op3 = buildRefExpression(obj, "operand3");
+	public RTExpression buildConditionalExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+		RTExpression op3 = buildRefExpression(obj, "operand3");
 
 		if (op1 != null && op2 != null && op3 != null)
 			return new Conditional(op1, op2, op3);
-		
+
 		return op1;
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildBooleanOrExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBooleanOrExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(BinaryOperation.OR, op1, op2);
-		
+
 		return op1;
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildBooleanAndExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBooleanAndExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(BinaryOperation.AND, op1, op2);
-		
+
 		return op1;
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildBitwiseXorExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBitwiseXorExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(BIT_XOR, op1, op2);
-		
+
 		return op1;
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildBitwiseOrExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBitwiseOrExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(BIT_OR, op1, op2);
-		
+
 		return op1;
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Expression buildBitwiseAndExpression(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBitwiseAndExpression(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(BIT_AND, op1, op2);
-		
+
 		return op1;
 	}
-	
 
-	public Expression buildBinaryOperation(EObject obj) {
-		
-		Expression op1 = buildRefExpression(obj, "operand1");
-		Expression op2 = buildRefExpression(obj, "operand2");
+	public RTExpression buildBinaryOperation(EObject obj) {
+
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
 		String operator = getLiteral(obj, "operator");
 
 		if (op1 != null && op2 != null)
 			return new BinaryOperation(operator, op1, op2);
-		
+
 		return op1;
 	}
 
-	
 	@FunctionMethod("build")
-	public Expression buildEqualityExpression(EObject obj) {
+	public RTExpression buildEqualityExpression(EObject obj) {
 		return buildBinaryOperation(obj);
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Expression buildRelationalExpression(EObject obj) {
+	public RTExpression buildRelationalExpression(EObject obj) {
 		return buildBinaryOperation(obj);
 	}
-	
+
 	@FunctionMethod("build")
-	public Expression buildShiftExpression(EObject obj) {
+	public RTExpression buildShiftExpression(EObject obj) {
 		return buildBinaryOperation(obj);
 	}
-	
+
 	@FunctionMethod("build")
-	public Expression buildAdditiveExpression(EObject obj) {
+	public RTExpression buildAdditiveExpression(EObject obj) {
 		return buildBinaryOperation(obj);
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Expression buildMultiplicativeExpression(EObject obj) {
+	public RTExpression buildMultiplicativeExpression(EObject obj) {
 		return buildBinaryOperation(obj);
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildUnaryExpression(EObject obj) {
-		
-		Expression operand = buildRefExpression(obj, "operand");
+	public RTExpression buildUnaryExpression(EObject obj) {
+
+		RTExpression operand = buildRefExpression(obj, "operand");
 		String operator = getLiteral(obj, "operator");
 
 		if (operand != null && operator != null)
 			return new UnaryOperation(operator, operand);
-		
+
 		return operand;
 	}
-	
-	
+
 	@FunctionMethod("build")
-	public Expression buildNestedExpression(EObject obj) {
+	public RTExpression buildNestedExpression(EObject obj) {
 		return buildRefExpression(obj, "expression");
 	}
-	
 
 	@FunctionMethod("build")
-	public Expression buildLiteralValue(EObject obj) {
-		
-		String value = getString(obj, "value");	
-		
+	public RTExpression buildLiteralValue(EObject obj) {
+
+		String value = getString(obj, "value");
+
 		// TODO: nur provisrisch - Grammatik muss noch ge�ndert werden,
 		// um unterschiedliche Literaltypen unterscheiden zu koennen
 		if (value != null) {
-			
-			if ("true".equals(value)) return new Constant(Boolean.TRUE);
-			
-			if ("false".equals(value)) return new Constant(Boolean.FALSE);
+
+			if ("true".equals(value))
+				return new Constant(Boolean.TRUE);
+
+			if ("false".equals(value))
+				return new Constant(Boolean.FALSE);
 
 			try { // flaot
 				if (value.contains("."))
 					return new Constant(Float.parseFloat(value));
-			} catch (NumberFormatException e1) { /* need to be quite */ }
+			} catch (NumberFormatException e1) { /* need to be quite */
+			}
 
 			try { // hex int
 				if (value.startsWith("0x"))
-					return new Constant(new Integer(Integer.parseInt(value.substring(2), 16)));
-			} catch (NumberFormatException e1) { /* need to be quite */ }
-						
+					return new Constant(new Integer(Integer.parseInt(
+							value.substring(2), 16)));
+			} catch (NumberFormatException e1) { /* need to be quite */
+			}
+
 			try { // int
 				return new Constant(new Integer(Integer.parseInt(value)));
-			} catch (NumberFormatException e1) { /* need to be quite */ }
-			
-			if ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith("\"") && value.endsWith("\"")) )
-				return new Constant(value.substring(1, value.length()-1));
+			} catch (NumberFormatException e1) { /* need to be quite */
+			}
+
+			if ((value.startsWith("'") && value.endsWith("'"))
+					|| (value.startsWith("\"") && value.endsWith("\"")))
+				return new Constant(value.substring(1, value.length() - 1));
 		}
-		
+
 		return new Constant(value);
 	}
-	
 
-	
 	@FunctionMethod("build")
-	public Expression buildVariableReference(EObject obj) {
-		
-		EObject var = getToOne(obj, "variable");	
+	public RTExpression buildVariableReference(EObject obj) {
+
+		EObject var = getToOne(obj, "variable");
 		return new VariableRef(getString(var, "identifier"));
 	}
-	
 
 	/**
-	 * Internal build method that performs the dynamic dispatch to the appropriate (correctly named) 
-	 * builder function. The parameter is always a EObject and since we have use dynamic EMF we match by 
-	 * naming conventions.
+	 * Internal build method that performs the dynamic dispatch to the
+	 * appropriate (correctly named) builder function. The parameter is always a
+	 * EObject and since we have use dynamic EMF we match by naming conventions.
 	 * 
 	 * @param obj
 	 * @return
 	 */
 	protected Object build(EObject obj) {
-		if ( obj == null ) return null;
-		
-		Object[] params = new Object[]{obj};
-		
-		Function f = ExpressionBuilder.lookup(this.getClass(), "build" + obj.eClass().getName(), paramTypes);
-		if (f != null) 
+		if (obj == null)
+			return null;
+
+		Object[] params = new Object[] { obj };
+
+		Function f = ExpressionBuilder.lookup(this.getClass(), "build"
+				+ obj.eClass().getName(), paramTypes);
+		if (f != null)
 			return f.execute(params);
-		
-		throw new BuilderException("No builder function for: " + obj.eClass().getName());
+
+		throw new BuilderException("No builder function for: "
+				+ obj.eClass().getName());
 	}
-	
-	
-	protected Expression buildRefExpression(EObject obj, String refName) {
-		return (Expression) build(getToOne(obj, refName));
+
+	protected RTExpression buildRefExpression(EObject obj, String refName) {
+		return (RTExpression) build(getToOne(obj, refName));
 	}
-	
 
-	
 	protected EObject getToOne(EObject obj, String name) {
 		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
-		if ( feature instanceof EReference ) {
+		if (feature instanceof EReference) {
 			EReference ref = (EReference) feature;
 			return (EObject) obj.eGet(ref);
 		}
 		throw new BuilderException();
 	}
-	
-	
+
 	protected String getString(EObject obj, String name) {
 		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
-		if ( feature instanceof EAttribute ) {
+		if (feature instanceof EAttribute) {
 			EAttribute attr = (EAttribute) feature;
 			return (String) obj.eGet(attr);
 		}
 		throw new BuilderException();
 	}
 
-
 	protected Integer getInteger(EObject obj, String name) {
 		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
-		if ( feature instanceof EAttribute ) {
+		if (feature instanceof EAttribute) {
 			EAttribute attr = (EAttribute) feature;
 			return (Integer) obj.eGet(attr);
 		}
 		throw new BuilderException();
 	}
 
-	
 	protected String getLiteral(EObject obj, String name) {
 		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
-		if ( feature instanceof EAttribute ) {
+		if (feature instanceof EAttribute) {
 			EAttribute attr = (EAttribute) feature;
 			String literalValue = ((Enumerator) obj.eGet(attr)).getName();
-		
+
 			return ("NULL".equals(literalValue)) ? null : literalValue;
 		}
 		throw new BuilderException();
 	}
-	
-	
 
 }
-

+ 421 - 0
plugins/org.yakindu.sct.simulation.runtime/src/org/yakindu/sct/simulation/runtime/stext/builder/STextBuilder.java

@@ -0,0 +1,421 @@
+/**
+ * Copyright (c) 2010-2011 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.simulation.runtime.stext.builder;
+
+import static org.yakindu.sct.simulation.runtime.stext.CoreFunction.*;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.emf.common.util.Enumerator;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EEnumLiteral;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+
+import org.yakindu.sct.simulation.runtime.stext.*;
+
+/**
+ * The ExpressionBuilder creates an executable structure from the textual expression definition 
+ * or the corresponding EMF based abstract syntax tree. This AST is defined by the expression grammer 
+ * of this plugin.<br>
+ * <br>
+ * Expression builder instances are state-less so a single instance can be used to build 
+ * expression runtime structures multiple times and in parallel.<br>
+ * <br>
+ * The current implementation mainly consist of a set of builder functions with a name prefix 'build'. 
+ * This class defines a build function for each grammar rule. So when changing the grammer it will be necessary
+ * to change, add or remove build methods for the affected grammar rules. The implementation strongly relies on dynamic dispatch of build function calls and therefore
+ * makes use of the 'pluggable adapter' pattern and Java reflection. <br>
+ * <br>
+ * Example:
+ * <code>
+ * (new ExpressionBuilder()).build("a = 2*b; a <<= 2;");
+ * </code>
+ * 
+ * @author terfloth@itemis.de
+ *
+ */
+public class STextBuilder extends Function {
+
+	/** Builder functions always*/
+	protected static Class<?>[] paramTypes = new Class[]{EObject.class};
+
+	protected static Map<String, String> assignFunctionMap = new HashMap<String, String>();
+
+	static {
+		assignFunctionMap.put("multAssign", "*"); 
+		assignFunctionMap.put("divAssign", "/"); 
+		assignFunctionMap.put("modAssign", "%");
+		assignFunctionMap.put("addAssign", "+"); 
+		assignFunctionMap.put("subAssign", "-"); 
+		assignFunctionMap.put("leftShiftAssign", "<<"); 
+		assignFunctionMap.put("rightShiftAssign", ">>"); 
+		assignFunctionMap.put("andAssign", "&"); 
+		assignFunctionMap.put("xorAssign", "^"); 
+		assignFunctionMap.put("orAssign", "|");
+	}
+	
+	
+	@FunctionMethod("build")
+	public List<RTTrigger> buildTriggerExpression(EObject obj) {
+		List<RTTrigger> triggers = new ArrayList<RTTrigger>();
+
+		for (EObject child : obj.eContents()) {
+			RTTrigger trigger = (RTTrigger) build(child);
+			if(trigger != null) triggers.add(trigger);
+		}
+
+		return triggers;
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTTrigger buildTrigger(EObject obj) {
+		return (RTTrigger) build(getToOne(obj, "event"));
+	}
+	
+	@FunctionMethod("build")
+	public RTTrigger buildSignalEvent(EObject obj) {
+		return new RTTrigger.SignalEvent(getString(obj, "identifier"));
+	}
+	
+	@FunctionMethod("build")
+	public RTTrigger buildTimeEvent(EObject obj) {
+		return new RTTrigger.TimeEvent(buildRefExpression(obj, "duration"));
+	}
+	
+	@FunctionMethod("build")
+	public RTExpression buildTimeConstant(EObject obj) {
+		int duration = getInteger(obj, "value");
+		String unit = getLiteral(obj, "unit");
+		
+		if (unit != null) {
+			if ("second".equals(unit)) duration *= 1000;
+			else if ("nanosecond".equals(unit)) duration /= 1000;
+		}
+		
+		return new Constant(duration);
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTExpression buildGuardExpression(EObject obj) {
+		return buildRefExpression(obj, "expression");
+	}
+	
+	
+	@FunctionMethod("build")
+	public Statement buildActionExpression(EObject obj) {
+		StatementSequence seq = new StatementSequence();
+		
+		for (EObject child : obj.eContents()) {
+			Statement stmt = (Statement) build(child);
+			if(stmt != null) seq.add(stmt);
+		}
+		
+		return seq;
+	}
+	
+	
+	@FunctionMethod("build")
+	public Statement buildEventRaising(EObject obj) {
+		
+		EObject event = getToOne(obj, "event");		
+		return new Raise(getString(event, "identifier"));
+	}
+	
+
+	@FunctionMethod("build")
+	public Statement buildProcedureCall(EObject obj) {
+		
+		EObject event = getToOne(obj, "procedure");	
+		return new ProcedureCall(getString(event, "identifier"));
+	}
+	
+
+	@FunctionMethod("build")
+	public Statement buildVariableAssignment(EObject obj) {
+		
+		VariableRef var = (VariableRef) build(getToOne(obj, "variableReference"));
+		String operator = getLiteral(obj, "operator");
+		RTExpression exp = (RTExpression) build(getToOne(obj, "value"));
+
+		if (! "assign".equals(operator)) {
+			String mappedOperator = assignFunctionMap.get(operator);
+			if (mappedOperator == null) 
+				throw new BuilderException("Assignment operator '" + operator + "' can not be resolved to a function.");
+			
+			exp = new BinaryOperation(mappedOperator, var, exp);
+		}
+		
+		return new Assign(var, exp);
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildConditionalExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+		RTExpression op3 = buildRefExpression(obj, "operand3");
+
+		if (op1 != null && op2 != null && op3 != null)
+			return new Conditional(op1, op2, op3);
+		
+		return op1;
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildBooleanOrExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(BinaryOperation.OR, op1, op2);
+		
+		return op1;
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildBooleanAndExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(BinaryOperation.AND, op1, op2);
+		
+		return op1;
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildBitwiseXorExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(BIT_XOR, op1, op2);
+		
+		return op1;
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildBitwiseOrExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(BIT_OR, op1, op2);
+		
+		return op1;
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTExpression buildBitwiseAndExpression(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(BIT_AND, op1, op2);
+		
+		return op1;
+	}
+	
+
+	public RTExpression buildBinaryOperation(EObject obj) {
+		
+		RTExpression op1 = buildRefExpression(obj, "operand1");
+		RTExpression op2 = buildRefExpression(obj, "operand2");
+		String operator = getLiteral(obj, "operator");
+
+		if (op1 != null && op2 != null)
+			return new BinaryOperation(operator, op1, op2);
+		
+		return op1;
+	}
+
+	
+	@FunctionMethod("build")
+	public RTExpression buildEqualityExpression(EObject obj) {
+		return buildBinaryOperation(obj);
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTExpression buildRelationalExpression(EObject obj) {
+		return buildBinaryOperation(obj);
+	}
+	
+	@FunctionMethod("build")
+	public RTExpression buildShiftExpression(EObject obj) {
+		return buildBinaryOperation(obj);
+	}
+	
+	@FunctionMethod("build")
+	public RTExpression buildAdditiveExpression(EObject obj) {
+		return buildBinaryOperation(obj);
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTExpression buildMultiplicativeExpression(EObject obj) {
+		return buildBinaryOperation(obj);
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildUnaryExpression(EObject obj) {
+		
+		RTExpression operand = buildRefExpression(obj, "operand");
+		String operator = getLiteral(obj, "operator");
+
+		if (operand != null && operator != null)
+			return new UnaryOperation(operator, operand);
+		
+		return operand;
+	}
+	
+	
+	@FunctionMethod("build")
+	public RTExpression buildNestedExpression(EObject obj) {
+		return buildRefExpression(obj, "expression");
+	}
+	
+
+	@FunctionMethod("build")
+	public RTExpression buildLiteralValue(EObject obj) {
+		
+		String value = getString(obj, "value");	
+		
+		// TODO: nur provisrisch - Grammatik muss noch ge�ndert werden,
+		// um unterschiedliche Literaltypen unterscheiden zu koennen
+		if (value != null) {
+			
+			if ("true".equals(value)) return new Constant(Boolean.TRUE);
+			
+			if ("false".equals(value)) return new Constant(Boolean.FALSE);
+
+			try { // flaot
+				if (value.contains("."))
+					return new Constant(Float.parseFloat(value));
+			} catch (NumberFormatException e1) { /* need to be quite */ }
+
+			try { // hex int
+				if (value.startsWith("0x"))
+					return new Constant(new Integer(Integer.parseInt(value.substring(2), 16)));
+			} catch (NumberFormatException e1) { /* need to be quite */ }
+						
+			try { // int
+				return new Constant(new Integer(Integer.parseInt(value)));
+			} catch (NumberFormatException e1) { /* need to be quite */ }
+			
+			if ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith("\"") && value.endsWith("\"")) )
+				return new Constant(value.substring(1, value.length()-1));
+		}
+		
+		return new Constant(value);
+	}
+	
+
+	
+	@FunctionMethod("build")
+	public RTExpression buildVariableReference(EObject obj) {
+		
+		EObject var = getToOne(obj, "variable");	
+		return new VariableRef(getString(var, "identifier"));
+	}
+	
+
+	/**
+	 * Internal build method that performs the dynamic dispatch to the appropriate (correctly named) 
+	 * builder function. The parameter is always a EObject and since we have use dynamic EMF we match by 
+	 * naming conventions.
+	 * 
+	 * @param obj
+	 * @return
+	 */
+	protected Object build(EObject obj) {
+		if ( obj == null ) return null;
+		
+		Object[] params = new Object[]{obj};
+		
+		Function f = STextBuilder.lookup(this.getClass(), "build" + obj.eClass().getName(), paramTypes);
+		if (f != null) 
+			return f.execute(params);
+		
+		throw new BuilderException("No builder function for: " + obj.eClass().getName());
+	}
+	
+	
+	protected RTExpression buildRefExpression(EObject obj, String refName) {
+		return (RTExpression) build(getToOne(obj, refName));
+	}
+	
+
+	
+	protected EObject getToOne(EObject obj, String name) {
+		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
+		if ( feature instanceof EReference ) {
+			EReference ref = (EReference) feature;
+			return (EObject) obj.eGet(ref);
+		}
+		throw new BuilderException();
+	}
+	
+	
+	protected String getString(EObject obj, String name) {
+		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
+		if ( feature instanceof EAttribute ) {
+			EAttribute attr = (EAttribute) feature;
+			return (String) obj.eGet(attr);
+		}
+		throw new BuilderException();
+	}
+
+
+	protected Integer getInteger(EObject obj, String name) {
+		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
+		if ( feature instanceof EAttribute ) {
+			EAttribute attr = (EAttribute) feature;
+			return (Integer) obj.eGet(attr);
+		}
+		throw new BuilderException();
+	}
+
+	
+	protected String getLiteral(EObject obj, String name) {
+		EStructuralFeature feature = obj.eClass().getEStructuralFeature(name);
+		if ( feature instanceof EAttribute ) {
+			EAttribute attr = (EAttribute) feature;
+			String literalValue = ((Enumerator) obj.eGet(attr)).getName();
+		
+			return ("NULL".equals(literalValue)) ? null : literalValue;
+		}
+		throw new BuilderException();
+	}
+	
+	
+
+}
+

+ 3 - 6
test-plugins/org.yakindu.sct.simulation.runtime.test/src/org/yakindu/sct/simulation/runtime/sgraph/builder/test/StatechartBuilderTest.java

@@ -12,8 +12,10 @@ package org.yakindu.sct.simulation.runtime.sgraph.builder.test;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNotSame;
+import static junit.framework.Assert.assertSame;
 import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.*;
+import static junit.framework.Assert.fail;
 
 import java.util.List;
 
@@ -28,18 +30,15 @@ import org.yakindu.sct.model.sgraph.Event;
 import org.yakindu.sct.model.sgraph.FinalState;
 import org.yakindu.sct.model.sgraph.Region;
 import org.yakindu.sct.model.sgraph.SGraphFactory;
-import org.yakindu.sct.model.sgraph.SGraphPackage;
 import org.yakindu.sct.model.sgraph.Scope;
 import org.yakindu.sct.model.sgraph.State;
 import org.yakindu.sct.model.sgraph.Statechart;
 import org.yakindu.sct.model.sgraph.Transition;
-import org.yakindu.sct.model.sgraph.Variable;
 import org.yakindu.sct.model.stext.stext.Direction;
 import org.yakindu.sct.model.stext.stext.EventDefinition;
 import org.yakindu.sct.model.stext.stext.StextFactory;
 import org.yakindu.sct.model.stext.stext.Type;
 import org.yakindu.sct.model.stext.stext.VariableDefinition;
-import org.yakindu.sct.simulation.runtime.sgraph.ActionStatement;
 import org.yakindu.sct.simulation.runtime.sgraph.PseudostateKind;
 import org.yakindu.sct.simulation.runtime.sgraph.RTCompoundState;
 import org.yakindu.sct.simulation.runtime.sgraph.RTFinalState;
@@ -50,8 +49,6 @@ import org.yakindu.sct.simulation.runtime.sgraph.RTSimpleState;
 import org.yakindu.sct.simulation.runtime.sgraph.RTStatechart;
 import org.yakindu.sct.simulation.runtime.sgraph.RTTransition;
 import org.yakindu.sct.simulation.runtime.sgraph.builder.SGraphBuilder;
-import org.yakindu.sct.simulation.runtime.stext.Statement;
-import org.yakindu.sct.simulation.runtime.stext.StatementSequence;
 
 /**
  * 

Plik diff jest za duży
+ 307 - 212
test-plugins/org.yakindu.sct.simulation.runtime.test/src/org/yakindu/sct/simulation/runtime/stext/builder/test/BuilderTest.java