Exemplo n.º 1
0
        /// <summary>
        /// Encode boolean expression
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model)
        {
            if (IsBinaryExpression())
            {
                ExpressionBDDEncoding result = new ExpressionBDDEncoding();
                if (IsBinaryLogicalExpression())
                {
                    result = TranslateLogicalBinaryExpression(model);
                }
                else if (IsRelationalExpression())
                {
                    result = TranslateRelationalBinaryExpression(model);
                }

                return(result);
            }
            else if (this.Operator == NOT)
            {
                ExpressionBDDEncoding result = new ExpressionBDDEncoding();

                ExpressionBDDEncoding operandBddEncoding = this.Argument1.TranslateBoolExpToBDD(model);
                result.AddNodeToGuard(CUDD.Function.Not(operandBddEncoding.GuardDDs));

                return(result);
            }
            //
            throw new Exception("Unknown operator");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return only guards for complete boolean expression, no expression
        /// Use this when we want to encode a single assignment or the assignment does not depend other assignments.
        /// </summary>
        public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model)
        {
            ExpressionBDDEncoding result = new ExpressionBDDEncoding();
            ExpressionBDDEncoding variableBddEncoding = new VariablePrime(this.LeftHandSide).TranslateIntExpToBDD(model);

            ExpressionBDDEncoding valueBddEncoding = this.RightHandSide.TranslateIntExpToBDD(model);

            for (int i = 0; i < variableBddEncoding.Count(); i++)
            {
                for (int j = 0; j < valueBddEncoding.Count(); j++)
                {
                    CUDD.Ref(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]);
                    CUDDNode guardDD = CUDD.Function.And(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }

                    CUDD.Ref(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]);
                    CUDDNode assignmentDD = CUDD.Function.Equal(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]);

                    guardDD = CUDD.Function.And(guardDD, assignmentDD);
                    result.AddNodeToGuard(guardDD);
                }
            }
            //remove unused expression
            variableBddEncoding.DeRef();
            valueBddEncoding.DeRef();

            return(result);
        }
Exemplo n.º 3
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.º 4
0
        /// <summary>
        /// Encode this assignment as a single statement
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model)
        {
            ExpressionBDDEncoding result = new ExpressionBDDEncoding();

            ExpressionBDDEncoding variableBddEncoding = new PrimitiveApplication(PrimitiveApplication.ARRAYPRIME, this.RecordExpression,
                                                                                    this.PropertyExpression).TranslateIntExpToBDD(model);
            ExpressionBDDEncoding valueBddEncoding = this.RightHandExpression.TranslateIntExpToBDD(model);
            for (int i = 0; i < variableBddEncoding.Count(); i++)
            {
                for (int j = 0; j < valueBddEncoding.Count(); j++)
                {
                    CUDD.Ref(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]);
                    CUDDNode guardDD = CUDD.Function.And(variableBddEncoding.GuardDDs[i], valueBddEncoding.GuardDDs[j]);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }

                    CUDD.Ref(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]);
                    CUDDNode assignmentDD = CUDD.Function.Equal(variableBddEncoding.ExpressionDDs[i], valueBddEncoding.ExpressionDDs[j]);

                    guardDD = CUDD.Function.And(guardDD, assignmentDD);
                    result.AddNodeToGuard(guardDD);
                }
            }
            //remove unused expression
            variableBddEncoding.DeRef();
            valueBddEncoding.DeRef();

            return result;
        }
Exemplo n.º 5
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.º 6
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);
            }
        }
