Kaynağa Gözat

Added Operation Callbacks to Simulation

Andreas Mülder 12 yıl önce
ebeveyn
işleme
8f057075c6

+ 3 - 0
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/IStatementInterpreter.java

@@ -10,6 +10,8 @@
  */
 package org.yakindu.sct.model.sexec.interpreter;
 
+import java.util.List;
+
 import org.yakindu.sct.model.sgraph.Statement;
 import org.yakindu.sct.simulation.core.runtime.ExecutionException;
 import org.yakindu.sct.simulation.core.runtime.IExecutionContext;
@@ -30,4 +32,5 @@ public interface IStatementInterpreter {
 	public Object evaluateStatement(Statement statement,
 			IExecutionContext context) throws ExecutionException;
 
+	public void setOperationCallbacks(List<Object> callbacks);
 }

+ 2 - 1
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/impl/ExecutionFlowInterpreter.xtend

@@ -81,8 +81,9 @@ class ExecutionFlowInterpreter extends AbstractExecutionFacade implements IExecu
 		for(scope : flow.scopes){
 			scope.declareContents
 		} 
-		executionContext.initStateConfigurationVector(flow.stateVector.size)
 		
+		executionContext.initStateConfigurationVector(flow.stateVector.size)
+		interpreter.setOperationCallbacks(super.callbacks)
 		brc = SexecFactory::eINSTANCE.createTraceBeginRunCycle
 		erc = SexecFactory::eINSTANCE.createTraceEndRunCycle
 		

+ 17 - 0
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/AbstractStatementInterpreter.java

@@ -11,9 +11,12 @@
 package org.yakindu.sct.model.sexec.interpreter.stext;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import org.eclipse.xtext.util.PolymorphicDispatcher;
 import org.yakindu.sct.model.sexec.interpreter.IStatementInterpreter;
