Пример #1
0
        public State OnMarkRule(State start, RulePath path)
        {
            if (markAction != null)
                return markAction(start, path);

            return start;
        }
Пример #2
0
		//public static State ToNfa(this DfaState dfaStart)
		//{
		//    int total = 0;
		//    dfaStart.ForEach((state) => { state.Index = total++; });

		//    var states = new State[total];
		//    for (int i = 0; i < states.Length; i++)
		//        states[i] = new State();

		//    dfaStart.ForEach((dfaState) =>
		//        {
		//            var start = states[dfaState.Index];
		//            var end = start;

		//            if (dfaState.AllMarks.Count > 0)
		//            {
		//                end = new State();
		//                foreach (var mark in dfaState.AllMarks)
		//                {
		//                    var state = new State();
		//                    state.CopyIMarkFrom(mark);
		//                    state.Transition.Add(State.Epsilon, end);
		//                    start.Transition.Add(State.Epsilon, state);
		//                }
		//            }

		//            for (int i = 0; i < dfaState.Transition.Length; i++)
		//                if (dfaState.Transition[i] != null)
		//                    end.Transition.Add((byte)i, states[dfaState.Transition[i].Index]);
		//        });

		//    return states[dfaStart.Index];
		//}

		public static State ToNfa2(this DfaState dfaStart)
		{
			int total = 0;
			dfaStart.ForEach((state) => { state.Index = total++; });

			var states = new State[total];
			for (int i = 0; i < states.Length; i++)
				states[i] = new State();

			dfaStart.ForEach((dfaState) =>
			{
				var nfaState = states[dfaState.Index];

				nfaState.AddRangeMarks(dfaState.AllMarks);

				//foreach (var mark in dfaState.AllMarks)
				//{
				//    var copyMark = new State();
				//    copyMark.CopyIMarkFrom(mark);

				//    nfaState.PackedStates.Add(copyMark.Id);
				//}

				for (int i = 0; i < dfaState.Transition.Length; i++)
					if (dfaState.Transition[i] != null)
						nfaState.Transition.Add((byte)i, states[dfaState.Transition[i].Index]);
			});

			return states[dfaStart.Index];
		}
Пример #3
0
    void Start()
    {
        _idleState  = Fsm_IdleState;
        _otherState = Fsm_OtherState;

        _fsm = new Fsm();
        _fsm.Start(_idleState);
    }
Пример #4
0
		private State OnMarkRule(State start, List<string> rulenames)
		{
			if (MarkRule != null)
			{
				var args = new MarkRuleEventArgs(start, rulenames);
				MarkRule(this, args);
				return args.Start;
			}
			return start;
		}
Пример #5
0
		public void Initialize()
		{
			_state1 = new State(1, new State(2, new State()));

			_state2 = new State();
			_state2.Transition.Add(1, _state2);
			_state2.Transition.Add(2, new State(3, new State(4, new State())));

			_state3 = new State(1, new State(2, new State() { Tag = "end", }));
			_state3.Transition.Add(12, new State() { Tag = "end", });

			_state4 = new State(null, new State(null, new State(null, new State())));
			_state4.Transition.Add(null, new State(null, new State()));
		}
Пример #6
0
        public static State Create(byte from, byte to)
        {
            State start = new State();
            State end = new State();

            //for (byte i = from; i <= to; i++)
            //    start.Transition.Add(i,
            //        new State(Epsilon, end));

            for (int i = from; i <= to; i++)
                start.Transition.Add((byte)i, end);

            return start;
        }
Пример #7
0
        public static State Create(byte[] sequence1, byte[] sequence2)
        {
            var start = new State();

            var state1 = start;
            for (int i = 0; i < sequence1.Length; i++)
            {
                var state2 = new State();

                state1.Transition.Add(sequence1[i], state2);
                if (sequence2 != null && sequence1[i] != sequence2[i])
                    state1.Transition.Add(sequence2[i], state2);

                state1 = state2;
            }

            return start;
        }
