@@ -47,13 +47,9 @@ public class NodePattern extends SemgrexPattern {
47
47
private final String name ;
48
48
private String descString ;
49
49
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 ;
53
50
54
51
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 ) {
57
53
this .reln = r ;
58
54
this .negDesc = negDesc ;
59
55
this .isLink = isLink ;
@@ -65,20 +61,21 @@ public NodePattern(GraphRelation r, boolean negDesc,
65
61
this .regexPartialAttributes = new ArrayList <>();
66
62
67
63
descString = "{" ;
68
- for (Triple <String , String , Boolean > entry : attrs .attributes ()) {
64
+ for (Quadruple <String , String , Boolean , List < Pair < Integer , String >> > entry : attrs .attributes ()) {
69
65
if (!descString .equals ("{" ))
70
66
descString += ";" ;
71
67
String key = entry .first ();
72
68
String value = entry .second ();
73
69
boolean negated = entry .third ();
70
+ List <Pair <Integer , String >> varGroups = entry .fourth ();
74
71
75
72
// Add the attributes for this key
76
73
if (value .equals ("__" )) {
77
- attributes .add (new Attribute (key , true , true , negated ));
74
+ attributes .add (new Attribute (key , true , true , negated , varGroups ));
78
75
} else if (value .matches ("/.*/" )) {
79
- attributes .add (buildRegexAttribute (key , value , negated ));
76
+ attributes .add (buildRegexAttribute (key , value , negated , varGroups ));
80
77
} else { // raw description
81
- attributes .add (new Attribute (key , value , value , negated ));
78
+ attributes .add (new Attribute (key , value , value , negated , varGroups ));
82
79
}
83
80
84
81
if (negated ) {
@@ -93,6 +90,8 @@ public NodePattern(GraphRelation r, boolean negDesc,
93
90
String key = entry .second ();
94
91
String value = entry .third ();
95
92
boolean negated = entry .fourth ();
93
+ // TODO: can add varGroups, especially for the regex matches
94
+ List <Pair <Integer , String >> varGroups = Collections .emptyList ();
96
95
97
96
Class <?> clazz = AnnotationLookup .getValueType (AnnotationLookup .toCoreKey (annotation ));
98
97
boolean isMap = clazz != null && Map .class .isAssignableFrom (clazz );
@@ -108,11 +107,11 @@ public NodePattern(GraphRelation r, boolean negDesc,
108
107
} else {
109
108
// Add the attributes for this key
110
109
if (value .equals ("__" )) {
111
- attr = new Attribute (key , true , true , negated );
110
+ attr = new Attribute (key , true , true , negated , varGroups );
112
111
} else if (value .matches ("/.*/" )) {
113
- attr = buildRegexAttribute (key , value , negated );
112
+ attr = buildRegexAttribute (key , value , negated , varGroups );
114
113
} else { // raw description
115
- attr = new Attribute (key , value , value , negated );
114
+ attr = new Attribute (key , value , value , negated , varGroups );
116
115
}
117
116
partialAttributes .add (new Pair <>(annotation , attr ));
118
117
}
@@ -141,15 +140,13 @@ public NodePattern(GraphRelation r, boolean negDesc,
141
140
this .child = null ;
142
141
this .isRoot = attrs .root ();
143
142
this .isEmpty = attrs .empty ();
144
-
145
- this .variableGroups = Collections .unmodifiableList (variableGroups );
146
143
}
147
144
148
145
/**
149
146
* Tests the value to see if it's really a regex, or just a string wrapped in regex.
150
147
* Return an Attribute which matches this expression
151
148
*/
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 ) {
153
150
boolean isRegexp = false ;
154
151
for (int i = 1 ; i < value .length () - 1 ; ++i ) {
155
152
char chr = value .charAt (i );
@@ -163,9 +160,9 @@ private Attribute buildRegexAttribute(String key, String value, boolean negated)
163
160
return new Attribute (key ,
164
161
Pattern .compile (patternContent ),
165
162
Pattern .compile (patternContent , Pattern .CASE_INSENSITIVE |Pattern .UNICODE_CASE ),
166
- negated );
163
+ negated , varGroups );
167
164
} else {
168
- return new Attribute (key , patternContent , patternContent , negated );
165
+ return new Attribute (key , patternContent , patternContent , negated , varGroups );
169
166
}
170
167
}
171
168
@@ -233,7 +230,9 @@ public boolean nodeAttrMatch(IndexedWord node, final SemanticGraph sg, boolean i
233
230
// }
234
231
// System.out.println(nodeValue);
235
232
233
+ // TODO: check varGroups here
236
234
boolean matches = checkMatch (attr , ignoreCase , nodeValue );
235
+
237
236
if (!matches ) {
238
237
// System.out.println("doesn't match");
239
238
// System.out.println("");
@@ -404,6 +403,7 @@ private static class NodeMatcher extends SemgrexMatcher {
404
403
private SemgrexMatcher childMatcher ;
405
404
private boolean matchedOnce = false ;
406
405
private boolean committedVariables = false ;
406
+ private VariableStrings localVariableStrings = null ;
407
407
408
408
private String nextMatchReln = null ;
409
409
private SemanticGraphEdge nextMatchEdge = null ;
@@ -413,7 +413,7 @@ private static class NodeMatcher extends SemgrexMatcher {
413
413
private boolean relnNamedFirst = false ;
414
414
private boolean edgeNamedFirst = false ;
415
415
416
- private boolean ignoreCase = false ;
416
+ private final boolean ignoreCase ;
417
417
418
418
// universal: childMatcher is null if and only if
419
419
// myNode.child == null OR resetChild has never been called
@@ -470,7 +470,8 @@ private void goToNextNodeMatch() {
470
470
decommitNamedNodes ();
471
471
decommitNamedRelations ();
472
472
finished = true ;
473
- Matcher m = null ;
473
+ VariableStrings tempVariableStrings = new VariableStrings ();
474
+
474
475
while (nodeMatchCandidateIterator .hasNext ()) {
475
476
if (myNode .reln .getName () != null ) {
476
477
String foundReln = namesToRelations .get (myNode .reln .getName ());
@@ -508,47 +509,23 @@ private void goToNextNodeMatch() {
508
509
}
509
510
}
510
511
} else {
512
+ // TODO: pass in all varstrings and local varstrings
511
513
boolean found = myNode .nodeAttrMatch (nextMatch ,
512
514
hyp ? sg : sg_aligned ,
513
515
ignoreCase );
514
516
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
-
528
517
// nodeAttrMatch already checks negDesc, so no need to
529
518
// check for that here
530
519
finished = false ;
531
520
break ;
532
521
}
533
522
}
534
523
} else { // try to match the description pattern.
524
+ // TODO: pass in all varstrings and local varstrings
535
525
boolean found = myNode .nodeAttrMatch (nextMatch ,
536
526
hyp ? sg : sg_aligned ,
537
527
ignoreCase );
538
528
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
-
552
529
// nodeAttrMatch already checks negDesc, so no need to
553
530
// check for that here
554
531
finished = false ;
@@ -582,30 +559,23 @@ private void goToNextNodeMatch() {
582
559
// TODO FIXME: this would need to read all of the matchers used
583
560
// (eg, from the various attributes)
584
561
// and commit all of them
585
- commitVariableGroups (m ); // commit my variable groups.
562
+ commitVariableGroups (tempVariableStrings ); // commit my variable groups.
586
563
}
587
564
// finished is false exiting this if and only if nextChild exists
588
565
// and has a label or backreference that matches
589
566
// (also it will just have been reset)
590
567
}
591
568
592
- private void commitVariableGroups (Matcher m ) {
569
+ private void commitVariableGroups (VariableStrings tempVariableStrings ) {
593
570
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 );
602
573
}
603
574
604
575
private void decommitVariableGroups () {
605
576
if (committedVariables ) {
606
- for (Pair <Integer , String > varGroup : myNode .variableGroups ) {
607
- variableStrings .unsetVar (varGroup .second ());
608
- }
577
+ variableStrings .unsetVars (localVariableStrings );
578
+ localVariableStrings = null ;
609
579
}
610
580
committedVariables = false ;
611
581
}
0 commit comments