+import org.yakindu.sct.model.stext.stext.OperationDefinition;
 
 /**
  * 
@@ -25,6 +28,8 @@ public abstract class AbstractStatementInterpreter extends CoreFunction
 
 	protected static Map<String, String> assignFunctionMap = new HashMap<String, String>();
 
+	protected List<Object> operationCallback;
+
 	static {
 		assignFunctionMap.put("multAssign", "*");
 		assignFunctionMap.put("divAssign", "/");
@@ -48,4 +53,16 @@ public abstract class AbstractStatementInterpreter extends CoreFunction
 		return super.lookup(getClass(), name, params);
 	}
 
+	public void setOperationCallbacks(List<Object> callbacks) {
+		this.operationCallback = callbacks;
+	}
+
+	public Object executeOperationCallback(OperationDefinition definition,
+			Object ... parameter) {
+		PolymorphicDispatcher<Object> dispatcher = new PolymorphicDispatcher<Object>(
+				definition.getName(), definition.getParameters().size(),
+				definition.getParameters().size(), operationCallback);
+		return dispatcher.invoke(parameter);
+	}
+
 }

+ 5 - 0
plugins/org.yakindu.sct.model.sexec.interpreter/src/org/yakindu/sct/model/sexec/interpreter/stext/StextStatementInterpreter.xtend

@@ -44,6 +44,7 @@ import org.yakindu.sct.model.stext.stext.StringLiteral
 import org.yakindu.sct.model.stext.stext.VariableDefinition
 import org.yakindu.sct.simulation.core.runtime.IExecutionContext
 import org.yakindu.sct.model.stext.stext.ParenthesizedExpression
+import org.yakindu.sct.model.stext.stext.OperationDefinition
 
 /**
  * 
@@ -123,6 +124,10 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 	} 
 	
 	def dispatch execute(ElementReferenceExpression expression){
+		if(expression.operationCall && super.operationCallback.size() > 0){
+			var parameter = expression.args.map(it| execute)
+			return super.executeOperationCallback(expression.reference as OperationDefinition, parameter.toArray)
+		}
 		var variableRef = context.getVariable(expression.reference.fullyQualifiedName.toString)
 		if(variableRef != null){
 			return variableRef.getValue

+ 2 - 0
plugins/org.yakindu.sct.model.sexec.interpreter/xtend-gen/org/yakindu/sct/model/sexec/interpreter/impl/ExecutionFlowInterpreter.java

@@ -92,6 +92,8 @@ public class ExecutionFlowInterpreter extends AbstractExecutionFacade implements
     StateVector _stateVector = flow.getStateVector();
     int _size = _stateVector.getSize();
     this.executionContext.initStateConfigurationVector(_size);
+    List<Object> _callbacks = super.getCallbacks();
+    this.interpreter.setOperationCallbacks(_callbacks);
     TraceBeginRunCycle _createTraceBeginRunCycle = SexecFactory.eINSTANCE.createTraceBeginRunCycle();
     this.brc = _createTraceBeginRunCycle;
     TraceEndRunCycle _createTraceEndRunCycle = SexecFactory.eINSTANCE.createTraceEndRunCycle();

+ 30 - 4
plugins/org.yakindu.sct.model.sexec.interpreter/xtend-gen/org/yakindu/sct/model/sexec/interpreter/stext/StextStatementInterpreter.java

@@ -5,9 +5,12 @@ import com.google.inject.Inject;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
+import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.xtext.naming.QualifiedName;
 import org.eclipse.xtext.util.SimpleAttributeResolver;
+import org.eclipse.xtext.xbase.lib.Functions.Function1;
+import org.eclipse.xtext.xbase.lib.ListExtensions;
 import org.yakindu.sct.model.sexec.interpreter.stext.AbstractStatementInterpreter;
 import org.yakindu.sct.model.sexec.interpreter.stext.CoreFunction;
 import org.yakindu.sct.model.sgraph.RegularState;
@@ -40,6 +43,7 @@ import org.yakindu.sct.model.stext.stext.MultiplicativeOperator;
 import org.yakindu.sct.model.stext.stext.NumericalAddSubtractExpression;
 import org.yakindu.sct.model.stext.stext.NumericalMultiplyDivideExpression;
 import org.yakindu.sct.model.stext.stext.NumericalUnaryExpression;
+import org.yakindu.sct.model.stext.stext.OperationDefinition;
 import org.yakindu.sct.model.stext.stext.ParenthesizedExpression;
 import org.yakindu.sct.model.stext.stext.PrimitiveValueExpression;
 import org.yakindu.sct.model.stext.stext.RealLiteral;
@@ -199,16 +203,38 @@ public class StextStatementInterpreter extends AbstractStatementInterpreter {
   }
   
   protected Object _execute(final ElementReferenceExpression expression) {
-    EObject _reference = expression.getReference();
-    QualifiedName _fullyQualifiedName = this.provider.getFullyQualifiedName(_reference);
+    boolean _and = false;
+    boolean _isOperationCall = expression.isOperationCall();
+    if (!_isOperationCall) {
+      _and = false;
+    } else {
+      int _size = super.operationCallback.size();
+      boolean _greaterThan = (_size > 0);
+      _and = (_isOperationCall && _greaterThan);
+    }
+    if (_and) {
+      EList<Expression> _args = expression.getArgs();
+      final Function1<Expression,Object> _function = new Function1<Expression,Object>() {
+          public Object apply(final Expression it) {
+            Object _execute = StextStatementInterpreter.this.execute(it);
+            return _execute;
+          }
+        };
+      List<Object> parameter = ListExtensions.<Expression, Object>map(_args, _function);
+      EObject _reference = expression.getReference();
+      Object[] _array = parameter.toArray();
+      return super.executeOperationCallback(((OperationDefinition) _reference), _array);
+    }
+    EObject _reference_1 = expression.getReference();
+    QualifiedName _fullyQualifiedName = this.provider.getFullyQualifiedName(_reference_1);
     String _string = _fullyQualifiedName.toString();
     ExecutionVariable variableRef = this.context.getVariable(_string);
     boolean _notEquals = (!Objects.equal(variableRef, null));
     if (_notEquals) {
       return variableRef.getValue();
     }
-    EObject _reference_1 = expression.getReference();
-    QualifiedName _fullyQualifiedName_1 = this.provider.getFullyQualifiedName(_reference_1);
+    EObject _reference_2 = expression.getReference();
+    QualifiedName _fullyQualifiedName_1 = this.provider.getFullyQualifiedName(_reference_2);
     String _string_1 = _fullyQualifiedName_1.toString();
     return Boolean.valueOf(this.context.isEventRaised(_string_1));
   }