Search in sources :

Example 1 with OperationDefinition

use of org.yakindu.sct.model.stext.stext.OperationDefinition in project statecharts by Yakindu.

the class StextTestFactory method _createOperation.

public static OperationDefinition _createOperation(String name, Scope scope) {
    OperationDefinition e = StextFactory.eINSTANCE.createOperationDefinition();
    e.setName(name);
    if (scope != null)
        scope.getDeclarations().add(e);
    return e;
}
Also used : OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition)

Example 2 with OperationDefinition

use of org.yakindu.sct.model.stext.stext.OperationDefinition in project statecharts by Yakindu.

the class ModelSequencertDeclarationsTest method testOperationMapping.

/**
 * The OperationCalls must map to Operations in Scopes inside the Flow..
 */
@Test
public void testOperationMapping() {
    Statechart sc = _createStatechart("test");
    InterfaceScope s_scope = _createInterfaceScope("Interface", sc);
    OperationDefinition _operation = _createOperation("value", s_scope);
    Region r = _createRegion("main", sc);
    State s1 = _createState("S1", r);
    State s2 = _createState("S2", r);
    Transition t = _createTransition(s1, s2);
    ReactionTrigger tr = _createReactionTrigger(t);
    tr.setGuard(createGuardExpression(_createValue(true)));
    ReactionEffect tr1 = _createReactionEffect(t);
    FeatureCall _operationCall = _createOperationCall(_operation);
    tr1.getActions().add(_operationCall);
    ExecutionFlow flow = sequencer.transform(sc);
    OperationDefinition _o1 = (OperationDefinition) flow.getScopes().get(0).getDeclarations().get(0);
    assertNotSame(_operation, _o1);
    assertEquals(_operation.getName(), _o1.getName());
    Step step = flow.getStates().get(0).getReactSequence().getSteps().get(0);
    If _if = (If) assertedSequence(assertedSequence(assertedSequence(step).getSteps().get(0)).getSteps().get(0)).getSteps().get(0);
    Step thenSequence = assertedSequence(((Call) _if.getThenStep()).getStep()).getSteps().get(1);
    Execution call = (Execution) assertedSequence(thenSequence).getSteps().get(0);
    assertNotSame(_operationCall, call.getStatement());
    assertSame(_o1, ((FeatureCall) call.getStatement()).getFeature());
}
Also used : ExecutionFlow(org.yakindu.sct.model.sexec.ExecutionFlow) InterfaceScope(org.yakindu.sct.model.stext.stext.InterfaceScope) StextTestFactory._createInterfaceScope(org.yakindu.sct.model.stext.test.util.StextTestFactory._createInterfaceScope) ReactionEffect(org.yakindu.sct.model.stext.stext.ReactionEffect) StextTestFactory._createReactionEffect(org.yakindu.sct.model.stext.test.util.StextTestFactory._createReactionEffect) Step(org.yakindu.sct.model.sexec.Step) Execution(org.yakindu.sct.model.sexec.Execution) StextTestFactory._createReactionTrigger(org.yakindu.sct.model.stext.test.util.StextTestFactory._createReactionTrigger) ReactionTrigger(org.yakindu.sct.model.stext.stext.ReactionTrigger) SGraphTestFactory._createState(org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createState) State(org.yakindu.sct.model.sgraph.State) Transition(org.yakindu.sct.model.sgraph.Transition) SGraphTestFactory._createTransition(org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createTransition) Statechart(org.yakindu.sct.model.sgraph.Statechart) SGraphTestFactory._createStatechart(org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createStatechart) Region(org.yakindu.sct.model.sgraph.Region) SGraphTestFactory._createRegion(org.yakindu.sct.model.sgraph.test.util.SGraphTestFactory._createRegion) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) FeatureCall(org.yakindu.base.expressions.expressions.FeatureCall) If(org.yakindu.sct.model.sexec.If) Test(org.junit.Test)

Example 3 with OperationDefinition

use of org.yakindu.sct.model.stext.stext.OperationDefinition 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 4 with OperationDefinition

use of org.yakindu.sct.model.stext.stext.OperationDefinition in project statecharts by Yakindu.

the class STextJavaValidatorTest method checkOptionalArgumentsAreLast.

@Test
public void checkOptionalArgumentsAreLast() {
    Scope scope = (Scope) super.parseExpression("internal: ", InternalScope.class.getSimpleName());
    OperationDefinition op = StextTestFactory._createOperation("op", scope);
    tester.validate(scope).assertOK();
    // optional parameter last => no error
    op.getParameters().add(typesTestFactory.createParameter("p1", ITypeSystem.INTEGER, false));
    op.getParameters().add(typesTestFactory.createParameter("p2", ITypeSystem.INTEGER, true));
    tester.validate(op).assertOK();
    // optional parameter not last anymore => error
    op.getParameters().add(typesTestFactory.createParameter("p3", ITypeSystem.INTEGER, false));
    tester.validate(op).assertError(ERROR_OPTIONAL_MUST_BE_LAST_CODE);
}
Also used : Scope(org.yakindu.sct.model.sgraph.Scope) InternalScope(org.yakindu.sct.model.stext.stext.InternalScope) ImportScope(org.yakindu.sct.model.stext.stext.ImportScope) InterfaceScope(org.yakindu.sct.model.stext.stext.InterfaceScope) OperationDefinition(org.yakindu.sct.model.stext.stext.OperationDefinition) Test(org.junit.Test)

Example 5 with OperationDefinition

use of org.yakindu.sct.model.stext.stext.OperationDefinition 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

OperationDefinition (org.yakindu.sct.model.stext.stext.OperationDefinition)5 Test (org.junit.Test)3 ElementReferenceExpression (org.yakindu.base.expressions.expressions.ElementReferenceExpression)2 Statechart (org.yakindu.sct.model.sgraph.Statechart)2 InterfaceScope (org.yakindu.sct.model.stext.stext.InterfaceScope)2 InternalScope (org.yakindu.sct.model.stext.stext.InternalScope)2 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 Check (org.eclipse.xtext.validation.Check)1 Argument (org.yakindu.base.expressions.expressions.Argument)1 FeatureCall (org.yakindu.base.expressions.expressions.FeatureCall)1 Declaration (org.yakindu.base.types.Declaration)1 Parameter (org.yakindu.base.types.Parameter)1 Execution (org.yakindu.sct.model.sexec.Execution)1 ExecutionFlow (org.yakindu.sct.model.sexec.ExecutionFlow)1 If (org.yakindu.sct.model.sexec.If)1 Step (org.yakindu.sct.model.sexec.Step)1 Region (org.yakindu.sct.model.sgraph.Region)1 Scope (org.yakindu.sct.model.sgraph.Scope)1