public void TestCycletree() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/cycletree.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.AreEqual <int>(1, fti.queryRes.Count); }
//[TestMethod] public void TestNondetCS() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/nondetcs.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.IsTrue(fti.treeDefinitions["A"].transducers.Count == 1); StringBuilder sb = new StringBuilder(); fti.ToFast(sb); var q = new List <TreeTransducer>(fti.treeDefinitions["A"].transducers.Values)[0]; var A = q.InputAlphabet; var T = A.MkAttr(A.TT.Z.True); Func <Expr, Expr> one = x => A.MkTree("one", T, x); Expr zero = A.MkTree("zero", T); var t1 = one(one(one(zero))); var t1s = q[t1]; var t2 = one(one(one(one(zero)))); var t2s = q[t2]; Assert.AreEqual <int>(8, t1s.Length); Assert.AreEqual <int>(16, t2s.Length); }
public void TestApply() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/apply.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.IsTrue(fti.treeDefinitions["A"].transducers.Count == 1); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); var q = new List <TreeTransducer>(fti.treeDefinitions["A"].transducers.Values)[0]; var A = q.InputAlphabet; Func <Expr, Expr, Expr> two = (x, y) => A.MkTree("two", A.MkAttr(), x, y); Func <Expr, Expr> one = x => A.MkTree("one", A.MkAttr(), x); Expr zero = A.MkTree("zero", A.MkAttr()); var t1 = two(one(one(one(two(zero, zero)))), one(one(one(zero)))); var t1_out_expected = one(zero); var t2 = two(one(one(one(zero))), one(one(one(two(zero, zero))))); var t1_out = q[t1]; var t2_out = q[t2]; Assert.AreEqual <int>(1, t1_out.Length); Assert.AreEqual <Expr>(t1_out_expected, t1_out[0]); Assert.AreEqual <int>(0, t2_out.Length, "t2 is not accepted because the right subtree contains twos"); }
public void TestIntersection() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/intersect.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //var t1 = fti.treeDefinitions["A"].trees["t_1"]; //var t2 = fti.treeDefinitions["A"].trees["t_2"]; //var t12 = fti.treeDefinitions["A"].trees["t_12"]; //var tnot = fti.treeDefinitions["A"].trees["t_not"]; //var q1 = fti.treeDefinitions["A"].acceptors["q1"]; //var r1 = fti.treeDefinitions["A"].acceptors["r1"]; //var inters = fti.treeDefinitions["A"].acceptors["intersect_q1r1"]; //Assert.IsTrue(q1.Apply(t12).SequenceEqual(inters.Apply(t12))); //Assert.IsTrue(r1.Apply(t12).SequenceEqual(inters.Apply(t12))); //Assert.IsFalse(q1.Apply(t1).SequenceEqual(inters.Apply(t1))); //Assert.IsTrue(r1.Apply(t1).SequenceEqual(inters.Apply(t1))); //Assert.IsTrue(q1.Apply(t2).SequenceEqual(inters.Apply(t2))); //Assert.IsFalse(r1.Apply(t2).SequenceEqual(inters.Apply(t2))); //Assert.IsTrue(q1.Apply(tnot).SequenceEqual(inters.Apply(tnot))); //Assert.IsTrue(r1.Apply(tnot).SequenceEqual(inters.Apply(tnot))); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/intersect.cs", "Microsoft.Fast.Tests.Intersection"); }
public static void Run(string source, TextWriter tw = null, string cs_master_file = "", string filename = "pgm.fast") { if (tw == null) { tw = Console.Out; } try { var pgm = FastPgmParser.Parse(source, filename); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm, tw, LogLevel.Minimal); FastTransducerInstance.DisposeZ3P(); } catch (FastParseException e) { tw.WriteLine(e.ToString()); return; } catch (FastAssertException e) { tw.WriteLine("{3}({0},{1}): error : AssertionViolated, {2}", min1(e.line), min1(e.pos), e.Message, filename); return; } catch (FastException e) { tw.WriteLine("{3}({0},{1}): error : FastError, {2}", 1, 1, e.Message, filename); return; } catch (Exception e) { tw.WriteLine("{3}({0},{1}): error : InternalError, {2}", 1, 1, filename); return; } }
public void TestOperators() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/operators.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); FastTransducerInstance.DisposeZ3P(); }
public void TestHtmlSanitizer() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/htmlSanitizer.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); FastTransducerInstance.DisposeZ3P(); }
/// <summary> /// Generates C# code from a Fast program /// </summary> /// <param name="inputPath">the Fast file from which it generates the CSharp code</param> /// <param name="outputPath">the C# file into which the CSharp code is generated</param> /// <param name="ns">the package the generated file will belong to</param> /// <param name="flag">true to generate the transducer's optimized version</param> public static bool GenerateCSharp(String inputPath, String outputPath, String ns, bool flag) { var pgm = Parser.ParseFromFile(inputPath); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); StringBuilder sb = new StringBuilder(); fti.ToFast(sb); pgm = Parser.ParseFromString(sb.ToString()); return(CsharpGenerator.GenerateCode(pgm, outputPath, ns)); }
public void AAAtest() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/aaa.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.AreEqual <int>(5, fti.treeDefinitions["Node"].transducers.Count); Assert.AreEqual <int>(3, fti.treeDefinitions["Node"].acceptors.Count); Assert.AreEqual <int>(1, fti.alphabets.Count); Assert.AreEqual <int>(2, fti.queryRes.Count); }
//[TestMethod] public void TestMinimization() { var pgm = Parser.ParseFromFile("../../../src/Fast.Tests/FastSamples/minimization.fast"); //Build transducers FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //Compile to fast and back //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); }
public void TestEquivalence() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/equivalence.fast"); //Build transducers FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //Compile to fast and back //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); }
public void TestPreimage() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/preimage.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); }
public void TestTrees() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/trees.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/trees.cs", "Microsoft.Fast.Tests.Trees"); }
public void TestCompositionAsserts() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/compose1.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.IsTrue(fti.treeDefinitions["C"].trees["t_112"].SequenceEqual(fti.treeDefinitions["C"].trees["t_12"])); Assert.IsTrue(fti.treeDefinitions["C"].trees["t_212"].SequenceEqual(fti.treeDefinitions["C"].trees["t_22"])); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); }
public void TestUnion() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/union.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); var t1 = fti.treeDefinitions["A"].trees["t_1"]; var t2 = fti.treeDefinitions["A"].trees["t_2"]; var t12 = fti.treeDefinitions["A"].trees["t_12"]; var tnot = fti.treeDefinitions["A"].trees["t_not"]; var q1 = fti.treeDefinitions["A"].acceptors["q1"]; var r1 = fti.treeDefinitions["A"].acceptors["r1"]; var union = fti.treeDefinitions["A"].acceptors["union_q1r1"]; //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //var pgm2 = Parser.ParseFromString(sb.ToString()); //Assert.IsTrue(q1.Apply(t12).SequenceEqual(union.Apply(t12))); // Assert.IsTrue(r1.Apply(t12).SequenceEqual(union.Apply(t12))); //Assert.IsTrue(q1.Apply(t1).SequenceEqual(union.Apply(t1))); var res1 = new Microsoft.Automata.Sequence <Expr>(r1.Apply(t1)); Assert.IsTrue(res1.Length == 0); var res2 = new Microsoft.Automata.Sequence <Expr>(union.Apply(t1)); Assert.IsTrue(res2.Length == 1); Assert.IsFalse(q1.Apply(t2).SequenceEqual(union.Apply(t2))); Assert.IsTrue(r1.Apply(t2).SequenceEqual(union.Apply(t2))); Assert.IsTrue(q1.Apply(tnot).SequenceEqual(union.Apply(tnot))); Assert.IsTrue(r1.Apply(tnot).SequenceEqual(union.Apply(tnot))); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/union.cs", "Microsoft.Fast.Tests.Intersection"); }
public void TestComposition() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/compose.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //Assert.IsTrue(fti.treeDefinitions["A"].Transducers.Count == 2); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/compose.cs", "Microsoft.Fast.Tests.Composition"); }
public void TestChars() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/shiftchars.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.AreEqual <int>(fti.treeDefinitions["STR"].transducers.Count, 1); var ta = fti.treeDefinitions["STR"].transducers["caesar1"]; Assert.AreEqual <int>(2, ta.StateCount); var da = ta.ComputeDomainAcceptor(); var sfa = da.ConvertToSFA(); //sfa.ShowGraph(); var aut = sfa.Concretize(100); //sfa.Solver.CharSetProvider.ShowGraph(aut, "test"); }
public static TreeTransducer GetFastProg( string path) { var pgm = Microsoft.Fast.Parser.ParseFromFile(path); var fti = FastTransducerInstance.MkFastTransducerInstance(pgm); foreach (var td in fti.treeDefinitions) { var tcd = td.Value; foreach (var pair in tcd.acceptors) { return(pair.Value); } } return(null); }
public void TestDomain() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/domain.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); //Assert.IsTrue(fti.treeDefinitions["A"].trees["t_11"].SequenceEqual(fti.treeDefinitions["A"].trees["t_12"])); //Assert.IsTrue(fti.treeDefinitions["A"].trees["t_21"].SequenceEqual(fti.treeDefinitions["A"].trees["t_22"])); //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/domain.cs", "Microsoft.Fast.Tests.Domain"); }
public void TestConstants() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/constants.fast"); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); FastTransducerInstance.DisposeZ3P(); Assert.IsTrue(pgm.defs.Count == 8); StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/constants.cs", "Microsoft.Fast.Tests.Constants"); }
public void TestComplement() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/complement.fast"); //Build transducers FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); Assert.AreEqual <int>(1, fti.alphabets.Count); Assert.AreEqual <int>(1, fti.treeDefinitions.Count); Assert.AreEqual <int>(1, fti.queryRes.Count); //Compile to fast and back //StringBuilder sb = new StringBuilder(); //fti.ToFast(sb); //pgm = Parser.ParseFromString(sb.ToString()); }
public void TestFunctions() { var pgm = Parser.ParseFromFile(srcDirectory + "Fast.Tests/FastSamples/functions.fast"); Assert.IsTrue(pgm.defs.Count == 6); FastTransducerInstance fti = FastTransducerInstance.MkFastTransducerInstance(pgm); StringBuilder sb = new StringBuilder(); fti.ToFast(sb); //TBD: ToFast produces code in old prefix notation //pgm = Parser.ParseFromString(sb.ToString()); //bool b = CsharpGenerator.GenerateCode(pgm, "../../../src/Fast.Tests/GeneratedCS/functions.cs", "Microsoft.Fast.Tests.Functions"); Assert.IsTrue(pgm.defs.Count == 6); }
//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); } } } } } } } } } } }