示例#1
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);
        }
示例#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;
        }