@Test public void testPartitioningInSequentialMatching() throws Exception { String document = "11\n11ab\n1122\n11"; String script = " "; script += "\"11\" -> T1;\r\n"; script += "\"[0-9]\" -> T2;\r\n"; script += "ADDRETAINTYPE(WS);\r\n"; script += "a:(T1 Annotation*{PARTOF({W,T2})}){-> T3};\r\n"; script += "REMOVERETAINTYPE(WS);"; CAS cas = RutaTestUtils.getCAS(document); Ruta.apply(cas, script); RutaTestUtils.assertAnnotationsEquals(cas, 1, 4, "11", "11", "11", "11"); Assert.assertEquals(10, cas.getAnnotationIndex(cas.getTypeSystem().getType(RutaTestUtils.TYPE + "2")).size()); RutaTestUtils.assertAnnotationsEquals(cas, 3, 4, "11", "11ab", "1122", "11"); }
@Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { RuleApply ruleApply = new RuleApply(this, false); crowd.beginVisit(this, ruleApply); MatchContext context = new MatchContext(getParent()); String regexpString = regexpExpr.getStringValue(context, stream); if (regexpString == null) { crowd.endVisit(this, ruleApply); return ruleApply; } AnnotationFS documentAnnotation = stream.getDocumentAnnotation(); String document = documentAnnotation.getCoveredText(); int delta = documentAnnotation.getBegin(); Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream); Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream); Pattern pattern = RutaPatternCache.getPattern(regexpString, false); Matcher matcher = pattern.matcher(document); int groupCount = matcher.groupCount(); while (matcher.find()) { RegExpRuleMatch ruleMatch = new RegExpRuleMatch(this); MatchResult matchResult = matcher.toMatchResult(); for (int i = 0; i <= groupCount; i++) { int begin = matchResult.start(i); int end = matchResult.end(i); List<Type> types = groupTypes.get(i); if (types != null) { createAnnotations(i, delta, begin, end, types, fa, matchResult, ruleMatch, stream); } else if (i == 0) { CAS cas = stream.getCas(); AnnotationFS afs = cas.createAnnotation(cas.getAnnotationType(), delta + begin, delta + end); ruleMatch.addMatched(0, afs); } } List<AnnotationFS> matchedAnnotationsOfRoot = ruleMatch.getMatchedAnnotationsOfRoot(); if (matchedAnnotationsOfRoot != null && !matchedAnnotationsOfRoot.isEmpty()) { ruleApply.add(ruleMatch, stream); } } crowd.endVisit(this, ruleApply); return ruleApply; }
RegExpRule extends AbstractRule { @Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { RuleApply ruleApply = new RuleApply(this, false); crowd.beginVisit(this, ruleApply); MatchContext context = new MatchContext(getParent()); String regexpString = regexpExpr.getStringValue(context, stream); if (regexpString == null) { crowd.endVisit(this, ruleApply); return ruleApply; } AnnotationFS documentAnnotation = stream.getDocumentAnnotation(); String document = documentAnnotation.getCoveredText(); int delta = documentAnnotation.getBegin(); Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream); Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream); Pattern pattern = RutaPatternCache.getPattern(regexpString, false); Matcher matcher = pattern.matcher(document); int groupCount = matcher.groupCount(); while (matcher.find()) { RegExpRuleMatch ruleMatch = new RegExpRuleMatch(this); MatchResult matchResult = matcher.toMatchResult(); for (int i = 0; i <= groupCount; i++) { int begin = matchResult.start(i); int end = matchResult.end(i); List<Type> types = groupTypes.get(i); if (types != null) { createAnnotations(i, delta, begin, end, types, fa, matchResult, ruleMatch, stream); } else if (i == 0) { CAS cas = stream.getCas(); AnnotationFS afs = cas.createAnnotation(cas.getAnnotationType(), delta + begin, delta + end); ruleMatch.addMatched(0, afs); } } List<AnnotationFS> matchedAnnotationsOfRoot = ruleMatch.getMatchedAnnotationsOfRoot(); if (matchedAnnotationsOfRoot != null && !matchedAnnotationsOfRoot.isEmpty()) { ruleApply.add(ruleMatch, stream); } } crowd.endVisit(this, ruleApply); return ruleApply; } }
RegExpRule extends AbstractRule { @Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { RuleApply ruleApply = new RuleApply(this, false); crowd.beginVisit(this, ruleApply); MatchContext context = new MatchContext(getParent()); String regexpString = regexpExpr.getStringValue(context, stream); if (regexpString == null) { crowd.endVisit(this, ruleApply); return ruleApply; } AnnotationFS documentAnnotation = stream.getDocumentAnnotation(); String document = documentAnnotation.getCoveredText(); int delta = documentAnnotation.getBegin(); Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream); Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream); Pattern pattern = RutaPatternCache.getPattern(regexpString, false); Matcher matcher = pattern.matcher(document); int groupCount = matcher.groupCount(); while (matcher.find()) { RegExpRuleMatch ruleMatch = new RegExpRuleMatch(this); MatchResult matchResult = matcher.toMatchResult(); for (int i = 0; i <= groupCount; i++) { int begin = matchResult.start(i); int end = matchResult.end(i); List<Type> types = groupTypes.get(i); if (types != null) { createAnnotations(i, delta, begin, end, types, fa, matchResult, ruleMatch, stream); } else if (i == 0) { CAS cas = stream.getCas(); AnnotationFS afs = cas.createAnnotation(cas.getAnnotationType(), delta + begin, delta + end); ruleMatch.addMatched(0, afs); } } List<AnnotationFS> matchedAnnotationsOfRoot = ruleMatch.getMatchedAnnotationsOfRoot(); if (matchedAnnotationsOfRoot != null && !matchedAnnotationsOfRoot.isEmpty()) { ruleApply.add(ruleMatch, stream); } } crowd.endVisit(this, ruleApply); return ruleApply; } RegExpRule(AbstractStringExpression regexp,
          Map<ITypeExpression, INumberExpression> typeMap, int id, RutaBlock parent); }
