Search in sources :

Example 16 with ElementReferenceExpression

use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.

the class TypeInferrerTest method testOperationCallWithOptionalParameter.

@Test
public void testOperationCallWithOptionalParameter() {
    OperationDefinition opDef = StextTestFactory._createOperation("opWithOptionals", StextFactory.eINSTANCE.createInternalScope());
    Parameter pReq = typesFactory.createParameter("pReq", ITypeSystem.INTEGER, false);
    Parameter pOpt = typesFactory.createParameter("pOpt", ITypeSystem.INTEGER, true);
    opDef.getParameters().add(pReq);
    opDef.getParameters().add(pOpt);
    Argument boolArg = (Argument) parseExpression("true", Argument.class.getSimpleName());
    Argument intArg = (Argument) parseExpression("17", Argument.class.getSimpleName());
    // opWithOptionals(17, 17) => valid
    ElementReferenceExpression opCall1 = StextTestFactory._createOperationCall(opDef, intArg, intArg);
    expectNoErrors(opCall1);
    // opWithOptionals(17) => valid, because of optional parameter
    ElementReferenceExpression opCall2 = StextTestFactory._createOperationCall(opDef, intArg);
    expectNoErrors(opCall2);
    // opWithOptionals(true) => invalid
    ElementReferenceExpression opCall3 = StextTestFactory._createOperationCall(opDef, boolArg);
    expectError(opCall3, ITypeSystemInferrer.NOT_COMPATIBLE_CODE);
}
Also used : Argument(org.yakindu.base.expressions.expressions.Argument) Parameter(org.yakindu.base.types.Parameter) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) Test(org.junit.Test) AbstractTypeInferrerTest(org.yakindu.sct.model.stext.test.util.AbstractTypeInferrerTest)

Example 17 with ElementReferenceExpression

use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.

the class Assert method assertAssignment.

public static void assertAssignment(Step step, String variableName, AssignmentOperator operator, String value) {
    assertClass(Execution.class, step);
    Execution exec = (Execution) step;
    assertTrue(exec.getStatement() instanceof AssignmentExpression);
    AssignmentExpression assignment = (AssignmentExpression) exec.getStatement();
    assertEquals(operator, assignment.getOperator());
    Expression varRef = assignment.getVarRef();
    if (varRef instanceof ElementReferenceExpression) {
        ElementReferenceExpression elementRef = (ElementReferenceExpression) varRef;
        assertEquals(variableName, ((NamedElement) elementRef.getReference()).getName());
    } else if (varRef instanceof FeatureCall) {
        FeatureCall call = (FeatureCall) varRef;
        assertEquals(variableName, ((NamedElement) call.getFeature()).getName());
    }
    assertExpressionEquals(value, assignment.getExpression());
}
Also used : Execution(org.yakindu.sct.model.sexec.Execution) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) Expression(org.yakindu.base.expressions.expressions.Expression) PrimitiveValueExpression(org.yakindu.base.expressions.expressions.PrimitiveValueExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) FeatureCall(org.yakindu.base.expressions.expressions.FeatureCall) NamedElement(org.yakindu.base.base.NamedElement)

Example 18 with ElementReferenceExpression

use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.

the class StextTestFactory method _createElementReferenceExpression.

public static ElementReferenceExpression _createElementReferenceExpression(NamedElement target) {
    ElementReferenceExpression referenceExpression = ExpressionsFactory.eINSTANCE.createElementReferenceExpression();
    referenceExpression.setReference(target);
    return referenceExpression;
}
Also used : ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression)

Example 19 with ElementReferenceExpression

use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.

the class STextJavaValidator method checkValueDefinitionExpression.

@Check(CheckType.FAST)
public void checkValueDefinitionExpression(VariableDefinition definition) {
    // applies only to constants
    if (!definition.isConst())
        return;
    Expression initialValue = definition.getInitialValue();
    if (initialValue == null) {
        error(CONST_MUST_HAVE_VALUE_MSG, definition, null, CONST_MUST_HAVE_VALUE_CODE);
        return;
    }
    List<Expression> toCheck = Lists.newArrayList(initialValue);
    TreeIterator<EObject> eAllContents = initialValue.eAllContents();
    while (eAllContents.hasNext()) {
        EObject next = eAllContents.next();
        if (next instanceof Expression)
            toCheck.add((Expression) next);
    }
    for (Expression expression : toCheck) {
        EObject referencedObject = null;
        if (expression instanceof FeatureCall)
            referencedObject = ((FeatureCall) expression).getFeature();
        else if (expression instanceof ElementReferenceExpression)
            referencedObject = ((ElementReferenceExpression) expression).getReference();
        if (referencedObject instanceof VariableDefinition) {
            if (!((VariableDefinition) referencedObject).isConst()) {
                error(REFERENCE_TO_VARIABLE, StextPackage.Literals.VARIABLE_DEFINITION__INITIAL_VALUE);
            }
        }
    }
}
Also used : VariableDefinition(org.yakindu.sct.model.stext.stext.VariableDefinition) EventRaisingExpression(org.yakindu.sct.model.stext.stext.EventRaisingExpression) Expression(org.yakindu.base.expressions.expressions.Expression) AssignmentExpression(org.yakindu.base.expressions.expressions.AssignmentExpression) EventValueReferenceExpression(org.yakindu.sct.model.stext.stext.EventValueReferenceExpression) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) PostFixUnaryExpression(org.yakindu.base.expressions.expressions.PostFixUnaryExpression) EObject(org.eclipse.emf.ecore.EObject) FeatureCall(org.yakindu.base.expressions.expressions.FeatureCall) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) Check(org.eclipse.xtext.validation.Check)

