示例#1
0
        public IEnumerable <Pattern> DescendantNodesAndSelf()
        {
            if (p1 != null)
            {
                foreach (var p in p1.DescendantNodesAndSelf())
                {
                    yield return(p);
                }
            }

            if (p2 != null)
            {
                foreach (var p in p2.DescendantNodesAndSelf())
                {
                    yield return(p);
                }
            }

            yield return(this);
        }
示例#2
0
        public State Simulate(Pattern elem)
        {
            if (Seen.Contains (elem))
                return Pattern2State [elem];

            var ElemState = new State () { IsNullable = elem.Nullable };
            var root = elem;

            if (!Pattern2State.ContainsKey (root)) {
                Pattern2State.Add (root, ElemState);
            }

            Queue<Pattern> queue = new Queue<Pattern> ();
            queue.Enqueue (root);

            var leaves = elem.DescendantNodesAndSelf ().Where (x =>
                x.Type == PatternType.Attribute ||
                         x.Type == PatternType.Ref).Distinct ().ToList ();

            while (queue.Count > 0) {
                var current = queue.Dequeue ();
                var cstate = Pattern2State [current];

                if (Seen.Contains (current))
                    continue;

                current.DescendantNodesAndSelf ()
                    .Where (x => x.Type == PatternType.Define)
                    .Where (x => x.Nullable)
                    .ToList ()
                    .ForEach (x => cstate.NullableDefines.Add (x.Name));

                Seen.Add (current);

                foreach (var leaf in leaves) {

                    var derived = Deriv (current, leaf);

                    if (derived.Type == PatternType.NotAllowed)
                        continue;

                    if (!Pattern2State.ContainsKey (derived)) {
                        Pattern2State.Add (derived, new State () { IsNullable = derived.Nullable });
                        queue.Enqueue (derived);
                    }

                    var dstate = Pattern2State [derived];

                    if (leaf.Type == PatternType.Attribute) {
                        if (leaf.Namespace.Length > 0)
                            cstate.AttrStates.Add ("{" + leaf.Namespace + "}" + leaf.Name, dstate);
                        else
                            cstate.AttrStates.Add (leaf.Name, dstate);
                    } else if (leaf.Type == PatternType.Ref) {
                        cstate.ChildStates.Add (leaf.Name, dstate);
                    }
                }
            }
            if (!Pattern2State.ContainsValue (ElemState))
                throw new Exception ();
            return ElemState;
        }
示例#3
0
        public State Simulate(Pattern elem)
        {
            if (Seen.Contains(elem))
            {
                return(Pattern2State [elem]);
            }

            var ElemState = new State()
            {
                IsNullable = elem.Nullable
            };
            var root = elem;

            if (!Pattern2State.ContainsKey(root))
            {
                Pattern2State.Add(root, ElemState);
            }

            Queue <Pattern> queue = new Queue <Pattern> ();

            queue.Enqueue(root);

            var leaves = elem.DescendantNodesAndSelf().Where(x =>
                                                             x.Type == PatternType.Attribute ||
                                                             x.Type == PatternType.Ref).Distinct().ToList();

            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                var cstate  = Pattern2State [current];

                if (Seen.Contains(current))
                {
                    continue;
                }

                current.DescendantNodesAndSelf()
                .Where(x => x.Type == PatternType.Define)
                .Where(x => x.Nullable)
                .ToList()
                .ForEach(x => cstate.NullableDefines.Add(x.Name));

                Seen.Add(current);

                foreach (var leaf in leaves)
                {
                    var derived = Deriv(current, leaf);

                    if (derived.Type == PatternType.NotAllowed)
                    {
                        continue;
                    }

                    if (!Pattern2State.ContainsKey(derived))
                    {
                        Pattern2State.Add(derived, new State()
                        {
                            IsNullable = derived.Nullable
                        });
                        queue.Enqueue(derived);
                    }

                    var dstate = Pattern2State [derived];

                    if (leaf.Type == PatternType.Attribute)
                    {
                        if (leaf.Namespace.Length > 0)
                        {
                            cstate.AttrStates.Add("{" + leaf.Namespace + "}" + leaf.Name, dstate);
                        }
                        else
                        {
                            cstate.AttrStates.Add(leaf.Name, dstate);
                        }
                    }
                    else if (leaf.Type == PatternType.Ref)
                    {
                        cstate.ChildStates.Add(leaf.Name, dstate);
                    }
                }
            }
            if (!Pattern2State.ContainsValue(ElemState))
            {
                throw new Exception();
            }
            return(ElemState);
        }