// find the next local match @Override public boolean matches() { // System.out.println(toString()); // System.out.println(namesToNodes); // System.err.println("matches: " + myNode.reln); // this is necessary so that a negated/optional node matches only once if (finished) { // System.out.println(false); return false; } while (!finished) { if (matchChild()) { if (myNode.isNegated()) { // negated node only has to fail once finished = true; return false; // cannot be optional and negated } else { if (myNode.isOptional()) { finished = true; } // System.out.println(true); return true; } } else { goToNextNodeMatch(); } } if (myNode.isNegated()) { // couldn't match my relation/pattern, so // succeeded! return true; } else { // couldn't match my relation/pattern, so failed! nextMatch = null; decommitVariableGroups(); decommitNamedNodes(); decommitNamedRelations(); // didn't match, but return true anyway if optional return myNode.isOptional(); } }
@Override public String toString() { return "node matcher for: " + myNode.localString(); }
// when finished = false; break; is called, it means I successfully matched. @SuppressWarnings("null") private void goToNextNodeMatch() { decommitVariableGroups(); // make sure variable groups are free. decommitNamedNodes(); decommitNamedRelations(); finished = true; Matcher m = null; while (nodeMatchCandidateIterator.hasNext()) { if (myNode.reln.getName() != null) { String foundReln = namesToRelations.get(myNode.reln.getName()); nextMatchReln = ((GraphRelation.SearchNodeIterator) nodeMatchCandidateIterator).getReln(); if ((foundReln != null) && (!nextMatchReln.equals(foundReln))) { nextMatch = nodeMatchCandidateIterator.next(); continue; } } nextMatch = nodeMatchCandidateIterator.next(); // System.err.println("going to next match: " + nextMatch.word() + " " + // myNode.descString + " " + myNode.isLink); if (myNode.descString.equals("{}") && myNode.isLink) { IndexedWord otherNode = namesToNodes.get(myNode.name); if (otherNode != null) { if (otherNode.equals(nextMatch)) { if (!myNode.negDesc) { finished = false; break; } } else { if (myNode.negDesc) { finished = false; break; } } } else { boolean found = myNode.nodeAttrMatch(nextMatch, hyp ? sg : sg_aligned, ignoreCase); if (found) { for (Pair<Integer, String> varGroup : myNode.variableGroups) { // if variables have been captured from a regex, they // must match any previous matchings String thisVariable = varGroup.second(); String thisVarString = variableStrings.getString(thisVariable); if (thisVarString != null && !thisVarString.equals(m.group(varGroup.first()))) { // failed to match a variable found = false; break; } } // nodeAttrMatch already checks negDesc, so no need to // check for that here finished = false; break; } } } else { // try to match the description pattern. boolean found = myNode.nodeAttrMatch(nextMatch, hyp ? sg : sg_aligned, ignoreCase); if (found) { for (Pair<Integer, String> varGroup : myNode.variableGroups) { // if variables have been captured from a regex, they // must match any previous matchings String thisVariable = varGroup.second(); String thisVarString = variableStrings.getString(thisVariable); if (thisVarString != null && !thisVarString.equals(m.group(varGroup.first()))) { // failed to match a variable found = false; break; } } // nodeAttrMatch already checks negDesc, so no need to // check for that here finished = false; break; } } } // end while if (!finished) { // I successfully matched. resetChild(); if (myNode.name != null) { // note: have to fill in the map as we go for backreferencing if (!namesToNodes.containsKey(myNode.name)) { // System.err.println("making namedFirst"); namedFirst = true; } // System.err.println("adding named node: " + myNode.name + "=" + // nextMatch.word()); namesToNodes.put(myNode.name, nextMatch); } if (myNode.reln.getName() != null) { if (!namesToRelations.containsKey(myNode.reln.getName())) relnNamedFirst = true; namesToRelations.put(myNode.reln.getName(), nextMatchReln); } commitVariableGroups(m); // commit my variable groups. } // finished is false exiting this if and only if nextChild exists // and has a label or backreference that matches // (also it will just have been reset) }
public final NodePattern Description(GraphRelation r) throws ParseException { Token name = null; boolean link = false; boolean isRoot = false; boolean isEmpty = false; Token attr = null; Token value = null; Map<String, String> attributes = Generics.newHashMap(); NodePattern pat; jj_consume_token(23); switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) { case IDENTIFIER: { attr = jj_consume_token(IDENTIFIER); jj_consume_token(12); switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) { case IDENTIFIER: { value = jj_consume_token(IDENTIFIER); break; } case REGEX: { value = jj_consume_token(REGEX); break; } default: jj_la1[22] = jj_gen; jj_consume_token(-1); throw new ParseException(); } if (attr != null && value != null) attributes.put(attr.image, value.image); label_6: while (true) { switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) { case 24: {; break; } default: jj_la1[23] = jj_gen; break label_6; } jj_consume_token(24); attr = jj_consume_token(IDENTIFIER); jj_consume_token(12); switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) { case IDENTIFIER: { value = jj_consume_token(IDENTIFIER); break; } case REGEX: { value = jj_consume_token(REGEX); break; } default: jj_la1[24] = jj_gen; jj_consume_token(-1); throw new ParseException(); } if (attr != null && value != null) attributes.put(attr.image, value.image); } jj_consume_token(25); break; } case ROOT: { attr = jj_consume_token(ROOT); jj_consume_token(25); isRoot = true; break; } case EMPTY: { attr = jj_consume_token(EMPTY); jj_consume_token(25); isEmpty = true; break; } case 25: { jj_consume_token(25); break; } default: jj_la1[25] = jj_gen; jj_consume_token(-1); throw new ParseException(); } switch ((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) { case 22: { jj_consume_token(22); link = true; name = jj_consume_token(IDENTIFIER); String nodeName = name.image; if (underNegation) { if (!knownVariables.contains(nodeName)) { { if (true) throw new ParseException( "Cannot add new variable names under negation. Node '" + nodeName + "' not seen before"); } } } else { knownVariables.add(nodeName); } break; } default: jj_la1[26] = jj_gen; ; } pat = new NodePattern( r, underNodeNegation, attributes, isRoot, isEmpty, name != null ? name.image : null); if (link) pat.makeLink(); { if ("" != null) return pat; } throw new Error("Missing return statement in function"); }