Beispiel #1
0
        private static Expression CreateAssignment(GeneLink b, int i,
                                                   Func <string, Expression, Expression> accumulatorFunc)
        {
            Expression ass;
            var        from = Formater.FormatParameter(b.From, i);
            var        to   = Formater.FormatParameter(b.To, i + 1);

            if (b.IsPositive)
            {
                //ass = new Assignment(b.To,
                //    new Variable(b.From));

                ass = accumulatorFunc.Invoke(to, new Variable(from));
            }
            else
            {
                ass = accumulatorFunc.Invoke(to,
                                             new PrimitiveApplication(
                                                 PrimitiveApplication.AND, new PrimitiveApplication(nOT, new Variable(from))));

                //ass = new PrimitiveApplication(PrimitiveApplication.EQUAL,
                //            new Variable(to),
                //            new PrimitiveApplication(
                //                PrimitiveApplication.AND, new PrimitiveApplication(nOT, new Variable(from))));
            }
            return(ass);
        }
Beispiel #2
0
        public static Dictionary <string, bool> CreateDictBasedOnAutomata(GeneNode automata)
        {
            Dictionary <string, bool> result = new Dictionary <string, bool>();

            if (automata == null || automata.Transitions == null || !automata.Transitions.Any())
            {
                return(null);
            }

            int i = 0;

            automata.Visit(l =>
            {
                var tr = BDDSolver.GetTransitions(l);

                if (tr == null)
                {
                    return;
                }

                tr
                .ForEach(
                    f =>
                {
                    var key   = Formater.FormatParameter(f.Key, i);
                    var value = f.Value.Value;

                    result[key] = value;
                });
                i++;
            });


            return(result);
        }
Beispiel #3
0
        private static Expression CreateFunctionApplication(
            Dictionary <string, List <int> > availableFunctions,
            IEnumerable <IGrouping <string, GeneLink> > toDictionary,
            int i,
            Mode mode,
            Dictionary <string, bool> values)
        {
            Expression res = null;


            toDictionary.ToList().ForEach(ff =>
            {
                Expression ass = null;
                var to         = ff.Key;
                var froms      = ff.Where(a => !a.IsOptional).ToList();

                // can be null!!

                if (availableFunctions == null || !availableFunctions.ContainsKey(to))
                {
                    res = AddIfExist(res, CreateAssignment(ff.FirstOrDefault(), i, GetFunction(mode)), mode);
                }
                else
                {
                    var availableFunc        = availableFunctions[to];
                    var funcAssignmentHelper = new FuncAssignmentHelper();
                    var toFormatted          = Formater.FormatParameter(to, i + 1);

                    availableFunc.ForEach(f =>
                    {
                        ass =
                            funcAssignmentHelper.CreateFuncAssignment(to, froms, i, f);

                        //if (values != null && values.ContainsKey(toFormatted))
                        //{
                        //    ass = new PrimitiveApplication(PrimitiveApplication.AND, ass, new BoolConstant(values[toFormatted]));
                        //    values.Remove(toFormatted);
                        //}
                    });


                    if (mode == Mode.Equal)
                    {
                        res = new PrimitiveApplication(PrimitiveApplication.EQUAL, new Variable(toFormatted),
                                                       ass);
                    }
                    else
                    {
                        res = GetFunction(mode).Invoke(toFormatted, ass);
                    }
                }
            });

            return(res);
        }
Beispiel #4
0
        private static Expression SetGoalBasedOnFunction(List <GeneLink> booleanNetwok,
                                                         Dictionary <string, List <int> > availableFunctions, int depth)
        {
            Expression seq = null;

            var toDictionary = booleanNetwok.GroupBy(a => a.To);

            for (int i = 0; i < depth - 1; i++)
            {
                Expression ass = null;


                toDictionary.ToList().ForEach(ff =>
                {
                    var b    = ff.FirstOrDefault();
                    var from = Formater.FormatParameter(b.From, i);
                    var to   = Formater.FormatParameter(b.To, i + 1);

                    if (b.IsPositive)
                    {
                        //ass = new Assignment(b.To,
                        //    new Variable(b.From));

                        ass = new PrimitiveApplication(PrimitiveApplication.EQUAL,
                                                       new Variable(to), new Variable(from));
                    }
                    else
                    {
                        ass = new PrimitiveApplication(PrimitiveApplication.EQUAL,
                                                       new Variable(to),
                                                       new PrimitiveApplication(
                                                           PrimitiveApplication.AND, new PrimitiveApplication(nOT, new Variable(from))));
                    }

                    if (seq == null)
                    {
                        seq = ass;
                    }
                    else
                    {
                        seq =
                            new PrimitiveApplication(PrimitiveApplication.AND,
                                                     seq,
                                                     ass);
                    }
                });
            }

            return(seq);
        }
