Esempio n. 1
0
        public EXEReferencingVariable FindReferencingVariableByName(String Name)
        {
            EXEReferencingVariable Result = null;
            EXEScope CurrentScope         = this;

            while (CurrentScope != null)
            {
                foreach (EXEReferencingVariable CurrentVariable in CurrentScope.ReferencingVariables)
                {
                    if (String.Equals(CurrentVariable.Name, Name))
                    {
                        Result = CurrentVariable;
                        break;
                    }
                }

                if (Result != null)
                {
                    break;
                }

                CurrentScope = CurrentScope.SuperScope;
            }
            return(Result);
        }
Esempio n. 2
0
        public bool AddVariable(EXEReferencingVariable Variable)
        {
            bool Result = false;

            if (!VariableNameExists(Variable.Name))
            {
                this.ReferencingVariables.Add(Variable);
                Result = true;
            }

            return(Result);
        }
Esempio n. 3
0
        public override bool Execute(OALProgram OALProgram, EXEScope Scope)
        {
            bool Result = false;
            EXEReferencingVariable Variable = Scope.FindReferencingVariableByName(this.VariableName);

            if (Variable != null)
            {
                bool DestructionSuccess = OALProgram.ExecutionSpace.DestroyInstance(Variable.ClassName, Variable.ReferencedInstanceId);
                if (DestructionSuccess)
                {
                    Result = Scope.UnsetReferencingVariables(Variable.ClassName, Variable.ReferencedInstanceId);
                }
            }
            return(Result);
        }
Esempio n. 4
0
        //SetUloh1
        // We have variable name, attribute name and scope, in which to look for variable
        // We need to get the value of given attribute of given variable
        // If this does not exist, return null
        // You will use EXEScope.FindReferencingVariableByName() method, but you need to implement it first
        // user.name

        public String EvaluateAttributeValue(String ReferencingVariableName, String AttributeName, EXEScope Scope, CDClassPool ExecutionSpace)
        {
            EXEReferencingVariable ReferencingVariable = Scope.FindReferencingVariableByName(ReferencingVariableName);

            if (ReferencingVariable == null)
            {
                return(null);
            }
            CDClassInstance ClassInstance = ExecutionSpace.GetClassInstanceById(ReferencingVariable.ClassName, ReferencingVariable.ReferencedInstanceId);

            if (ClassInstance == null)
            {
                return(null);
            }
            return(ClassInstance.GetAttributeValue(AttributeName));
        }
Esempio n. 5
0
        public String EvaluateEmpty(String Operand, EXEScope Scope)
        {
            String Result = null;

            if (EXETypes.UnitializedName.Equals(Operand))
            {
                Result = EXETypes.BooleanTrue;
                return(Result);
            }

            EXEReferencingVariable SingleInstanceVariable = Scope.FindReferencingVariableByName(Operand);

            if (SingleInstanceVariable != null)
            {
                if (SingleInstanceVariable.IsInitialized())
                {
                    Result = EXETypes.BooleanFalse;
                }
                else
                {
                    Result = EXETypes.BooleanTrue;
                }

                return(Result);
            }

            EXEReferencingSetVariable MultiInstanceVariable = Scope.FindSetReferencingVariableByName(Operand);

            if (MultiInstanceVariable != null)
            {
                if (MultiInstanceVariable.GetReferencingVariables().Any())
                {
                    Result = EXETypes.BooleanFalse;
                }
                else
                {
                    Result = EXETypes.BooleanTrue;
                }

                return(Result);
            }

            return(Result);
        }
Esempio n. 6
0
        public String EvaluateCardinality(String Operand, EXEScope Scope)
        {
            String Result = null;

            if (EXETypes.UnitializedName.Equals(Operand))
            {
                Result = "0";
                return(Result);
            }

            EXEReferencingVariable SingleInstanceVariable = Scope.FindReferencingVariableByName(Operand);

            if (SingleInstanceVariable != null)
            {
                if (SingleInstanceVariable.IsInitialized())
                {
                    Result = "1";
                }
                else
                {
                    Result = "0";
                }

                return(Result);
            }

            EXEReferencingSetVariable MultiInstanceVariable = Scope.FindSetReferencingVariableByName(Operand);

            if (MultiInstanceVariable != null)
            {
                if (MultiInstanceVariable.GetReferencingVariables().Any())
                {
                    Result = MultiInstanceVariable.GetReferencingVariables().Count.ToString();
                }
                else
                {
                    Result = "0";
                }

                return(Result);
            }

            return(Result);
        }
