/// <summary> /// Performs the semantic analysis of the statement /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance) { bool retVal = base.SemanticAnalysis(instance); if (retVal) { ListExpression.SemanticAnalysis(instance); Types.Collection collectionType = ListExpression.GetExpressionType() as Types.Collection; if (collectionType != null) { IteratorVariable.Type = collectionType.Type; } Value.SemanticAnalysis(instance); Types.Type valueType = Value.GetExpressionType(); if (valueType != null) { if (!valueType.Match(collectionType.Type)) { AddError("Type of " + Value + " does not match collection type " + collectionType); } } else { AddError("Cannot determine type of " + Value); } if (Condition != null) { Condition.SemanticAnalysis(instance); } } return(retVal); }
/// <summary> /// Checks the statement for semantical errors /// </summary> public override void CheckStatement() { Value.checkExpression(); Types.Collection targetListType = ListExpression.GetExpressionType() as Types.Collection; if (targetListType != null) { Types.Type elementType = Value.GetExpressionType(); if (elementType != targetListType.Type) { Root.AddError("Inserted element type does not corresponds to list type"); } Condition.checkExpression(); Types.BoolType conditionType = Condition.GetExpressionType() as Types.BoolType; if (conditionType == null) { Root.AddError("Condition does not evaluates to boolean"); } } else { Root.AddError("Cannot determine collection type of " + ListExpression); } }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void checkExpression() { base.checkExpression(); Types.Collection listExpressionType = ListExpression.GetExpressionType() as Types.Collection; if (listExpressionType != null) { IteratorExpression.checkExpression(); } }
/// <summary> /// Checks the statement for semantical errors /// </summary> public override void CheckStatement() { InterpretationContext context = new InterpretationContext(Root); Types.Collection listExpressionType = ListExpression.GetExpressionType() as Types.Collection; if (listExpressionType == null) { Root.AddError("Target does not references a list variable"); } context.LocalScope.setVariable(IteratorVariable); Call.CheckStatement(context); }
/// <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> /// Provides the type of this expression /// </summary> /// <param name="context">The interpretation context</param> /// <returns></returns> public override Types.Type GetExpressionType() { Types.Type retVal = null; Types.Collection listType = ListExpression.GetExpressionType() as Types.Collection; if (listType != null) { retVal = listType.Type; } else { AddError("Cannot evaluate list type of " + ToString()); } return(retVal); }
/// <summary> /// Creates a valid right side IValue, according to the target variable (left side) /// </summary> /// <param name="variable">The target variable</param> /// <param name="duplicate">Indicates that a duplication of the variable should be performed</param> /// <returns></returns> public override Values.IValue RightSide(Variables.IVariable variable, bool duplicate) { ListValue retVal = this; // Complete the list with empty values Types.Collection collectionType = variable.Type as Types.Collection; if (collectionType != null) { Values.EmptyValue emptyValue = EFSSystem.EmptyValue; while (retVal.Val.Count < collectionType.getMaxSize()) { retVal.Val.Add(emptyValue); } } retVal.Enclosing = variable; return(retVal); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void checkExpression() { base.checkExpression(); Types.Type initialValueType = InitialValue.GetExpressionType(); if (initialValueType != null) { Types.Collection listExpressionType = ListExpression.GetExpressionType() as Types.Collection; if (listExpressionType != null) { IteratorExpression.checkExpression(); } } else { AddError("Cannot determine initial value expression type for " + ToString()); } }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="instance">the instance on which the function is evaluated</param> /// <param name="actuals">the actual parameters values</param> /// <returns>The value for the function application</returns> public override Values.IValue Evaluate(Interpreter.InterpretationContext context, Dictionary <Variables.Actual, Values.IValue> actuals) { Values.IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); Types.Collection collectionType = (Types.Collection)EFSSystem.findType(OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Kernel.SpeedAndDistanceMonitoring.TargetSupervision"), "Kernel.SpeedAndDistanceMonitoring.TargetSupervision.Targets"); Values.ListValue collection = new Values.ListValue(collectionType, new List <Values.IValue>()); // compute targets from the MRSP Function function1 = context.findOnStack(Targets1).Value as Functions.Function; if (function1 != null && !function1.Name.Equals("EMPTY")) { Graph graph1 = createGraphForValue(context, function1); ComputeTargets(graph1.Function, collection); } // compute targets from the MA Function function2 = context.findOnStack(Targets2).Value as Functions.Function; if (function2 != null && !function2.Name.Equals("EMPTY")) { Graph graph2 = createGraphForValue(context, function2); ComputeTargets(graph2.Function, collection); } // compute targets from the SR Function function3 = context.findOnStack(Targets3).Value as Functions.Function; if (function3 != null && !function3.Name.Equals("EMPTY")) { Graph graph3 = createGraphForValue(context, function3); ComputeTargets(graph3.Function, collection); } context.LocalScope.PopContext(token); retVal = collection; return(retVal); }
/// <summary> /// Checks the statement for semantical errors /// </summary> public override void CheckStatement() { Types.Collection targetListType = ListExpression.GetExpressionType() as Types.Collection; if (targetListType == null) { Root.AddError("Cannot determine type of " + ListExpression); } else { if (Condition != null) { Condition.checkExpression(); Types.BoolType conditionType = Condition.GetExpressionType() as Types.BoolType; if (conditionType == null) { Root.AddError("Condition does not evaluates to boolean"); } } } }
/// <summary> /// Provides the type of this expression /// </summary> /// <param name="context">The interpretation context</param> /// <returns></returns> public override Types.Type GetExpressionType() { Types.Type retVal = null; Types.Type iteratorType = IteratorExpression.GetExpressionType(); if (iteratorType != null) { Types.Collection collection = (Types.Collection)Generated.acceptor.getFactory().createCollection(); collection.Enclosing = EFSSystem; collection.Type = iteratorType; retVal = collection; } else { AddError("Cannot evaluate iterator type for " + ToString()); } return(retVal); }
/// <summary> /// Checks the statement for semantical errors /// </summary> public override void CheckStatement() { Value.checkExpression(); Types.Collection targetListType = ListExpression.GetExpressionType() as Types.Collection; if (targetListType != null) { Types.Type elementType = Value.GetExpressionType(); if (elementType != targetListType.Type) { Root.AddError("Inserted element type does not corresponds to list type"); } } else { Root.AddError("Cannot determine collection type of " + ListExpression); } if (ReplaceElement != null) { Types.Type replaceElementType = ReplaceElement.GetExpressionType(); if (replaceElementType != null) { if (targetListType.Type != null) { if (replaceElementType != targetListType.Type) { Root.AddError("The replace element type (" + replaceElementType.FullName + ") does not correspond to the list type (" + targetListType.Type.FullName + ")"); } } else { Root.AddError("Cannot determine list element's type for " + targetListType.FullName); } } else { Root.AddError("Cannot determine replacement element type"); } } }
/// <summary> /// Performs the semantic analysis of the statement /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance) { bool retVal = base.SemanticAnalysis(instance); if (retVal) { ListExpression.SemanticAnalysis(instance); Types.Collection collectionType = ListExpression.GetExpressionType() as Types.Collection; if (collectionType != null) { IteratorVariable.Type = collectionType.Type; } if (Condition != null) { Condition.SemanticAnalysis(instance); } } return(retVal); }
/// <summary> /// Performs the semantic analysis of the expression /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation) { bool retVal = base.SemanticAnalysis(instance, expectation); if (retVal) { Types.Type elementType = null; foreach (Expression expr in ListElements) { expr.SemanticAnalysis(instance, expectation); Types.Type current = expr.GetExpressionType(); if (elementType == null) { elementType = current; } else { if (current != elementType) { AddError("Cannot mix types " + current.ToString() + " and " + elementType.ToString() + "in collection"); } } } if (elementType != null) { ExpressionType = (Types.Collection)Generated.acceptor.getFactory().createCollection(); ExpressionType.Type = elementType; ExpressionType.Name = "ListOf_" + elementType.FullName; ExpressionType.Enclosing = Root.EFSSystem; } else { ExpressionType = new Types.GenericCollection(EFSSystem); } } return(retVal); }
/// <summary> /// Performs the semantic analysis of the expression /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation) { bool retVal = base.SemanticAnalysis(instance, expectation); if (retVal) { ListExpression.SemanticAnalysis(instance, Filter.IsRightSide); Types.Collection collectionType = ListExpression.GetExpressionType() as Types.Collection; if (collectionType != null) { IteratorVariable.Type = collectionType.Type; PreviousIteratorVariable.Type = collectionType.Type; } else { AddError("Cannot determine collection type on list expression " + ToString()); } } return(retVal); }
/// <summary> /// Provides the value of the function /// </summary> /// <param name="instance">the instance on which the function is evaluated</param> /// <param name="actuals">the actual parameters values</param> /// <param name="localScope">the values of local variables</param> /// <returns>The value for the function application</returns> public override Values.IValue Evaluate(Interpreter.InterpretationContext context, Dictionary <Variables.Actual, Values.IValue> actuals) { Values.IValue retVal = null; int token = context.LocalScope.PushContext(); AssignParameters(context, actuals); Values.ListValue value = context.findOnStack(Collection) as Values.ListValue; if (value != null) { Types.Collection collectionType = value.Type as Types.Collection; if (collectionType != null && collectionType.Type != null) { Types.Type elementType = collectionType.Type; int i = 0; while (i < value.Val.Count && value.Val[i] != EFSSystem.EmptyValue) { i += 1; } if (i < value.Val.Count) { retVal = elementType.DefaultValue; value.Val[i] = retVal; } else { AddError("Cannot allocate element in list : list full"); } } } context.LocalScope.PopContext(token); return(retVal); }
/// <summary> /// Fills the given structure with the values provided from the database /// </summary> /// <param name="aNameSpace">Namespace of the structure</param> /// <param name="fields">Fields to be copied into the structure</param> /// <param name="index">Index (of fields list) from which we have to start copying</param> /// <param name="aStructure">The structure to be filled</param> private void FillStructure(Types.NameSpace aNameSpace, ArrayList fields, ref int index, Values.StructureValue aStructure) { int j = 0; for (int i = index; i < fields.Count; i++) { Tests.DBElements.DBField field = fields[i] as Tests.DBElements.DBField; KeyValuePair <string, Variables.IVariable> pair = aStructure.SubVariables.ElementAt(j); Variables.IVariable variable = pair.Value; if (variable.Name.StartsWith(field.Variable)) // we use StartsWith and not Equals because we can have N_ITER_1 and N_ITER { if (variable.Type is Types.Enum) { Types.Enum type = variable.Type as Types.Enum; foreach (DataDictionary.Constants.EnumValue enumValue in type.Values) { int value = Int32.Parse(enumValue.getValue()); if (value == field.Value) { variable.Value = enumValue; j++; break; } } } else if (variable.Type is Types.Range) { Types.Range type = variable.Type as Types.Range; variable.Value = new Values.IntValue(type, (decimal)field.Value); j++; } else { throw new Exception("Unhandled variable type"); } if (field.Variable.Equals("N_ITER")) // we have to create a sequence { KeyValuePair <string, Variables.IVariable> sequencePair = aStructure.SubVariables.ElementAt(j); Variables.IVariable sequenceVariable = sequencePair.Value; Types.Collection collectionType = (Types.Collection)EFSSystem.findType(aNameSpace, sequenceVariable.TypeName); Values.ListValue sequence = new Values.ListValue(collectionType, new List <Values.IValue>()); for (int k = 0; k < field.Value; k++) { Types.Structure structureType = (Types.Structure)EFSSystem.findType(aNameSpace, sequence.CollectionType.Type.FullName); Values.StructureValue structureValue = new Values.StructureValue(structureType, structureType.NameSpace); FillStructure(aNameSpace, fields, ref index, structureValue); sequence.Val.Add(structureValue); } sequenceVariable.Value = sequence; j++; } } // if all the fields of the structue are filled, we terminated if (j == aStructure.SubVariables.Count) { index = i; break; } } }
private string format_eurobalise_message(DBElements.DBMessage message) { DataDictionary.Types.NameSpace nameSpace = OverallNameSpaceFinder.INSTANCE.findByName(EFSSystem.Dictionaries[0], "Messages"); Types.Structure structureType = (Types.Structure)EFSSystem.findType(nameSpace, "Messages.EUROBALISE.Message"); Values.StructureValue structure = new Values.StructureValue(structureType, nameSpace); int index = 0; FillStructure(nameSpace, message.Fields, ref index, structure); // fills the message fields // then we fill the packets KeyValuePair <string, Variables.IVariable> subSequencePair = structure.SubVariables.ElementAt(structure.SubVariables.Count - 1); Variables.IVariable subSequenceVariable = subSequencePair.Value; Types.Collection collectionType = (Types.Collection)EFSSystem.findType(nameSpace, "Messages.EUROBALISE.Collection1"); Values.ListValue collection = new Values.ListValue(collectionType, new List <Values.IValue>()); Types.Structure subStructure1Type = (Types.Structure)EFSSystem.findType(nameSpace, "Messages.EUROBALISE.SubStructure1"); Values.StructureValue subStructure1 = new Values.StructureValue(subStructure1Type, nameSpace); Types.Structure packetStructure = (Types.Structure)EFSSystem.findType(nameSpace, "Messages.PACKET.TRACK_TO_TRAIN.Message"); Values.StructureValue packetValue = new Values.StructureValue(packetStructure, nameSpace); // will contain the list of all packets of the message and then be added to the structure packetValue ArrayList subStructures = new ArrayList(); foreach (DBElements.DBPacket packet in message.Packets) { Tests.DBElements.DBField nidPacketField = packet.Fields[0] as Tests.DBElements.DBField; if (nidPacketField.Value != 255) // 255 means "end of information" { Values.StructureValue subStructure = FindStructure(nidPacketField.Value); index = 0; FillStructure(nameSpace, packet.Fields, ref index, subStructure); subStructures.Add(subStructure); } } // the collection of the message packets is copied to the structure packetValue int i = 0; foreach (KeyValuePair <string, Variables.IVariable> pair in packetValue.SubVariables) { if (i == subStructures.Count) { break; } string variableName = pair.Key; Values.StructureValue structureValue = subStructures[i] as Values.StructureValue; if (structureValue.Structure.FullName.Contains(variableName)) { Variables.IVariable variable = pair.Value; variable.Value = structureValue; i++; } } subStructure1.SubVariables.ElementAt(0).Value.Value = packetValue; collection.Val.Add(subStructure1); subSequenceVariable.Value = collection; return(structure.Name); }
/// <summary> /// Provides the type of this expression /// </summary> /// <returns></returns> public override Types.Type GetExpressionType() { Types.Type retVal = null; Types.Type leftType = Left.GetExpressionType(); if (leftType == null) { AddError("Cannot determine expression type (1) for " + Left.ToString()); } else { Types.Type rightType = Right.GetExpressionType(); if (rightType == null) { AddError("Cannot determine expression type (2) for " + Right.ToString()); } else { switch (Operation) { case OPERATOR.EXP: case OPERATOR.MULT: case OPERATOR.DIV: case OPERATOR.ADD: case OPERATOR.SUB: if (leftType.Match(rightType)) { if (leftType is Types.IntegerType || leftType is Types.DoubleType) { retVal = rightType; } else { retVal = leftType; } } else { retVal = leftType.CombineType(rightType, Operation); } break; case OPERATOR.AND: case OPERATOR.OR: if (leftType == EFSSystem.BoolType && rightType == EFSSystem.BoolType) { retVal = EFSSystem.BoolType; } break; case OPERATOR.EQUAL: case OPERATOR.NOT_EQUAL: case OPERATOR.LESS: case OPERATOR.LESS_OR_EQUAL: case OPERATOR.GREATER: case OPERATOR.GREATER_OR_EQUAL: if (leftType.Match(rightType) || rightType.Match(leftType)) { retVal = EFSSystem.BoolType; } break; case OPERATOR.IN: case OPERATOR.NOT_IN: Types.Collection collection = rightType as Types.Collection; if (collection != null) { if (collection.Type == null) { retVal = EFSSystem.BoolType; } else if (collection.Type == leftType) { retVal = EFSSystem.BoolType; } } else { Types.StateMachine stateMachine = rightType as Types.StateMachine; if (stateMachine != null && leftType.Match(stateMachine)) { retVal = EFSSystem.BoolType; } } break; case OPERATOR.UNDEF: break; } } } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="type"></param> public ListValue(Types.Collection type, List <IValue> val) : base(type, val) { }
/// <summary> /// Performs the semantic analysis of the expression /// </summary> /// <param name="instance">the reference instance on which this element should analysed</param> /// <paraparam name="expectation">Indicates the kind of element we are looking for</paraparam> /// <returns>True if semantic analysis should be continued</returns> public override bool SemanticAnalysis(Utils.INamable instance, Filter.AcceptableChoice expectation) { bool retVal = base.SemanticAnalysis(instance, expectation); if (retVal) { Types.Type elementType = null; foreach (Expression expr in ListElements) { expr.SemanticAnalysis(instance, expectation); Types.Type current = expr.GetExpressionType(); if (elementType == null) { elementType = current; } else { if (current != elementType) { AddError("Cannot mix types " + current.ToString() + " and " + elementType.ToString() + "in collection"); } } } if (elementType != null) { ExpressionType = (Types.Collection)Generated.acceptor.getFactory().createCollection(); ExpressionType.Type = elementType; ExpressionType.Name = "ListOf_" + elementType.FullName; ExpressionType.Enclosing = Root.EFSSystem; } else { ExpressionType = new Types.GenericCollection(EFSSystem); } } return retVal; }