示例#1
0
        public static Node MergeNodes(Node first, Node second)
        {
            Node merged = new Node();

            merged.Id = "NM";
            if (first.Id == "S" || second.Id == "S")
            {
                merged.Id = "S";
            }
            if (first.isFinishNode || second.isFinishNode)
            {
                merged.isFinishNode = true;
            }
            var outputs = first.Outputs.Concat(second.Outputs).ToList();
            HashSet <(Node, char)> outLinks = new HashSet <(Node, char)>();

            foreach (var output in outputs)
            {
                if (output.Destination == first || output.Destination == second)
                {
                    outLinks.Add((merged, output.Subj));
                }
                else
                {
                    outLinks.Add((output.Destination, output.Subj));
                }
                GraphAutomat.UnBind(output);
            }

            var inputs = first.Inputs.Concat(second.Inputs).ToList();
            HashSet <(Node, char)> inLinks = new HashSet <(Node, char)>();

            foreach (var input in inputs)
            {
                if (input.Start == first || input.Start == second)
                {
                    inLinks.Add((merged, input.Subj));
                }
                else
                {
                    inLinks.Add((input.Start, input.Subj));
                }
                GraphAutomat.UnBind(input);
            }

            foreach (var link in outLinks)
            {
                GraphAutomat.Bind(merged, link.Item1, link.Item2);
            }
            foreach (var link in inLinks)
            {
                GraphAutomat.Bind(link.Item1, merged, link.Item2);
            }
            return(merged);
        }
示例#2
0
        public static Node GetNKA(List <char> polska)
        {
            Stack <Node> nodes = new Stack <Node>();
            Node         init  = new Node();

            //Node first = null;
            nodes.Push(init);
            bool isFirst = true;

            foreach (var ch in polska)
            {
                if (Utils.alphabet.Contains(ch))
                {
                    Node charN = new GraphAutomat(ch);
                    if (isFirst)
                    {
                        isFirst = false;
                        GraphAutomat.Bind(init, charN);
                        //first = charN;
                    }
                    nodes.Push(charN);
                }
                else if (ch == '&')
                {
                    Node a   = nodes.Pop();
                    Node b   = nodes.Pop();
                    Node res = GraphAutomat.GetConcatAuto(b, a);
                    nodes.Push(res);
                }
                else if (ch == '|')
                {
                    Node a   = nodes.Pop();
                    Node b   = nodes.Pop();
                    Node res = GraphAutomat.GetChooseAuto(b, a);
                    nodes.Push(res);
                }
                else if (ch == '*')
                {
                    Node a   = nodes.Pop();
                    Node res = GraphAutomat.GetStar(a);
                    nodes.Push(res);
                }
                else if (ch == '+')
                {
                    Node a   = nodes.Pop();
                    Node res = GraphAutomat.GetPlus(a);
                    nodes.Push(res);
                }
            }
            //return first;
            return(init);
        }
示例#3
0
 public void Bind(DState next, char c)
 {
     GraphAutomat.Bind(this, next, c);
 }