Selaa lähdekoodia

Using literal value as runtime value for enumerators.* refactored toExecutionState* #677: Literal values are derived from index in owning enumerator list. Changed literal value type to EInt.* #677: Fixed interpreter tests for enumerations.* #677:

Using literal value as runtime value for enumerators. Adjusted interpreter tests.
Andreas Mülder 9 vuotta sitten
vanhempi
commit
2a879f7a7b

+ 2 - 1
plugins/org.yakindu.base.types/model/types.ecore

@@ -61,7 +61,8 @@
   <eClassifiers xsi:type="ecore:EClass" name="Enumerator" eSuperTypes="#//Declaration">
     <eStructuralFeatures xsi:type="ecore:EReference" name="owningEnumeration" eType="#//EnumerationType"
         eOpposite="#//EnumerationType/enumerator"/>
-    <eStructuralFeatures xsi:type="ecore:EAttribute" name="literalValue" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EJavaObject"/>
+    <eStructuralFeatures xsi:type="ecore:EAttribute" name="literalValue" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
+        changeable="false" volatile="true" transient="true" derived="true"/>
   </eClassifiers>
   <eClassifiers xsi:type="ecore:EClass" name="TypeConstraint">
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EJavaObject"/>

+ 2 - 15
plugins/org.yakindu.base.types/src-gen/org/yakindu/base/types/Enumerator.java

@@ -2,8 +2,6 @@
  */
 package org.yakindu.base.types;
 
-import org.yakindu.base.base.NamedElement;
-
 /**
  * <!-- begin-user-doc -->
  * A representation of the model object '<em><b>Enumerator</b></em>'.
@@ -60,20 +58,9 @@ public interface Enumerator extends Declaration {
 	 * </p>
 	 * <!-- end-user-doc -->
 	 * @return the value of the '<em>Literal Value</em>' attribute.
-	 * @see #setLiteralValue(Object)
 	 * @see org.yakindu.base.types.TypesPackage#getEnumerator_LiteralValue()
-	 * @model
-	 * @generated
-	 */
-	Object getLiteralValue();
-
-	/**
-	 * Sets the value of the '{@link org.yakindu.base.types.Enumerator#getLiteralValue <em>Literal Value</em>}' attribute.
-	 * <!-- begin-user-doc -->
-	 * <!-- end-user-doc -->
-	 * @param value the new value of the '<em>Literal Value</em>' attribute.
-	 * @see #getLiteralValue()
+	 * @model transient="true" changeable="false" volatile="true" derived="true"
 	 * @generated
 	 */
-	void setLiteralValue(Object value);
+	int getLiteralValue();
 } // Enumerator

+ 5 - 49
plugins/org.yakindu.base.types/src-gen/org/yakindu/base/types/impl/EnumeratorImpl.java

@@ -8,7 +8,6 @@ import org.eclipse.emf.ecore.EClass;
 import org.eclipse.emf.ecore.InternalEObject;
 import org.eclipse.emf.ecore.impl.ENotificationImpl;
 import org.eclipse.emf.ecore.util.EcoreUtil;
-import org.yakindu.base.base.impl.NamedElementImpl;
 import org.yakindu.base.types.EnumerationType;
 import org.yakindu.base.types.Enumerator;
 import org.yakindu.base.types.TypesPackage;
