Пример #1
0
        public static JsonTmDefinition LoadJ(string index)
        {
            var path = FindLibraryDirectory() + "\\" + index + ".tmj";
            var s    = File.ReadAllText(path, UTF8Encoding.UTF8);
            var j    = JsonTmDefinition.Restore(s);

            return(j);
        }
Пример #2
0
        public JsonTmDefinition ToJson(string name)
        {
            var j = new JsonTmDefinition();

            j.OriginalDefinition = FullDefinitionString;

            j.NonfinalStates = Q
                               .Where(p => Qa == null || p.Key != Qa.Name)
                               .Where(p => Qr == null || p.Key != Qr.Name)
                               .Select(p => p.Key)
                               .ToArray();
            if (Qa != null)
            {
                j.AcceptingState = Qa.Name;
            }
            if (Qr != null)
            {
                j.RefusingState = Qr.Name;
            }
            j.InitialState = Q0.Name;

            j.Sigma = Sigma.OrderBy(p => p.Key).Select(p => p.Value).ToArray();
            j.Gamma = Gamma.OrderBy(p => p.Key).Select(p => p.Value).ToArray();

            var delta = new List <JsonTmDefinition.Transition> ();
            var qs    = Q.Values
                        .Where(q => q.Delta != null)
                        .SelectMany(q => q.Delta);

            foreach (var ts in qs)
            {
                if (ts == null)
                {
                    continue;
                }
                var t = new JsonTmDefinition.Transition();
                t.From  = ts.Source.Name;
                t.Read  = Gamma[ts.Read];
                t.To    = ts.Next.Name;
                t.Write = Gamma[ts.Write];
                t.Dir   = ts.Direction == 0 ? "S" : ts.Direction == 1 ? "R" : "L";
                delta.Add(t);
            }
            j.Delta = delta.ToArray();

            j.SuggestedMacroSize = SuggestedMacroSize;

            j.Info_Name           = name;
            j.Info_Comment        = GetComments(FullDefinitionString).ToArray();
            j.Info_Url            = null;
            j.Info_ExpectedResult = null;

            return(j);
        }
Пример #3
0
		public JsonTmDefinition ToJson (string name)
		{
			var j = new JsonTmDefinition ();

			j.OriginalDefinition = FullDefinitionString;

			j.NonfinalStates = Q
				.Where (p => Qa == null || p.Key != Qa.Name)
				.Where (p => Qr == null || p.Key != Qr.Name)
				.Select (p => p.Key)
				.ToArray ();
			if (Qa != null) {
				j.AcceptingState = Qa.Name;
			}
			if (Qr != null) {
				j.RefusingState = Qr.Name;
			}
			j.InitialState = Q0.Name;

			j.Sigma = Sigma.OrderBy (p => p.Key).Select (p => p.Value).ToArray ();
			j.Gamma = Gamma.OrderBy (p => p.Key).Select (p => p.Value).ToArray ();

			var delta = new List<JsonTmDefinition.Transition> ();
			var qs = Q.Values
				.Where (q => q.Delta != null)
				.SelectMany (q => q.Delta);
			foreach (var ts in qs) {
				if (ts == null) {
					continue;
				}
				var t = new JsonTmDefinition.Transition ();
				t.From = ts.Source.Name;
				t.Read = Gamma[ts.Read];
				t.To = ts.Next.Name;
				t.Write = Gamma[ts.Write];
				t.Dir = ts.Direction == 0 ? "S" : ts.Direction == 1 ? "R" : "L";
				delta.Add (t);
			}
			j.Delta = delta.ToArray ();

			j.SuggestedMacroSize = SuggestedMacroSize;

			j.Info_Name = name;
			j.Info_Comment = GetComments (FullDefinitionString).ToArray ();
			j.Info_Url = null;
			j.Info_ExpectedResult = null;

			return j;
		}
Пример #4
0
		public TmDefinition (JsonTmDefinition def)
		{
			if (def == null)
				throw new ArgumentNullException ();

			Gamma = MapSymbols (def.Gamma, 0);
			Sigma = new Dictionary<byte, string> ();
			foreach (var sig in def.Sigma) {
				Sigma.Add (Gamma.Where (p => p.Value == sig).First ().Key, sig);
			}

			Func<string, State> createState = name => {
				var halting = false
					|| def.AcceptingState == name
					|| def.RefusingState == name;
				var ts = halting ? null : new SimpleTransition[Gamma.Count];
				var q = new State (Q.Count, name, ts);
				Q.Add (name, q);
				return q;
			};

			Q0 = createState (def.InitialState);

			foreach (var qs in def.NonfinalStates) {
				if (qs == def.InitialState) {
					continue;
				}
				createState (qs);
			}

			if (def.AcceptingState != null) {
				createState (def.AcceptingState);
			}
			if (def.RefusingState != null) {
				createState (def.RefusingState);
			}

			foreach (var t in def.Delta) {
				var qin = Q[t.From];
				byte gin = Gamma.First (p => p.Value == t.Read).Key;
				var qout = Q[t.To];
				byte gout = Gamma.First (p => p.Value == t.Write).Key;
				short dir;
				switch (t.Dir) {
					case "R":
						dir = 1;
						break;

					case "L":
						dir = -1;
						break;

					case "S":
						dir = 0;
						break;

					default:
						throw new Exception ();
				}

				var st = new SimpleTransition (qin, gin, qout, gout, dir);
				qin.Delta[gin] = st;
				qout.Sources.Add (st);
			}

			SuggestedMacroSize = def.SuggestedMacroSize;

			UpdateShortDefinitionString ();
		}
Пример #5
0
        public TmDefinition(JsonTmDefinition def)
        {
            if (def == null)
            {
                throw new ArgumentNullException();
            }

            Gamma = MapSymbols(def.Gamma, 0);
            Sigma = new Dictionary <byte, string> ();
            foreach (var sig in def.Sigma)
            {
                Sigma.Add(Gamma.Where(p => p.Value == sig).First().Key, sig);
            }

            Func <string, State> createState = name => {
                var halting = false ||
                              def.AcceptingState == name ||
                              def.RefusingState == name;
                var ts = halting ? null : new SimpleTransition[Gamma.Count];
                var q  = new State(Q.Count, name, ts);
                Q.Add(name, q);
                return(q);
            };

            Q0 = createState(def.InitialState);

            foreach (var qs in def.NonfinalStates)
            {
                if (qs == def.InitialState)
                {
                    continue;
                }
                createState(qs);
            }

            if (def.AcceptingState != null)
            {
                createState(def.AcceptingState);
            }
            if (def.RefusingState != null)
            {
                createState(def.RefusingState);
            }

            foreach (var t in def.Delta)
            {
                var   qin  = Q[t.From];
                byte  gin  = Gamma.First(p => p.Value == t.Read).Key;
                var   qout = Q[t.To];
                byte  gout = Gamma.First(p => p.Value == t.Write).Key;
                short dir;
                switch (t.Dir)
                {
                case "R":
                    dir = 1;
                    break;

                case "L":
                    dir = -1;
                    break;

                case "S":
                    dir = 0;
                    break;

                default:
                    throw new Exception();
                }

                var st = new SimpleTransition(qin, gin, qout, gout, dir);
                qin.Delta[gin] = st;
                qout.Sources.Add(st);
            }

            SuggestedMacroSize = def.SuggestedMacroSize;

            UpdateShortDefinitionString();
        }