Esempio n. 7
0
        // SetUloh2
        public override bool Execute(OALProgram OALProgram, EXEScope Scope)
        {
            //Create an instance of given class -> will affect ExecutionSpace.
            //If ReferencingVariableName is provided (is not ""), create a referencing variable pointing to this instance -> will affect scope
            CDClass Class = OALProgram.ExecutionSpace.getClassByName(this.ClassName);

            if (Class == null)
            {
                return(false);
            }

            EXEReferencingVariable Variable = Scope.FindReferencingVariableByName(this.ReferencingVariableName);

            if (Variable != null)
            {
                if (!String.Equals(this.ClassName, Variable.ClassName))
                {
                    return(false);
                }
            }

            CDClassInstance Instance = Class.CreateClassInstance();

            if (Instance == null)
            {
                return(false);
            }

            if (!"".Equals(this.ReferencingVariableName))
            {
                if (Variable != null)
                {
                    Variable.ReferencedInstanceId = Instance.UniqueID;
                }
                else
                {
                    Variable = new EXEReferencingVariable(this.ReferencingVariableName, Class.Name, Instance.UniqueID);
                    return(Scope.AddVariable(Variable));
                }
            }

            return(true);
        }
Esempio n. 8
0
        //SetUloh1
        // Similar as task above, but this time we set the attribute value to "NewValue" parameter
        // But it's not that easy, you need to check if attribute type and NewValue type are the same (e.g. both are integer)
        // To do that, you need to find the referencing variable's class (via Scope) and then the attribute's type (vie ExecutionSpace)
        // When you know the type of attribute, use EXETypes.IsValidValue to see if you can or cannot assign that value to that attribute
        // You assign it in Scope
        // Return if you could assign it or not
        // EXETypes.determineVariableType()
        public Boolean SetAttributeValue(String ReferencingVariableName, String AttributeName, EXEScope Scope, CDClassPool ExecutionSpace, String NewValue)
        {
            EXEReferencingVariable ReferencingVariable = Scope.FindReferencingVariableByName(ReferencingVariableName);

            if (ReferencingVariable == null)
            {
                return(false);
            }

            CDClassInstance ClassInstance = ExecutionSpace.GetClassInstanceById(ReferencingVariable.ClassName, ReferencingVariable.ReferencedInstanceId);

            if (ClassInstance == null)
            {
                return(false);
            }

            CDClass Class = ExecutionSpace.getClassByName(ReferencingVariable.ClassName);

            if (Class == null)
            {
                return(false);
            }

            CDAttribute Attribute = Class.GetAttributeByName(AttributeName);

            if (Attribute == null)
            {
                return(false);
            }

            String NewValueType = EXETypes.DetermineVariableType(null, NewValue);

            if (!EXETypes.CanBeAssignedToAttribute(AttributeName, Attribute.Type, NewValueType))
            {
                return(false);
            }

            ClassInstance.SetAttribute(AttributeName, EXETypes.AdjustAssignedValue(Attribute.Type, NewValue));

            return(true);
        }
Esempio n. 9
0
        public Dictionary <String, String> GetRefStateAttrsDictRecursive(CDClassPool ExecutionSpace, String VarName)
        {
            Dictionary <String, String> Result = new Dictionary <String, String>();
            EXEReferencingVariable      Var    = this.FindReferencingVariableByName(VarName);

            if (Var == null)
            {
                return(Result);
            }
            CDClassInstance Inst = Var.RetrieveReferencedClassInstance(ExecutionSpace);

            if (Inst == null)
            {
                return(Result);
            }
            foreach (var Attribute in Inst.GetStateWithoutID())
            {
                Result[VarName + "." + Attribute.Key] = Attribute.Value;
            }

            return(Result);
        }
Esempio n. 10
0
        // Create a relationship instance (between two variables pointing to class instances)
        // Based on class names get the CDRelationship from RelationshipSpace
        // Based on variable names get the instance ids from Scope.ReferencingVariables
        // Create relationship between the given instance ids (CDRelationship.CreateRelationship) and return result of it
        public override bool Execute(OALProgram OALProgram, EXEScope Scope)
        {
            EXEReferencingVariable Variable1 = Scope.FindReferencingVariableByName(this.Variable1Name);

            if (Variable1 == null)
            {
                return(false);
            }
            EXEReferencingVariable Variable2 = Scope.FindReferencingVariableByName(this.Variable2Name);

            if (Variable2 == null)
            {
                return(false);
            }
            CDRelationship Relationship = OALProgram.RelationshipSpace.GetRelationship(this.RelationshipName, Variable1.ClassName, Variable2.ClassName);

            if (Relationship == null)
            {
                return(false);
            }
            bool Success = Relationship.DestroyRelationship(Variable1.ReferencedInstanceId, Variable2.ReferencedInstanceId);

            return(Success);
        }
