瀏覽代碼

Moved defaultValue to typesystem

Andreas Mülder 12 年之前
父節點
當前提交
6e813acc92

+ 4 - 0
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/types/ISTextTypeSystem.java

@@ -120,5 +120,9 @@ public interface ISTextTypeSystem extends ITypeSystem {
 	public boolean isRealType(Type type);
 
 	public boolean isStringType(Type type);
+	
+	public Object defaultValue(InferredType type);
+	
+	public Object defaultValue(Type type);
 
 }

+ 110 - 215
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/types/STextDefaultTypeSystem.java

@@ -11,6 +11,7 @@ import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.yakindu.base.types.AbstractTypeSystem;
 import org.yakindu.base.types.ComplexType;
+import org.yakindu.base.types.EnumerationType;
 import org.yakindu.base.types.ITypeSystem;
 import org.yakindu.base.types.PrimitiveType;
 import org.yakindu.base.types.Type;
@@ -29,8 +30,7 @@ import com.google.inject.Singleton;
  * 
  */
 @Singleton
-public class STextDefaultTypeSystem extends AbstractTypeSystem implements
-		ISTextTypeSystem {
+public class STextDefaultTypeSystem extends AbstractTypeSystem implements ISTextTypeSystem {
 
 	/**
 	 * Dummy resource. Xtext linker expects types to be contained in a resource.
@@ -46,8 +46,7 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		if (resource == null) {
 			resource = new ResourceImpl();
 			// use old uri for backwards compatibility
-			resource.setURI(URI
-					.createURI("platform:/plugin/org.yakindu.sct.model.stext/libraries/Primitives.types"));
+			resource.setURI(URI.createURI("platform:/plugin/org.yakindu.sct.model.stext/libraries/Primitives.types"));
 		}
 		return resource;
 	}
@@ -120,8 +119,7 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		throw new IllegalArgumentException("Literal of unknown kind " + literal);
 	}
 
-	public InferenceResult inferType(InferredType operandType,
-			ITypeSystemOperator unaryOperator) {
+	public InferenceResult inferType(InferredType operandType, ITypeSystemOperator unaryOperator) {
 		// some defense programming
 		if (operandType == null) {
 			throw new NullPointerException("operandType may not be null.");
@@ -130,21 +128,14 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		// if the operand type could not be inferred, there is not much we can
 		// do here
 		if (operandType.getType() == null) {
-			return new InferenceResult(
-					null,
-					new InferenceIssue(
-							"Could not infer a type for the operation "
-									+ unaryOperator.getSymbol()
-									+ ", because no type was inferred for its operand.",
-							IStatus.ERROR));
+			return new InferenceResult(null, new InferenceIssue("Could not infer a type for the operation "
+					+ unaryOperator.getSymbol() + ", because no type was inferred for its operand.", IStatus.ERROR));
 		}
 
 		// check we have a primitive type
 		if (!(operandType.getType() instanceof PrimitiveType)) {
-			return new InferenceResult(null, new InferenceIssue("Operator "
-					+ unaryOperator.getSymbol()
-					+ " may only be applied on a primitive type.",
-					IStatus.ERROR));
+			return new InferenceResult(null, new InferenceIssue("Operator " + unaryOperator.getSymbol()
+					+ " may only be applied on a primitive type.", IStatus.ERROR));
 		}
 
 		// infer type based on given operator
@@ -152,52 +143,35 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		switch (o) {
 		case COMPLEMENT:
 			if (!isIntegerType(operandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Bitwise operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on integer types, not on "
-										+ operandType.getType().getName() + ".",
-								IStatus.ERROR));
+				return new InferenceResult(null, new InferenceIssue("Bitwise operator '" + o.getSymbol()
+						+ "' may only be applied on integer types, not on " + operandType.getType().getName() + ".",
+						IStatus.ERROR));
 
 			}
 			return new InferenceResult(operandType);
 		case NEGATIVE:
 		case POSITIVE:
-			if (!isIntegerType(operandType.getType())
-					&& !isRealType(operandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Arithmetic operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on numeric types, not on "
-										+ operandType.getType().getName() + ".",
-								IStatus.ERROR));
+			if (!isIntegerType(operandType.getType()) && !isRealType(operandType.getType())) {
+				return new InferenceResult(null, new InferenceIssue("Arithmetic operator '" + o.getSymbol()
+						+ "' may only be applied on numeric types, not on " + operandType.getType().getName() + ".",
+						IStatus.ERROR));
 			}
 			return new InferenceResult(operandType);
 		case LOGICAL_NOT:
 			if (!isBooleanType(operandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Logical operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on boolean types, not on "
-										+ operandType.getType().getName() + ".",
-								IStatus.ERROR));
+				return new InferenceResult(null, new InferenceIssue("Logical operator '" + o.getSymbol()
+						+ "' may only be applied on boolean types, not on " + operandType.getType().getName() + ".",
+						IStatus.ERROR));
 
 			}
 			return new InferenceResult(operandType);
 		default:
-			throw new IllegalArgumentException("Unsupported unary operator: "
-					+ unaryOperator);
+			throw new IllegalArgumentException("Unsupported unary operator: " + unaryOperator);
 		}
 	}
 
-	public InferenceResult inferType(InferredType firstOperandType,
-			InferredType secondOperandType, ITypeSystemOperator binaryOperator) {
+	public InferenceResult inferType(InferredType firstOperandType, InferredType secondOperandType,
+			ITypeSystemOperator binaryOperator) {
 		// some defense programming
 		if (firstOperandType == null || secondOperandType == null) {
 			throw new NullPointerException("Operand types may not be null.");
@@ -205,15 +179,10 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 
 		// if the operand type could not be inferred, there is not much we can
 		// do here
-		if (firstOperandType.getType() == null
-				|| secondOperandType.getType() == null) {
-			return new InferenceResult(
-					null,
-					new InferenceIssue(
-							"Could not infer a type for the operation "
-									+ binaryOperator.getSymbol()
-									+ ", because types were not inferred for all of its operands.",
-							IStatus.ERROR));
+		if (firstOperandType.getType() == null || secondOperandType.getType() == null) {
+			return new InferenceResult(null, new InferenceIssue("Could not infer a type for the operation "
+					+ binaryOperator.getSymbol() + ", because types were not inferred for all of its operands.",
+					IStatus.ERROR));
 		}
 
 		// infer type base on operator (fist pass: check types are valid, second
@@ -231,18 +200,10 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		case ASSIGN_LEFT_SHIFT:
 		case ASSIGN_RIGHT_SHIFT:
 			// check only integer types are used
-			if (!isIntegerType(firstOperandType.getType())
-					|| !isIntegerType(secondOperandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Bitwise operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on integer types, not on "
-										+ firstOperandType.getType().getName()
-										+ " and "
-										+ secondOperandType.getType().getName()
-										+ ".", IStatus.ERROR));
+			if (!isIntegerType(firstOperandType.getType()) || !isIntegerType(secondOperandType.getType())) {
+				return new InferenceResult(null, new InferenceIssue("Bitwise operator '" + o.getSymbol()
+						+ "' may only be applied on integer types, not on " + firstOperandType.getType().getName()
+						+ " and " + secondOperandType.getType().getName() + ".", IStatus.ERROR));
 			}
 			// second pass: compute common type
 			switch (o) {
@@ -274,18 +235,10 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		case ASSIGN_DIV:
 		case ASSIGN_MOD:
 			// only numeric types
-			if (!isNumericType(firstOperandType.getType())
-					|| !isNumericType(secondOperandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Arithmetic operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on numeric types, not on "
-										+ firstOperandType.getType().getName()
-										+ " and "
-										+ secondOperandType.getType().getName()
-										+ ".", IStatus.ERROR));
+			if (!isNumericType(firstOperandType.getType()) || !isNumericType(secondOperandType.getType())) {
+				return new InferenceResult(null, new InferenceIssue("Arithmetic operator '" + o.getSymbol()
+						+ "' may only be applied on numeric types, not on " + firstOperandType.getType().getName()
+						+ " and " + secondOperandType.getType().getName() + ".", IStatus.ERROR));
 			}
 
 			// second pass: compute type
@@ -309,18 +262,10 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		case LOGICAL_OR:
 		case LOGICAL_AND:
 			// only boolean types allowed
-			if (!isBooleanType(firstOperandType.getType())
-					|| !isBooleanType(secondOperandType.getType())) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Logical operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on boolean types, not on "
-										+ firstOperandType.getType().getName()
-										+ " and "
-										+ secondOperandType.getType().getName()
-										+ ".", IStatus.ERROR));
+			if (!isBooleanType(firstOperandType.getType()) || !isBooleanType(secondOperandType.getType())) {
+				return new InferenceResult(null, new InferenceIssue("Logical operator '" + o.getSymbol()
+						+ "' may only be applied on boolean types, not on " + firstOperandType.getType().getName()
+						+ " and " + secondOperandType.getType().getName() + ".", IStatus.ERROR));
 			}
 
 			// second pass: compute type
@@ -338,36 +283,19 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 			// check we have primitive types
 			if (!(firstOperandType.getType() instanceof PrimitiveType)
 					|| !(secondOperandType.getType() instanceof PrimitiveType)) {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Comparison operator '"
-										+ o.getSymbol()
-										+ "' may only be applied on primitive types, not on "
-										+ firstOperandType.getType().getName()
-										+ " and "
-										+ secondOperandType.getType().getName()
-										+ ".", IStatus.ERROR));
+				return new InferenceResult(null, new InferenceIssue("Comparison operator '" + o.getSymbol()
+						+ "' may only be applied on primitive types, not on " + firstOperandType.getType().getName()
+						+ " and " + secondOperandType.getType().getName() + ".", IStatus.ERROR));
 			}
 			// check types have the same base type
-			if (!EcoreUtil.equals(
-					getBaseType((PrimitiveType) firstOperandType.getType()),
+			if (!EcoreUtil.equals(getBaseType((PrimitiveType) firstOperandType.getType()),
 					getBaseType((PrimitiveType) secondOperandType.getType()))) {
 				// exclude coercion case (integer -> real)
-				if (!(isNumericType(firstOperandType.getType()) && isNumericType(secondOperandType
-						.getType()))) {
-					return new InferenceResult(
-							null,
-							new InferenceIssue(
-									"Comparison operator '"
-											+ o.getSymbol()
-											+ "' may only be applied on compatible types, not on "
-											+ firstOperandType.getType()
-													.getName()
-											+ " and "
-											+ secondOperandType.getType()
-													.getName() + ".",
-									IStatus.ERROR));
+				if (!(isNumericType(firstOperandType.getType()) && isNumericType(secondOperandType.getType()))) {
+					return new InferenceResult(null, new InferenceIssue("Comparison operator '" + o.getSymbol()
+							+ "' may only be applied on compatible types, not on "
+							+ firstOperandType.getType().getName() + " and " + secondOperandType.getType().getName()
+							+ ".", IStatus.ERROR));
 				}
 
 			}
@@ -380,74 +308,39 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 			if (firstOperandType.getType() instanceof PrimitiveType
 					&& secondOperandType.getType() instanceof PrimitiveType) {
 				// check primitive types have the same base type
-				if (!EcoreUtil
-						.equals(getBaseType((PrimitiveType) firstOperandType
-								.getType()),
-								getBaseType((PrimitiveType) secondOperandType
-										.getType()))) {
+				if (!EcoreUtil.equals(getBaseType((PrimitiveType) firstOperandType.getType()),
+						getBaseType((PrimitiveType) secondOperandType.getType()))) {
 					// handle coercion
-					if (!(isNumericType(firstOperandType.getType()) && isNumericType(secondOperandType
-							.getType()))) {
-						return new InferenceResult(
-								null,
-								o.equals(BinaryOperators.ASSIGN) ? new InferenceIssue(
-										"Assignment operator '"
-												+ o.getSymbol()
-												+ "' may only be applied on compatible types, not on "
-												+ firstOperandType.getType()
-														.getName()
-												+ " and "
-												+ secondOperandType.getType()
-														.getName() + ".",
-										IStatus.ERROR)
-										: new InferenceIssue(
-												"Comparison operator '"
-														+ o.getSymbol()
-														+ "' may only be applied on compatible types, not on "
-														+ firstOperandType
-																.getType()
-																.getName()
-														+ " and "
-														+ secondOperandType
-																.getType()
-																.getName()
-														+ ".", IStatus.ERROR));
+					if (!(isNumericType(firstOperandType.getType()) && isNumericType(secondOperandType.getType()))) {
+						return new InferenceResult(null, o.equals(BinaryOperators.ASSIGN) ? new InferenceIssue(
+								"Assignment operator '" + o.getSymbol()
+										+ "' may only be applied on compatible types, not on "
+										+ firstOperandType.getType().getName() + " and "
+										+ secondOperandType.getType().getName() + ".", IStatus.ERROR)
+								: new InferenceIssue("Comparison operator '" + o.getSymbol()
+										+ "' may only be applied on compatible types, not on "
+										+ firstOperandType.getType().getName() + " and "
+										+ secondOperandType.getType().getName() + ".", IStatus.ERROR));
 					} else {
-						if (o.equals(BinaryOperators.ASSIGN)
-								&& !isRealType(firstOperandType.getType())
+						if (o.equals(BinaryOperators.ASSIGN) && !isRealType(firstOperandType.getType())
 								&& isRealType(secondOperandType.getType())) {
 							// may only assign integer to real, not vice versa
-							return new InferenceResult(
-									null,
-									new InferenceIssue(
-											"Assignment operator '"
-													+ o.getSymbol()
-													+ "' may only be applied on compatible types, not on "
-													+ firstOperandType
-															.getType()
-															.getName()
-													+ " and "
-													+ secondOperandType
-															.getType()
-															.getName() + ".",
-											IStatus.ERROR));
+							return new InferenceResult(null, new InferenceIssue("Assignment operator '" + o.getSymbol()
+									+ "' may only be applied on compatible types, not on "
+									+ firstOperandType.getType().getName() + " and "
+									+ secondOperandType.getType().getName() + ".", IStatus.ERROR));
 						}
 					}
 				}
-			} else if (firstOperandType instanceof ComplexType
-					&& secondOperandType.getType() instanceof ComplexType) {
+			} else if (firstOperandType instanceof ComplexType && secondOperandType.getType() instanceof ComplexType) {
 				// no further checks for complex types required here
 				// (computation of type in second pass will result in null, if
 				// they are not compatible)
 			} else {
-				return new InferenceResult(
-						null,
-						new InferenceIssue(
-								"Assignment and equality operations may only be applied on types of the same kind, not on "
-										+ firstOperandType.getType().getName()
-										+ " and "
-										+ secondOperandType.getType().getName()
-										+ ".", IStatus.ERROR));
+				return new InferenceResult(null, new InferenceIssue(
+						"Assignment and equality operations may only be applied on types of the same kind, not on "
+								+ firstOperandType.getType().getName() + " and "
+								+ secondOperandType.getType().getName() + ".", IStatus.ERROR));
 			}
 
 			// second pass, compute types
@@ -463,32 +356,24 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 				throw new IllegalStateException("Unsupported operator kind.");
 			}
 		default:
-			throw new IllegalArgumentException("Unsupported binary operator: "
-					+ binaryOperator);
+			throw new IllegalArgumentException("Unsupported binary operator: " + binaryOperator);
 		}
 	}
 
-	public InferenceResult inferType(InferredType firstOperandType,
-			InferredType secondOperandType, InferredType thirdOperandType,
-			ITypeSystemOperator ternaryOperator) {
+	public InferenceResult inferType(InferredType firstOperandType, InferredType secondOperandType,
+			InferredType thirdOperandType, ITypeSystemOperator ternaryOperator) {
 		// some defense programming
-		if (firstOperandType == null || secondOperandType == null
-				|| thirdOperandType == null) {
+		if (firstOperandType == null || secondOperandType == null || thirdOperandType == null) {
 			throw new NullPointerException("Operand types may not be null");
 		}
 
 		// if the operand type could not be inferred, there is not much we can
 		// do here
-		if (firstOperandType.getType() == null
-				|| secondOperandType.getType() == null
+		if (firstOperandType.getType() == null || secondOperandType.getType() == null
 				|| thirdOperandType.getType() == null) {
-			return new InferenceResult(
-					null,
-					new InferenceIssue(
-							"Could not infer a type for the operation "
-									+ ternaryOperator.getSymbol()
-									+ ", because types were not inferred for all of its operands.",
-							IStatus.ERROR));
+			return new InferenceResult(null, new InferenceIssue("Could not infer a type for the operation "
+					+ ternaryOperator.getSymbol() + ", because types were not inferred for all of its operands.",
+					IStatus.ERROR));
 		}
 
 		// infer type based on operator
@@ -504,38 +389,31 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 						IStatus.ERROR));
 			}
 			// compute a union of the second and third operand types
-			InferenceResult unionResult = union(secondOperandType,
-					thirdOperandType);
+			InferenceResult unionResult = union(secondOperandType, thirdOperandType);
 			unionResult.getIssues().addAll(issues);
 			return unionResult;
 		default:
-			throw new IllegalArgumentException("Unsupported ternary operator: "
-					+ ternaryOperator);
+			throw new IllegalArgumentException("Unsupported ternary operator: " + ternaryOperator);
 		}
 	}
 
 	public InferenceResult union(InferredType firstType, InferredType secondType) {
 		// defense programming
 		if (firstType == null || secondType == null) {
-			throw new NullPointerException(
-					"firstType and secondType may not be null.");
+			throw new NullPointerException("firstType and secondType may not be null.");
 		}
 
 		// if the operand type could not be inferred, there is not much we can
 		// do here
 		if (firstType.getType() == null || secondType.getType() == null) {
-			return new InferenceResult(
-					null,
-					new InferenceIssue(
-							"Could not infer a type union, because not all given types were properly inferred in advance.",
-							IStatus.ERROR));
+			return new InferenceResult(null, new InferenceIssue(
+					"Could not infer a type union, because not all given types were properly inferred in advance.",
+					IStatus.ERROR));
 		}
 
 		// infer the union of both given types
-		if (firstType.getType() instanceof PrimitiveType
-				&& secondType.getType() instanceof PrimitiveType) {
-			Type commonType = computeCommonType(
-					(PrimitiveType) firstType.getType(),
+		if (firstType.getType() instanceof PrimitiveType && secondType.getType() instanceof PrimitiveType) {
+			Type commonType = computeCommonType((PrimitiveType) firstType.getType(),
 					(PrimitiveType) secondType.getType());
 			if (commonType != null) {
 				return new InferenceResult(new InferredType(commonType));
@@ -551,22 +429,19 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 				}
 			}
 		}
-		return new InferenceResult(null, new InferenceIssue(
-				"Cannot compute a type union for the given types: " + firstType
-						+ ", " + secondType, IStatus.ERROR));
+		return new InferenceResult(null, new InferenceIssue("Cannot compute a type union for the given types: "
+				+ firstType + ", " + secondType, IStatus.ERROR));
 	}
 
 	public List<Type> getTypes(InferenceResult inferenceResult) {
 		if (inferenceResult == null) {
-			throw new NullPointerException(
-					"InferenceResult result may not be null.");
+			throw new NullPointerException("InferenceResult result may not be null.");
 		}
 		// we do not evaluate constraints and we only have one type of a kind
 		if (inferenceResult.getType() == null) {
 			return Collections.<Type> emptyList();
 		} else {
-			return Collections.singletonList(inferenceResult.getType()
-					.getType());
+			return Collections.singletonList(inferenceResult.getType().getType());
 		}
 	}
 
@@ -621,8 +496,7 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 	}
 
 	public boolean isNumericType(Type type) {
-		return EcoreUtil.equals(getIntegerType(), type)
-				|| EcoreUtil.equals(getRealType(), type);
+		return EcoreUtil.equals(getIntegerType(), type) || EcoreUtil.equals(getRealType(), type);
 	}
 
 	public boolean isNumericType(InferredType type) {
@@ -636,4 +510,25 @@ public class STextDefaultTypeSystem extends AbstractTypeSystem implements
 		return Collections.singletonList(inferredType.getType());
 	}
 
+	public Object defaultValue(Type type) {
+		if (isBooleanType(type))
+			return true;
+		if (isIntegerType(type))
+			return 0;
+		if (isRealType(type))
+			return 0.0;
+		if (isVoidType(type))
+			return null;
+		if (isStringType(type))
+			return "";
+		if (type instanceof EnumerationType) {
+			return ((EnumerationType) type).getEnumerator().get(0);
+		}
+		throw new RuntimeException("No default value for type " + type);
+	}
+
+	public Object defaultValue(InferredType type) {
+		return defaultValue(type.getType());
+	}
+
 }

+ 0 - 14
plugins/org.yakindu.sct.simulation.core.sexec/src/org/yakindu/sct/simulation/core/sexec/container/DefaultExecutionContextInitializer.xtend

@@ -12,7 +12,6 @@ package org.yakindu.sct.simulation.core.sexec.container
 
 import com.google.inject.Inject
 import org.eclipse.xtext.naming.IQualifiedNameProvider
-import org.yakindu.base.types.EnumerationType
 import org.yakindu.base.types.ITypeSystem.InferredType
 import org.yakindu.sct.model.sexec.ExecutionFlow
 import org.yakindu.sct.model.sexec.TimeEvent
@@ -67,17 +66,4 @@ class DefaultExecutionContextInitializer implements IExecutionContextInitializer
 		it.type = new InferredType(integerType)
 		it.value = defaultValue(it.type)
 	}
-
-	def Object defaultValue(InferredType type) {
-		switch (type) {
-			case isBooleanType(type): true
-			case isIntegerType(type): 0
-			case isRealType(type): 0.0
-			case isVoidType(type): null
-			case isStringType(type): ""
-			case type instanceof EnumerationType: (type as EnumerationType).enumerator.head
-			default: null
-		}
-	}
-
 }