Exemplo n.º 1
0
        /// <summary>
        /// [ REFS: 'result', DEREFS: 'resultBefore' ]
        /// </summary>
        public override ExpressionBDDEncoding TranslateStatementToBDD(ExpressionBDDEncoding resultBefore, Model model)
        {
            ExpressionBDDEncoding result = new ExpressionBDDEncoding();

            CUDDNode conditionDD = CUDD.Function.Or(this.Test.TranslateBoolExpToBDD(model).GuardDDs);

            do
            {
                ExpressionBDDEncoding tempResult = new ExpressionBDDEncoding();

                List<List<int>> updatedVariablesBefore = new List<List<int>>();
                for (int index = 0; index < resultBefore.Count(); index++)
                {
                    updatedVariablesBefore.Add(model.GetColSupportedVars(resultBefore.GuardDDs[index]));
                }

                List<int> usedVariablesInCondition = model.GetRowSupportedVars(conditionDD);

                for (int j1 = 0; j1 < resultBefore.Count(); j1++)
                {
                    foreach (int index in usedVariablesInCondition)
                    {
                        if (updatedVariablesBefore[j1].Contains(index))
                        {
                            conditionDD = CUDD.Variable.SwapVariables(conditionDD, model.GetRowVars(index), model.GetColVars(index));
                        }
                    }

                    //Add configuration making the While condition true
                    CUDD.Ref(resultBefore.GuardDDs[j1], conditionDD);
                    CUDDNode transition = CUDD.Function.And(resultBefore.GuardDDs[j1], conditionDD);
                    tempResult.AddNodeToGuard(transition);

                    //Add configuration making the While condition false
                    CUDD.Ref(resultBefore.GuardDDs[j1], conditionDD);
                    CUDDNode falseTransition = CUDD.Function.And(resultBefore.GuardDDs[j1], CUDD.Function.Not(conditionDD));
                    result.AddNodeToGuard(falseTransition);
                }

                //There is no any configuration making the While condition true
                if (tempResult.Count() > 0)
                {
                    resultBefore.DeRef();
                    resultBefore = this.Body.TranslateStatementToBDD(tempResult, model);
                }
                else
                {
                    break;
                }
            } while (true);

            resultBefore.DeRef();

            return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encode this assignment in a sequence of statements. Return variable values after this assignment based on the current value in resultBefore
        /// [ REFS: 'result', DEREFS: 'resultBefore' ]
        /// </summary>
        public override ExpressionBDDEncoding TranslateStatementToBDD(ExpressionBDDEncoding resultBefore, Model model)
        {
            ExpressionBDDEncoding newUpdate = this.TranslateBoolExpToBDD(model);

            ExpressionBDDEncoding tempResult = new ExpressionBDDEncoding();

            List<List<int>> updatedVariablesInNewUpdate = new List<List<int>>();
            for (int index = 0; index < newUpdate.Count(); index++)
            {
                updatedVariablesInNewUpdate.Add(model.GetColSupportedVars(newUpdate.GuardDDs[index]));
            }

            List<List<int>> usedVariableInNewUpdate = new List<List<int>>();
            for (int index = 0; index < newUpdate.Count(); index++)
            {
                usedVariableInNewUpdate.Add(model.GetRowSupportedVars(newUpdate.GuardDDs[index]));
            }

            List<List<int>> updatedVariablesBefore = new List<List<int>>();
            for (int index = 0; index < resultBefore.Count(); index++)
            {
                updatedVariablesBefore.Add(model.GetColSupportedVars(resultBefore.GuardDDs[index]));
            }

            for (int j1 = 0; j1 < resultBefore.Count(); j1++)
            {
                for (int j2 = 0; j2 < newUpdate.Count(); j2++)
                {
                    //a variable is already updated, now is updated again and it also apprears in the value expression
                    if (Assignment.IsComplexUpdate(updatedVariablesBefore[j1], updatedVariablesInNewUpdate[j2], usedVariableInNewUpdate[j2]))
                    {
                        model.CreateTemporaryVar();
                        Expression newUpdate1;
                        Expression newUpdate2;

                        PropertyAssignment assignment = this as PropertyAssignment;
                        newUpdate1 = new Assignment(Model.TEMPORARY_VARIABLE, this.RightHandExpression);
                        newUpdate2 = new PropertyAssignment(this.RecordExpression, this.PropertyExpression, new Variable(Model.TEMPORARY_VARIABLE));

                        resultBefore.Ref();
                        ExpressionBDDEncoding tempResult1 = newUpdate1.TranslateStatementToBDD(resultBefore, model);
                        ExpressionBDDEncoding tempResult2 = newUpdate2.TranslateStatementToBDD(tempResult1, model);

                        //Remove the temporary variable from transition
                        for (int i = 0; i < tempResult2.Count(); i++)
                        {
                            tempResult2.GuardDDs[i] = CUDD.Abstract.ThereExists(tempResult2.GuardDDs[i], model.GetRowVars(Model.TEMPORARY_VARIABLE));
                            tempResult2.GuardDDs[i] = CUDD.Abstract.ThereExists(tempResult2.GuardDDs[i], model.GetColVars(Model.TEMPORARY_VARIABLE));

                            tempResult.AddNodeToGuard(tempResult2.GuardDDs[i]);
                        }
                    }
                    else
                    {
                        //swap row, col updated variable in result in the new update command expression
                        CUDDNode update2 = newUpdate.GuardDDs[j2];
                        CUDD.Ref(update2);
                        foreach (int index in updatedVariablesBefore[j1])
                        {
                            if (usedVariableInNewUpdate[j2].Contains(index))
                            {
                                update2 = CUDD.Variable.SwapVariables(update2, model.GetColVars(index), model.GetRowVars(index));
                            }
                        }

                        //Restrict updated variable in new update of the old update
                        CUDDNode update1 = resultBefore.GuardDDs[j1];
                        CUDD.Ref(update1);
                        foreach (int index in updatedVariablesInNewUpdate[j2])
                        {
                            if (updatedVariablesBefore[j1].Contains(index))
                            {
                                update1 = CUDD.Abstract.ThereExists(update1, model.GetColVars(index));
                            }
                        }

                        CUDDNode transition = CUDD.Function.And(update1, update2);
                        tempResult.AddNodeToGuard(transition);
                    }
                }
            }

            resultBefore.DeRef();
            newUpdate.DeRef();

            return tempResult;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Return variable values after the If statement is executed based on resultBefore
        /// [ REFS: 'result', DEREFS: 'resultBefore' ]
        /// </summary>
        public override ExpressionBDDEncoding TranslateStatementToBDD(ExpressionBDDEncoding resultBefore, Model model)
        {
            ExpressionBDDEncoding tempResult1 = new ExpressionBDDEncoding();
            ExpressionBDDEncoding tempResult2 = new ExpressionBDDEncoding();

            List<List<int>> updatedVariablesBefore = new List<List<int>>();
            for (int index = 0; index < resultBefore.Count(); index++)
            {
                updatedVariablesBefore.Add(model.GetColSupportedVars(resultBefore.GuardDDs[index]));
            }

            ExpressionBDDEncoding conditionDD = this.Condition.TranslateBoolExpToBDD(model);
            List<List<int>> usedVariablesInCondition = new List<List<int>>();
            for (int index = 0; index < conditionDD.Count(); index++)
            {
                usedVariablesInCondition.Add(model.GetRowSupportedVars(conditionDD.GuardDDs[index]));
            }

            for (int j1 = 0; j1 < resultBefore.Count(); j1++)
            {
                for (int j2 = 0; j2 < conditionDD.Count(); j2++)
                {
                    CUDDNode condition = conditionDD.GuardDDs[j2];
                    CUDD.Ref(condition);
                    foreach (int index in usedVariablesInCondition[j2])
                    {
                        if (updatedVariablesBefore[j1].Contains(index))
                        {
                            condition = CUDD.Variable.SwapVariables(condition, model.GetRowVars(index), model.GetColVars(index));
                        }
                    }

                    CUDD.Ref(resultBefore.GuardDDs[j1]);
                    CUDDNode transition = CUDD.Function.And(resultBefore.GuardDDs[j1], condition);

                    tempResult1.AddNodeToGuard(transition);
                }
            }

            //the condition is not always false (a = 1; if(a > 1))
            if (tempResult1.Count() > 0)
            {
                tempResult1 = this.ThenPart.TranslateStatementToBDD(tempResult1, model);
            }

            //
            tempResult2 = new ExpressionBDDEncoding();
            for (int j1 = 0; j1 < resultBefore.Count(); j1++)
            {
                for (int j2 = 0; j2 < conditionDD.Count(); j2++)
                {
                    CUDDNode condition = conditionDD.GuardDDs[j2];
                    CUDD.Ref(condition);
                    condition = CUDD.Function.Not(condition);
                    foreach (int index in usedVariablesInCondition[j2])
                    {
                        if (updatedVariablesBefore[j1].Contains(index))
                        {
                            condition = CUDD.Variable.SwapVariables(condition, model.GetRowVars(index), model.GetColVars(index));
                        }
                    }

                    CUDD.Ref(resultBefore.GuardDDs[j1]);
                    CUDDNode transition = CUDD.Function.And(resultBefore.GuardDDs[j1], condition);

                    tempResult2.AddNodeToGuard(transition);
                }
            }

            if (this.ElsePart != null && tempResult2.Count() > 0)
            {
                tempResult2 = this.ElsePart.TranslateStatementToBDD(tempResult2, model);
            }

            conditionDD.DeRef();

            //Combine
            if (tempResult1.Count() == 0 && tempResult2.Count() == 0)//condition always false, no else part
            {
                return resultBefore;
            }
            else if (tempResult1.Count() == 0 && tempResult2.Count() > 0)//condition always false, having else part
            {
                resultBefore.DeRef();
                return tempResult2;
            }
            else
            {
                resultBefore.DeRef();
                tempResult1.AddNodeToGuard(tempResult2.GuardDDs);
                return tempResult1;
            }
        }