Ejemplo n.º 1
0
        public bool IsValidPath(GeneNode automata, List <GeneLink> booleanNetwok, Dictionary <string, List <int> > availableFunctions = null)
        {
            lock (locker)
            {
                var tempAutomata = automata;
                //    new GeneNode()
                //{
                //    NodeName = "Start",
                //    CurrentCondition = new Condition(),
                //    Transitions = new List<GeneTransition>()
                //    {
                //        new GeneTransition()
                //        {
                //            Node = automata
                //        }
                //    }
                //};

                var letters = new List <string>();

                int z     = 0;
                var depth = tempAutomata.NodeLength;

                tempAutomata.GetAllConditionLetters(letters);

                letters = letters.SelectMany(l => Enumerable.Range(0, depth).ToList().Select(n => Formater.FormatParameter(l, n))).ToList();

                logger.Info(tempAutomata.NodeLength + 1);

                Model.NUMBER_OF_EVENT             = tempAutomata.NodeLength + 2;
                Model.MAX_NUMBER_EVENT_PARAMETERS = 0;

                BDDEncoder encoder = new BDDEncoder();

                letters.Distinct().ToList().ForEach(l => encoder.model.AddLocalVar(l, 0, 1));
                logger.Info(string.Join(",", letters));

                SymbolicLTS lts = new SymbolicLTS();

                List <State> states = new List <State>();
                var          state0 = lts.AddState();
                states.Add(state0);


                lts.InitialState = states[0];

                var state1 = lts.AddState();
                states.Add(state1);
                //var goal2 = CreateExpressionBasedOnAutomata(tempAutomata);

                var        dictValues = BDDLogicHelper.CreateDictBasedOnAutomata(tempAutomata);
                Expression seq        = null;

                /*CreateExpressionsFromBooleanNetwork(booleanNetwok,
                 *      availableFunctions, depth, Mode.Assignment, dictValues);*/

                if (dictValues.Any())
                {
                    dictValues.ToList().ForEach(f =>
                    {
                        Expression curr = new Assignment(f.Key, new BoolConstant(f.Value));
                        seq             = seq == null ? curr : new Sequence(seq, curr);
                    });
                }



                //seq =  new Sequence(seq, goal2);
                //  //
                logger.Info("Assignments: " + seq);

                var trans1 =
                    new Transition(new Event("a0"), null, seq,
                                   states[0], states[1]);

                lts.Transitions.Add(trans1);
                logger.Info(lts);
                AutomataBDD systemBDD = lts.Encode(encoder);

                systemBDD.PrintTransition();
                CUDDNode initDD = CUDD.Function.Or(
                    systemBDD.initExpression.TranslateBoolExpToBDD(encoder.model).GuardDDs);

                bool reach1         = true;
                var  path           = new List <CUDDNode>();
                var  geneTransition = tempAutomata;
                InitInitialState(geneTransition, systemBDD, letters);
                var goal  = SetGoalsBasedOnAutomata(geneTransition);
                var goal3 = CreateExpressionsFromBooleanNetwork(booleanNetwok,
                                                                availableFunctions, depth, Mode.Equal);

                goal = new PrimitiveApplication(PrimitiveApplication.AND, goal, goal3);
                logger.Info("Goal: " + goal);
                path = IsExistPath(goal, encoder, path, initDD, systemBDD, letters, ref reach1);
                path.Clear();
                encoder.model.Close();
                return(reach1);
            }
        }
