public void TestApplyStatement() { Dictionary test = CreateDictionary("Test"); NameSpace n1 = CreateNameSpace(test, "N1"); Structure s1 = CreateStructure(n1, "S1"); StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean"); Structure s2 = CreateStructure(n1, "S2"); StructureElement el2 = CreateStructureElement(s2, "E2", "S1"); Function function = CreateFunction(n1, "f", "S1"); Collection collection = CreateCollection(n1, "Col", "S1", 10); Variable v = CreateVariable(n1, "V", "Col"); Compiler.Compile_Synchronous(true, true); RuleCondition rc = CreateRuleAndCondition(n1, "Rule1"); Parser parser = new Parser(); { ApplyStatement statement = parser.Statement(rc, "APPLY X <- X", true, true) as ApplyStatement; Assert.IsNotNull(statement); } { ApplyStatement statement = parser.Statement(rc, "APPLY X <- X ON V | X.", true, true) as ApplyStatement; Assert.IsNotNull(statement); DerefExpression deref = statement.ConditionExpression as DerefExpression; Assert.IsNotNull(deref); ITypedElement element = deref.Arguments[0].Ref as ITypedElement; Assert.IsNotNull(element); Assert.AreEqual(element.Type, s1); } }
public void TestMapExpression() { Dictionary test = CreateDictionary("Test"); NameSpace n1 = CreateNameSpace(test, "N1"); Structure s1 = CreateStructure(n1, "S1"); StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean"); Structure s2 = CreateStructure(n1, "S2"); StructureElement el2 = CreateStructureElement(s2, "E2", "S1"); Function function = CreateFunction(n1, "f", "S1"); Collection collection = CreateCollection(n1, "Col", "S1", 10); Variable v = CreateVariable(n1, "V", "Col"); Compiler.Compile_Synchronous(true, true); RuleCondition rc = CreateRuleAndCondition(n1, "Rule1"); Parser parser = new Parser(); { MapExpression expression = parser.Expression(rc, "MAP V | X. USING X IN X.E2", null, true, null, true, true) as MapExpression; Assert.IsNotNull(expression); DerefExpression deref = expression.Condition as DerefExpression; Assert.IsNotNull(deref); ITypedElement element = deref.Arguments[0].Ref as ITypedElement; Assert.IsNotNull(element); Assert.AreEqual(element.Type, s1); } }
public void TestStructureDereference() { Dictionary dictionary = CreateDictionary("Test"); NameSpace nameSpace = CreateNameSpace(dictionary, "N1"); Structure structure = CreateStructure(nameSpace, "Struct"); StructureElement el1 = CreateStructureElement(structure, "e1", "Boolean"); Function f = CreateFunction(nameSpace, "f", "Struct"); Variable v = CreateVariable(nameSpace, "v", "Struct"); Variable v2 = CreateVariable(nameSpace, "v2", "Boolean"); Parser parser = new Parser(); { DerefExpression expression = parser.Expression(nameSpace, "N1.v.e1") as DerefExpression; Assert.IsNotNull(expression); Assert.AreEqual(3, expression.Arguments.Count); Assert.AreEqual(v, expression.Arguments[1].Ref); Assert.AreEqual(el1, expression.Arguments [2].Ref); } { DerefExpression expression = parser.Expression(nameSpace, "N1.f().e1") as DerefExpression; Assert.IsNotNull(expression); Assert.AreEqual(2, expression.Arguments.Count); Assert.AreEqual(structure, expression.Arguments [0].Ref); Assert.AreEqual(el1, expression.Arguments [1].Ref); } { DerefExpression expression = parser.Expression(nameSpace, "N1.Struct.e1") as DerefExpression; Assert.IsNotNull(expression); Assert.AreEqual(3, expression.Arguments.Count); Assert.AreEqual(structure, expression.Arguments[1].Ref); Assert.AreEqual(el1, expression.Arguments[2].Ref); } RuleCondition condition = CreateRuleAndCondition(nameSpace, "Test"); Action action1 = CreateAction(condition, "Struct.e1 <- True"); Action action2 = CreateAction(condition, "v2 <- Struct.e1"); Action action3 = CreateAction(condition, "f().e1 <- True"); Action action4 = CreateAction(condition, "v2 <- f().e1"); Collection collection = CreateCollection(nameSpace, "Col", "Struct", 10); Variable v3 = CreateVariable(nameSpace, "v3", "Col"); Action action5 = CreateAction(condition, "(FIRST X IN v3).e1 <- True"); RuleCondition ruleCondition2 = CreateRuleAndCondition(structure, "Rule"); Action action6 = CreateAction(ruleCondition2, "THIS.e1 <- True"); Action action7 = CreateAction(ruleCondition2, "v2 <- THIS.e1"); RuleCheckerVisitor visitor = new RuleCheckerVisitor(dictionary); visitor.visit(nameSpace); Assert.True(HasMessagePart(action1, "structure should not be used to reference an instance")); Assert.True(HasMessagePart(action2, "structure should not be used to reference an instance")); Assert.False(HasMessagePart(action3, "structure should not be used to reference an instance")); Assert.False(HasMessagePart(action4, "structure should not be used to reference an instance")); Assert.False(HasMessagePart(action5, "structure should not be used to reference an instance")); Assert.False(HasMessagePart(action6, "structure should not be used to reference an instance")); Assert.False(HasMessagePart(action7, "structure should not be used to reference an instance")); }
/// <summary> /// Provides the context on which function evaluation should be performed /// </summary> /// <param name="context"></param> /// <param name="explain"></param> /// <returns></returns> private InterpretationContext GetContext(InterpretationContext context, ExplanationPart explain) { InterpretationContext retVal = context; DerefExpression deref = Call.Called as DerefExpression; if (deref != null) { IValue value = deref.GetPrefixValue(context, deref.Arguments.Count - 1, explain) as IValue; if (value != null) { retVal = new InterpretationContext(context, value); } } return(retVal); }
/// <summary> /// Provides the context on which function evaluation should be performed /// </summary> /// <param name="context"></param> /// <returns></returns> private InterpretationContext getContext(InterpretationContext context) { InterpretationContext retVal = context; DerefExpression deref = Call.Called as DerefExpression; if (deref != null) { Values.IValue value = deref.GetPrefixValue(context, deref.Arguments.Count - 1) as Values.IValue; if (value != null) { retVal = new InterpretationContext(context, value); } } return(retVal); }
protected override void VisitDerefExpression(DerefExpression derefExpression) { ModelElement backup = BaseLocation; foreach (Expression expression in derefExpression.Arguments) { if (expression != null) { ModelElement model = expression.Ref as ModelElement; if (model is Structure && expression is Call) { // Because this is the return value instead of the target element model = null; } if (model != null) { string referenceName = model.ReferenceName(BaseLocation); ReplaceText(referenceName, expression.Start, expression.End); break; } else { BaseLocation = backup; VisitExpression(expression); BaseLocation = expression.GetExpressionType(); } if (expression.Ref != null) { ITypedElement typedElement = expression.Ref as ITypedElement; if (typedElement != null && typedElement.Type != null) { BaseLocation = typedElement.Type; } else { BaseLocation = expression.Ref as ModelElement; } } } } BaseLocation = backup; }
/// <summary> /// Checks the statement for semantical errors /// </summary> public override void CheckStatement() { if (Call != null) { Call.CheckExpression(); Procedure procedure = Call.Called.Ref as Procedure; if (procedure == null) { if (Call.Called.Ref is Function) { Root.AddError("Invalid call : Function " + Call.Called + " called as a procedure"); } else { Root.AddError("Cannot determine called procedure " + Call.Called); } } else { if (procedure.Enclosing is Structure) { DerefExpression deref = Call.Called as DerefExpression; if (deref != null) { int count = deref.Arguments.Count; Expression baseExpression = deref.Arguments[count - 2]; INamable referenced = baseExpression.Ref; if ((referenced is NameSpace) || (referenced is Structure && !(baseExpression is Call))) { Root.AddError( "Invalid procedure call : context should be the instance on which the call is performed"); } } } } } else { Root.AddError("Cannot parse called procedure for " + ToString()); } }
public void TestFunctionCall() { Dictionary test = CreateDictionary("Test"); NameSpace n1 = CreateNameSpace(test, "N1"); Structure s1 = CreateStructure(n1, "S1"); StructureElement el1 = CreateStructureElement(s1, "E1", "Boolean"); Structure s2 = CreateStructure(n1, "S2"); StructureElement el2 = CreateStructureElement(s2, "E2", "S1"); Variable v = CreateVariable(n1, "V", "S1"); v.setDefaultValue("N1.S1 { E1 => True }"); Function function = CreateFunction(n1, "f", "S1"); Compiler.Compile_Synchronous(true, true); RuleCondition rc = CreateRuleAndCondition(n1, "Rule1"); Parser parser = new Parser(); { VariableUpdateStatement statement = parser.Statement(rc, "V <- f().S", true, true) as VariableUpdateStatement; Assert.IsNotNull(statement); Assert.AreEqual(statement.VariableIdentification.Ref, v); DerefExpression deref = statement.Expression as DerefExpression; Assert.IsNotNull(deref); Assert.AreEqual(deref.Arguments[0].Ref, s1); } { VariableUpdateStatement statement = parser.Statement(rc, "V <- f().", true, true) as VariableUpdateStatement; Assert.IsNotNull(statement); Assert.AreEqual(statement.VariableIdentification.Ref, v); DerefExpression deref = statement.Expression as DerefExpression; Assert.IsNotNull(deref); Assert.AreEqual(deref.Arguments[0].Ref, s1); } }
protected override void VisitDerefExpression(DerefExpression derefExpression) { ModelElement backup = BaseLocation; foreach (Expression expression in derefExpression.Arguments) { if (expression != null) { ModelElement model = expression.Ref as ModelElement; if (model != null) { string referenceName = model.ReferenceName(BaseLocation); ReplaceText(referenceName, expression.Start, expression.End); break; } else { BaseLocation = backup; VisitExpression(expression); BaseLocation = expression.GetExpressionType(); } if (expression.Ref != null) { ITypedElement typedElement = expression.Ref as ITypedElement; if (typedElement != null && typedElement.Type != null) { BaseLocation = typedElement.Type; } else { BaseLocation = expression.Ref as ModelElement; } } } } BaseLocation = backup; }
protected override void VisitDerefExpression(DerefExpression derefExpression) { ModelElement context = User; ResetRemoveIndexes(); int i = 1; foreach (Expression expression in derefExpression.Arguments) { if (expression != null) { bool replaced = false; if (ReplaceAllReferences) { if (i == derefExpression.Arguments.Count) { replaced = ReplaceNonTerminal(expression, expression.Ref as ModelElement); } } else { if (expression.Ref is Structure) { UnaryExpression unaryExpression = expression as UnaryExpression; if (expression is Call) { replaced = ReplaceNonTerminal(expression, null); } else if (unaryExpression != null && unaryExpression.Expression != null) { replaced = ReplaceNonTerminal(expression, null); } else if (unaryExpression != null && unaryExpression.Term != null && unaryExpression.Term.Designator != null && unaryExpression.Term.Designator.IsPredefined()) { replaced = ReplaceNonTerminal(expression, null); } else { replaced = ReplaceNonTerminal(expression, expression.Ref as ModelElement); } } else { replaced = ReplaceNonTerminal(expression, expression.Ref as ModelElement); } } if (!replaced) { if (expression.Ref is NameSpace || expression.Ref is StateMachine || expression.Ref is State) { // Remove all namespace prefixes, they will be taken into account in ReferenceName function StartRemove = Math.Min(expression.Start, StartRemove); EndRemove = Math.Max(expression.End + 1, EndRemove); } else { VisitExpression(expression); ResetRemoveIndexes(); User = expression.GetExpressionType(); } } } i += 1; } User = context; }
protected override void VisitDerefExpression(DerefExpression derefExpression) { ModelElement context = User; ResetRemoveIndexes(); int i = 1; foreach (Expression expression in derefExpression.Arguments) { if (expression != null) { bool replaced = false; if (ReplaceAllReferences) { if (i == derefExpression.Arguments.Count) { replaced = ReplaceNonTerminal(expression, expression.Ref as ModelElement); } } else { if (expression is Call && expression.Ref is Structure) { replaced = ReplaceNonTerminal(expression, null); } else { replaced = ReplaceNonTerminal(expression, expression.Ref as ModelElement); } } if (!replaced) { if (expression.Ref is NameSpace || expression.Ref is StateMachine || expression.Ref is State) { // Remove all namespace prefixes, they will be taken into account in ReferenceName function StartRemove = Math.Min(expression.Start, StartRemove); EndRemove = Math.Max(expression.End + 1, EndRemove); } else { VisitExpression(expression); ResetRemoveIndexes(); User = expression.GetExpressionType(); } } } i += 1; } User = context; }
protected override void VisitDerefExpression(DerefExpression derefExpression) { ModelElement backup = BaseLocation; foreach (Expression expression in derefExpression.Arguments) { if (expression != null) { ModelElement model = expression.Ref as ModelElement; if (model is Structure) { if (expression is Call || expression is ListExpression) { // Because this is the return value instead of the target element model = null; } else { UnaryExpression unaryExpression = expression as UnaryExpression; if (unaryExpression != null) { if (unaryExpression.Term != null && unaryExpression.Term.Designator != null && unaryExpression.Term.Designator.IsPredefined()) { // No need to refactor a predefined item model = null; } else if (unaryExpression.Expression != null) { // No need to change the enclosing parenthesed expression // Let's rely on the recursive call model = null; } } } } if (model != null) { string referenceName = model.ReferenceName(BaseLocation); ReplaceText(referenceName, expression.Start, expression.End); break; } else { BaseLocation = backup; VisitExpression(expression); BaseLocation = expression.GetExpressionType(); } if (expression.Ref != null) { ITypedElement typedElement = expression.Ref as ITypedElement; if (typedElement != null && typedElement.Type != null) { BaseLocation = typedElement.Type; } else { BaseLocation = expression.Ref as ModelElement; } } } } BaseLocation = backup; }