@@ -36,16 +35,7 @@ public class EnumeratorImpl extends DeclarationImpl implements Enumerator {
 	 * @generated
 	 * @ordered
 	 */
-	protected static final Object LITERAL_VALUE_EDEFAULT = null;
-	/**
-	 * The cached value of the '{@link #getLiteralValue() <em>Literal Value</em>}' attribute.
-	 * <!-- begin-user-doc -->
-	 * <!-- end-user-doc -->
-	 * @see #getLiteralValue()
-	 * @generated
-	 * @ordered
-	 */
-	protected Object literalValue = LITERAL_VALUE_EDEFAULT;
+	protected static final int LITERAL_VALUE_EDEFAULT = 0;
 	/**
 	 * <!-- begin-user-doc -->
 	 * <!-- end-user-doc -->
@@ -109,22 +99,10 @@ public class EnumeratorImpl extends DeclarationImpl implements Enumerator {
 	/**
 	 * <!-- begin-user-doc -->
 	 * <!-- end-user-doc -->
-	 * @generated
-	 */
-	public Object getLiteralValue() {
-		return literalValue;
-	}
-
-	/**
-	 * <!-- begin-user-doc -->
-	 * <!-- end-user-doc -->
-	 * @generated
+	 * @generated NOT
 	 */
-	public void setLiteralValue(Object newLiteralValue) {
-		Object oldLiteralValue = literalValue;
-		literalValue = newLiteralValue;
-		if (eNotificationRequired())
-			eNotify(new ENotificationImpl(this, Notification.SET, TypesPackage.ENUMERATOR__LITERAL_VALUE, oldLiteralValue, literalValue));
+	public int getLiteralValue() {
+		return getOwningEnumeration().getEnumerator().indexOf(this);
 	}
 
 	/**
@@ -198,9 +176,6 @@ public class EnumeratorImpl extends DeclarationImpl implements Enumerator {
 			case TypesPackage.ENUMERATOR__OWNING_ENUMERATION:
 				setOwningEnumeration((EnumerationType)newValue);
 				return;
-			case TypesPackage.ENUMERATOR__LITERAL_VALUE:
-				setLiteralValue(newValue);
-				return;
 		}
 		super.eSet(featureID, newValue);
 	}
@@ -216,9 +191,6 @@ public class EnumeratorImpl extends DeclarationImpl implements Enumerator {
 			case TypesPackage.ENUMERATOR__OWNING_ENUMERATION:
 				setOwningEnumeration((EnumerationType)null);
 				return;
-			case TypesPackage.ENUMERATOR__LITERAL_VALUE:
-				setLiteralValue(LITERAL_VALUE_EDEFAULT);
-				return;
 		}
 		super.eUnset(featureID);
 	}
@@ -234,25 +206,9 @@ public class EnumeratorImpl extends DeclarationImpl implements Enumerator {
 			case TypesPackage.ENUMERATOR__OWNING_ENUMERATION:
 				return getOwningEnumeration() != null;
 			case TypesPackage.ENUMERATOR__LITERAL_VALUE:
-				return LITERAL_VALUE_EDEFAULT == null ? literalValue != null : !LITERAL_VALUE_EDEFAULT.equals(literalValue);
+				return getLiteralValue() != LITERAL_VALUE_EDEFAULT;
 		}
 		return super.eIsSet(featureID);
 	}
 
-	/**
-	 * <!-- begin-user-doc -->
-	 * <!-- end-user-doc -->
-	 * @generated
-	 */
-	@Override
-	public String toString() {
-		if (eIsProxy()) return super.toString();
-
-		StringBuffer result = new StringBuffer(super.toString());
-		result.append(" (literalValue: ");
-		result.append(literalValue);
-		result.append(')');
-		return result.toString();
-	}
-
 } //EnumeratorImpl

+ 1 - 1
plugins/org.yakindu.base.types/src-gen/org/yakindu/base/types/impl/TypesPackageImpl.java

@@ -822,7 +822,7 @@ public class TypesPackageImpl extends EPackageImpl implements TypesPackage {
 
 		initEClass(enumeratorEClass, Enumerator.class, "Enumerator", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
 		initEReference(getEnumerator_OwningEnumeration(), this.getEnumerationType(), this.getEnumerationType_Enumerator(), "owningEnumeration", null, 0, 1, Enumerator.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_COMPOSITE, !IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
-		initEAttribute(getEnumerator_LiteralValue(), ecorePackage.getEJavaObject(), "literalValue", null, 0, 1, Enumerator.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
+		initEAttribute(getEnumerator_LiteralValue(), ecorePackage.getEInt(), "literalValue", null, 0, 1, Enumerator.class, IS_TRANSIENT, IS_VOLATILE, !IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, IS_DERIVED, IS_ORDERED);
 
 		initEClass(typeConstraintEClass, TypeConstraint.class, "TypeConstraint", !IS_ABSTRACT, !IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
 		initEAttribute(getTypeConstraint_Value(), ecorePackage.getEJavaObject(), "value", null, 0, 1, TypeConstraint.class, !IS_TRANSIENT, !IS_VOLATILE, IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);

+ 3 - 2
plugins/org.yakindu.sct.simulation.core.sexec/src/org/yakindu/sct/simulation/core/sexec/interpreter/DefaultExecutionFlowInterpreter.xtend

@@ -109,8 +109,9 @@ class DefaultExecutionFlowInterpreter implements IExecutionFlowInterpreter {
 	}
 
 	def ExecutionState toExecutionState(RegularState state) {
-		return flow.eAllContents.filter[
-			it instanceof ExecutionState && EcoreUtil::equals((it as ExecutionState).sourceElement, state)].head as ExecutionState
+		return flow.eAllContents.filter(ExecutionState).findFirst[
+			EcoreUtil.equals(sourceElement, state)
+		]
 	}
 
 	override runCycle() {

+ 7 - 4
plugins/org.yakindu.sct.simulation.core.sexec/src/org/yakindu/sct/simulation/core/sexec/interpreter/StextStatementInterpreter.xtend

@@ -53,6 +53,7 @@ import org.yakindu.sct.simulation.core.sruntime.ExecutionContext
 import org.yakindu.sct.simulation.core.sruntime.ExecutionEvent
 import org.yakindu.sct.simulation.core.sruntime.ExecutionVariable
 import org.yakindu.base.types.typesystem.GenericTypeSystem
+import org.yakindu.base.types.EnumerationType
 
 /**
  * 
@@ -92,6 +93,7 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 	}
 
 	def dispatch Object typeCast(Long value, Type type) {
+		if(type instanceof EnumerationType) return value
 		if(ts.isSuperType(type, ts.getType(GenericTypeSystem.INTEGER))) return value
 		if(ts.isSuperType(type, ts.getType(GenericTypeSystem.REAL))) return Double.valueOf(value)
 		throw new IllegalArgumentException("unknown type "+type.name)
@@ -131,8 +133,9 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 	def Object executeAssignment(AssignmentExpression assignment) {
 		var scopeVariable = context.resolve(assignment.varRef)
 		var result = assignment.expression.execute
-		if (assignment.operator == AssignmentOperator::ASSIGN) {
+		if (result instanceof Enumerator) result = result.literalValue
 
+		if (assignment.operator == AssignmentOperator::ASSIGN) {
 			//Strong typing, use the type of the scopeVariable instead of using new runtime type
 			scopeVariable.value = if(result != null) typeCast(result, scopeVariable.type) else null
 		} else {
@@ -172,9 +175,9 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 				return operationDelegate.execute((expression.reference as Operation), parameter.toArray)
 			}
 		}
-		// for enumeration types just return the referenced enumerator
+		// for enumeration types return the literal value
 		if (expression.reference instanceof Enumerator) {
-			return expression.reference
+			return new Long((expression.reference as Enumerator).literalValue)
 		}
 		
 		val executionSlot = context.resolve(expression)
@@ -292,7 +295,7 @@ class StextStatementInterpreter extends AbstractStatementInterpreter {
 			}
 
 		} else if (call.getFeature() instanceof Enumerator) {
-			return call.getFeature();
+			return new Long((call.getFeature() as Enumerator).literalValue)
 		}
 
 		var slot = context.resolve(call)

+ 5 - 3
plugins/org.yakindu.sct.simulation.ui/src/org/yakindu/sct/simulation/ui/view/ExecutionContextLabelProvider.java

@@ -14,7 +14,7 @@ import org.eclipse.draw2d.ColorConstants;
 import org.eclipse.jface.viewers.StyledCellLabelProvider;
 import org.eclipse.jface.viewers.ViewerCell;
 import org.eclipse.swt.custom.StyleRange;
-import org.yakindu.base.types.Enumerator;
+import org.yakindu.base.types.EnumerationType;
 import org.yakindu.sct.simulation.core.sruntime.CompositeSlot;
 import org.yakindu.sct.simulation.core.sruntime.ExecutionEvent;
 import org.yakindu.sct.simulation.core.sruntime.ExecutionSlot;
@@ -51,8 +51,10 @@ public class ExecutionContextLabelProvider extends StyledCellLabelProvider {
 		if (element instanceof ExecutionSlot) {
 			Object value = ((ExecutionSlot) element).getValue();
 			if (value != null) {
-				if (value instanceof Enumerator) {
-					cell.setText(((Enumerator) value).getName());
+				if (((ExecutionSlot) element).getType().getOriginType() instanceof EnumerationType) {
+					EnumerationType enumType = (EnumerationType) ((ExecutionSlot) element).getType().getOriginType();
+					String text = enumType.getEnumerator().get(((Long)value).intValue()).getName();
+					cell.setText(text);
 				} else {
 					cell.setText(value.toString());
 				}

+ 2 - 2
plugins/org.yakindu.sct.simulation.ui/src/org/yakindu/sct/simulation/ui/view/editing/EnumerationEditingSupport.java

@@ -86,9 +86,9 @@ public class EnumerationEditingSupport extends ScopeSlotEditingSupport {
 				int index = ((Integer) value).intValue();
 				EnumerationType e = getEnumerationType((ExecutionSlot) element);
 				if (index < 0) {
-					return e.getEnumerator().iterator().next();
+					return new Long(e.getEnumerator().iterator().next().getLiteralValue());
 				}
-				return e.getEnumerator().get(index);
+				return new Long(e.getEnumerator().get(index).getLiteralValue());
 			}
 		}
 		return null;

+ 10 - 3
test-plugins/org.yakindu.sct.simulation.core.sexec.test/src/org/yakindu/sct/model/sexec/interpreter/test/STextInterpreterTest.java

@@ -470,13 +470,16 @@ public class STextInterpreterTest extends AbstractSTextTest {
 	
 	@Test
 	public void testEnumEqualsExpression() {
-		assertEquals(true, execute("internal: var enumVar : EnumType", "enumVar == EnumType.A"));
+		String scope = "internal: var enumVar : EnumType";
+		execute(scope, "enumVar = EnumType.B");
+		assertEquals(false, execute(scope, "enumVar == EnumType.A"));
+		assertEquals(true, execute(scope, "enumVar == EnumType.B"));
 	}
 
 	@Test
 	public void testEnumAssignment() {
-		execute("internal: var enumVar : EnumType", "enumVar = EnumType.A");
-		assertEquals(((EnumerationType)typeSystem.getType("EnumType")).getEnumerator().get(0), getEnumValue());
+		execute("internal: var enumVar : EnumType", "enumVar = EnumType.B");
+		assertEquals(new Long(((EnumerationType)typeSystem.getType("EnumType")).getEnumerator().get(1).getLiteralValue()), getEnumValue());
 	}
 	
 	@Test
@@ -574,6 +577,10 @@ public class STextInterpreterTest extends AbstractSTextTest {
 		enumA.setName("A");
 		enumType.getEnumerator().add(enumA);
 		
+		Enumerator enumB = TypesFactory.eINSTANCE.createEnumerator();
+		enumB.setName("B");
+		enumType.getEnumerator().add(enumB);
+		
 		typeSystem.declareType(enumType, enumType.getName());
 		
 		Resource resource = new ResourceImpl(URI.createURI("types2"));