Forráskód Böngészése

added missing validation rule (#1674)

Andreas Mülder 8 éve
szülő
commit
6396a6e6d6

+ 36 - 23
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/validation/STextJavaValidator.java

@@ -106,6 +106,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
+
 /**
  * Several validations for nonsensical expressions.
  * 
@@ -435,26 +436,24 @@ public class STextJavaValidator extends AbstractSTextJavaValidator implements ST
 			}
 		}
 	}
-	
+
 	@Check(CheckType.FAST)
 	public void checkAnnotations(final Statechart statechart) {
 		Annotation eventDriven = statechart.getAnnotationOfType(EVENT_DRIVEN_ANNOTATION);
 		Annotation cycleBased = statechart.getAnnotationOfType(CYCLE_BASED_ANNOTATION);
-		
-		if(eventDriven != null && cycleBased != null) {
-			String errorMsg = String.format(CONTRADICTORY_ANNOTATIONS, String.join(
-					", ", eventDriven.getType().toString(), cycleBased.getType().toString()
-					));
+
+		if (eventDriven != null && cycleBased != null) {
+			String errorMsg = String.format(CONTRADICTORY_ANNOTATIONS,
+					String.join(", ", eventDriven.getType().toString(), cycleBased.getType().toString()));
 			error(errorMsg, cycleBased, null, -1);
 		}
-		
+
 		Annotation parentFirst = statechart.getAnnotationOfType(PARENT_FIRST_ANNOTATION);
 		Annotation childFirst = statechart.getAnnotationOfType(CHILD_FIRST_ANNOTATION);
-		
-		if(parentFirst != null && childFirst != null) {
-			String errorMsg = String.format(CONTRADICTORY_ANNOTATIONS, String.join(
-					", ", parentFirst.getType().toString(), childFirst.getType().toString()
-					));
+
+		if (parentFirst != null && childFirst != null) {
+			String errorMsg = String.format(CONTRADICTORY_ANNOTATIONS,
+					String.join(", ", parentFirst.getType().toString(), childFirst.getType().toString()));
 			error(errorMsg, parentFirst, null, -1);
 		}
 	}
@@ -533,11 +532,11 @@ public class STextJavaValidator extends AbstractSTextJavaValidator implements ST
 			}
 		}
 	}
-	
+
 	protected boolean isTopLevelRegion(final Region region) {
 		return region.eContainer() instanceof Statechart;
 	}
-	
+
 	@Check(CheckType.NORMAL)
 	public void checkTopLevelRegionHasEntry(final Region region) {
 		if (isTopLevelRegion(region)) {
@@ -612,15 +611,7 @@ public class STextJavaValidator extends AbstractSTextJavaValidator implements ST
 		for (int i = 0; i < reactionTrigger.getTriggers().size(); i++) {
 			EventSpec eventSpec = reactionTrigger.getTriggers().get(i);
 			if (eventSpec instanceof RegularEventSpec) {
-
-				Expression eventExpression = ((RegularEventSpec) eventSpec).getEvent();
-				EObject element = null;
-				if (eventExpression instanceof ElementReferenceExpression) {
-					element = ((ElementReferenceExpression) eventExpression).getReference();
-				} else if (eventExpression instanceof FeatureCall) {
-					element = ((FeatureCall) eventExpression).getFeature();
-				}
-
+				EObject element = unwrap(((RegularEventSpec) eventSpec).getEvent());
 				if (element != null && (!(element instanceof Event))) {
 					String elementName = "";
 					if (element instanceof NamedElement) {
@@ -633,6 +624,28 @@ public class STextJavaValidator extends AbstractSTextJavaValidator implements ST
 		}
 	}
 
+	@Check(CheckType.FAST)
+	public void checkRaisingExpressionEvent(EventRaisingExpression expression) {
+		EObject element = unwrap(expression.getEvent());
+		if (element != null && (!(element instanceof Event))) {
+			String elementName = "";
+			if (element instanceof NamedElement) {
+				elementName = ((NamedElement) element).getName();
+			}
+			error(String.format("'%s' is not an event.", elementName), StextPackage.Literals.EVENT_RAISING_EXPRESSION__EVENT, -1);
+		}
+	}
+
+	protected EObject unwrap(Expression eventExpression) {
+		EObject element = null;
+		if (eventExpression instanceof ElementReferenceExpression) {
+			element = ((ElementReferenceExpression) eventExpression).getReference();
+		} else if (eventExpression instanceof FeatureCall) {
+			element = ((FeatureCall) eventExpression).getFeature();
+		}
+		return element;
+	}
+
 	/**
 	 * Only Expressions that produce an effect should be used as actions.
 	 * 

+ 16 - 0
test-plugins/org.yakindu.sct.model.stext.test/src/org/yakindu/sct/model/stext/test/validation/STextJavaValidatorTest.java

@@ -453,6 +453,22 @@ public class STextJavaValidatorTest extends AbstractSTextValidationTest implemen
 		validationResult.assertAll(errorMsg("Trigger 'x' is no event."), errorMsg("Trigger 'y' is no event."));
 
 	}
+	
+	@Test
+	public void checkRaisingExpressionEvent() {
+
+		String scope = "interface : in event e  var x : integer  var y : integer  operation op():integer";
+
+		EObject model = super.parseExpression("raise e", ReactionEffect.class.getSimpleName(), scope);
+		AssertableDiagnostics validationResult = tester.validate(model);
+		validationResult.assertOK();
+		
+		model = super.parseExpression("raise y", ReactionEffect.class.getSimpleName(), scope);
+		validationResult = tester.validate(model);
+		validationResult.assertAll(errorMsg("'y' is not an event."));
+	}
+	
+	
 
 	/**
 	 * @see STextJavaValidator#checkReactionEffectActions(org.yakindu.sct.model.stext.stext.ReactionEffect)