RegExpRule extends AbstractRule { @Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { RuleApply ruleApply = new RuleApply(this, false); crowd.beginVisit(this, ruleApply); MatchContext context = new MatchContext(getParent()); String regexpString = regexpExpr.getStringValue(context, stream); if (regexpString == null) { crowd.endVisit(this, ruleApply); return ruleApply; } AnnotationFS documentAnnotation = stream.getDocumentAnnotation(); String document = documentAnnotation.getCoveredText(); int delta = documentAnnotation.getBegin(); Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream); Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream); Pattern pattern = RutaPatternCache.getPattern(regexpString, false); Matcher matcher = pattern.matcher(document); int groupCount = matcher.groupCount(); while (matcher.find()) { RegExpRuleMatch ruleMatch = new RegExpRuleMatch(this); MatchResult matchResult = matcher.toMatchResult(); for (int i = 0; i <= groupCount; i++) { int begin = matchResult.start(i); int end = matchResult.end(i); List<Type> types = groupTypes.get(i); if (types != null) { createAnnotations(i, delta, begin, end, types, fa, matchResult, ruleMatch, stream); } else if (i == 0) { CAS cas = stream.getCas(); AnnotationFS afs = cas.createAnnotation(cas.getAnnotationType(), delta + begin, delta + end); ruleMatch.addMatched(0, afs); } } List<AnnotationFS> matchedAnnotationsOfRoot = ruleMatch.getMatchedAnnotationsOfRoot(); if (matchedAnnotationsOfRoot != null && !matchedAnnotationsOfRoot.isEmpty()) { ruleApply.add(ruleMatch, stream); } } crowd.endVisit(this, ruleApply); return ruleApply; } RegExpRule(AbstractStringExpression regexp,
          Map<ITypeExpression, INumberExpression> typeMap, int id, RutaBlock parent); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd); @Override RutaEnvironment getEnvironment(); Map<ITypeExpression, INumberExpression> getTypeMap(); void setTypeMap(Map<ITypeExpression, INumberExpression> typeMap); IStringExpression getRegExp(); void setRegExp(IStringExpression regexp); void setFeatureAssignments(
          Map<ITypeExpression, Map<IStringExpression, IRutaExpression>> fa); Map<ITypeExpression, Map<IStringExpression, IRutaExpression>> getFeatureAssignments(); }
RegExpRule extends AbstractRule { @Override public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) { RuleApply ruleApply = new RuleApply(this, false); crowd.beginVisit(this, ruleApply); MatchContext context = new MatchContext(getParent()); String regexpString = regexpExpr.getStringValue(context, stream); if (regexpString == null) { crowd.endVisit(this, ruleApply); return ruleApply; } AnnotationFS documentAnnotation = stream.getDocumentAnnotation(); String document = documentAnnotation.getCoveredText(); int delta = documentAnnotation.getBegin(); Map<Integer, List<Type>> groupTypes = getGroup2Types(context, stream); Map<Integer, Map<Type, Map<String, Object>>> fa = getFeatureAssignmentMap(stream); Pattern pattern = RutaPatternCache.getPattern(regexpString, false); Matcher matcher = pattern.matcher(document); int groupCount = matcher.groupCount(); while (matcher.find()) { RegExpRuleMatch ruleMatch = new RegExpRuleMatch(this); MatchResult matchResult = matcher.toMatchResult(); for (int i = 0; i <= groupCount; i++) { int begin = matchResult.start(i); int end = matchResult.end(i); List<Type> types = groupTypes.get(i); if (types != null) { createAnnotations(i, delta, begin, end, types, fa, matchResult, ruleMatch, stream); } else if (i == 0) { CAS cas = stream.getCas(); AnnotationFS afs = cas.createAnnotation(cas.getAnnotationType(), delta + begin, delta + end); ruleMatch.addMatched(0, afs); } } List<AnnotationFS> matchedAnnotationsOfRoot = ruleMatch.getMatchedAnnotationsOfRoot(); if (matchedAnnotationsOfRoot != null && !matchedAnnotationsOfRoot.isEmpty()) { ruleApply.add(ruleMatch, stream); } } crowd.endVisit(this, ruleApply); return ruleApply; } RegExpRule(AbstractStringExpression regexp,
          Map<ITypeExpression, INumberExpression> typeMap, int id, RutaBlock parent); @Override ScriptApply apply(RutaStream stream, InferenceCrowd crowd); @Override RutaEnvironment getEnvironment(); Map<ITypeExpression, INumberExpression> getTypeMap(); void setTypeMap(Map<ITypeExpression, INumberExpression> typeMap); IStringExpression getRegExp(); void setRegExp(IStringExpression regexp); void setFeatureAssignments(
          Map<ITypeExpression, Map<IStringExpression, IRutaExpression>> fa); Map<ITypeExpression, Map<IStringExpression, IRutaExpression>> getFeatureAssignments();  }