public void TestUncacheFunctionBasedOnInnerFunctionCall() { Dictionary test = CreateDictionary("Test"); NameSpace n1 = CreateNameSpace(test, "N1"); Collection c = CreateCollection(n1, "C", "Integer", 10); Variable a = CreateVariable(n1, "a", "C"); a.setDefaultValue("[1]"); Variable b = CreateVariable(n1, "b", "C"); b.setDefaultValue("[10]"); Function f = CreateFunction(n1, "f", "C"); Case always_f = CreateCase(f, "Always", "g() + h()"); Function g = CreateFunction(n1, "g", "C"); Case always_g = CreateCase(g, "Always", "a"); Function h = CreateFunction(n1, "h", "C"); Case always_h = CreateCase(h, "Always", "b"); RuleCondition updateGlobalVariables = CreateRuleAndCondition(n1, "Update Global variables"); Action act1 = CreateAction(updateGlobalVariables, "a <- a + [2]"); Action act2 = CreateAction(updateGlobalVariables, "b <- b + [20]"); Compiler.Compile_Synchronous(true); Expression expression = new Parser().Expression(test, "N1.f()"); ListValue value = expression.GetExpressionValue(new InterpretationContext(), null) as ListValue; Assert.IsNotNull(value); Assert.AreEqual(2, value.Val.Count); // ReSharper disable once UseObjectOrCollectionInitializer Runner runner = new Runner(false); runner.CheckForCompatibleChanges = true; runner.Cycle(); value = expression.GetExpressionValue(new InterpretationContext(), null) as ListValue; Assert.IsNotNull(value); Assert.AreEqual(4, value.Val.Count); }
public void TestUpdateRule() { Dictionary dictionary = CreateDictionary("Test"); NameSpace nameSpace = CreateNameSpace(dictionary, "NameSpace"); Variable var = CreateVariable(nameSpace, "TheVariable", "Integer"); var.setDefaultValue("0"); RuleCondition condition1 = CreateRuleAndCondition(nameSpace, "Rule1"); Rule rule = condition1.EnclosingRule; Action action = CreateAction(condition1, "TheVariable <- 1"); Compiler.Compile_Synchronous(true); System.Runner = new Runner(false); Expression expression = new Parser().Expression(dictionary, "NameSpace.TheVariable"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value.LiteralName, "0"); System.Runner.Cycle(); value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value.LiteralName, "1"); Dictionary update = CreateDictionary("TestUpdate"); update.setUpdates(dictionary.Guid); NameSpace updNameSpace = CreateNameSpace(update, "NameSpace"); updNameSpace.setUpdates(nameSpace.Guid); RuleCondition updCondition = CreateRuleAndCondition(updNameSpace, "Rule1"); Rule updRule = updCondition.EnclosingRule; Action updAction = CreateAction(updCondition, "TheVariable <- 2"); updRule.setUpdates(rule.Guid); System.Runner.Cycle(); value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value.LiteralName, "2"); }
public void TestParameterTypeInDefaultNameSpace() { Dictionary dictionary = CreateDictionary("Test"); NameSpace defaultNameSpace = CreateNameSpace(dictionary, "Default"); NameSpace nameSpace = CreateNameSpace(dictionary, "N1"); Enum enumeration = CreateEnum(defaultNameSpace, "Enum"); EnumValue value1 = CreateEnumValue(enumeration, "First"); EnumValue value2 = CreateEnumValue(enumeration, "Second"); Function function = CreateFunction(nameSpace, "F1", "Boolean"); Parameter param = new Parameter(); param.setTypeName("Enum"); param.setName("Value"); function.appendParameters(param); Case cas1 = CreateCase(function, "Case 1", "True", "Value == Enum.First"); Case cas2 = CreateCase(function, "Case 2", "False"); Dictionary dictionary2 = CreateDictionary("TestUpdate"); dictionary2.setUpdates(dictionary.Guid); Function updatedFunction = function.CreateFunctionUpdate(dictionary2); Case cas3 = (Case) updatedFunction.Cases[0]; PreCondition preCondition = (PreCondition) cas3.PreConditions[0]; preCondition.ExpressionText = "Value == Enum.Second"; Compiler.Compile_Synchronous(true); RuleCheckerVisitor ruleChecker = new RuleCheckerVisitor(dictionary2); ruleChecker.visit(updatedFunction); Assert.IsNull(ErrorMessage(updatedFunction)); Expression expression = new Parser().Expression(dictionary, "N1.F1(Enum.Second)"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(System.BoolType.True, value); }
public void TestVariableAndTypeWithSameName() { Dictionary dictionary = CreateDictionary("Test"); NameSpace nameSpace = CreateNameSpace(dictionary, "NameSpace"); Structure structure = CreateStructure(nameSpace, "ModelElement"); StructureElement structElem = CreateStructureElement(structure, "Value", "Boolean"); structElem.setDefault("True"); Variable variable = CreateVariable(nameSpace, "ModelElement", "ModelElement"); variable.SubVariables["Value"].Value = System.BoolType.False; Expression expression = new Parser().Expression(dictionary, "NameSpace.ModelElement.Value"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value, variable.SubVariables["Value"].Value); }
/// <summary> /// Parses the image and provides the corresponding value /// </summary> /// <param name="image"></param> /// <returns></returns> public override IValue getValue(string image) { IValue retVal = null; Expression expression = new Parser().Expression(this, image); if (expression != null) { retVal = expression.GetExpressionValue(new InterpretationContext(this), null); } return retVal; }
public void TestParameterTypeReference() { Dictionary dictionary = CreateDictionary("Test"); NameSpace nameSpace = CreateNameSpace(dictionary, "N1"); Enum enumeration = CreateEnum(nameSpace, "Enum"); EnumValue value1 = CreateEnumValue(enumeration, "First"); Function function = CreateFunction(nameSpace, "f", "Bool"); Parameter param = new Parameter(); param.setTypeName("N1.Enum"); param.setName("Value"); function.appendParameters(param); Case cas1 = CreateCase(function, "Case 1", "True", "Value == Enum.First"); Dictionary dictionary2 = CreateDictionary("TestUpdate"); dictionary2.setUpdates(dictionary.Guid); Function updatedFunction = function.CreateFunctionUpdate(dictionary2); Case cas3 = (Case) updatedFunction.Cases[0]; cas3.ExpressionText = "False"; Compiler.Compile_Synchronous(true); Expression expression = new Parser().Expression(dictionary, "N1.f(N1.Enum.First)"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(System.BoolType.False, value); }
public void TestUpdateMultipleFunctions() { Dictionary dictionary = CreateDictionary("Test"); NameSpace nameSpace = CreateNameSpace(dictionary, "N1"); Function function = CreateFunction(nameSpace, "f", "Bool"); Case cas1 = CreateCase(function, "Case 1", "q()"); Function function2 = CreateFunction(nameSpace, "q", "Bool"); Case cas2 = CreateCase(function2, "Case 1", "True"); Dictionary dictionary2 = CreateDictionary("TestUpdate"); dictionary2.setUpdates(dictionary.Guid); Function updatedFunction = function.CreateFunctionUpdate(dictionary2); Case cas3 = (Case) updatedFunction.Cases[0]; cas3.ExpressionText = "NOT q(param => 3)"; Function updatedFunction2 = function2.CreateFunctionUpdate(dictionary2); Parameter intParameter = CreateParameter(updatedFunction2, "param", "Integer"); Case cas4 = (Case) updatedFunction2.Cases[0]; cas4.ExpressionText = "False"; Compiler.Compile_Synchronous(true); Expression expression = new Parser().Expression(dictionary, "N1.f()"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(System.BoolType.True, value); }
/// <summary> /// Applies this step activation be registering it in the activation cache /// </summary> /// <param name="runner"></param> public override void Apply(Runner runner) { base.Apply(runner); TimeLine.SubStepActivationCache[SubStep] = this; foreach (VariableUpdate update in Updates) { TimeLine.AddModelEvent(update, runner, true); } // Store the step corresponding expectations foreach (Expectation expectation in subStep.Expectations) { bool addExpectation = true; if (expectation.getKind() == acceptor.ExpectationKind.aInstantaneous) { if (!String.IsNullOrEmpty(expectation.getCondition())) { Expression expression = new Parser().Expression(expectation, expectation.getCondition()); BoolValue value = expression.GetExpressionValue(new InterpretationContext(expectation), null) as BoolValue; if (value != null) { addExpectation = value.Val; } else { throw new Exception("Cannot evaluate " + expectation.getCondition() + " as a boolean value"); } } } if (addExpectation) { TimeLine.AddModelEvent(new Expect(expectation, runner.CurrentPriority), runner, true); } } }
public void TestUncacheFunctionOfFunction() { Dictionary test = CreateDictionary("Test"); NameSpace n1 = CreateNameSpace(test, "N1"); Variable v = CreateVariable(n1, "Var", "Boolean"); v.setDefaultValue("True"); Function F1 = CreateFunction(n1, "FunOfVar", "Integer"); Case cas1_1 = CreateCase(F1, "Var is true", "1", "Var"); Case cas1_2 = CreateCase(F1, "Var is false", "2"); Function F2 = CreateFunction(n1, "FunOfFun", "Boolean"); Case cas2_1 = CreateCase(F2, "Value", "FunOfVar() == 2"); RuleCondition rc = CreateRuleAndCondition(n1, "Rule1"); Action a = CreateAction(rc, "Var <- False"); Compiler.Compile_Synchronous(true); Expression expression = new Parser().Expression(test, "N1.FunOfFun()"); IValue value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value, System.BoolType.False); // ReSharper disable once UseObjectOrCollectionInitializer Runner runner = new Runner(false); runner.CheckForCompatibleChanges = true; runner.Cycle(); value = expression.GetExpressionValue(new InterpretationContext(), null); Assert.AreEqual(value, System.BoolType.True); }