Exemplo 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);
            }
        }
Exemplo n.º 2
0
        public bool IsValidPath(GeneNode automata, List <GeneLink> booleanNetwok, Dictionary <string, List <int> > availableFunctions = null)
        {
            var nodeStore = new Dictionary <string, BDDNode>();
            var letters   = new List <string>();
            var depth     = automata.NodeLength;

            int z       = 0;
            var manager = new BDDManager(50);

            automata.GetAllConditionLetters(letters);


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

            int nodeNumber = 0;

            logger.Info(string.Join(",", letters));

            var assignments = BDDLogicHelper.CreateDictBasedOnAutomata(automata);

            nodeStore["1"] = manager.One;
            nodeStore["0"] = manager.Zero;

            var     assignmentsBDDNode = CreateAssignmentsBddNodes(assignments, manager, nodeStore, ref nodeNumber);
            BDDNode functionNodes      = null;

            var functionsKeys = CreateFunctionsKeys(availableFunctions);

            functionsKeys.ToList().ForEach(f =>
            {
                BDDNode currentNodeOr   = null;
                List <BDDNode> currents = new List <BDDNode>();
                f.Value.ForEach(d =>
                {
                    var c        = manager.Create(nodeNumber++, 1, 0);
                    nodeStore[d] = c;
                    currents.Add(c);

                    if (currentNodeOr == null)
                    {
                        currentNodeOr = c;
                    }
                    else
                    {
                        currentNodeOr = manager.Or(c, currentNodeOr);
                    }
                });

                currentNodeOr = manager.Equal(manager.One, currentNodeOr);

                if (functionNodes == null)
                {
                    functionNodes = currentNodeOr;
                }
                else
                {
                    functionNodes = manager.And(functionNodes, currentNodeOr);
                }
            });


            var relations =
                CreateExpressionsFromBooleanNetwork(manager, booleanNetwok, availableFunctions, depth, nodeStore);

            relations = manager.And(relations, functionNodes);
            var root = manager.And(relations, assignmentsBDDNode);


            //// LOG PART
            LogToDotFormat(root, manager, nodeStore);

            //logger.Info("relations");
            //logger.Info(manager.ToDot(relations, show_all: false));

            //logger.Info("assignments");
            //logger.Info(manager.ToDot(assignmentsBDDNode, show_all: false));


            IEnumerable <KeyValuePair <string, bool> > truth = BuildThruthTable(manager, root, nodeNumber);

            assignments.ToList().ForEach(a =>
            {
                var index = nodeStore[a.Key].Index;
                truth     = truth.Where(d =>
                                        d.Key[index] == (a.Value ? '1' : '0'));
            });

            var pathes = truth.Where(a => a.Value).ToList();

            LogValidPathes(pathes, nodeStore);

            return(pathes.Any());

            //       CheckThruthTable(truth, res);

            //return true;
        }