Exemplo n.º 7
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.º 8
0
        private ExpressionBDDEncoding TranslateRelationalBinaryExpression(Model model)
        {
            ExpressionBDDEncoding result = new ExpressionBDDEncoding();
            ExpressionBDDEncoding operandTranslation1 = this.Argument1.TranslateIntExpToBDD(model);
            ExpressionBDDEncoding operandTranslation2 = this.Argument2.TranslateIntExpToBDD(model);

            CUDDNode guardOfResult = CUDD.Constant(0);

            for (int i = 0; i < operandTranslation1.Count(); i++)
            {
                for (int j = 0; j < operandTranslation2.Count(); j++)
                {
                    CUDD.Ref(operandTranslation1.GuardDDs[i], operandTranslation2.GuardDDs[j]);
                    CUDDNode guardDD = CUDD.Function.And(operandTranslation1.GuardDDs[i], operandTranslation2.GuardDDs[j]);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }
                    CUDDNode expressionDD;
                    CUDD.Ref(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                    switch (this.Operator)
                    {
                    case EQUAL:
                        expressionDD = CUDD.Function.Equal(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    case NOT_EQUAL:
                        expressionDD = CUDD.Function.NotEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    case GREATER:
                        expressionDD = CUDD.Function.Greater(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    case GREATER_EQUAL:
                        expressionDD = CUDD.Function.GreaterEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    case LESS:
                        expressionDD = CUDD.Function.Less(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    case LESS_EQUAL:
                        expressionDD = CUDD.Function.LessEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                        break;

                    default:
                        throw new Exception("Unknown operator");
                    }
                    guardDD = CUDD.Function.And(guardDD, expressionDD);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }
                    guardOfResult = CUDD.Function.Or(guardOfResult, guardDD);
                }
            }

            result.AddNodeToGuard(guardOfResult);
            //Remove all old guard, expression
            operandTranslation1.DeRef();
            operandTranslation2.DeRef();
            return(result);
        }
Exemplo n.º 9
0
        private ExpressionBDDEncoding TranslateRelationalBinaryExpression(Model model)
        {
            ExpressionBDDEncoding result = new ExpressionBDDEncoding();
            ExpressionBDDEncoding operandTranslation1 = this.Argument1.TranslateIntExpToBDD(model);
            ExpressionBDDEncoding operandTranslation2 = this.Argument2.TranslateIntExpToBDD(model);

            CUDDNode guardOfResult = CUDD.Constant(0);
            for (int i = 0; i < operandTranslation1.Count(); i++)
            {
                for (int j = 0; j < operandTranslation2.Count(); j++)
                {
                    CUDD.Ref(operandTranslation1.GuardDDs[i], operandTranslation2.GuardDDs[j]);
                    CUDDNode guardDD = CUDD.Function.And(operandTranslation1.GuardDDs[i], operandTranslation2.GuardDDs[j]);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }
                    CUDDNode expressionDD;
                    CUDD.Ref(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                    switch (this.Operator)
                    {
                        case EQUAL:
                            expressionDD = CUDD.Function.Equal(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        case NOT_EQUAL:
                            expressionDD = CUDD.Function.NotEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        case GREATER:
                            expressionDD = CUDD.Function.Greater(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        case GREATER_EQUAL:
                            expressionDD = CUDD.Function.GreaterEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        case LESS:
                            expressionDD = CUDD.Function.Less(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        case LESS_EQUAL:
                            expressionDD = CUDD.Function.LessEqual(operandTranslation1.ExpressionDDs[i], operandTranslation2.ExpressionDDs[j]);
                            break;
                        default:
                            throw new Exception("Unknown operator");
                    }
                    guardDD = CUDD.Function.And(guardDD, expressionDD);
                    if (guardDD.Equals(CUDD.ZERO))
                    {
                        CUDD.Deref(guardDD);
                        continue;
                    }
                    guardOfResult = CUDD.Function.Or(guardOfResult, guardDD);
                }
            }

            result.AddNodeToGuard(guardOfResult);
            //Remove all old guard, expression
            operandTranslation1.DeRef();
            operandTranslation2.DeRef();
            return result;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Encode boolean expression
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override ExpressionBDDEncoding TranslateBoolExpToBDD(Model model)
        {
            if (IsBinaryExpression())
            {
                ExpressionBDDEncoding result = new ExpressionBDDEncoding();
                if (IsBinaryLogicalExpression())
                {
                    result = TranslateLogicalBinaryExpression(model);
                }
                else if (IsRelationalExpression())
                {
                    result = TranslateRelationalBinaryExpression(model);
                }

                return result;
            }
            else if (this.Operator == NOT)
            {
                ExpressionBDDEncoding result = new ExpressionBDDEncoding();

                ExpressionBDDEncoding operandBddEncoding = this.Argument1.TranslateBoolExpToBDD(model);
                result.AddNodeToGuard(CUDD.Function.Not(operandBddEncoding.GuardDDs));

                return result;
            }
            //
            throw new Exception("Unknown operator");
        }
Exemplo n.º 11
0
        /// <summary>
        /// Return the variable values after the assignment based on the current variable values 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 (IsComplexUpdate(updatedVariablesBefore[j1], updatedVariablesInNewUpdate[j2], usedVariableInNewUpdate[j2]))
                    {
                        model.CreateTemporaryVar();
                        Expression newUpdate1;
                        Expression newUpdate2;

                        newUpdate1 = new Assignment(Model.TEMPORARY_VARIABLE, this.RightHandSide);
                        newUpdate2 = new Assignment(this.LeftHandSide, new Variable(Model.TEMPORARY_VARIABLE));

                        resultBefore.Ref();
                        ExpressionBDDEncoding tempResult1 = newUpdate1.TranslateStatementToBDD(resultBefore, model);
                        ExpressionBDDEncoding tempResult2 = newUpdate2.TranslateStatementToBDD(tempResult1, model);
                        for (int i = 0; i < tempResult2.Count(); i++)
                        {
                            tempResult2.GuardDDs[i] = CUDD.Abstract.ThereExists(tempResult2.GuardDDs[i], model.GetRowVars(model.GetVarIndex(Model.TEMPORARY_VARIABLE)));
                            tempResult2.GuardDDs[i] = CUDD.Abstract.ThereExists(tempResult2.GuardDDs[i], model.GetColVars(model.GetVarIndex(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.º 12
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;
            }
        }