public void Pattern_TreePattern() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; Pattern psimp = new Pattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Assert.AreEqual(true, psimp.Match(sinx2, sinx2.DrivenByPort), "B01"); Assert.AreEqual(false, psimp.Match(x2, x2.DrivenByPort), "B02"); TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinsqr.Add(new Pattern(new EntityCondition(new MathIdentifier("Square", "Std")))); Assert.AreEqual(true, psinsqr.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinsqr.Match(x2, x2.DrivenByPort), "B04"); TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); psinadd.Add(new Pattern(new EntityCondition(new MathIdentifier("Add", "Std")))); Assert.AreEqual(false, psinadd.Match(sinx2, sinx2.DrivenByPort), "B03"); Assert.AreEqual(false, psinadd.Match(x2, x2.DrivenByPort), "B04"); }
private static int traverseHill(int right = 3, int down = 1) { int currentRow = 0; int currentColumn = 0; int treesEncountered = 0; Console.WriteLine($"Going down the hill at {right}X{down}"); // foreach(string hillRow in TreePattern) for (currentRow = 0; currentRow < TreePattern.Count(); currentRow += down) { if (currentRow % down == 1) { Console.WriteLine("skipping"); continue; } string hillRow = TreePattern[currentRow]; if (hillRow[currentColumn] == '#') { treesEncountered++; } // at the end index the row and column //currentRow += 1; currentColumn += right; currentColumn %= hillRow.Length; } Console.WriteLine("Reached the bottom"); Console.WriteLine($"Encountered {treesEncountered} trees!"); return(treesEncountered); }
public VisitTreeWizardContextVisitor(TreeWizard outer, IContextVisitor visitor, IDictionary <string, object> labels, TreePattern tpattern) { _outer = outer; _visitor = visitor; _labels = labels; _tpattern = tpattern; }
public TreeForNode(TreeContext context, SourceLocation location, TreePattern pattern, TreeReference collection, TreeReference body) : base(context, location) { Pattern = pattern; Collection = collection; Body = body; }
/// <summary> /// Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels /// on the various nodes and '.' (dot) as the node/subtree wildcard, /// return true if the pattern matches and fill the labels Map with /// the labels pointing at the appropriate nodes. Return false if /// the pattern is malformed or the tree does not match. /// </summary> /// <remarks> /// If a node specifies a text arg in pattern, then that must match /// for that node in t. /// /// </remarks> public bool Parse(object t, string pattern, IDictionary labels) { TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); bool matched = _Parse(t, tpattern, labels); return(matched); }
/** <summary> * Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels * on the various nodes and '.' (dot) as the node/subtree wildcard, * return true if the pattern matches and fill the labels Map with * the labels pointing at the appropriate nodes. Return false if * the pattern is malformed or the tree does not match. * </summary> * * <remarks> * If a node specifies a text arg in pattern, then that must match * for that node in t. * * TODO: what's a better way to indicate bad pattern? Exceptions are a hassle * </remarks> */ public bool Parse(object t, string pattern, IDictionary <string, object> labels) { TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); /* * System.out.println("t="+((Tree)t).toStringTree()); * System.out.println("scant="+tpattern.toStringTree()); */ bool matched = ParseCore(t, tpattern, labels); return(matched); }
/// <summary> /// For all subtrees that match the pattern, execute the visit action. /// </summary> /// <remarks> /// The implementation uses the root node of the pattern in combination /// with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. /// Patterns with wildcard roots are also not allowed. /// </remarks> public void Visit(object t, string pattern, ContextVisitor visitor) { // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) { return; } //IDictionary labels = new Hashtable(); // reused for each _parse int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new TreeWizard.InvokeVisitorOnPatternMatchContextVisitor(this, tpattern, visitor)); }
/// <summary>Return a List of subtrees matching pattern</summary> public IList Find(object t, string pattern) { IList subtrees = new ArrayList(); // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) { return(null); } int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new TreeWizard.PatternMatchingContextVisitor(this, tpattern, subtrees)); return(subtrees); }
/** <summary> * Do the work for parse. Check to see if the t2 pattern fits the * structure and token types in t1. Check text if the pattern has * text arguments on nodes. Fill labels map with pointers to nodes * in tree matched against nodes in pattern with labels. * </summary> */ protected virtual bool ParseCore(object t1, TreePattern tpattern, IDictionary <string, object> labels) { // make sure both are non-null if (t1 == null || tpattern == null) { return(false); } // check roots (wildcard matches anything) if (tpattern.GetType() != typeof(WildcardTreePattern)) { if (adaptor.GetType(t1) != tpattern.Type) { return(false); } // if pattern has text, check node text if (tpattern.hasTextArg && !adaptor.GetText(t1).Equals(tpattern.Text)) { return(false); } } if (tpattern.label != null && labels != null) { // map label in pattern to node in t1 labels[tpattern.label] = t1; } // check children int n1 = adaptor.GetChildCount(t1); int n2 = tpattern.ChildCount; if (n1 != n2) { return(false); } for (int i = 0; i < n1; i++) { object child1 = adaptor.GetChild(t1, i); TreePattern child2 = (TreePattern)tpattern.GetChild(i); if (!ParseCore(child1, child2, labels)) { return(false); } } return(true); }
/// <summary> /// Do the work for Parse(). Check to see if the t2 pattern fits the /// structure and token types in t1. Check text if the pattern has /// text arguments on nodes. Fill labels map with pointers to nodes /// in tree matched against nodes in pattern with labels. /// </summary> protected bool _Parse(object t1, TreePattern t2, IDictionary labels) { // make sure both are non-null if (t1 == null || t2 == null) { return(false); } // check roots (wildcard matches anything) if (t2.GetType() != typeof(WildcardTreePattern)) { if (adaptor.GetNodeType(t1) != t2.Type) { return(false); } if (t2.hasTextArg && !adaptor.GetNodeText(t1).Equals(t2.Text)) { return(false); } } if (t2.label != null && labels != null) { // map label in pattern to node in t1 labels[t2.label] = t1; } // check children int n1 = adaptor.GetChildCount(t1); int n2 = t2.ChildCount; if (n1 != n2) { return(false); } for (int i = 0; i < n1; i++) { object child1 = adaptor.GetChild(t1, i); TreePattern child2 = (TreePattern)t2.GetChild(i); if (!_Parse(child1, child2, labels)) { return(false); } } return(true); }
/** <summary> * For all subtrees that match the pattern, execute the visit action. * The implementation uses the root node of the pattern in combination * with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. * Patterns with wildcard roots are also not allowed. * </summary> */ public void Visit(object t, string pattern, IContextVisitor visitor) { // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if (tpattern == null || tpattern.IsNil || tpattern.GetType() == typeof(WildcardTreePattern)) { return; } IDictionary <string, object> labels = new Dictionary <string, object>(); // reused for each _parse int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new VisitTreeWizardContextVisitor(this, visitor, labels, tpattern)); }
/** <summary> * Do the work for parse. Check to see if the t2 pattern fits the * structure and token types in t1. Check text if the pattern has * text arguments on nodes. Fill labels map with pointers to nodes * in tree matched against nodes in pattern with labels. * </summary> */ protected virtual bool _Parse( object t1, TreePattern tpattern, IDictionary<string, object> labels ) { // make sure both are non-null if ( t1 == null || tpattern == null ) { return false; } // check roots (wildcard matches anything) if ( tpattern.GetType() != typeof( WildcardTreePattern ) ) { if ( adaptor.GetType( t1 ) != tpattern.Type ) { return false; } // if pattern has text, check node text if ( tpattern.hasTextArg && !adaptor.GetText( t1 ).Equals( tpattern.Text ) ) { return false; } } if ( tpattern.label != null && labels != null ) { // map label in pattern to node in t1 labels[tpattern.label] = t1; } // check children int n1 = adaptor.GetChildCount( t1 ); int n2 = tpattern.ChildCount; if ( n1 != n2 ) { return false; } for ( int i = 0; i < n1; i++ ) { object child1 = adaptor.GetChild( t1, i ); TreePattern child2 = (TreePattern)tpattern.GetChild( i ); if ( !_Parse( child1, child2, labels ) ) { return false; } } return true; }
public FindTreeWizardContextVisitor( TreeWizard outer, TreePattern tpattern, IList subtrees ) { _outer = outer; _tpattern = tpattern; _subtrees = subtrees; }
public VisitTreeWizardContextVisitor( TreeWizard outer, IContextVisitor visitor, IDictionary<string, object> labels, TreePattern tpattern ) { _outer = outer; _visitor = visitor; _labels = labels; _tpattern = tpattern; }
protected virtual bool ParseCore(object t1, TreePattern tpattern, IDictionary<string, object> labels) { if ((t1 == null) || (tpattern == null)) { return false; } if (tpattern.GetType() != typeof(WildcardTreePattern)) { if (this.adaptor.GetType(t1) != tpattern.Type) { return false; } if (tpattern.hasTextArg && !this.adaptor.GetText(t1).Equals(tpattern.Text)) { return false; } } if ((tpattern.label != null) && (labels != null)) { labels[tpattern.label] = t1; } int childCount = this.adaptor.GetChildCount(t1); int num2 = tpattern.ChildCount; if (childCount != num2) { return false; } for (int i = 0; i < childCount; i++) { object child = this.adaptor.GetChild(t1, i); TreePattern pattern = (TreePattern) tpattern.GetChild(i); if (!this.ParseCore(child, pattern, labels)) { return false; } } return true; }
public PatternMatchingContextVisitor(TreeWizard owner, TreePattern pattern, IList list) { this.owner = owner; this.pattern = pattern; this.list = list; }
public InvokeVisitorOnPatternMatchContextVisitor(TreeWizard owner, TreePattern pattern, ContextVisitor visitor) { this.owner = owner; this.pattern = pattern; this.visitor = visitor; }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List<CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }
public TreePatternArm(TreePattern pattern, TreeReference?guard, TreeReference body) { Pattern = pattern; Guard = guard; Body = body; }
public void Pattern_CoalescedTreeDeduction() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Square(x); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; TreePattern psinadd = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinadd_add = new Pattern(new EntityCondition(new MathIdentifier("Add", "Std"))); psinadd.Add(psinadd_add); psinadd_add.Group = "add"; TreePattern psinsqr = new TreePattern(new EntityCondition(new MathIdentifier("Sine", "Std"))); Pattern psinsqr_sqr = new Pattern(new EntityCondition(new MathIdentifier("Square", "Std"))); psinsqr.Add(psinsqr_sqr); psinsqr.Group = "sin"; psinsqr_sqr.Group = "sqr"; // generate coalesced tree CoalescedTreeNode root; List <CoalescedTreeNode> list = CoalescedTreeNode.CreateRootTree(out root); psinadd.MergeToCoalescedTree(new MathIdentifier("SinAdd", "Test"), list); psinsqr.MergeToCoalescedTree(new MathIdentifier("SinSqr", "Test"), list); // test whether the tree was generated correctly Assert.AreEqual(1, root.ConditionAxis.Count, "D01"); Assert.AreEqual(0, root.PatternAxis.Count, "D02"); CoalescedTreeNode csin = root.ConditionAxis[0]; Assert.AreEqual(true, csin.Condition is EntityCondition, "D03"); Assert.AreEqual(new MathIdentifier("Sine", "Std"), ((EntityCondition)csin.Condition).EntityId, "D04"); Assert.AreEqual(1, csin.GroupAxis.Count, "D05"); Assert.AreEqual(0, csin.SubscriptionAxis.Count, "D06"); Assert.AreEqual("sin", csin.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D07"); Assert.AreEqual(1, csin.PatternAxis.Count, "D08"); Assert.AreEqual(1, csin.PatternAxis[0].ChildrenAxis.Count, "D09"); Assert.AreEqual(true, csin.PatternAxis[0].ChildrenAxis[0].Condition is AlwaysTrueCondition, "D10"); CoalescedTreeNode cadd = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[0]; Assert.AreEqual(true, cadd.Condition is EntityCondition, "D11"); Assert.AreEqual(new MathIdentifier("Add", "Std"), ((EntityCondition)cadd.Condition).EntityId, "D12"); Assert.AreEqual(1, cadd.GroupAxis.Count, "D13"); Assert.AreEqual(1, cadd.SubscriptionAxis.Count, "D14"); Assert.AreEqual("add", cadd.GroupAxis[new MathIdentifier("SinAdd", "Test")], "D15"); Assert.AreEqual(new MathIdentifier("SinAdd", "Test"), cadd.SubscriptionAxis[0], "D16"); Assert.AreEqual(0, cadd.PatternAxis.Count, "D18"); CoalescedTreeNode csqr = csin.PatternAxis[0].ChildrenAxis[0].ConditionAxis[1]; Assert.AreEqual(true, csqr.Condition is EntityCondition, "D19"); Assert.AreEqual(new MathIdentifier("Square", "Std"), ((EntityCondition)csqr.Condition).EntityId, "D20"); Assert.AreEqual(1, csqr.GroupAxis.Count, "D21"); Assert.AreEqual(1, csqr.SubscriptionAxis.Count, "D22"); Assert.AreEqual("sqr", csqr.GroupAxis[new MathIdentifier("SinSqr", "Test")], "D23"); Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), csqr.SubscriptionAxis[0], "D24"); Assert.AreEqual(0, csqr.PatternAxis.Count, "D26"); // test whether the tree works as expected MatchCollection res = Match.MatchAll(sinx2, sinx2.DrivenByPort, root); Assert.AreEqual(true, res.Contains(new MathIdentifier("SinSqr", "Test")), "D27"); Assert.AreEqual(false, res.Contains(new MathIdentifier("SinAdd", "Test")), "D28"); Match match = res[new MathIdentifier("SinSqr", "Test")]; Assert.AreEqual(new MathIdentifier("SinSqr", "Test"), match.PatternId, "D29"); Assert.AreEqual(2, match.GroupCount, "D30"); Assert.AreEqual(1, match["sin"].Count, "D31"); Assert.AreEqual(sinx2.InstanceId, match["sin"][0].First.InstanceId, "D32"); Assert.AreEqual(1, match["sqr"].Count, "D33"); Assert.AreEqual(x2.InstanceId, match["sqr"][0].First.InstanceId, "D34"); }
public TreeLetNode(TreeContext context, SourceLocation location, TreePattern pattern, TreeReference initializer) : base(context, location) { Pattern = pattern; Initializer = initializer; }
public FindTreeWizardContextVisitor(TreeWizard outer, TreePattern tpattern, IList subtrees) { _outer = outer; _tpattern = tpattern; _subtrees = subtrees; }