Пример #8
0
        public void NfaToDfaTest1()
        {
            var state9 = new State() { Tag = "9", };
            var state8 = new State(1, state9) { Tag = "8", };
            var state7 = new State(1, state8) { Tag = "7", };
            var state6 = new State(State.Epsilon, state7) { Tag = "6", };
            var state5 = new State(State.Epsilon, state6) { Tag = "5", };
            var state4 = new State(2, state5) { Tag = "4", };
            var state3 = new State(State.Epsilon, state6) { Tag = "3", };
            var state2 = new State(1, state3) { Tag = "2", };
            var state1 = new State(State.Epsilon, state2, state4) { Tag = "1", };
            state6.Transition.Add(State.Epsilon, state1);
            var state0 = new State(State.Epsilon, state1, state7) { Tag = "0", };

            int count;
            var a = state0.ToDfa3(out count, false);
            var b = a.Transition[1];
            var c = a.Transition[2];
            var d = b.Transition[1];

            Assert.AreEqual("{0,1,2,4,7}", a.ToString());
            Assert.AreEqual("{1,2,3,4,6,7,8}", b.ToString());
            Assert.AreEqual("{1,2,4,5,6,7}", c.ToString());
            Assert.AreEqual("{1,2,3,4,6,7,8,9}", d.ToString());

            //Assert.AreEqual(2, a.Transition.Count);
            Assert.AreEqual(b, a.Transition[1]);
            Assert.AreEqual(c, a.Transition[2]);

            //Assert.AreEqual(2, b.Transition.Count);
            Assert.AreEqual(d, b.Transition[1]);
            Assert.AreEqual(c, b.Transition[2]);

            //Assert.AreEqual(2, c.Transition.Count);
            Assert.AreEqual(b, c.Transition[1]);
            Assert.AreEqual(c, c.Transition[2]);

            //Assert.AreEqual(2, d.Transition.Count);
            Assert.AreEqual(d, d.Transition[1]);
            Assert.AreEqual(c, d.Transition[2]);
        }
Пример #9
0
		private static int PackInternal(State start)
		{
			var optimizes = new List<State>();

			start.ForEach(
				new HashSet<State>(),
				(curent, key, next) =>
				{
					if (curent.Transition.GetOne(State.Epsilon) != null)
						optimizes.Add(curent);
				});

			foreach (var state in optimizes)
			{
				var nexts = new List<State>();
				nexts.AddRange(state.Transition.Get(State.Epsilon));

				foreach (var next in nexts)
				{
					state.Transition.Remove(State.Epsilon, next);

					if (state != next)
					{
						state.Transition.AddAll(State.Epsilon, next.Transition.Get(State.Epsilon));
						for (int i = 0; i <= 255; i++)
						{
							byte key = (byte)i;
							state.Transition.AddAll(key, next.Transition.Get(key));
						}

						state.AddRangeMarks(next.AllMarks);
					}

					//state.PackedStates.Add(next.Id);
					//state.PackedStates.AddRange(next.PackedStates);
				}
			}

			return optimizes.Count;
		}
Пример #10
0
    void Fsm_OtherState(Fsm fsm, Fsm.Step step, Fsm.State state)
    {
        if (step == Fsm.Step.Enter)
        {
            //On Enter
            Debug.Log($"On ${nameof(Fsm_OtherState)} Enter");
        }
        else if (step == Fsm.Step.Update)
        {
            //On Update
            Debug.Log($"On ${nameof(Fsm_OtherState)} Update");

            //On A press transition to IdleState
            if (Input.GetKeyDown(KeyCode.A))
            {
                fsm.TransitionTo(_idleState);
            }
        }
        else if (step == Fsm.Step.Exit)
        {
            //On Exit
            Debug.Log($"On ${nameof(Fsm_OtherState)} Exit");
        }
    }
