/// <summary> /// Provides the value associated to this Expression /// </summary> /// <param name="instance">The instance on which the value is computed</param> /// <param name="localScope">The local scope used to compute the value of this expression</param> /// <param name="globalFind">Indicates that the search should be performed globally</param> /// <returns></returns> public override Values.IValue GetValue(InterpretationContext context) { Values.StructureValue retVal = null; Types.Structure structureType = Structure.GetExpressionType() as Types.Structure; if (structureType != null) { retVal = new Values.StructureValue(structureType, Root); foreach (KeyValuePair <string, Expression> pair in Associations) { Values.IValue val = pair.Value.GetValue(new InterpretationContext(context)); Variables.Variable var = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); var.Name = pair.Key; var.Value = val; var.Enclosing = retVal; retVal.set(var); } } else { AddError("Cannot determine structure type for " + ToString()); } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="structure"></param> public StructureValue(StructureValue other) : base(other.Structure, new Dictionary <string, Utils.INamable>()) { Enclosing = other.Structure; foreach (KeyValuePair <string, Utils.INamable> pair in other.Val) { Variables.Variable variable = pair.Value as Variables.Variable; if (variable != null) { Variables.Variable var2 = (Variables.Variable)DataDictionary.Generated.acceptor.getFactory().createVariable(); var2.Type = variable.Type; var2.Name = variable.Name; var2.Mode = variable.Mode; var2.Default = variable.Default; var2.Enclosing = this; if (variable.Value != null) { var2.Value = variable.Value.RightSide(var2, true); } else { var2.Value = null; } set(var2); } } }
/// <summary> /// Adds a model element in this model element /// </summary> /// <param name="copy"></param> public override void AddModelElement(Utils.IModelElement element) { { Range item = element as Range; if (item != null) { appendRanges(item); } } { Enum item = element as Enum; if (item != null) { appendEnumerations(item); } } { Structure item = element as Structure; if (item != null) { appendStructures(item); } } { Collection item = element as Collection; if (item != null) { appendCollections(item); } } { Functions.Function item = element as Functions.Function; if (item != null) { appendFunctions(item); } } { Functions.Procedure item = element as Functions.Procedure; if (item != null) { appendProcedures(item); } } { Rules.Rule item = element as Rules.Rule; if (item != null) { appendRules(item); } } { Variables.Variable item = element as Variables.Variable; if (item != null) { appendVariables(item); } } }
/// <summary> /// Sets the default values to each variable /// </summary> /// <param name="variable">The variable to set</param> /// <param name="subNodes">Indicates whether sub nodes should be considered</param> public override void visit(Variable variable, bool subNodes) { Variables.Variable var = (Variables.Variable)variable; var.Value = var.DefaultValue; base.visit(variable, subNodes); }
/// <summary> /// Constructor /// </summary> /// <param name="listExpression"></param> /// <param name="condition"></param> /// <param name="function"></param> /// <param name="initialValue"></param> /// <param name="root">the root element for which this expression should be parsed</param> public ReduceExpression(ModelElement root, Expression listExpression, Expression condition, Expression function, Expression initialValue) : base(root, listExpression, condition, function) { InitialValue = initialValue; InitialValue.Enclosing = this; AccumulatorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); AccumulatorVariable.Enclosing = this; AccumulatorVariable.Name = "RESULT"; Utils.ISubDeclaratorUtils.AppendNamable(this, AccumulatorVariable); }
public override void visit(Generated.Variable obj, bool visitSubNodes) { Variables.Variable variable = (Variables.Variable)obj; if (variable.Type == Target) { Usages.Add(variable); } base.visit(obj, visitSubNodes); }
/// <summary> /// Evaluates the rules associated to a single variable /// </summary> /// <param name="priority"></param> /// <param name="activations"></param> /// <param name="variable"></param> private void EvaluateVariable(Generated.acceptor.RulePriority priority, HashSet <Activation> activations, Variables.IVariable variable) { if (variable != null) { if (variable.Type is Types.Structure) { List <Rules.RuleCondition> rules = new List <RuleCondition>(); Types.Structure structure = variable.Type as Types.Structure; foreach (Rule rule in structure.Rules) { rule.Evaluate(this, priority, variable, rules); } Activation.RegisterRules(activations, rules, variable); StructureValue value = variable.Value as StructureValue; if (value != null) { foreach (Variables.IVariable subVariable in value.SubVariables.Values) { EvaluateVariable(priority, activations, subVariable); } } } else if (variable.Type is Types.StateMachine) { List <Rules.RuleCondition> rules = new List <RuleCondition>(); EvaluateStateMachine(rules, priority, variable); Activation.RegisterRules(activations, rules, variable); } else if (variable.Type is Types.Collection) { Types.Collection collectionType = variable.Type as Types.Collection; if (variable.Value != EFSSystem.EmptyValue) { ListValue val = variable.Value as ListValue; int i = 1; foreach (IValue subVal in val.Val) { Variables.Variable tmp = new Variables.Variable(); tmp.Name = variable.Name + '[' + i + ']'; tmp.Type = collectionType.Type; tmp.Value = subVal; EvaluateVariable(priority, activations, tmp); i = i + 1; } } } } }
/// <summary> /// Coputes targets from the function and adds them to the collection /// </summary> /// <param name="function">Function containing targets</param> /// <param name="collection">Collection to be filled with targets</param> private void ComputeTargets(Function function, Values.ListValue collection) { if (function != null) { Graph graph = function.Graph; if (graph != null && graph.Segments.Count > 1) { double prevSpeed = Double.MaxValue; for (int i = 1; i < graph.Segments.Count; i++) { Graph.Segment s = graph.Segments[i]; Types.Structure structureType = (Types.Structure)EFSSystem.findType(OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Kernel.SpeedAndDistanceMonitoring.TargetSupervision"), "Kernel.SpeedAndDistanceMonitoring.TargetSupervision.Target"); Values.StructureValue value = new Values.StructureValue(structureType, structureType.NameSpace); Variables.Variable speed = (Variables.Variable)DataDictionary.Generated.acceptor.getFactory().createVariable(); speed.Type = EFSSystem.findType(OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Default.BaseTypes"), "Default.BaseTypes.Speed"); speed.Name = "Speed"; speed.Mode = Generated.acceptor.VariableModeEnumType.aInternal; speed.Default = "0.0"; speed.Enclosing = value; speed.Value = new Values.DoubleValue(EFSSystem.DoubleType, s.Val(s.Start)); value.set(speed); Variables.Variable location = (Variables.Variable)DataDictionary.Generated.acceptor.getFactory().createVariable(); location.Type = EFSSystem.findType(OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Default.BaseTypes"), "Default.BaseTypes.Distance"); location.Name = "Location"; location.Mode = Generated.acceptor.VariableModeEnumType.aInternal; location.Default = "0.0"; location.Enclosing = value; location.Value = new Values.DoubleValue(EFSSystem.DoubleType, s.Start); value.set(location); Variables.Variable length = (Variables.Variable)DataDictionary.Generated.acceptor.getFactory().createVariable(); length.Type = EFSSystem.findType(OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Default.BaseTypes"), "Default.BaseTypes.Length"); length.Name = "Length"; length.Mode = Generated.acceptor.VariableModeEnumType.aInternal; length.Default = "0.0"; length.Enclosing = value; length.Value = new Values.DoubleValue(EFSSystem.DoubleType, s.End); value.set(length); if (s.Val(s.Start) < prevSpeed) { collection.Val.Add(value); } prevSpeed = s.Val(s.Start); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="listExpression"></param> /// <param name="function"></param> /// <param name="root">the root element for which this expression should be parsed</param> public ListOperatorExpression(ModelElement root, Expression listExpression) : base(root) { ListExpression = listExpression; ListExpression.Enclosing = this; IteratorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); IteratorVariable.Enclosing = this; IteratorVariable.Name = "X"; PreviousIteratorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); PreviousIteratorVariable.Enclosing = this; PreviousIteratorVariable.Name = "prevX"; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this element is built</param> /// <param name="value">The value to insert in the list</param> /// <param name="listExpression">The list affected by the replace statement</param> /// <param name="condition">The condition which indicates the value to be replaced</param> public ReplaceStatement(ModelElement root, Expression value, Expression listExpression, Expression condition) : base(root) { Value = value; Value.Enclosing = this; ListExpression = listExpression; ListExpression.Enclosing = this; Condition = condition; Condition.Enclosing = this; IteratorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); IteratorVariable.Enclosing = this; IteratorVariable.Name = "X"; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this element is built</param> /// <param name="condition">The corresponding function call designator</param> /// <param name="position">The position in which the element should be removed</param> /// <param name="listExpression">The expressions used to compute the parameters</param> public RemoveStatement(ModelElement root, Expression condition, PositionEnum position, Expression listExpression) : base(root) { Condition = condition; if (condition != null) { condition.Enclosing = this; } Position = position; ListExpression = listExpression; ListExpression.Enclosing = this; IteratorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); IteratorVariable.Enclosing = this; IteratorVariable.Name = "X"; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="types">Dictionary of types</param> public ExtendedDictionaryVariableDetector(Dictionary <string, object> types) : base(new Dictionary <string, FormulaEditor.Interfaces.IOperationAcceptor>()) { foreach (string key in types.Keys) { Variables.Variable v = null; object t = types[key]; if (t.Equals((double)0)) { v = new Variables.VariableDouble(key); } else { v = new Variables.Variable(t, key); } dvariables[key] = v; dictionary[key] = v; } }
/// <summary> /// Constructor /// </summary> /// <param name="structure"></param> public StructureValue(Types.Structure structure, Utils.INamable enclosing) : base(structure, new Dictionary <string, Utils.INamable>()) { Enclosing = structure; foreach (Types.StructureElement element in Structure.Elements) { Variables.Variable variable = (Variables.Variable)DataDictionary.Generated.acceptor.getFactory().createVariable(); if (element.Type != null) { variable.Type = element.Type; } variable.Name = element.Name; variable.Mode = element.Mode; variable.Default = element.Default; variable.Enclosing = enclosing; variable.Enclosing = this; set(variable); } }
/// <summary> /// Constructor /// </summary> /// <param name="root"></param> /// <param name="expression">The expression to stabilize</param> /// <param name="initialValue">The initial value for this stabilisation computation</param> /// <param name="condition">The condition which indicates that the stabilisation is not complete</param> public StabilizeExpression(ModelElement root, Expression expression, Expression initialValue, Expression condition) : base(root) { Expression = expression; Expression.Enclosing = this; InitialValue = initialValue; InitialValue.Enclosing = this; Condition = condition; Condition.Enclosing = this; LastIteration = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); LastIteration.Enclosing = this; LastIteration.Name = "PREVIOUS"; CurrentIteration = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); CurrentIteration.Enclosing = this; CurrentIteration.Name = "CURRENT"; InitDeclaredElements(); }
/// <summary> /// Constructor /// </summary> /// <param name="root">The root element for which this element is built</param> /// <param name="call">The corresponding function call designator</param> /// <param name="parameters">The expressions used to compute the parameters</param> public ApplyStatement(ModelElement root, ProcedureCallStatement call, Expression listExpression, Expression conditionExpression) : base(root) { DeclaredElements = new Dictionary <string, List <Utils.INamable> >(); Call = call; Call.Enclosing = this; ListExpression = listExpression; ListExpression.Enclosing = this; ConditionExpression = conditionExpression; if (ConditionExpression != null) { ConditionExpression.Enclosing = this; } IteratorVariable = (Variables.Variable)Generated.acceptor.getFactory().createVariable(); IteratorVariable.Enclosing = this; IteratorVariable.Name = "X"; InitDeclaredElements(); }
/// <summary> /// Evaluates the rules associated to a single variable /// </summary> /// <param name="priority">The priority in which this variable is evaluated</param> /// <param name="activations">The activation list result of this evaluation</param> /// <param name="variable">The variable to evaluate</param> /// <param name="explanation">The explanation part to be filled</param> private void EvaluateVariable(acceptor.RulePriority priority, HashSet <Activation> activations, IVariable variable, ExplanationPart explanation) { if (variable != null && variable.Value != EfsSystem.Instance.EmptyValue) { if (variable.Type != null && variable.Type.ApplicableRule(priority)) { if (variable.Type is Structure) { Structure structure = variable.Type as Structure; foreach (Rules.Rule rule in structure.Rules) { rule.Evaluate(this, priority, variable, activations, explanation); } StructureValue value = variable.Value as StructureValue; if (value != null) { foreach (IVariable subVariable in value.SubVariables.Values) { EvaluateVariable(priority, activations, subVariable, explanation); } } } else if (variable.Type is StateMachine) { EvaluateStateMachine(activations, priority, variable, explanation); } else if (variable.Type is Collection) { Collection collectionType = variable.Type as Collection; if (variable.Value != EfsSystem.Instance.EmptyValue) { ListValue val = variable.Value as ListValue; if (val != null) { foreach (IValue subVal in val.Val) { Variables.Variable tmp = new Variables.Variable { Name = "list_entry", Type = collectionType.Type, Value = subVal }; EvaluateVariable(priority, activations, tmp, explanation); } } else { ModelElement element = variable as ModelElement; if (element != null) { element.AddError("Variable " + variable.Name + " does not hold a collection but " + variable.Value); } else { throw new Exception("Variable " + variable.Name + " does not hold a collection but " + variable.Value); } } } } } } }
/// <summary> /// Evaluates the rules associated to a single variable /// </summary> /// <param name="priority"></param> /// <param name="activations"></param> /// <param name="variable"></param> private void EvaluateVariable(Generated.acceptor.RulePriority priority, HashSet<Activation> activations, Variables.IVariable variable) { if (variable != null) { if (variable.Type is Types.Structure) { List<Rules.RuleCondition> rules = new List<RuleCondition>(); Types.Structure structure = variable.Type as Types.Structure; foreach (Rule rule in structure.Rules) { rule.Evaluate(this, priority, variable, rules); } Activation.RegisterRules(activations, rules, variable); StructureValue value = variable.Value as StructureValue; if (value != null) { foreach (Variables.IVariable subVariable in value.SubVariables.Values) { EvaluateVariable(priority, activations, subVariable); } } } else if (variable.Type is Types.StateMachine) { List<Rules.RuleCondition> rules = new List<RuleCondition>(); EvaluateStateMachine(rules, priority, variable); Activation.RegisterRules(activations, rules, variable); } else if (variable.Type is Types.Collection) { Types.Collection collectionType = variable.Type as Types.Collection; if (variable.Value != EFSSystem.EmptyValue) { ListValue val = variable.Value as ListValue; int i = 1; foreach (IValue subVal in val.Val) { Variables.Variable tmp = new Variables.Variable(); tmp.Name = variable.Name + '[' + i + ']'; tmp.Type = collectionType.Type; tmp.Value = subVal; EvaluateVariable(priority, activations, tmp); i = i + 1; } } } } }