Esempio n. 11
0
        public override Boolean Execute(OALProgram OALProgram, EXEScope Scope)
        {
            this.OALProgram = OALProgram;

            OALProgram.AccessInstanceDatabase();
            EXEReferencingVariable    IteratorVariable = this.FindReferencingVariableByName(this.IteratorName);
            EXEReferencingSetVariable IterableVariable = this.FindSetReferencingVariableByName(this.IterableName);

            OALProgram.LeaveInstanceDatabase();

            Boolean Success = true;

            // We cannot iterate over not existing reference set
            if (Success && IterableVariable == null)
            {
                Success = false;
            }

            // If iterator already exists and its class does not match the iterable class, we cannot do this
            if (Success && IteratorVariable != null && !IteratorVariable.ClassName.Equals(IterableVariable.ClassName))
            {
                Success = false;
            }

            // If iterator name is already taken for another variable, we quit again. Otherwise we create the iterator variable
            if (Success && IteratorVariable == null)
            {
                IteratorVariable = new EXEReferencingVariable(this.IteratorName, IterableVariable.ClassName, -1);
                Success          = this.GetSuperScope().AddVariable(IteratorVariable);
            }

            if (Success)
            {
                foreach (EXEReferencingVariable CurrentItem in IterableVariable.GetReferencingVariables())
                {
                    //!!NON-RECURSIVE!!
                    this.ClearVariables();

                    IteratorVariable.ReferencedInstanceId = CurrentItem.ReferencedInstanceId;

                    Console.WriteLine("ForEach: " + CurrentItem.ReferencedInstanceId);

                    foreach (EXECommand Command in this.Commands)
                    {
                        if (this.CurrentLoopControlCommand != LoopControlStructure.None)
                        {
                            break;
                        }

                        Success = Command.SynchronizedExecute(OALProgram, this);
                        if (!Success)
                        {
                            break;
                        }
                    }

                    if (!Success)
                    {
                        break;
                    }

                    if (this.CurrentLoopControlCommand == LoopControlStructure.Break)
                    {
                        this.CurrentLoopControlCommand = LoopControlStructure.None;
                        break;
                    }
                    else if (this.CurrentLoopControlCommand == LoopControlStructure.Continue)
                    {
                        this.CurrentLoopControlCommand = LoopControlStructure.None;
                        continue;
                    }
                }
            }


            return(Success);
        }
        // SetUloh2
        public override bool Execute(OALProgram OALProgram, EXEScope Scope)
        {
            //Select instances of given class that match the criteria and assign them to variable with given name
            // ClassName tells us which class we are interested in
            // Cardinality tells us whether we want one random instance (matching the criteria) or all of them
            // "Many" - we create variable EXEReferencingSetVariable, "Any" - we create variable EXEReferencingVariable
            // Variable name tells us how to name the newly created referencing variable
            // Where condition tells us which instances to select from all instances of the class (just do EXEASTNode.Evaluate and return true if the result "true" and false for "false")
            // When making unit tests, do not use the "where" causule yet, because its evaluation is not yet implemented
            // If relationship selection does not exists, this is problem

            if (this.RelationshipSelection == null)
            {
                return(false);
            }

            CDClass Class = OALProgram.ExecutionSpace.getClassByName(this.RelationshipSelection.GetLastClassName());

            if (Class == null)
            {
                return(false);
            }

            // We need to check, if the variable already exists, it must be of corresponding type
            if (Scope.VariableNameExists(this.VariableName))
            {
                if
                (
                    !(
                        (EXECommandQuerySelect.CardinalityAny.Equals(this.Cardinality)
                         &&
                         this.RelationshipSelection.GetLastClassName() == Scope.FindReferencingVariableByName(this.VariableName).ClassName)
                        ||
                        (EXECommandQuerySelect.CardinalityMany.Equals(this.Cardinality)
                         &&
                         this.RelationshipSelection.GetLastClassName() == Scope.FindSetReferencingVariableByName(this.VariableName).ClassName)
                        )
                )
                {
                    return(false);
                }
            }

            // Evaluate relationship selection. If it fails, execution fails too
            List <long> SelectedIds = this.RelationshipSelection.Evaluate(OALProgram.RelationshipSpace, Scope);

            if (SelectedIds == null)
            {
                return(false);
            }
            // If class has no instances, command may execute successfully, but we better verify references in the WHERE condition
            if (SelectedIds.Count() == 0 && this.WhereCondition != null)
            {
                return(this.WhereCondition.VerifyReferences(Scope, OALProgram.ExecutionSpace));
            }

            // Now let's evaluate the condition
            if (this.WhereCondition != null && SelectedIds.Any())
            {
                String TempSelectedVarName = "selected";

                EXEReferencingVariable SelectedVar = new EXEReferencingVariable(TempSelectedVarName, this.RelationshipSelection.GetLastClassName(), -1);
                if (!Scope.AddVariable(SelectedVar))
                {
                    return(false);
                }

                List <long> ResultIds = new List <long>();
                foreach (long Id in SelectedIds)
                {
                    SelectedVar.ReferencedInstanceId = Id;
                    String ConditionResult = this.WhereCondition.Evaluate(Scope, OALProgram.ExecutionSpace);
                    //Console.Write(Id + " : " + ConditionResult);

                    if (!EXETypes.IsValidValue(ConditionResult, EXETypes.BooleanTypeName))
                    {
                        Scope.DestroyReferencingVariable(TempSelectedVarName);
                        return(false);
                    }

                    if (EXETypes.BooleanTrue.Equals(ConditionResult))
                    {
                        ResultIds.Add(Id);
                    }
                }

                SelectedIds = ResultIds;
                Scope.DestroyReferencingVariable(TempSelectedVarName);
            }

            // Now we have ids of selected instances. Let's assign them to a variable
            if (EXECommandQuerySelect.CardinalityMany.Equals(this.Cardinality))
            {
                EXEReferencingSetVariable Variable = Scope.FindSetReferencingVariableByName(this.VariableName);
                if (Variable == null)
                {
                    Variable = new EXEReferencingSetVariable(this.VariableName, this.RelationshipSelection.GetLastClassName());
                    if (!Scope.AddVariable(Variable))
                    {
                        return(false);
                    }
                }
                foreach (long Id in SelectedIds)
                {
                    Variable.AddReferencingVariable(new EXEReferencingVariable("", Variable.ClassName, Id));
                }
            }
            else if (EXECommandQuerySelect.CardinalityAny.Equals(this.Cardinality))
            {
                EXEReferencingVariable Variable = Scope.FindReferencingVariableByName(this.VariableName);
                if (Variable == null)
                {
                    long ResultId = SelectedIds.Any() ? SelectedIds[new Random().Next(SelectedIds.Count)] : -1;
                    Variable = new EXEReferencingVariable(this.VariableName, this.RelationshipSelection.GetLastClassName(), ResultId);
                    if (!Scope.AddVariable(Variable))
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Esempio n. 13
0
 public void AddReferencingVariable(EXEReferencingVariable NewReferencingVariable)
 {
     this.ReferencingVariables.Add(NewReferencingVariable);
 }
Esempio n. 14
0
        public bool VerifyReferences(EXEScope Scope, CDClassPool ExecutionSpace)
        {
            bool Result = false;
            EXEExpressionEvaluator      Evaluator       = new EXEExpressionEvaluator();
            EXEEvaluatorHandleOperators HandleEvaluator = new EXEEvaluatorHandleOperators();
            EXEReferenceEvaluator       AccessEvaluator = new EXEReferenceEvaluator();

            // If we just calculate with ints, reals, bools, strings
            if (Evaluator.IsSimpleOperator(this.Operation))
            {
                foreach (EXEASTNode Operand in this.Operands)
                {
                    if (!Operand.VerifyReferences(Scope, ExecutionSpace))
                    {
                        return(false);
                    }
                }
                Result = true;
            }
            // If we have handle operators
            else if (HandleEvaluator.IsHandleOperator(this.Operation))
            {
                if (this.Operands.Count() == 1 && Scope.FindReferenceHandleByName(((EXEASTNodeLeaf)this.Operands[0]).GetNodeValue()) != null)
                {
                    Result = true;
                }
            }
            // If we have access operator - we either access attribute or have decimal number. There are always 2 operands
            else if (".".Equals(this.Operation) && this.Operands.Count == 2)
            {
                if (EXETypes.IntegerTypeName.Equals(EXETypes.DetermineVariableType("", this.Operands[0].GetNodeValue())) &&
                    EXETypes.IntegerTypeName.Equals(EXETypes.DetermineVariableType("", this.Operands[1].GetNodeValue()))
                    )
                {
                    Result = true;
                }
                else if (EXETypes.ReferenceTypeName.Equals(EXETypes.DetermineVariableType("", this.Operands[0].GetNodeValue())) &&
                         EXETypes.ReferenceTypeName.Equals(EXETypes.DetermineVariableType("", this.Operands[1].GetNodeValue()))
                         )
                {
                    EXEReferencingVariable Variable = Scope.FindReferencingVariableByName(this.Operands[0].GetNodeValue());
                    if (Variable == null)
                    {
                        return(false);
                    }
                    CDClass Class = ExecutionSpace.getClassByName(Variable.ClassName);
                    if (Class == null)
                    {
                        return(false);
                    }
                    CDAttribute Attribute = Class.GetAttributeByName(this.Operands[1].GetNodeValue());
                    if (Attribute == null)
                    {
                        return(false);
                    }
                    CDClassInstance Instance = Variable.RetrieveReferencedClassInstance(ExecutionSpace);
                    if (Instance == null)
                    {
                        return(false);
                    }
                    Result = true;
                }
            }
            return(Result);
        }