Пример #11
0
		public static State MarkCustom(State begin1, string name, string select, string custom, string type)
		{
			switch (select)
			{
				case "Begin":

					begin1.AddMark(new MarkImpl(Marks.Custom, name) { Value = custom, Type = type, });

					return begin1;

				//return new State(Epsilon, begin1)
				//{
				//    Mark = Marks.Custom,
				//    Name = name,
				//    Value = custom,
				//    Type = type,
				//};

				case "End":

					begin1.FindEnd().AddMark(new MarkImpl(Marks.Custom, name) { Value = custom, Type = type, });

					//var end2 = new State()
					//{
					//    Mark = Marks.Custom,
					//    Name = name,
					//    Value = custom,
					//    Type = type,
					//};

					//begin1.FindEnd().Transition.Add(Epsilon, end2);

					return begin1;
			}

			throw new Exception("Not implemented");
		}
Пример #12
0
		private State Clone(Dictionary<int, State> proccessed)
		{
			var copy = new State();
			//copy.CopyIMarkFrom(this);
			copy.Tag = Tag;

			proccessed.Add(Id, copy);

			foreach (var pair in Transition)
			{
				State nextState;
				if (proccessed.TryGetValue(pair.Value.Id, out nextState) == false)
					nextState = pair.Value.Clone(proccessed);
				copy.Transition.Add(pair.Key, nextState);
			}

			//if (packedStates != null)
			//    copy.packedStates = new List<int>(packedStates);

			if (_allMarks != null)
				copy._allMarks = new List<IMark>(_allMarks);

			return copy;
		}
Пример #13
0
		public static State NoCloneAlternation(params State[] alternations)
		{
			if (alternations.Length > 1)
			{
				var start = new State();
				var end = new State();

				foreach (var item in alternations)
				{
					start.Transition.Add(Epsilon, item);
					foreach (var oldend in item.FindEnds())
						oldend.Transition.Add(Epsilon, end);
				}

				return start;
			}

			return alternations[0];
		}
Пример #14
0
		/// <summary>
		/// Alternation
		/// </summary>
		public static State operator |(State state1, State state2)
		{
			var start = new State();
			start.Transition.Add(Epsilon, state1.Clone());
			start.Transition.Add(Epsilon, state2.Clone());

			var end = new State();
			foreach (var oldend in start.FindEnds())
				oldend.Transition.Add(Epsilon, end);

			return start;
		}
Пример #15
0
		public static void MarkRange(ref State begin1, string name)
		{
			begin1 = MarkRange(begin1, name, 0, 0);
		}
Пример #16
0
		public ChangeRuleEventArgs(State[] states, List<string> rulenames)
		{
			States = states;
			Rulenames = rulenames;
		}
Пример #17
0
        public State MarkRuleHandler(State start, RulePath path)
        {
            ActionsDescription mark;
            if (marks.TryGetValue(path.Value, out mark) && mark.IsEmpty == false)
            {
                mark.UseCount++;

                foreach (var action in mark.Actions)
                {
                    switch (action.Mark)
                    {
                        case Marks.Custom:
                            start = State.MarkCustom(start, action.Args[0], action.Args[1], action.Args[2], action.Args[3]);
                            break;
                        case Marks.Const:
                            start.MarkConst(action.Args[0], action.Args[1], int.Parse(action.Args[2]));
                            break;
                        case Marks.Range:
                            start = State.MarkRange(start, action.Args[0], int.Parse(action.Args[1]), int.Parse(action.Args[2]));
                            break;
                        case Marks.BeginRange:
                            start = State.MarkBeginRange(start, action.Args[0], action.Args[1] == "AtBegin", int.Parse(action.Args[2]));
                            break;
                        case Marks.EndRange:
                        case Marks.EndRangeIfInvalid:
                            start = State.MarkEndRange(start, action.Mark, action.Args[0], action.Args[1] == "AtBegin", int.Parse(action.Args[2]));
                            break;
                        case Marks.ContinueRange:
                            start.MarkContinueRange(action.Args[0]);
                            break;
                        case Marks.Decimal:
                            start.MarkDecimal(action.Args[0], action.Args[1], action.Args[2]);
                            break;
                        case Marks.Hex:
                            start.MarkHex(action.Args[0], action.Args[1], action.Args[2]);
                            break;
                        case Marks.Count:
                            start.MarkCount(action.Args[0], int.Parse(action.Args[1]), int.Parse(action.Args[2]));
                            break;
                        case Marks.Bool:
                        case Marks.BoolEx:
                        case Marks.BoolExNot:
                            start.MarkBool(action.Mark, action.Args[0]);
                            break;
                        case Marks.ResetRange:
                            start.MarkReset(action.Args[0]);
                            break;
                        case Marks.ResetRangeIfInvalid:
                            start.MarkResetIfInvalid(action.Args[0]);
                            break;
                        default:
                            throw new Exception();
                    }
                }
            }

            return start;
        }