Example 20 with ElementReferenceExpression

use of org.yakindu.base.expressions.expressions.ElementReferenceExpression in project statecharts by Yakindu.

the class STextJavaValidator method checkUnusedVariablesInInternalScope.

@Check(CheckType.FAST)
public void checkUnusedVariablesInInternalScope(InternalScope internalScope) {
    EList<Declaration> internalScopeDeclarations = internalScope.getDeclarations();
    EObject rootContainer = EcoreUtil.getRootContainer(internalScope);
    Resource rootRes = getResource(rootContainer);
    Statechart statechart = (Statechart) EcoreUtil.getObjectByType(rootRes.getContents(), SGraphPackage.Literals.STATECHART);
    if (statechart == null)
        return;
    List<ElementReferenceExpression> allUsedElementReferences = EcoreUtil2.getAllContentsOfType(statechart, ElementReferenceExpression.class);
    if (statechart.getSpecification() != null) {
        for (Declaration internalDeclaration : internalScopeDeclarations) {
            boolean internalDeclarationUsed = false;
            for (ElementReferenceExpression elementReference : allUsedElementReferences) {
                if (elementReference.getReference().eContainer() instanceof InternalScope) {
                    if (elementReference.getReference() instanceof VariableDefinition) {
                        if (((VariableDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof VariableDefinition) {
                            internalDeclarationUsed = true;
                            break;
                        }
                    } else if (elementReference.getReference() instanceof EventDefinition) {
                        if (((EventDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof EventDefinition) {
                            internalDeclarationUsed = true;
                            break;
                        }
                    } else if (elementReference.getReference() instanceof OperationDefinition) {
                        if (((OperationDefinition) elementReference.getReference()).getName().equals(internalDeclaration.getName()) && internalDeclaration instanceof OperationDefinition) {
                            internalDeclarationUsed = true;
                            break;
                        }
                    }
                }
            }
            if (!internalDeclarationUsed) {
                if (internalDeclaration instanceof VariableDefinition || internalDeclaration instanceof EventDefinition || internalDeclaration instanceof OperationDefinition)
                    warning(INTERNAL_DECLARATION_UNUSED, internalDeclaration, null, -1);
            }
        }
    }
}
Also used : VariableDefinition(org.yakindu.sct.model.stext.stext.VariableDefinition) EObject(org.eclipse.emf.ecore.EObject) AbstractSCTResource(org.yakindu.sct.model.sgraph.resource.AbstractSCTResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) InternalScope(org.yakindu.sct.model.stext.stext.InternalScope) Statechart(org.yakindu.sct.model.sgraph.Statechart) Declaration(org.yakindu.base.types.Declaration) EventDefinition(org.yakindu.sct.model.stext.stext.EventDefinition) ElementReferenceExpression(org.yakindu.base.expressions.expressions.ElementReferenceExpression) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) Check(org.eclipse.xtext.validation.Check)

Aggregations

ElementReferenceExpression (org.yakindu.base.expressions.expressions.ElementReferenceExpression)23 EObject (org.eclipse.emf.ecore.EObject)10 Test (org.junit.Test)10 AssignmentExpression (org.yakindu.base.expressions.expressions.AssignmentExpression)9 FeatureCall (org.yakindu.base.expressions.expressions.FeatureCall)9 ReactionTrigger (org.yakindu.sct.model.stext.stext.ReactionTrigger)9 Expression (org.yakindu.base.expressions.expressions.Expression)7 Statechart (org.yakindu.sct.model.sgraph.Statechart)7 EventDefinition (org.yakindu.sct.model.stext.stext.EventDefinition)7 StextTestFactory._createReactionTrigger (org.yakindu.sct.model.stext.test.util.StextTestFactory._createReactionTrigger)7 Transition (org.yakindu.sct.model.sgraph.Transition)6 VariableDefinition (org.yakindu.sct.model.stext.stext.VariableDefinition)6 Check (org.eclipse.xtext.validation.Check)5 PrimitiveValueExpression (org.yakindu.base.expressions.expressions.PrimitiveValueExpression)5 NamedElement (org.yakindu.base.base.NamedElement)4 PostFixUnaryExpression (org.yakindu.base.expressions.expressions.PostFixUnaryExpression)4 ExecutionFlow (org.yakindu.sct.model.sexec.ExecutionFlow)4 If (org.yakindu.sct.model.sexec.If)4 Region (org.yakindu.sct.model.sgraph.Region)4 SGraphTestFactory._createRegion (org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createRegion)4