@@ -47,13 +47,9 @@ public class NodePattern extends SemgrexPattern {
4747 private final String name ;
4848 private String descString ;
4949 SemgrexPattern child ;
50- // specifies the groups in a regex that are captured as
51- // matcher-global string variables
52- private final List <Pair <Integer , String >> variableGroups ;
5350
5451 public NodePattern (GraphRelation r , boolean negDesc ,
55- NodeAttributes attrs , boolean isLink , String name ,
56- List <Pair <Integer , String >> variableGroups ) {
52+ NodeAttributes attrs , boolean isLink , String name ) {
5753 this .reln = r ;
5854 this .negDesc = negDesc ;
5955 this .isLink = isLink ;
@@ -65,20 +61,21 @@ public NodePattern(GraphRelation r, boolean negDesc,
6561 this .regexPartialAttributes = new ArrayList <>();
6662
6763 descString = "{" ;
68- for (Triple <String , String , Boolean > entry : attrs .attributes ()) {
64+ for (Quadruple <String , String , Boolean , List < Pair < Integer , String >> > entry : attrs .attributes ()) {
6965 if (!descString .equals ("{" ))
7066 descString += ";" ;
7167 String key = entry .first ();
7268 String value = entry .second ();
7369 boolean negated = entry .third ();
70+ List <Pair <Integer , String >> varGroups = entry .fourth ();
7471
7572 // Add the attributes for this key
7673 if (value .equals ("__" )) {
77- attributes .add (new Attribute (key , true , true , negated ));
74+ attributes .add (new Attribute (key , true , true , negated , varGroups ));
7875 } else if (value .matches ("/.*/" )) {
79- attributes .add (buildRegexAttribute (key , value , negated ));
76+ attributes .add (buildRegexAttribute (key , value , negated , varGroups ));
8077 } else { // raw description
81- attributes .add (new Attribute (key , value , value , negated ));
78+ attributes .add (new Attribute (key , value , value , negated , varGroups ));
8279 }
8380
8481 if (negated ) {
@@ -93,6 +90,8 @@ public NodePattern(GraphRelation r, boolean negDesc,
9390 String key = entry .second ();
9491 String value = entry .third ();
9592 boolean negated = entry .fourth ();
93+ // TODO: can add varGroups, especially for the regex matches
94+ List <Pair <Integer , String >> varGroups = Collections .emptyList ();
9695
9796 Class <?> clazz = AnnotationLookup .getValueType (AnnotationLookup .toCoreKey (annotation ));
9897 boolean isMap = clazz != null && Map .class .isAssignableFrom (clazz );
@@ -108,11 +107,11 @@ public NodePattern(GraphRelation r, boolean negDesc,
108107 } else {
109108 // Add the attributes for this key
110109 if (value .equals ("__" )) {
111- attr = new Attribute (key , true , true , negated );
110+ attr = new Attribute (key , true , true , negated , varGroups );
112111 } else if (value .matches ("/.*/" )) {
113- attr = buildRegexAttribute (key , value , negated );
112+ attr = buildRegexAttribute (key , value , negated , varGroups );
114113 } else { // raw description
115- attr = new Attribute (key , value , value , negated );
114+ attr = new Attribute (key , value , value , negated , varGroups );
116115 }
117116 partialAttributes .add (new Pair <>(annotation , attr ));
118117 }
@@ -141,15 +140,13 @@ public NodePattern(GraphRelation r, boolean negDesc,
141140 this .child = null ;
142141 this .isRoot = attrs .root ();
143142 this .isEmpty = attrs .empty ();
144-
145- this .variableGroups = Collections .unmodifiableList (variableGroups );
146143 }
147144
148145 /**
149146 * Tests the value to see if it's really a regex, or just a string wrapped in regex.
150147 * Return an Attribute which matches this expression
151148 */
152- private Attribute buildRegexAttribute (String key , String value , boolean negated ) {
149+ private Attribute buildRegexAttribute (String key , String value , boolean negated , List < Pair < Integer , String >> varGroups ) {
153150 boolean isRegexp = false ;
154151 for (int i = 1 ; i < value .length () - 1 ; ++i ) {
155152 char chr = value .charAt (i );
@@ -163,9 +160,9 @@ private Attribute buildRegexAttribute(String key, String value, boolean negated)
163160 return new Attribute (key ,
164161 Pattern .compile (patternContent ),
165162 Pattern .compile (patternContent , Pattern .CASE_INSENSITIVE |Pattern .UNICODE_CASE ),
166- negated );
163+ negated , varGroups );
167164 } else {
168- return new Attribute (key , patternContent , patternContent , negated );
165+ return new Attribute (key , patternContent , patternContent , negated , varGroups );
169166 }
170167 }
171168
@@ -233,7 +230,9 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
233230 // }
234231 // System.out.println(nodeValue);
235232
233+ // TODO: check varGroups here
236234 boolean matches = checkMatch (attr , ignoreCase , nodeValue );
235+
237236 if (!matches ) {
238237 // System.out.println("doesn't match");
239238 // System.out.println("");
@@ -404,6 +403,7 @@ private static class NodeMatcher extends SemgrexMatcher {
404403 private SemgrexMatcher childMatcher ;
405404 private boolean matchedOnce = false ;
406405 private boolean committedVariables = false ;
406+ private VariableStrings localVariableStrings = null ;
407407
408408 private String nextMatchReln = null ;
409409 private SemanticGraphEdge nextMatchEdge = null ;
@@ -413,7 +413,7 @@ private static class NodeMatcher extends SemgrexMatcher {
413413 private boolean relnNamedFirst = false ;
414414 private boolean edgeNamedFirst = false ;
415415
416- private boolean ignoreCase = false ;
416+ private final boolean ignoreCase ;
417417
418418 // universal: childMatcher is null if and only if
419419 // myNode.child == null OR resetChild has never been called
@@ -470,7 +470,8 @@ private void goToNextNodeMatch() {
470470 decommitNamedNodes ();
471471 decommitNamedRelations ();
472472 finished = true ;
473- Matcher m = null ;
473+ VariableStrings tempVariableStrings = new VariableStrings ();
474+
474475 while (nodeMatchCandidateIterator .hasNext ()) {
475476 if (myNode .reln .getName () != null ) {
476477 String foundReln = namesToRelations .get (myNode .reln .getName ());
@@ -508,47 +509,23 @@ private void goToNextNodeMatch() {
508509 }
509510 }
510511 } else {
512+ // TODO: pass in all varstrings and local varstrings
511513 boolean found = myNode .nodeAttrMatch (nextMatch ,
512514 hyp ? sg : sg_aligned ,
513515 ignoreCase );
514516 if (found ) {
515- for (Pair <Integer , String > varGroup : myNode .variableGroups ) {
516- // if variables have been captured from a regex, they
517- // must match any previous matchings
518- String thisVariable = varGroup .second ();
519- String thisVarString = variableStrings .getString (thisVariable );
520- if (thisVarString != null &&
521- !thisVarString .equals (m .group (varGroup .first ()))) {
522- // failed to match a variable
523- found = false ;
524- break ;
525- }
526- }
527-
528517 // nodeAttrMatch already checks negDesc, so no need to
529518 // check for that here
530519 finished = false ;
531520 break ;
532521 }
533522 }
534523 } else { // try to match the description pattern.
524+ // TODO: pass in all varstrings and local varstrings
535525 boolean found = myNode .nodeAttrMatch (nextMatch ,
536526 hyp ? sg : sg_aligned ,
537527 ignoreCase );
538528 if (found ) {
539- for (Pair <Integer , String > varGroup : myNode .variableGroups ) {
540- // if variables have been captured from a regex, they
541- // must match any previous matchings
542- String thisVariable = varGroup .second ();
543- String thisVarString = variableStrings .getString (thisVariable );
544- if (thisVarString != null &&
545- !thisVarString .equals (m .group (varGroup .first ()))) {
546- // failed to match a variable
547- found = false ;
548- break ;
549- }
550- }
551-
552529 // nodeAttrMatch already checks negDesc, so no need to
553530 // check for that here
554531 finished = false ;
@@ -582,30 +559,23 @@ private void goToNextNodeMatch() {
582559 // TODO FIXME: this would need to read all of the matchers used
583560 // (eg, from the various attributes)
584561 // and commit all of them
585- commitVariableGroups (m ); // commit my variable groups.
562+ commitVariableGroups (tempVariableStrings ); // commit my variable groups.
586563 }
587564 // finished is false exiting this if and only if nextChild exists
588565 // and has a label or backreference that matches
589566 // (also it will just have been reset)
590567 }
591568
592- private void commitVariableGroups (Matcher m ) {
569+ private void commitVariableGroups (VariableStrings tempVariableStrings ) {
593570 committedVariables = true ; // commit all my variable groups.
594- if (myNode .variableGroups .size () > 0 ) {
595- System .out .println (myNode .variableGroups );
596- }
597- for (Pair <Integer , String > varGroup : myNode .variableGroups ) {
598- System .out .println (varGroup );
599- String thisVarString = m .group (varGroup .first ());
600- variableStrings .setVar (varGroup .second (), thisVarString );
601- }
571+ localVariableStrings = tempVariableStrings ;
572+ variableStrings .setVars (tempVariableStrings );
602573 }
603574
604575 private void decommitVariableGroups () {
605576 if (committedVariables ) {
606- for (Pair <Integer , String > varGroup : myNode .variableGroups ) {
607- variableStrings .unsetVar (varGroup .second ());
608- }
577+ variableStrings .unsetVars (localVariableStrings );
578+ localVariableStrings = null ;
609579 }
610580 committedVariables = false ;
611581 }
0 commit comments