Пример #18
0
		////////////////////////////////////////////////////////////////////

		//public void AddPacked(State next)
		//{
		//    int oldsize = packedStates.Length;
		//    int length = packedStates.Length + 1 + next.PackedStates.Length;
		//
		//    Array.Resize<int>(ref packedStates, length);
		//    packedStates[oldsize] = next.Id;
		//    Array.Copy(next.PackedStates, 0, packedStates, oldsize + 1, next.PackedStates.Length);
		//}

		////////////////////////////////////////////////////////////////////

		/// <summary>
		/// 
		/// </summary>
		public static State Repeat(int repeat1, int repeat2, State state1)
		{
			if (repeat1 == -1 && repeat2 == -1)
				return -state1;

			if (repeat1 == -1)
				repeat1 = 0;

			State result = new State();
			for (int i = 0; i < repeat1; i++)
				result = result + state1;

			if (repeat2 != -1)
			{
				for (int i = repeat1; i < repeat2; i++)
					result = result + !state1;
			}
			else
			{
				result = result + -state1;
			}

			return result;
		}
Пример #19
0
		public static State Substract(State nfa1, State nfa2)
		{
			nfa1.FindEnd().AddMark(Marks.Service1);
			nfa2.FindEnd().AddMark(Marks.Service2);

			int count;
			var dfa1 = nfa1.ToDfa3(out count, false);
			var dfa2 = nfa2.ToDfa3(out count, false);

			dfa1.Minimize(false);
			dfa2.Minimize(false);

			var error = new DfaState(new[] { new State().Id, });
			for (int i = 0; i <= 255; i++)
				error.AddTransition((byte)i, error);
			dfa2.ForEachNR((state) =>
			{
				if (state != error)
				{
					for (int i = 0; i <= 255; i++)
					{
						byte key = (byte)i;
						if (state.Transition[i] == null)
							state.AddTransition(key, error);
					}
				}
			});



			var nfa3 = DfaIntersect.Intersect(dfa1, dfa2).ToNfa2();


			var ends = new List<State>();
			nfa3.ForEach((state) =>
				{
					bool s1 = false, s2 = false;

					state.RemoveAllMarks((mark) =>
					{
						if (mark.Mark == Marks.Service1)
							s1 = true;

						if (mark.Mark == Marks.Service2)
							s2 = true;

						return mark.Mark == Marks.Service1 || mark.Mark == Marks.Service2;
					});

					if (s1 == true && s2 == false)
						ends.Add(state);
				});


			var end = new State();
			foreach (var item in ends)
				item.Transition.Add(Epsilon, end);


			return nfa3;
		}