Ejemplo n.º 2
0
        public void Test1Test()
        {
            var    sb   = new StringBuilder();
            string varX = "x";
            string varY = "y";

            //Set number of action names, 2 for a, b
            Model.NUMBER_OF_EVENT             = 3;
            Model.MAX_NUMBER_EVENT_PARAMETERS = 0;

            BDDEncoder encoder = new BDDEncoder();

            encoder.model.AddGlobalVar(varX, 0, 10);
            encoder.model.AddGlobalVar(varY, 0, 10);

            SymbolicLTS lts = new SymbolicLTS();

            State state1 = lts.AddState();
            State state2 = lts.AddState();
            State state3 = lts.AddState();
            State state4 = lts.AddState();

            lts.InitialState = state1;

            var primitiveApplication1 = new Assignment(varX, new PrimitiveApplication(PrimitiveApplication.PLUS,
                                                                                      new Variable(varX),
                                                                                      new IntConstant(1)));


            var primitiveApplication2 = new Assignment(varY, new PrimitiveApplication(PrimitiveApplication.PLUS,
                                                                                      new Variable(varY),
                                                                                      new IntConstant(4)));


            /*
             *
             * for (int i = 0; i < exps.Count; i++)
             * {
             *  //Update eventParameterVariables[i] = exps[i]
             *  //Don't need to update exps because later after synchronization, not updated variable keeps the same value
             *  update = new Sequence(update, new Assignment(model.eventParameterVariables[i], exps[i]));
             * }
             */
            var primitiveApplication = new Sequence(primitiveApplication1,
                                                    primitiveApplication2);

            /*PrimitiveApplication.CombineProgramBlock(
             * primitiveApplication1,
             * primitiveApplication2);*/
            logger.Info(primitiveApplication);

            Transition trans1 = new Transition(new Event("a"), null,
                                               primitiveApplication,
                                               state1,
                                               state2);

            Expression assignment =
                new Assignment(varX, new PrimitiveApplication(PrimitiveApplication.PLUS,
                                                              new Variable(varX),
                                                              new IntConstant(2)));

            logger.Info("Assignments: " + assignment);

            var secAssignment =
                new Assignment(varY,
                               new PrimitiveApplication(PrimitiveApplication.PLUS,
                                                        new Variable(varY), new WildConstant()));



            Transition trans2 = new Transition(new Event("b"), null,
                                               primitiveApplication,
                                               state2,
                                               state3);

            Transition trans3 = new Transition(new Event("c"), null,
                                               primitiveApplication,
                                               state3,
                                               state4);

            lts.AddTransition(trans1);
            lts.AddTransition(trans2);
            lts.AddTransition(trans3);

            AutomataBDD systemBDD = lts.Encode(encoder);

            //Variable x is initialised to 1
            systemBDD.initExpression = new PrimitiveApplication(PrimitiveApplication.AND,
                                                                systemBDD.initExpression,
                                                                new PrimitiveApplication(PrimitiveApplication.EQUAL,
                                                                                         new Variable(varX), new IntConstant(1)));

            systemBDD.initExpression = new PrimitiveApplication(PrimitiveApplication.AND,
                                                                systemBDD.initExpression,
                                                                new PrimitiveApplication(PrimitiveApplication.EQUAL,
                                                                                         new Variable(varY), new IntConstant(1)));

            CUDDNode initDD = CUDD.Function.Or(systemBDD.initExpression.TranslateBoolExpToBDD(encoder.model).GuardDDs);

            logger.Info("init: " + systemBDD.initExpression);

            var u = 1;

            for (; u < 2; u++)
            {
                logger.Info($"U is {u}");
                //Define 2 goals
                Expression goal1 = new PrimitiveApplication(PrimitiveApplication.EQUAL,
                                                            new Variable(varX), new IntConstant(3));

                goal1 = new PrimitiveApplication(PrimitiveApplication.AND, goal1, new PrimitiveApplication(PrimitiveApplication.EQUAL, new Variable(varY), new IntConstant(9)));

                //Encode 2 goals to BDD
                CUDDNode goal1DD = CUDD.Function.Or(goal1.TranslateBoolExpToBDD(encoder.model).GuardDDs);
                logger.Info("Goal: " + goal1);

                List <CUDDNode> path = new List <CUDDNode>();

                bool reach1 = encoder.model.PathForward(initDD, goal1DD, new List <List <CUDDNode> >()
                {
                    systemBDD.transitionBDD
                }, path, true);


                if (reach1)
                {
                    sb.AppendLine("goal1 is reachable");

                    foreach (var cuddNode in path)
                    {
                        encoder.model.PrintAllVariableValue(cuddNode);
                        logger.Info("after");
                        CUDD.Print.PrintMinterm(cuddNode);
                        //     CUDD.Print.PrintBDDTree(path);
                        int valueOfX = encoder.model.GetRowVarValue(cuddNode, varX);
                        sb.AppendLine(varX + " = " + valueOfX);

                        int valueOfY = encoder.model.GetRowVarValue(cuddNode, varY);
                        sb.AppendLine(varY + " = " + valueOfY);
                    }
                }
                else
                {
                    sb.AppendLine("goal1 is unreachable");
                }

                path.Clear();
            }



            /*
             * bool reach2 = encoder.model.PathForward(initDD, goal2DD, new List<List<CUDDNode>>() { systemBDD.transitionBDD }, path, true);
             * if (reach2)
             * {
             *  sb.AppendLine("goal2 is reachable");
             *  foreach (var cuddNode in path)
             *  {
             *      int valueOfX = encoder.model.GetRowVarValue(cuddNode, varX);
             *      sb.AppendLine(varX + " = " + valueOfX);
             *  }
             * }
             * else
             * {
             *  sb.AppendLine("goal2 is unreachable");
             * }
             */
            logger.Info(sb);
            encoder.model.Close();
        }