Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public static Node GetPlus(Node a)
        {
            GraphAutomat res = new GraphAutomat();

            ReBindStart(res, a);
            ReBindFinish(res, a);
            Bind(res.Start, a);
            Bind(a, res.Finish);
            Bind(a, a);
            return(res);
        }
Ejemplo n.º 3
0
        public static Node GetConcatAuto(Node a, Node b)
        {
            GraphAutomat res = new GraphAutomat();

            ReBindStart(res, a);
            ReBindFinish(res, b);
            Bind(res.Start, a);
            Bind(a, b);
            Bind(b, res.Finish);
            return(res);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public void Bind(DState next, char c)
 {
     GraphAutomat.Bind(this, next, c);
 }