Пример #20
0
		public static State MarkEndRange(State begin1, Marks mark, string name, bool atBegin, int offset)
		{
			if (atBegin)
			{
				begin1.AddMark(new MarkImpl()
					{
						Mark = mark,
						Name = name,
						Offset = offset,
					});

				return begin1;

				//return new State(Epsilon, begin1)
				//{
				//    Mark = mark,
				//    Name = name,
				//    Offset = offset,
				//};
			}
			else
			{
				begin1.FindEnd().AddMark(new MarkImpl(mark, name) { Offset = offset, });

				//var end2 = new State()
				//{
				//    Mark = mark,
				//    Name = name,
				//    Offset = offset,
				//};

				//begin1.FindEnd().Transition.Add(Epsilon, end2);

				return begin1;
			}
		}
Пример #21
0
		public State(byte? c, State s1, State s2)
			: this()
		{
			_transition.Add(c, s1);
			_transition.Add(c, s2);
		}
Пример #22
0
		public static State MarkRange(State begin1, string name, int lookup, int beginOffset)
		{
			//State begin2;

			if (lookup == 0)
			{
				//begin2 = new State(Epsilon, begin1);
				////begin2.IsBeginRange = true;
				////begin2.RangeName = name;
				//begin2.Mark = Marks.BeginRange;
				//begin2.Name = name;
				//begin2.Offset = beginOffset;

				begin1.AddMark(new MarkImpl(Marks.BeginRange, name) { Offset = beginOffset, });
			}
			else if (lookup == 1)
			{
				//begin2 = begin1;
				//begin2.MarkNext(name, Marks.BeginRange, beginOffset);
				begin1.MarkNext(name, Marks.BeginRange, beginOffset);
			}
			else
				throw new ArgumentOutOfRangeException("lookup", "Must be 0 or 1, other value is not supported.");



			begin1.FindEnd().AddMark(new MarkImpl(Marks.EndRange, name));

			return begin1;

			//var end2 = new State();
			////end2.IsEndRange = true;
			////end2.RangeName = name;
			//end2.Mark = Marks.EndRange;
			//end2.Name = name;

			//var end1 = begin1.FindEnd();
			//end1.Transition.Add(Epsilon, end2);

			//return begin2;
		}
Пример #23
0
			protected State OnMarkRule(State start, RulePath path)
			{
				return builder.OnMarkRule(start, path);
			}
Пример #24
0
		/// <summary>
		/// Example: arg, arg, arg
		/// </summary>
		public static State NoCloneRepeatBy(State item, State separator)
		{
			separator.FindEnd().Transition.Add(State.Epsilon, item);

			var itemEnd = item.FindEnd();
			itemEnd.Transition.Add(State.Epsilon, new State());
			itemEnd.Transition.Add(State.Epsilon, separator);

			return item;
		}
Пример #25
0
		public static State NoCloneOption(State start)
		{
			var end = new State();
			var oldEnd = start.FindEnd();

			oldEnd.Transition.Add(Epsilon, end);
			start.Transition.Add(Epsilon, end);

			return start;
		}
Пример #26
0
		public MarkRuleEventArgs(State start, List<string> rulenames)
		{
			Start = start;
			Rulenames = rulenames;
		}
Пример #27
0
		public static State NoCloneStar(State state)
		{
			var oldStart = state;

			var end = new State();
			var start = new State(Epsilon, oldStart, end);
			var oldEnd = start.FindEnd();

			oldEnd.Transition.Add(Epsilon, oldStart);
			oldEnd.Transition.Add(Epsilon, end);

			return start;
		}
Пример #28
0
		public static State MarkBeginCount(State begin1, string name, int max)
		{
			begin1.AddMark(new MarkImpl()
				{
					Mark = Marks.Count,
					Name = name,
					Max = max,
				});

			return begin1;

			//return new State(Epsilon, begin1)
			//{
			//    Mark = Marks.Count,
			//    Name = name,
			//    Max = max,
			//};
		}
Пример #29
0
		public State(byte? c, State s)
			: this()
		{
			_transition.Add(c, s);
		}