Beispiel #5
0
        private static Expression CreateExpressionBasedOnAutomata(GeneNode automata)
        {
            Expression goal1 = null;

            if (automata == null || automata.Transitions == null || !automata.Transitions.Any())
            {
                return(null);
            }

            int i = 0;

            automata.Visit(l =>
            {
                var tr = GetTransitions(l);

                if (tr == null)
                {
                    return;
                }

                tr
                .ForEach(
                    f =>
                {
                    var primitiveApplication =
                        new Assignment(Formater.FormatParameter(f.Key, i), new BoolConstant(f.Value.Value));

                    if (goal1 == null)
                    {
                        goal1 = primitiveApplication;
                    }
                    else
                    {
                        goal1 = new Sequence(
                            goal1,
                            primitiveApplication);
                    }
                });
                i++;
            });


            logger.Info("Goal: " + goal1);
            return(goal1);
        }
Beispiel #6
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);
            }
        }
        private static Expression AppyToAll(List <GeneLink> froms, int i,
                                            bool value, string func)
        {
            if (func == PrimitiveApplication.AND)
            {
                Expression app = null;

                froms.ForEach(f =>
                {
                    var formatParameter = Formater.FormatParameter(f.From, i);

                    Expression primitiveApplication =
                        new Variable(formatParameter);

                    if (app == null)
                    {
                        app = primitiveApplication;
                    }
                    else
                    {
                        app = new PrimitiveApplication(func, app, primitiveApplication);
                    }
                });

                if (!value)
                {
                    app = new PrimitiveApplication(PrimitiveApplication.NOT, app);
                }

                return(new PrimitiveApplication(func, app));
            }
            else // or
            {
                Expression app = null;

                froms.ForEach(f =>
                {
                    var formatParameter = Formater.FormatParameter(f.From, i);

                    Expression primitiveApplication =
                        new Variable(formatParameter);


                    if (app == null)
                    {
                        app = primitiveApplication;
                    }
                    else
                    {
                        app = new PrimitiveApplication(PrimitiveApplication.OR,
                                                       app,
                                                       primitiveApplication);
                    }
                });

                if (!value)
                {
                    app = new PrimitiveApplication(PrimitiveApplication.NOT, app);
                }

                return(new PrimitiveApplication(PrimitiveApplication.OR, app, new BoolConstant(true)));
            }


            return(null);
        }
Beispiel #8
0
        private BDDNode CreateFunctionApplication(
            BDDManager manager,
            Dictionary <string, List <int> > availableFunctions,
            IEnumerable <IGrouping <string, GeneLink> > toDictionary,
            int i,
            Dictionary <string, BDDNode> nodeStore)
        {
            BDDNode res = null;


            toDictionary.ToList().ForEach(ff =>
            {
                BDDNode ass = null;
                var froms   = ff.Where(a => !a.IsOptional).ToList();

                // can be null!!
                var to = Formater.FormatParameter(ff.Key, i + 1);

                if (availableFunctions == null || !availableFunctions.ContainsKey(ff.Key))
                {
                    var from1 = Formater.FormatParameter(ff.FirstOrDefault().From, i);
                    //TODO: NOT!
                    res = manager.Equal(nodeStore[from1], nodeStore[to]);
                }
                else // real functions
                {
                    var availableFunc        = availableFunctions[ff.Key];
                    var funcAssignmentHelper =
                        new BddNodeFuncAssignmentHelper(manager, nodeStore);
                    BDDNode rightSide = null;

                    var leftSide =
                        BDDSharpSolver.CreateNodeBasedOnAutomata(
                            to, true, manager,
                            nodeStore[to].Index);

                    availableFunc.ForEach(f =>
                    {
                        ass =
                            funcAssignmentHelper.CreateFuncAssignment(to, froms, i, f);

                        ass = manager.And(nodeStore[CreateFunctionKey(f, ff.Key)], ass);

                        if (rightSide == null)
                        {
                            rightSide = ass;
                        }
                        else
                        {
                            rightSide = manager.Or(rightSide, ass);
                        }
                    });

                    logger.Info("Right Side");
                    LogToDotFormat(rightSide, manager, nodeStore);

                    res = manager.Equal(leftSide, rightSide);
                }
            });

            return(res);
        }
Beispiel #9
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;
        }
Beispiel #10
0
 public static PrimitiveApplication SetBooleanValue(int i, bool value, string parameter)
 {
     return new PrimitiveApplication(PrimitiveApplication.EQUAL,
         new Variable(Formater.FormatParameter(parameter, i)),
         new BoolConstant(value));
 }