public void TestRegularLookaheadComposition3() { Z3Provider Z = new Z3Provider(); var A = (Z.TT.MkRankedAlphabet("A", Z.IntSort, new string[] { "z", "u", "b" }, new int[] { 0, 1, 2 })); var B = (Z.TT.MkRankedAlphabet("B", Z.IntSort, new string[] { "z", "u", "b" }, new int[] { 0, 1, 2 })); var C = (Z.TT.MkRankedAlphabet("C", Z.IntSort, new string[] { "z", "u", "b" }, new int[] { 0, 1, 2 })); var _0 = Z.MkInt(0); var _1 = Z.MkInt(1); var _2 = Z.MkInt(2); var _7 = Z.MkInt(7); var AB_r0 = Z.TT.MkTreeRule(A, B, 0, "u", Z.MkLe(_0, A.AttrVar), B.MkTree("u", Z.MkAdd(_1, A.AttrVar), A.MkTrans(B, 1, 1)), new int[][] { new int[] { 3, 1 } }); var AB_r1 = Z.TT.MkTreeRule(A, B, 1, "u", Z.MkLe(_1, A.AttrVar), B.MkTree("u", Z.MkAdd(_1, A.AttrVar), A.MkTrans(B, 0, 1)), new int[][] { new int[] { 2, 0 } }); var AB_r2 = Z.TT.MkTreeRule(A, B, 1, "u", Z.MkEq(_2, A.AttrVar), B.MkTree("z", Z.MkAdd(_1, A.AttrVar))); var AB_q2 = Z.TT.MkTreeRule(A, B, 2, "u", Z.MkGe(_0, A.AttrVar), null, new int[][] { new int[] { 3 } }); var AB_q3a = Z.TT.MkTreeRule(A, B, 3, "u", Z.MkGe(_1, A.AttrVar), null, new int[][] { new int[] { 2 } }); var AB_q3b = Z.TT.MkTreeRule(A, B, 3, "u", Z.MkEq(_2, A.AttrVar), null); //just accept the input if the attribute is 1, delete the child subtree and return zeroC(1) var BC_r0 = Z.TT.MkTreeRule(B, C, 0, "u", Z.MkEq(_1, B.AttrVar), C.MkTree("z", Z.MkAdd(_7, B.AttrVar))); var AB = Z.TT.MkTreeAutomaton(0, A, B, new TreeRule[] { AB_r0, AB_r1, AB_r2, AB_q2, AB_q3a, AB_q3b }); var BC = Z.TT.MkTreeAutomaton(0, B, C, new TreeRule[] { BC_r0 }); var AC = TreeTransducer.ComposeR(AB, BC); Assert.AreEqual <int>(5, AC.RuleCount); }
/// <summary> /// Write the tree automaton definition to sb. /// If isAcceptor is true, the output will be a language acceptor definition. /// </summary> public void ToFast(String name, TreeTransducer ta, StringBuilder sb, bool isAcc) { this.name = name; this.initialState = int.Parse(ta.Root.ToString()); //ToFastTrans(ta, sb); if (!ta.IsEmpty) { //if (ta.IsPureAcceptorState(ta.InitialState)) // ToFastLang(ta, sb); //else ToFastTrans(ta, sb); } else { #region Empty transducers or languages if (!isAcc) { sb.Append(string.Format("Public Trans {0} : {1} -> {2}", GetStateName(int.Parse(ta.Root.ToString())), ta.InputAlphabet.AlphabetSort.Name.ToString(), ta.OutputAlphabet.AlphabetSort.Name.ToString())); sb.AppendLine("{ }"); } else { sb.Append(string.Format("Public Lang {0} : {1}", GetStateName(int.Parse(ta.Root.ToString())), ta.InputAlphabet.AlphabetSort.Name.ToString())); sb.AppendLine("{ }"); } sb.AppendLine(string.Format("// WARNING: The transducer {0} is empty", name)); Console.WriteLine("The transducer {0} is empty", name); #endregion } }
public void TestRegularLookaheadComposition2() { Z3Provider Z = new Z3Provider(); var A = (Z.TT.MkRankedAlphabet("A", Z.IntSort, new string[] { "zeroA", "oneA", "twoA" }, new int[] { 0, 1, 2 })); var B = (Z.TT.MkRankedAlphabet("B", Z.IntSort, new string[] { "zeroB", "oneB", "twoB" }, new int[] { 0, 1, 2 })); var C = (Z.TT.MkRankedAlphabet("C", Z.IntSort, new string[] { "zeroC", "oneC", "twoC" }, new int[] { 0, 1, 2 })); var _0 = Z.MkInt(0); var _1 = Z.MkInt(1); var _2 = Z.MkInt(2); var AB_r0 = Z.TT.MkTreeRule(A, B, 0, "oneA", Z.MkEq(_0, A.AttrVar), B.MkTree("oneB", Z.MkAdd(_1, A.AttrVar), A.MkTrans(B, 1, 1)), new int[][] { new int[] { 1 } }); var AB_r1 = Z.TT.MkTreeRule(A, B, 1, "oneA", Z.MkEq(_1, A.AttrVar), B.MkTree("oneB", Z.MkAdd(_1, A.AttrVar), A.MkTrans(B, 0, 1)), new int[][] { new int[] { 0 } }); var AB_r2 = Z.TT.MkTreeRule(A, B, 1, "oneA", Z.MkEq(_2, A.AttrVar), B.MkTree("zeroB", Z.MkAdd(_1, A.AttrVar))); //just accept the input if the attribute is 1 and delete the child subtree var BC_r0 = Z.TT.MkTreeRule(B, C, 0, "oneB", Z.MkEq(_1, B.AttrVar), C.MkTree("zeroC", B.AttrVar)); var AB = Z.TT.MkTreeAutomaton(0, A, B, new TreeRule[] { AB_r0, AB_r1, AB_r2 }); var BC = Z.TT.MkTreeAutomaton(0, B, C, new TreeRule[] { BC_r0 }); var AC = TreeTransducer.ComposeR(AB, BC); Assert.AreEqual <int>(4, AC.RuleCount); }
public static void RunTest() { DirectoryInfo detDir = new DirectoryInfo(pathDet); FileInfo[] detFiles = detDir.GetFiles("*"); numFiles = detFiles.Length; HashSet <int> detFileNames = new HashSet <int>(); foreach (var f in detFiles) { detFileNames.Add(int.Parse(f.Name)); } DirectoryInfo compDir = new DirectoryInfo(pathComp); FileInfo[] compFiles = compDir.GetFiles("*"); HashSet <int> compFileNames = new HashSet <int>(); foreach (var f in compFiles) { compFileNames.Add(int.Parse(f.Name)); } if (startFile == 1) { using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(Program.path + Program.fastFile)) { outfile.WriteLine("ID, StateCount, RuleCount, CompleteRuleCount, MinStateCount, MinRuleCount, Algo1, Algo2, SFABased"); } } for (int fileNum = startFile; fileNum <= numFiles; fileNum++) { TreeTransducer detAut = null; if (detFileNames.Contains(fileNum)) { Console.WriteLine(fileNum); detAut = GetFastProg(pathDet + fileNum); detAut = detAut.Clean(); TreeTransducer compAut = null; if (compFileNames.Contains(fileNum)) { compAut = detAut.Complete(); } Util.RunAllAlgorithms(detAut, compAut, fileNum.ToString(), Program.fastFile); } } }
public void TestRegularLookaheadComposition1() { Z3Provider Z = new Z3Provider(); var A = (Z.TT.MkRankedAlphabet("A", Z.IntSort, new string[] { "zeroA", "oneA", "twoA" }, new int[] { 0, 1, 2 })); var B = (Z.TT.MkRankedAlphabet("B", Z.IntSort, new string[] { "zeroB", "oneB", "twoB" }, new int[] { 0, 1, 2 })); var C = (Z.TT.MkRankedAlphabet("C", Z.IntSort, new string[] { "zeroC", "oneC", "twoC" }, new int[] { 0, 1, 2 })); //(two (plus 1 x0) (one (plus 1 x0) (q x1)) (one (plus 2 x0) (q x2))) var b = Z.MkApp(B["twoB"], Z.MkAdd(Z.MkInt(1), A.AttrVar), B.MkTree("oneB", Z.MkAdd(Z.MkInt(1), A.AttrVar), A.MkTrans(B, 0, 1)), B.MkTree("oneB", Z.MkAdd(Z.MkInt(2), A.AttrVar), A.MkTrans(B, 0, 2))); //(two (plus 1 x0) (zero x0) (one (plus 100 x0) (q x2))) var b2 = Z.MkApp(B["twoB"], Z.MkAdd(Z.MkInt(1), A.AttrVar), B.MkTree("zeroB", A.AttrVar), B.MkTree("oneB", Z.MkAdd(Z.MkInt(9), A.AttrVar), A.MkTrans(B, 0, 2))); var rule0 = Z.TT.MkTreeRule(A, B, 0, "zeroA", Z.True, B.MkTree("zeroB", A.AttrVar)); var rule1 = Z.TT.MkTreeRule(A, B, 0, "twoA", Z.MkGt(A.AttrVar, Z.MkInt(0)), b); var rule2 = Z.TT.MkTreeRule(A, B, 0, "twoA", Z.MkGt(A.AttrVar, Z.MkInt(0)), b2); var rule3 = Z.TT.MkTreeRule(A, B, 0, "oneA", Z.MkGt(A.AttrVar, Z.MkInt(0)), B.MkTree("oneB", A.AttrVar, A.MkTrans(B, 0, 1))); var trans1 = Z.TT.MkTreeAutomaton(0, A, B, new TreeRule[] { rule0, rule1, rule2, rule3 }); //(two x0 (one (plus 1 x0) (p x1)) (one (plus 2 x0) (p x2))) var a = A.MkTree("twoA", C.AttrVar, A.MkTree("oneA", Z.MkAdd(Z.MkInt(1), C.AttrVar), C.MkTrans(A, 1, 1)), A.MkTree("oneA", Z.MkAdd(Z.MkInt(2), C.AttrVar), C.MkTrans(A, 1, 2))); var a2 = A.MkTree("zeroA", C.AttrVar); var rule4 = Z.TT.MkTreeRule(C, A, 1, "twoC", Z.MkGt(C.AttrVar, Z.MkInt(-2)), a); var rule5 = Z.TT.MkTreeRule(C, A, 1, "zeroC", Z.MkGt(C.AttrVar, Z.MkInt(-3)), a2); var trans2 = Z.TT.MkTreeAutomaton(1, C, A, new TreeRule[] { rule4, rule5 }); var trans12 = TreeTransducer.ComposeR(trans2, trans1); var rulesOut = trans12.GetRules(trans12.Root, C["twoC"]); Assert.AreEqual <int>(2, rulesOut.Count); var rulesOut2 = trans12.GetRules(trans12.Root, C["zeroC"]); Assert.AreEqual <int>(1, rulesOut2.Count); var tin = C.MkTree("twoC", Z.MkInt(55), C.MkTree("zeroC", Z.MkInt(66)), C.MkTree("zeroC", Z.MkInt(77))); var res = trans12[tin]; Assert.AreEqual <int>(2, res.Length); Assert.AreEqual <int>(3, trans12.RuleCount); }
public static void SanityCheck() { DirectoryInfo detDir = new DirectoryInfo(pathDet); FileInfo[] detFiles = detDir.GetFiles("*"); int numFiles = detFiles.Length; HashSet <int> detFileNames = new HashSet <int>(); foreach (var f in detFiles) { detFileNames.Add(int.Parse(f.Name)); } DirectoryInfo compDir = new DirectoryInfo(pathComp); FileInfo[] compFiles = compDir.GetFiles("*"); HashSet <int> compFileNames = new HashSet <int>(); foreach (var f in compFiles) { compFileNames.Add(int.Parse(f.Name)); } for (int fileNum = startFile; fileNum <= numFiles; fileNum++) { Console.WriteLine(fileNum); TreeTransducer detAut = null; if (detFileNames.Contains(fileNum)) { detAut = VataParsing.ParseVataFile(pathDet + fileNum); } var min1 = detAut.Minimize(); var min2 = detAut.MinimizeViaSFA(); Console.WriteLine("StatesMin:"); Console.WriteLine(detAut.StateCount + " " + min1.StateCount + " " + min2.StateCount); Console.WriteLine("TransMin:"); Console.WriteLine(detAut.RuleCount + " " + min1.RuleCount + " " + min2.RuleCount); //if (!min1.Complement().Intersect(min2).IsEmpty) // Console.WriteLine("Should be empty!"); //if (!min2.Complement().Intersect(min1).IsEmpty) // Console.WriteLine("Should be empty!"); } }
internal RankedAlphabet( TreeTheory tt, string[] symbols, Dictionary <string, int> idMap, Sort alphabetSort, Sort nodeSort, int[] ranks, FuncDecl[] constructors, FuncDecl[][] accessors, FuncDecl[] testers, FuncDecl acceptor, Expr[] vars ) { this.tt = tt; this.symbols = new List <string>(symbols).AsReadOnly(); this.idMap = idMap; this.alphabetSort = alphabetSort; this.nodeSort = nodeSort; this.ranks = ranks; this.constructors = constructors; this.accessors = accessors; this.testers = testers; this.acceptor = acceptor; this.vars = vars; this.trans = tt.GetTrans(alphabetSort, alphabetSort); this.emptyAcceptor = TreeTransducer.MkEmpty(this); this.fullAcceptor = TreeTransducer.MkFull(this); this.idAut = TreeTransducer.MkId(this); this.symbolsOfRank = new Dictionary <int, List <FuncDecl> >(); for (int i = 0; i < ranks.Length; i++) { var r = ranks[i]; if (!symbolsOfRank.ContainsKey(r)) { symbolsOfRank[r] = new List <FuncDecl>(); } symbolsOfRank[r].Add(constructors[i]); } var attrDomain = tt.Z.MkFreshFuncDecl("_", new Sort[] { nodeSort }, tt.Z.BoolSort); this.attrExpr = tt.Z.MkApp(attrDomain, vars[0]); tt.Z.AssertAxiom(this.attrExpr, tt.Z.True, vars[0]); }
public static string GetDTAMINFormatString(TreeTransducer aut) { Z3Provider Z = new Z3Provider(); StringBuilder sb = new StringBuilder(); var transition = aut.GetRules(); foreach (var rule in transition) { sb.Append(Z.GetNumeralInt(rule.State) + 1); sb.Append(" "); sb.Append(rule.Symbol.Name.ToString()); for (int j = 1; j <= rule.Rank; j++) { var args_j = rule.Lookahead(j - 1); if (args_j.ToList().Count > 1) { throw new Exception("Some i has more than one state"); } foreach (var rlastate in args_j) { sb.Append(" "); sb.Append(Z.GetNumeralInt(rlastate) + 1); } } sb.AppendLine(); } int c = 0; foreach (var st in aut.roots) { if (c == aut.roots.Count - 1) { sb.Append("0 " + (Z.GetNumeralInt(st) + 1)); } else { sb.AppendLine("0 " + (Z.GetNumeralInt(st) + 1)); } c++; } return(sb.ToString()); }
/// <summary> /// Make a new tree automaton or tree transducer. /// </summary> /// <param name="initStates">initial states</param> /// <param name="inputAlphabet">input alphabet</param> /// <param name="outputAlphabet">output alphabet</param> /// <param name="rules">rules of the automaton or transducer</param> /// <returns></returns> public TreeTransducer MkTreeAutomaton(IEnumerable <int> initStates, RankedAlphabet inputAlphabet, RankedAlphabet outputAlphabet, IEnumerable <TreeRule> rules) { foreach (var st in initStates) { if (st < 0) { throw new AutomataException(AutomataExceptionKind.TreeTransducer_InvalidStateId); } } var stateList = new List <Expr>(); var stateSet = new HashSet <Expr>(); var rulesList = new List <TreeRule>(rules); var q0_list = new List <Expr>(); foreach (var st in initStates) { q0_list.Add(Z.MkInt(st)); } #region perform basic sanity checks foreach (var rule in rulesList) { if (rule.State.Equals(identityState)) { throw new AutomataException(AutomataExceptionKind.TreeTransducer_InvalidUseOfIdentityState); } if (stateSet.Add(rule.state)) { stateList.Add(rule.state); } } foreach (var rule in rulesList) { if (!rule.IsTrueForAllStates(st => (stateSet.Contains(st) || st.Equals(identityState)))) { throw new AutomataException(AutomataExceptionKind.TreeTransducer_InvalidStateId); } } #endregion var ta = new TreeTransducer(q0_list, inputAlphabet, outputAlphabet, stateList, rulesList); var ta1 = ta.Clean(); return(ta1); }
public static string GetFastProg(TreeTransducer aut, string accName) { StringBuilder sb = new StringBuilder(); FastGen fg = new FastGen(aut.TT.Z); fg.ToFast(aut.InputAlphabet, sb); fg.ToFast("fastacc", aut, sb, true); return(sb.ToString()); //foreach (var td in fti.treeDefinitions) //{ // var tcd = td.Value; // var aut = tcd.acceptors[accName]; // var tokenSource = new CancellationTokenSource(); // CancellationToken token = tokenSource.Token; // TreeTransducer detAut = null; // var task = Task.Factory.StartNew(() => // { // aut.IsDeterminstic(); // if(complete) // detAut = aut.Determinize(); // else // detAut = aut.DeterminizeWithoutCompletion().RemoveUselessStates(); // }, token); // if (!task.Wait(Program.timeOut, token)) // { // Console.WriteLine("The Task timed out!"); // return null; // } // else // { // StringBuilder sb = new StringBuilder(); // FastGen fg = new FastGen(detAut.TT.Z); // fg.ToFast(td.Value.alphabet.alph, sb); // fg.ToFast("fastacc", detAut, sb, true); // return sb.ToString(); // } //} //return null; }
internal RankedAlphabet( TreeTheory tt, string[] symbols, Dictionary<string, int> idMap, Sort alphabetSort, Sort nodeSort, int[] ranks, FuncDecl[] constructors, FuncDecl[][] accessors, FuncDecl[] testers, FuncDecl acceptor, Expr[] vars ) { this.tt = tt; this.symbols = new List<string>(symbols).AsReadOnly(); this.idMap = idMap; this.alphabetSort = alphabetSort; this.nodeSort = nodeSort; this.ranks = ranks; this.constructors = constructors; this.accessors = accessors; this.testers = testers; this.acceptor = acceptor; this.vars = vars; this.trans = tt.GetTrans(alphabetSort, alphabetSort); this.emptyAcceptor = TreeTransducer.MkEmpty(this); this.fullAcceptor = TreeTransducer.MkFull(this); this.idAut = TreeTransducer.MkId(this); this.symbolsOfRank = new Dictionary<int, List<FuncDecl>>(); for (int i = 0; i < ranks.Length; i++) { var r = ranks[i]; if (!symbolsOfRank.ContainsKey(r)) symbolsOfRank[r] = new List<FuncDecl>(); symbolsOfRank[r].Add(constructors[i]); } var attrDomain = tt.Z.MkFreshFuncDecl("_", new Sort[] { nodeSort }, tt.Z.BoolSort); this.attrExpr = tt.Z.MkApp(attrDomain, vars[0]); tt.Z.AssertAxiom(this.attrExpr, tt.Z.True, vars[0]); }
public static string GetDTAMINFormatString(TreeTransducer aut) { Z3Provider Z = new Z3Provider(); StringBuilder sb = new StringBuilder(); var transition = aut.GetRules(); foreach (var rule in transition) { sb.Append(Z.GetNumeralInt(rule.State) + 1); sb.Append(" "); sb.Append(rule.Symbol.Name.ToString()); for (int j = 1; j <= rule.Rank; j++) { var args_j = rule.Lookahead(j - 1); if (args_j.ToList().Count > 1) throw new Exception("Some i has more than one state"); foreach (var rlastate in args_j) { sb.Append(" "); sb.Append(Z.GetNumeralInt(rlastate)+1); } } sb.AppendLine(); } int c = 0; foreach (var st in aut.roots) { if(c==aut.roots.Count-1) sb.Append("0 " + (Z.GetNumeralInt(st) + 1)); else sb.AppendLine("0 " + (Z.GetNumeralInt(st) + 1)); c++; } return sb.ToString(); }
public static void GenerateTests() { HashSet <Tuple <int, int> > alreadyFound = new HashSet <Tuple <int, int> >(); DirectoryInfo detDir = new DirectoryInfo(pathDet); int fileName = detDir.GetFiles().Length + 1; foreach (var suff in suffs) { DirectoryInfo d = new DirectoryInfo(path + suff + @"\"); FileInfo[] Files = d.GetFiles("*"); foreach (FileInfo file in Files) { Console.WriteLine(suff + @"\" + file.Name); var aut = VataParsing.ParseVataFile(path + suff + @"\" + file.Name); if (aut != null) { Console.WriteLine(aut.StateCount + " " + aut.RuleCount); if (aut.StateCount < 301 || aut.RuleCount < 3407) { var stTr = new Tuple <int, int>(aut.StateCount, aut.RuleCount); if (!alreadyFound.Contains(stTr)) { alreadyFound.Add(stTr); var tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; TreeTransducer detAut = null; var task = Task.Factory.StartNew(() => { aut.IsDeterminstic(); detAut = aut.DeterminizeWithoutCompletion().RemoveUselessStates(); }, token); if (!task.Wait(Program.timeOut, token)) { Console.WriteLine("The determinization Task timed out!"); } else { var outstring = VataParsing.GetVataFormatString(detAut); VataParsing.ParseVataFormat(outstring); using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(pathDet + fileName)) { outfile.Write(outstring); fileName++; } if (detAut.InputAlphabet.MaxRank < 6 && detAut.RuleCount < 4000) { TreeTransducer completeAut = null; CancellationToken token2 = tokenSource.Token; var task2 = Task.Factory.StartNew(() => { completeAut = aut.Determinize(); }, token2); if (!task2.Wait(Program.timeOut, token2)) { Console.WriteLine("The completion Task timed out!"); } else { outstring = VataParsing.GetVataFormatString(completeAut); VataParsing.ParseVataFormat(outstring); using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(pathComp + (fileName - 1))) { outfile.Write(outstring); } } } else { Console.WriteLine(detAut.RuleCount + " rules, skip completion"); } } } } } } } }
static private void testTreeTransducer() { FunctionalProgramTransducerFacade transducerFacade = new FunctionalProgramTransducerFacade(); List <Rule <FunctionalProgramElement> > rules = new List <Rule <FunctionalProgramElement> >(); { FunctionalProgramElement matching = new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE)); matching.children = new List <FunctionalProgramElement>(); matching.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); matching.children[0].expression = "+"; matching.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.REWRITE_VARIABLE))); matching.children[1].expression = "a"; matching.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.REWRITE_VARIABLE))); matching.children[2].expression = "b"; FunctionalProgramElement target = new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE)); target.children = new List <FunctionalProgramElement>(); target.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[0].expression = "NODE"; target.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE))); target.children[1].children = new List <FunctionalProgramElement>(); target.children[1].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[1].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[1].children[0].expression = "emit"; target.children[1].children[1].expression = "newinstruction"; target.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE))); target.children[2].children = new List <FunctionalProgramElement>(); target.children[2].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[2].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.REWRITE_VARIABLE))); target.children[2].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[2].children[0].expression = "calc"; target.children[2].children[1].expression = "a"; target.children[2].children[2].expression = "aResult"; target.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE))); target.children[3].children = new List <FunctionalProgramElement>(); target.children[3].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[3].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.REWRITE_VARIABLE))); target.children[3].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[3].children[0].expression = "calc"; target.children[3].children[1].expression = "b"; target.children[3].children[2].expression = "bResult"; target.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE))); target.children[4].children = new List <FunctionalProgramElement>(); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); target.children[4].children[0].expression = "emit"; target.children[4].children[1].expression = "slot1"; target.children[4].children[2].expression = "+"; target.children[4].children[2].expression = "aResult"; target.children[4].children[2].expression = "bResult"; target.children[4].children[2].expression = "RESULT"; rules.Add(new Rule <FunctionalProgramElement>(matching, target)); // TODO } FunctionalProgramElement apply = new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.BRACE)); apply.children = new List <FunctionalProgramElement>(); apply.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); apply.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); apply.children.Add(new FunctionalProgramElement(new FunctionalProgramElementType(FunctionalProgramElementType.EnumType.VARIABLE))); apply.children[0].expression = "+"; apply.children[1].expression = "myA"; apply.children[2].expression = "myB"; FunctionalProgramElement translated = TreeTransducer <FunctionalProgramElement> .tryToApplyRulesRecursivly(rules, apply, transducerFacade); /* * rules.Add(new Rule<TreePayload>()); * rules[0].matching = new TreeElement<TreePayload>(); * rules[0].matching.type = TreeElement<TreePayload>.EnumType.VALUE; * rules[0].matching.value = n * * * rules[0].matching.type = TreeElement<TreePayload>.EnumType.VARIABLE; * rules[0].matching.variable = "a"; * * rules[0].rewriteTarget = new TreeElement<TreePayload>(); * rules[0].rewriteTarget.type = TreeElement<TreePayload>.EnumType.VALUE; * rules[0].rewriteTarget.value = new TreePayload(); * rules[0].rewriteTarget.value.value = 5; * * * TreeElement<TreePayload> toTranslate = new TreeElement<TreePayload>(); * toTranslate.value = new TreePayload(); * toTranslate.value.value = 42; * * * TreeElement<TreePayload> translated = TreeTransducer<TreePayload>.tryToApplyRulesRecursivly(rules, toTranslate); */ int debugHere = 1; }
public static string GetVataFormatString(TreeTransducer aut) { Z3Provider Z = new Z3Provider(); StringBuilder sb = new StringBuilder(); sb.Append("Ops"); var alph = aut.InputAlphabet; foreach (var constructor in alph.constructors) { sb.AppendFormat(" {0}:{1}", constructor.Name, constructor.Arity - 1); } sb.AppendLine(); sb.AppendLine(); sb.AppendLine("Automaton a"); sb.AppendLine(); sb.Append("States"); var states = aut.GetStates(); foreach (var state in states) { sb.AppendFormat(" q{0}", int.Parse(state.ToString())); } sb.AppendLine(); sb.Append("Final States"); var finalStates = aut.roots; foreach (var state in finalStates) { sb.AppendFormat(" q{0}", int.Parse(state.ToString())); } sb.AppendLine(); sb.AppendLine("Transitions"); var transition = aut.GetRules(); foreach (var rule in transition) { sb.Append(rule.Symbol.Name.ToString()); sb.Append("("); for (int j = 1; j <= rule.Rank; j++) { var args_j = rule.Lookahead(j - 1); if (args_j.ToList().Count > 1) throw new Exception("Some i has more than one state"); foreach (var rlastate in args_j) { if (j > 1) sb.Append(","); sb.Append("q" + Z.GetNumeralInt(rlastate)); } } sb.Append(") -> "); sb.AppendLine("q" + Z.GetNumeralInt(rule.State)); } sb.AppendLine(); return sb.ToString(); }
//Examples generated with params (6,15,1000), (10,12,1000), public static void GenerateTAUsingFast(int langRules, int maxSt, int totGen) { DirectoryInfo detDir = new DirectoryInfo(pathDet); int fileName = detDir.GetFiles().Length + 1; langRulesNumb = langRules; maxStateNum = maxSt; totgenerations = totGen; for (seed = 0; seed < totgenerations; seed++) { Console.WriteLine(seed); random = new Random(seed); stateNum = random.Next(4, maxStateNum); //Generate one lang sbForGen = new StringBuilder(); GenerateRecognizer(); var pgm = Microsoft.Fast.Parser.ParseFromString(sbForGen.ToString()); FastTransducerInstance fti = null; try { fti = FastTransducerInstance.MkFastTransducerInstance(pgm); } catch (AutomataException e) { fti = null; } if (fti != null) { foreach (var td in fti.treeDefinitions) { var tcd = td.Value; foreach (var acceptor in tcd.acceptors) { var aut = acceptor.Value.Clean(); if (aut.IsEmpty) { break; } var tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; TreeTransducer detAut = null; var task = Task.Factory.StartNew(() => { aut.IsDeterminstic(); detAut = aut.DeterminizeWithoutCompletion().RemoveUselessStates(); Console.WriteLine("done with det no comp"); }, token); if (!task.Wait(Program.timeOut, token)) { Console.WriteLine("The det Task timed out!"); } else { Console.WriteLine("DET:" + detAut.RuleCount); if (detAut.RuleCount < 100000 && detAut.RuleCount > 1) { var outstring = GetFastProg(detAut, acceptor.Key); Microsoft.Fast.Parser.ParseFromString(outstring); using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(pathDet + fileName)) { outfile.Write(outstring); } fileName++; if (detAut.RuleCount < 5000) { TreeTransducer completeAut = null; CancellationToken token2 = tokenSource.Token; var task2 = Task.Factory.StartNew(() => { completeAut = detAut.Complete(); Console.WriteLine("done with det with comp"); }, token2); if (!task2.Wait(Program.timeOut, token2)) { Console.WriteLine("The compl Task timed out!"); } else { Console.WriteLine("COM:" + completeAut.RuleCount); if (completeAut.RuleCount < 100000) { outstring = GetFastProg(completeAut, acceptor.Key); Microsoft.Fast.Parser.ParseFromString(outstring); using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(pathComp + (fileName - 1))) { outfile.Write(outstring); } } } } } } } } } } }
void ToFastLang(TreeTransducer ta, StringBuilder sb) { foreach (var state in ta.GetStates()) { sb.AppendLine(string.Format((state.Equals(ta.Root) ? "Public" : "") + " Lang {0} : {1}", GetStateName(int.Parse(state.ToString())), ta.InputAlphabet.AlphabetSort.Name.ToString())); sb.AppendLine("{"); bool needCaseSeparator = false; foreach (var func in ta.InputAlphabet.Symbols) { var rules = ta.GetRules(state, func); for (int i = 0; i < rules.Count; i++) { sb.Append("\t"); if (needCaseSeparator) { sb.Append("| "); //case separator } else { sb.Append(" "); } var rule = rules[i]; sb.Append(rule.Symbol.Name.ToString()); sb.Append("("); for (int j = 1; j <= rule.Rank; j++) { if (j > 1) { sb.Append(","); } sb.Append("x" + j); } sb.Append(") where "); ToFastExpr(rule.Guard, sb, true); bool output_given = true; for (int j = 1; j <= rule.Rank; j++) { var given_j = rule.Lookahead(j - 1); foreach (var given_c in given_j) { if (!given_c.Equals(tt.identityState)) { if (output_given) { sb.Append(" given "); output_given = false; } sb.Append("("); sb.Append(GetStateName(tt.Z.GetNumeralInt(given_c))); sb.Append(" x" + j); sb.Append(")"); } } } needCaseSeparator = true; sb.AppendLine(); } } sb.AppendLine("}"); } }
public static string GetFastProg(TreeTransducer aut, string accName) { StringBuilder sb = new StringBuilder(); FastGen fg = new FastGen(aut.TT.Z); fg.ToFast(aut.InputAlphabet, sb); fg.ToFast("fastacc", aut, sb, true); return sb.ToString(); //foreach (var td in fti.treeDefinitions) //{ // var tcd = td.Value; // var aut = tcd.acceptors[accName]; // var tokenSource = new CancellationTokenSource(); // CancellationToken token = tokenSource.Token; // TreeTransducer detAut = null; // var task = Task.Factory.StartNew(() => // { // aut.IsDeterminstic(); // if(complete) // detAut = aut.Determinize(); // else // detAut = aut.DeterminizeWithoutCompletion().RemoveUselessStates(); // }, token); // if (!task.Wait(Program.timeOut, token)) // { // Console.WriteLine("The Task timed out!"); // return null; // } // else // { // StringBuilder sb = new StringBuilder(); // FastGen fg = new FastGen(detAut.TT.Z); // fg.ToFast(td.Value.alphabet.alph, sb); // fg.ToFast("fastacc", detAut, sb, true); // return sb.ToString(); // } //} //return null; }
public static string GetVataFormatString(TreeTransducer aut) { Z3Provider Z = new Z3Provider(); StringBuilder sb = new StringBuilder(); sb.Append("Ops"); var alph = aut.InputAlphabet; foreach (var constructor in alph.constructors) { sb.AppendFormat(" {0}:{1}", constructor.Name, constructor.Arity - 1); } sb.AppendLine(); sb.AppendLine(); sb.AppendLine("Automaton a"); sb.AppendLine(); sb.Append("States"); var states = aut.GetStates(); foreach (var state in states) { sb.AppendFormat(" q{0}", int.Parse(state.ToString())); } sb.AppendLine(); sb.Append("Final States"); var finalStates = aut.roots; foreach (var state in finalStates) { sb.AppendFormat(" q{0}", int.Parse(state.ToString())); } sb.AppendLine(); sb.AppendLine("Transitions"); var transition = aut.GetRules(); foreach (var rule in transition) { sb.Append(rule.Symbol.Name.ToString()); sb.Append("("); for (int j = 1; j <= rule.Rank; j++) { var args_j = rule.Lookahead(j - 1); if (args_j.ToList().Count > 1) { throw new Exception("Some i has more than one state"); } foreach (var rlastate in args_j) { if (j > 1) { sb.Append(","); } sb.Append("q" + Z.GetNumeralInt(rlastate)); } } sb.Append(") -> "); sb.AppendLine("q" + Z.GetNumeralInt(rule.State)); } sb.AppendLine(); return(sb.ToString()); }
public static void SanityCheck() { DirectoryInfo detDir = new DirectoryInfo(pathDet); FileInfo[] detFiles = detDir.GetFiles("*"); numFiles = detFiles.Length; HashSet <int> detFileNames = new HashSet <int>(); foreach (var f in detFiles) { detFileNames.Add(int.Parse(f.Name)); } DirectoryInfo compDir = new DirectoryInfo(pathComp); FileInfo[] compFiles = compDir.GetFiles("*"); HashSet <int> compFileNames = new HashSet <int>(); foreach (var f in compFiles) { compFileNames.Add(int.Parse(f.Name)); } if (startFile == 1) { using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(Program.path + Program.fastFile)) { outfile.WriteLine("ID, StateCount, RuleCount, CompleteRuleCount, MinStateCount, MinRuleCount, Algo1, Algo2, SFABased"); } } for (int fileNum = startFile; fileNum <= numFiles; fileNum++) { Console.WriteLine(fileNum); TreeTransducer detAut = null; if (detFileNames.Contains(fileNum)) { detAut = GetFastProg(pathDet + fileNum); } else { break; } TreeTransducer compAut = null; if (compFileNames.Contains(fileNum)) { compAut = detAut.Complete(); } var min1 = detAut.Minimize(); var min2 = detAut.MinimizeViaSFA(); Console.WriteLine("StatesMin:"); Console.WriteLine(detAut.StateCount + " " + min1.StateCount + " " + min2.StateCount); Console.WriteLine("TransMin:"); Console.WriteLine(detAut.RuleCount + " " + min1.RuleCount + " " + min2.RuleCount); if (!min1.Complement().Intersect(min2).IsEmpty) { Console.WriteLine("Should be empty!"); } if (!min2.Complement().Intersect(min1).IsEmpty) { Console.WriteLine("Should be empty!"); } //Util.RunAllAlgorithms(detAut, compAut, fileNum.ToString(), Program.fastFile); } }
//Runs the three algorithms. It requires the deterministic automton, the total automaton, and the output file public static void RunAllAlgorithms(TreeTransducer detAut, TreeTransducer totAut, string exampleName, string outputFileName) { TreeTransducer algo1Min = null; TreeTransducer algo2Min = null; TreeTransducer algoSFAMin = null; var algo1TO = false; var algo2TO = false; var algoSFATO = false; if (detAut == null) { return; } // Completion time //int time = System.Environment.TickCount; //if (totAut != null) //{ // for (int i = 0; i < Program.numTests; i++) // if (ActionTimesOut(() => { algo1Min = detAut.Complete(); })) // { // compTO = true; break; // } //} //var timeComp = (System.Environment.TickCount - time) / Program.numTests; detAut.Clean(); // Algorithm 1 int time = System.Environment.TickCount; if (totAut != null) { //TODO Remove this if (totAut.RuleCount < complimit) { for (int i = 0; i < Program.numTests; i++) { if (ActionTimesOut(() => { algo1Min = totAut.Minimize(); })) { algo1TO = true; break; } } } else { algo1TO = true; } } else { algo1TO = true; } var timeAlgo1 = (System.Environment.TickCount - time) / Program.numTests; // Algorithm 2 time = System.Environment.TickCount; if (detAut != null) { //TODO Remove this for (int i = 0; i < Program.numTests; i++) { if (ActionTimesOut(() => { algo2Min = detAut.Minimize(); })) { algo2TO = true; break; } } } else { algo2TO = true; } var timeAlgo2 = (System.Environment.TickCount - time) / Program.numTests; //Console.WriteLine(algo2Min.StateCount); // Algorithm SFA time = System.Environment.TickCount; if (detAut != null) { if (detAut.RuleCount < detlimit) { for (int i = 0; i < Program.numTests; i++) { if (ActionTimesOut(() => { algoSFAMin = detAut.MinimizeViaSFA(); })) { algoSFATO = true; break; } } } } else { algoSFATO = true; } var timeAlgoSFA = (System.Environment.TickCount - time) / Program.numTests; //Check that results are correct if (algoSFAMin != null && algo2Min != null) { if (algo2Min.StateCount - algoSFAMin.StateCount > 1 || algo2Min.StateCount - algoSFAMin.StateCount < -1) { detAut.Clean(); var v = detAut.IsDeterminstic(); Console.WriteLine(v); Console.WriteLine("wrong mini"); } } // Append results on file // ID, StateCount, RuleCount, CompleteRuleCount, MinStateCount, MinRuleCount, PTime, CTime using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(Program.path + outputFileName, true)) { outfile.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}", exampleName, detAut.StateCount, detAut.RuleCount, totAut == null ? -1 : totAut.RuleCount, algo2Min == null ? "NA" : algo2Min.StateCount.ToString(), algo2Min == null ? "NA" : algo2Min.RuleCount.ToString(), //compTO ? Program.timeOut.ToString() : timeComp.ToString(), algo1TO ? Program.timeOut.ToString() : timeAlgo1.ToString(), algo2TO ? Program.timeOut.ToString() : timeAlgo2.ToString(), algoSFATO ? Program.timeOut.ToString() : timeAlgoSFA.ToString()); } }
void ToFastTrans(TreeTransducer ta, StringBuilder sb) { foreach (var state in ta.GetStates()) { //Check if state is a trans or a lang if (ta.IsPureAcceptorState(state) || ta.IsPureTransducerState(state)) { if (ta.IsPureAcceptorState(state)) { sb.AppendFormat((state.Equals(ta.Root) ? "Public " : "") + "Lang {0} : {1} ", GetStateName(int.Parse(state.ToString())), ta.InputAlphabet.AlphabetSort.Name.ToString()); } else { sb.AppendFormat((state.Equals(ta.Root) ? "Public " : "") + "Trans {0} : {1} -> {2} ", GetStateName(int.Parse(state.ToString())), ta.InputAlphabet.AlphabetSort.Name.ToString(), ta.OutputAlphabet.AlphabetSort.Name.ToString()); } sb.AppendLine("{"); bool needCaseSeparator = false; foreach (var func in ta.InputAlphabet.Symbols) { var rules = ta.GetRules(state, func); for (int i = 0; i < rules.Count; i++) { sb.Append("\t"); if (needCaseSeparator) { sb.Append("| "); //case separator } else { sb.Append(" "); } var rule = rules[i]; sb.Append(rule.Symbol.Name.ToString()); sb.Append("("); for (int j = 1; j <= rule.Rank; j++) { if (j > 1) { sb.Append(","); } sb.Append("x" + j); } sb.Append(") where "); ToFastExpr(rule.Guard, sb, true); var output_given = true; for (int j = 1; j <= rule.Rank; j++) { var args_j = rule.Lookahead(j - 1); foreach (var rlastate in args_j) { if ((!rlastate.Equals(tt.identityState)) && ta.IsPureAcceptorState(rlastate)) { if (output_given) { sb.Append(" given "); output_given = false; } sb.Append("("); sb.Append(GetStateName(tt.Z.GetNumeralInt(rlastate))); sb.Append(" x" + j); sb.Append(")"); } } } if (ta.IsPureTransducerState(state)) { sb.Append(" to "); ToFastExpr(rule.Output, sb, false); } sb.AppendLine(); needCaseSeparator = true; } } sb.AppendLine("}"); } else { throw new Exception("Method To fast trans"); } } }