You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2019/11/18 12:19:33 UTC
svn commit: r1869967 [6/12] - in /uima/uv3/ruta-v3/trunk: ./
example-projects/ExampleProject/descriptor/
example-projects/ExampleProject/descriptor/uima/ruta/example/
example-projects/ExtensionsExample/ example-projects/GermanNovels/
example-projects/M...
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java Mon Nov 18 12:19:31 2019
@@ -20,6 +20,7 @@
package org.apache.uima.ruta.rule;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -37,6 +38,8 @@ import org.apache.uima.ruta.visitor.Infe
public abstract class AbstractRuleElement extends RutaElement implements RuleElement {
+ protected static final InferenceCrowd EMPTY_CROWD = new InferenceCrowd(Collections.emptyList());
+
protected RuleElementQuantifier quantifier;
protected List<AbstractRutaCondition> conditions;
@@ -51,9 +54,6 @@ public abstract class AbstractRuleElemen
protected RutaBlock parent;
- @SuppressWarnings("unchecked")
- protected final InferenceCrowd emptyCrowd = new InferenceCrowd(Collections.EMPTY_LIST);
-
protected List<List<RutaStatement>> inlinedConditionRuleBlocks = new ArrayList<>();
protected List<List<RutaStatement>> inlinedActionRuleBlocks = new ArrayList<>();
@@ -78,29 +78,98 @@ public abstract class AbstractRuleElemen
}
}
+ @Override
+ public List<RuleMatch> continueSideStep(boolean after, RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream,
+ InferenceCrowd crowd) {
+ List<RuleMatch> result = new ArrayList<RuleMatch>();
+ boolean newDirection = !after;
+ List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOfElement(this);
+ AnnotationFS annotation = null;
+ if (!matchedAnnotationsOf.isEmpty()) {
+ if (newDirection) {
+ annotation = matchedAnnotationsOf.get(matchedAnnotationsOf.size() - 1);
+ } else {
+ annotation = matchedAnnotationsOf.get(0);
+ }
+ ComposedRuleElementMatch sideStepContainerMatch = containerMatch;
+ if (!containerMatch.getRuleElement().equals(getContainer())) {
+ List<List<RuleElementMatch>> matchInfo = ruleMatch
+ .getMatchInfo((ComposedRuleElement) getContainer());
+ if (newDirection) {
+ List<RuleElementMatch> list = matchInfo.get(matchInfo.size() - 1);
+ sideStepContainerMatch = (ComposedRuleElementMatch) list.get(list.size() - 1);
+ } else {
+ List<RuleElementMatch> list = matchInfo.get(0);
+ sideStepContainerMatch = (ComposedRuleElementMatch) list.get(0);
+ }
+ }
+ MatchContext context = new MatchContext(this, ruleMatch, newDirection);
+ if (quantifier.continueMatch(newDirection, context, annotation, sideStepContainerMatch,
+ stream, crowd)) {
+ continueMatch(newDirection, annotation, ruleMatch, ruleApply, sideStepContainerMatch, null,
+ entryPoint, stream, crowd);
+ } else {
+ RuleElement nextRuleElement = getContainer().getNextElement(newDirection, this);
+ if (nextRuleElement != null) {
+ result = nextRuleElement.continueMatch(newDirection, annotation, ruleMatch, ruleApply,
+ sideStepContainerMatch, null, null, stream, crowd);
+ } else if (getContainer() instanceof ComposedRuleElement) {
+ ComposedRuleElement composed = (ComposedRuleElement) getContainer();
+ result = composed.fallbackContinue(newDirection, false, annotation, ruleMatch, ruleApply,
+ sideStepContainerMatch, null, entryPoint, stream, crowd);
+ }
+ }
+ }
+ return result;
+ }
+
protected void doneMatching(RuleMatch ruleMatch, RuleApply ruleApply, RutaStream stream,
InferenceCrowd crowd) {
if (!ruleMatch.isApplied()) {
ruleApply.add(ruleMatch, stream);
+ RutaRule rule = ruleMatch.getRule();
+ Collection<String> localVariables = rule.getLabels();
if (ruleMatch.matchedCompletely()) {
- ruleMatch.getRule().getRoot().applyRuleElements(ruleMatch, stream, crowd);
+ rule.getEnvironment().acceptTempVariableValues(localVariables);
+ rule.getRoot().applyRuleElements(ruleMatch, stream, crowd);
+ } else {
+ rule.getEnvironment().clearTempVariables(localVariables);
}
ruleMatch.setApplied(true);
}
}
- protected List<List<ScriptApply>> processInlinedActionRules(RuleMatch ruleMatch, RutaStream stream,
+ protected void processInlinedActionRules(RuleMatch ruleMatch, RutaStream stream,
InferenceCrowd crowd) {
+ List<List<RuleElementMatch>> matchInfo = ruleMatch.getMatchInfo(this);
+ // TODO: which rule element match should be used? all? should context matter?
+ if (matchInfo == null || matchInfo.isEmpty() || matchInfo.get(0) == null
+ || matchInfo.get(0).isEmpty()) {
+ return;
+ }
+ RuleElementMatch ruleElementMatch = matchInfo.get(0).get(0);
+ processInlinedActionRules(ruleMatch, ruleElementMatch, stream, crowd);
+ }
+
+ protected List<List<ScriptApply>> processInlinedActionRules(RuleMatch ruleMatch,
+ RuleElementMatch ruleElementMatch, RutaStream stream, InferenceCrowd crowd) {
if (inlinedActionRuleBlocks != null && !inlinedActionRuleBlocks.isEmpty()) {
- return processInlinedRules(inlinedActionRuleBlocks, ruleMatch, stream, crowd);
+ List<List<ScriptApply>> inlinedBlocksApplies = processInlinedRules(inlinedActionRuleBlocks,
+ ruleMatch, stream, crowd);
+ ruleElementMatch.setInlinedActionRules(inlinedBlocksApplies);
+ return inlinedBlocksApplies;
}
return null;
}
- protected List<List<ScriptApply>> processInlinedConditionRules(RuleMatch ruleMatch, RutaStream stream,
- InferenceCrowd crowd) {
+ protected List<List<ScriptApply>> processInlinedConditionRules(RuleMatch ruleMatch,
+ RuleElementMatch ruleElementMatch, RutaStream stream, InferenceCrowd crowd) {
if (inlinedConditionRuleBlocks != null && !inlinedConditionRuleBlocks.isEmpty()) {
- return processInlinedRules(inlinedConditionRuleBlocks, ruleMatch, stream, crowd);
+ List<List<ScriptApply>> inlinedBlocksApplies = processInlinedRules(inlinedConditionRuleBlocks,
+ ruleMatch, stream, crowd);
+ ruleElementMatch.setInlinedConditionRules(inlinedBlocksApplies);
+ return inlinedBlocksApplies;
}
return null;
}
@@ -108,20 +177,14 @@ public abstract class AbstractRuleElemen
protected List<List<ScriptApply>> processInlinedRules(List<List<RutaStatement>> inlinedRuleBlocks,
RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
List<List<ScriptApply>> result = new ArrayList<>();
+
List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOfElement(this);
- // TODO where to implement the explanation of inlined rules?
- // BlockApply blockApply = new BlockApply(this);
- // RuleApply dummyRuleApply = getDummyRuleApply(ruleMatch);
- // blockApply.setRuleApply(dummyRuleApply);
- // ruleMatch.addDelegateApply(this, blockApply);
for (AnnotationFS annotationFS : matchedAnnotationsOf) {
RutaStream windowStream = stream.getWindowStream(annotationFS, annotationFS.getType());
for (List<RutaStatement> inlinedRules : inlinedRuleBlocks) {
List<ScriptApply> blockResult = new ArrayList<>();
for (RutaStatement each : inlinedRules) {
ScriptApply apply = each.apply(windowStream, crowd);
- // blockApply.add(apply);
- ruleMatch.addDelegateApply(this, apply);
blockResult.add(apply);
}
result.add(blockResult);
@@ -137,16 +200,19 @@ public abstract class AbstractRuleElemen
action.execute(new MatchContext(this, ruleMatch), stream, crowd);
crowd.endVisit(action, null);
}
+
processInlinedActionRules(ruleMatch, stream, crowd);
}
- protected boolean matchInnerRules(RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd) {
+ protected boolean matchInlinedRules(RuleMatch ruleMatch, RuleElementMatch ruleElementMatch,
+ RutaStream stream, InferenceCrowd crowd) {
- List<List<ScriptApply>> blockResults = processInlinedConditionRules(ruleMatch, stream, crowd);
+ List<List<ScriptApply>> blockResults = processInlinedConditionRules(ruleMatch, ruleElementMatch,
+ stream, crowd);
if (blockResults == null) {
return true;
}
-
+
boolean matched = true;
for (List<ScriptApply> list : blockResults) {
matched &= atLeastOneRuleMatched(list);
@@ -180,7 +246,7 @@ public abstract class AbstractRuleElemen
@Override
public List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches,
MatchContext context, RutaStream stream) {
- return quantifier.evaluateMatches(matches, context, stream, emptyCrowd);
+ return quantifier.evaluateMatches(matches, context, stream, EMPTY_CROWD);
}
@Override
@@ -287,11 +353,6 @@ public abstract class AbstractRuleElemen
}
@Override
- public RutaRule getRule() {
- return container.getRule();
- }
-
- @Override
public RuleElementContainer getContainer() {
return container;
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java Mon Nov 18 12:19:31 2019
@@ -87,8 +87,10 @@ public class ComposedRuleElement extends
List<RuleMatch> startRuleMatches = each.startMatch(extendedMatch, null, composedMatch, this,
stream, crowd);
for (RuleMatch startRuleMatch : startRuleMatches) {
+
ComposedRuleElementMatch startElementMatch = (ComposedRuleElementMatch) startRuleMatch
.getLastMatch(this, true);
+
ruleMatches.put(startRuleMatch, startElementMatch);
}
}
@@ -102,19 +104,19 @@ public class ComposedRuleElement extends
MatchContext context = new MatchContext(null, this, eachRuleMatch, true);
AnnotationFS lastAnnotation = eachRuleMatch.getLastMatchedAnnotation(context, stream);
boolean failed = !eachComposedMatch.matched();
+
+ RuleElement sideStepOrigin = hasAncestor(false) ? this : null;
+
List<RuleMatch> fallbackContinue = fallbackContinue(true, failed, lastAnnotation,
- eachRuleMatch, ruleApply, eachComposedMatch, null, entryPoint, stream, crowd);
+ eachRuleMatch, ruleApply, eachComposedMatch, sideStepOrigin, entryPoint, stream,
+ crowd);
result.addAll(fallbackContinue);
}
} else if (conjunct) {
// conjunctive
Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new LinkedHashMap<RuleMatch, ComposedRuleElementMatch>();
RuleElement anchoringRuleElement = getAnchoringRuleElement(stream);
- RutaRuleElement sideStepOrigin = null;
- if (anchoringRuleElement instanceof RutaRuleElement && hasAncestor(false)) {
- sideStepOrigin = (RutaRuleElement) anchoringRuleElement;
- }
ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch,
stream);
List<RuleMatch> startRuleMatches = anchoringRuleElement.startMatch(ruleMatch, null,
@@ -151,6 +153,9 @@ public class ComposedRuleElement extends
List<AnnotationFS> textsMatched = eachComposedMatch.getTextsMatched();
if ((!stream.isGreedyAnchoring() && !stream.isOnlyOnce())
|| !earlyExit(textsMatched.get(0), ruleApply, stream)) {
+
+ RuleElement sideStepOrigin = hasAncestor(false) ? this : null;
+
List<RuleMatch> fallbackContinue = fallbackContinue(true, failed, lastAnnotation,
eachRuleMatch, ruleApply, eachComposedMatch, sideStepOrigin, entryPoint, stream,
crowd);
@@ -181,9 +186,8 @@ public class ComposedRuleElement extends
@Override
public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd) {
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
if (conjunct == null) {
// inner next sequential
@@ -359,7 +363,7 @@ public class ComposedRuleElement extends
@Override
public List<RuleMatch> continueOwnMatch(boolean after, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
if (!stream.isSimpleGreedyForComposed()) {
@@ -409,11 +413,11 @@ public class ComposedRuleElement extends
public List<RuleMatch> fallbackContinue(boolean after, boolean failed, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
RuleElementContainer container = getContainer();
- doMatch(after, containerMatch, ruleMatch, stream, crowd);
+ doMatch(after, annotation, ruleMatch, containerMatch, isStartAnchor(), stream, crowd);
if (this.equals(entryPoint) && ruleApply == null) {
result.add(ruleMatch);
} else if (container == null) {
@@ -509,7 +513,7 @@ public class ComposedRuleElement extends
private List<RuleMatch> fallback(boolean after, boolean failed, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
RuleElementContainer parentContainer = getContainer();
@@ -538,21 +542,24 @@ public class ComposedRuleElement extends
}
}
- private void doMatch(boolean after, ComposedRuleElementMatch match, RuleMatch ruleMatch,
- RutaStream stream, InferenceCrowd crowd) {
- List<AnnotationFS> textsMatched = match.getTextsMatched();
+ @Override
+ public void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch, boolean ruleAnchor, RutaStream stream,
+ InferenceCrowd crowd) {
+
+ List<AnnotationFS> textsMatched = containerMatch.getTextsMatched();
if (textsMatched == null || textsMatched.isEmpty()) {
getParent().getEnvironment().addMatchToVariable(ruleMatch, this,
new MatchContext(getParent()), stream);
- match.evaluateInnerMatches(true, stream);
+ containerMatch.evaluateInnerMatches(true, stream);
return;
}
int begin = textsMatched.get(0).getBegin();
int end = textsMatched.get(textsMatched.size() - 1).getEnd();
- AnnotationFS annotation = stream.getCas().createAnnotation(stream.getCas().getAnnotationType(),
- begin, end);
+ AnnotationFS implicitAnnotation = stream.getCas()
+ .createAnnotation(stream.getCas().getAnnotationType(), begin, end);
- MatchContext context = new MatchContext(annotation, this, ruleMatch, after);
+ MatchContext context = new MatchContext(implicitAnnotation, this, ruleMatch, after);
RutaEnvironment environment = context.getParent().getEnvironment();
environment.addMatchToVariable(ruleMatch, this, context, stream);
@@ -567,14 +574,14 @@ public class ComposedRuleElement extends
break;
}
}
- match.setConditionInfo(evaluatedConditions);
- match.evaluateInnerMatches(true, stream);
- if (match.matched()) {
- boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
- match.setInlinedRulesMatched(inlinedRulesMatched);
+ containerMatch.setConditionInfo(evaluatedConditions);
+ containerMatch.evaluateInnerMatches(true, stream);
+ if (containerMatch.matched()) {
+ boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, containerMatch, stream, crowd);
+ containerMatch.setInlinedRulesMatched(inlinedRulesMatched);
} else {
// update label for failed match after evaluating conditions
- environment.addAnnotationsToVariable(null, getLabel(), context);
+ environment.removeVariableValue(getLabel(), context);
}
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ConjunctRulesRuleElement.java Mon Nov 18 12:19:31 2019
@@ -82,16 +82,15 @@ public class ConjunctRulesRuleElement ex
@Override
public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd) {
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
throw new UnsupportedOperationException();
}
@Override
public List<RuleMatch> continueOwnMatch(boolean after, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
throw new UnsupportedOperationException();
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElement.java Mon Nov 18 12:19:31 2019
@@ -40,13 +40,19 @@ public interface RuleElement {
InferenceCrowd crowd);
List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd);
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd);
List<RuleMatch> continueOwnMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd);
+
+ List<RuleMatch> continueSideStep(boolean after, RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream,
+ InferenceCrowd crowd);
+
+ void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch, boolean ruleAnchor, RutaStream stream,
InferenceCrowd crowd);
List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches, MatchContext context,
@@ -56,8 +62,6 @@ public interface RuleElement {
RutaBlock getParent();
- RutaRule getRule();
-
RuleElementContainer getContainer();
void setContainer(RuleElementContainer container);
@@ -85,9 +89,9 @@ public interface RuleElement {
void setLabel(String label);
String getLabel();
-
+
List<List<RutaStatement>> getInlinedConditionRuleBlocks();
-
+
List<List<RutaStatement>> getInlinedActionRuleBlocks();
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementCaretaker.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementCaretaker.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementCaretaker.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementCaretaker.java Mon Nov 18 12:19:31 2019
@@ -69,12 +69,13 @@ public class RuleElementCaretaker implem
for (RuleElement ruleElement : ruleElements) {
if (ruleElement.isStartAnchor()) {
return ruleElement;
- }
- if(ruleElement instanceof ComposedRuleElement) {
- RuleElement anchoringRuleElement = ((ComposedRuleElement) ruleElement).getAnchoringRuleElement(stream);
- if(anchoringRuleElement.isStartAnchor()) {
- return ruleElement;
- }
+ }
+ if (ruleElement instanceof ComposedRuleElement) {
+ RuleElement anchoringRuleElement = ((ComposedRuleElement) ruleElement)
+ .getAnchoringRuleElement(stream);
+ if (anchoringRuleElement.isStartAnchor()) {
+ return ruleElement;
+ }
}
}
@@ -123,11 +124,6 @@ public class RuleElementCaretaker implem
}
@Override
- public RutaRule getRule() {
- return container.getRule();
- }
-
- @Override
public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
if (after) {
return getElementAfter(ruleElement);
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementContainer.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementContainer.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementContainer.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementContainer.java Mon Nov 18 12:19:31 2019
@@ -28,10 +28,6 @@ public interface RuleElementContainer {
List<RuleElement> getRuleElements();
- // RuleElement getElementAfter(RuleElement element);
- //
- // RuleElement getElementBefore(RuleElement element);
-
RuleElement getFirstElement();
RuleElement getLastElement();
@@ -40,8 +36,6 @@ public interface RuleElementContainer {
void applyRuleElements(RuleMatch ruleMatch, RutaStream stream, InferenceCrowd crowd);
- RutaRule getRule();
-
RuleElement getNextElement(boolean after, RuleElement ruleElement);
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementIsolator.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementIsolator.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementIsolator.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementIsolator.java Mon Nov 18 12:19:31 2019
@@ -75,11 +75,6 @@ public class RuleElementIsolator impleme
}
@Override
- public RutaRule getRule() {
- return container.getRule();
- }
-
- @Override
public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
return null;
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleElementMatch.java Mon Nov 18 12:19:31 2019
@@ -24,6 +24,7 @@ import java.util.List;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.ScriptApply;
public class RuleElementMatch {
@@ -43,6 +44,10 @@ public class RuleElementMatch {
private boolean ruleAnchor = false;
+ private List<List<ScriptApply>> inlinedActionRules;
+
+ private List<List<ScriptApply>> inlinedConditionRules;
+
public RuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
super();
this.ruleElement = ruleElement;
@@ -54,8 +59,7 @@ public class RuleElementMatch {
return ruleElement.getLabel();
}
- public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts,
- RutaStream stream) {
+ public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts, RutaStream stream) {
baseConditionMatched = baseCondition;
textsMatched = texts;
conditionsMatched = baseConditionMatched;
@@ -63,7 +67,7 @@ public class RuleElementMatch {
containerMatch.addInnerMatch(ruleElement, this, stream);
}
}
-
+
public void setConditionInfo(boolean baseCondition, List<EvaluatedCondition> conditionList) {
baseConditionMatched = baseCondition;
conditions = conditionList;
@@ -74,7 +78,6 @@ public class RuleElementMatch {
}
}
}
-
public boolean matched() {
return baseConditionMatched && conditionsMatched && inlinedRulesMatched;
@@ -159,4 +162,22 @@ public class RuleElementMatch {
public void setInlinedRulesMatched(boolean inlinedRulesMatched) {
this.inlinedRulesMatched = inlinedRulesMatched;
}
+
+ public void setInlinedActionRules(List<List<ScriptApply>> inlinedBlocksApplies) {
+ this.inlinedActionRules = inlinedBlocksApplies;
+ }
+
+ public void setInlinedConditionRules(List<List<ScriptApply>> inlinedBlocksApplies) {
+ this.inlinedConditionRules = inlinedBlocksApplies;
+
+ }
+
+ public List<List<ScriptApply>> getInlinedActionRules() {
+ return this.inlinedActionRules;
+ }
+
+ public List<List<ScriptApply>> getInlinedConditionRules() {
+ return this.inlinedConditionRules;
+
+ }
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java Mon Nov 18 12:19:31 2019
@@ -53,11 +53,15 @@ public class RutaAnnotationTypeMatcher i
// just for forcing expression top initialize
// TODO this is maybe a bit expensive sometimes
expression.getType(context, stream);
- if (expression.getAnnotationExpression() != null) {
+ if (expression.getAnnotationListExpression() != null) {
+ result = expression.getAnnotationList(context, stream);
+ if (result == null) {
+ // avoid null lists here
+ result = Collections.emptyList();
+ }
+ } else if (expression.getAnnotationExpression() != null) {
result = new ArrayList<>(1);
result.add(expression.getAnnotation(context, stream));
- } else if (expression.getAnnotationListExpression() != null) {
- result = expression.getAnnotationList(context, stream);
} else {
// TODO defer to getter of expression?
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java Mon Nov 18 12:19:31 2019
@@ -20,9 +20,13 @@
package org.apache.uima.ruta.rule;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.ruta.RutaStream;
@@ -43,18 +47,35 @@ public class RutaLiteralMatcher implemen
public List<AnnotationFS> getMatchingAnnotations(RutaBlock parent, RutaStream stream) {
List<AnnotationFS> result = new ArrayList<AnnotationFS>();
AnnotationFS windowAnnotation = stream.getDocumentAnnotation();
- List<RutaBasic> list = stream.getBasicsInWindow(windowAnnotation);
- // TODO improve matching on literal strings
- for (RutaBasic each : list) {
- MatchContext context = new MatchContext(each, null, null, true);
- context.setParent(parent);
- if (each.getCoveredText().equals(expression.getStringValue(context, stream))) {
- result.add(each);
+
+ MatchContext context = new MatchContext(parent);
+ String text = windowAnnotation.getCoveredText();
+ String literal = expression.getStringValue(context, stream);
+
+ int indexOf = 0;
+ while ((indexOf = text.indexOf(literal, indexOf)) >= 0) {
+
+ int begin = indexOf + windowAnnotation.getBegin();
+ int end = begin + literal.length();
+ indexOf = end;
+
+ AnnotationFS annotation = getAnnotation(begin, end, stream);
+ if (stream.isVisible(annotation)) {
+ result.add(annotation);
}
}
return result;
}
+ private AnnotationFS getAnnotation(int begin, int end, RutaStream stream) {
+ RutaBasic beginAnchor = stream.getBeginAnchor(begin);
+ if (beginAnchor.getEnd() == end) {
+ return beginAnchor;
+ }
+ CAS cas = stream.getCas();
+ return cas.createAnnotation(cas.getAnnotationType(), begin, end);
+ }
+
@Override
public String toString() {
return "\"" + expression.toString() + "\"";
@@ -73,29 +94,64 @@ public class RutaLiteralMatcher implemen
@Override
public Collection<AnnotationFS> getAnnotationsAfter(RutaRuleElement ruleElement,
AnnotationFS annotation, RutaBlock parent, RutaStream stream) {
- return getNextAnnotations(false, annotation, stream, parent);
+
+ RutaBasic basicNextTo = stream.getBasicNextTo(false, annotation);
+ if (basicNextTo == null) {
+ return Collections.emptyList();
+ }
+
+ MatchContext context = new MatchContext(parent);
+ String literal = expression.getStringValue(context, stream);
+
+ if (!StringUtils.startsWith(literal, basicNextTo.getCoveredText())) {
+ return Collections.emptyList();
+ }
+ AnnotationFS windowAnnotation = stream.getDocumentAnnotation();
+ int begin = basicNextTo.getBegin();
+ int end = begin + literal.length();
+ if (begin < windowAnnotation.getBegin() || end > windowAnnotation.getEnd()) {
+ return Collections.emptyList();
+ }
+
+ String substring = stream.getCas().getDocumentText().substring(begin, end);
+ if (StringUtils.equals(literal, substring)) {
+ AnnotationFS matchedAnnotation = getAnnotation(begin, end, stream);
+ if (stream.isVisible(matchedAnnotation)) {
+ return Arrays.asList(matchedAnnotation);
+ }
+ }
+ return Collections.emptyList();
}
@Override
public Collection<AnnotationFS> getAnnotationsBefore(RutaRuleElement ruleElement,
AnnotationFS annotation, RutaBlock parent, RutaStream stream) {
- return getNextAnnotations(true, annotation, stream, parent);
- }
-
- private Collection<AnnotationFS> getNextAnnotations(boolean before, AnnotationFS annotation,
- RutaStream stream, RutaBlock parent) {
- List<AnnotationFS> result = new ArrayList<AnnotationFS>(1);
- RutaBasic basicNextTo = stream.getBasicNextTo(before, annotation);
+ RutaBasic basicNextTo = stream.getBasicNextTo(true, annotation);
if (basicNextTo == null) {
- return result;
+ return Collections.emptyList();
+ }
+
+ MatchContext context = new MatchContext(parent);
+ String literal = expression.getStringValue(context, stream);
+
+ if (!StringUtils.endsWith(literal, basicNextTo.getCoveredText())) {
+ return Collections.emptyList();
}
- MatchContext context = new MatchContext(annotation, null, null, !before);
- context.setParent(parent);
- String stringValue = expression.getStringValue(context, stream);
- if (stringValue != null && stringValue.equals(basicNextTo.getCoveredText())) {
- result.add(basicNextTo);
+ AnnotationFS windowAnnotation = stream.getDocumentAnnotation();
+ int begin = basicNextTo.getEnd() - literal.length();
+ int end = basicNextTo.getEnd();
+ if (begin < windowAnnotation.getBegin() || end > windowAnnotation.getEnd()) {
+ return Collections.emptyList();
}
- return result;
+
+ String substring = stream.getCas().getDocumentText().substring(begin, end);
+ if (StringUtils.equals(literal, substring)) {
+ AnnotationFS matchedAnnotation = getAnnotation(begin, end, stream);
+ if (stream.isVisible(matchedAnnotation)) {
+ return Arrays.asList(matchedAnnotation);
+ }
+ }
+ return Collections.emptyList();
}
@Override
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java Mon Nov 18 12:19:31 2019
@@ -57,7 +57,7 @@ public class RutaOptionalRuleElement ext
}
@Override
- protected void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ public void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
ComposedRuleElementMatch containerMatch, boolean ruleAnchor, RutaStream stream,
InferenceCrowd crowd) {
RuleElementMatch result = new RuleElementMatch(this, containerMatch);
@@ -88,11 +88,11 @@ public class RutaOptionalRuleElement ext
}
result.setConditionInfo(base, evaluatedConditions);
if (result.matched()) {
- boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+ boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
result.setInlinedRulesMatched(inlinedRulesMatched);
} else {
// update label for failed match after evaluating conditions
- environment.addAnnotationsToVariable(null, getLabel(), context);
+ environment.removeVariableValue(getLabel(), context);
}
ruleMatch.setMatched(ruleMatch.matched() && result.matched());
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRule.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRule.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRule.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRule.java Mon Nov 18 12:19:31 2019
@@ -19,6 +19,7 @@
package org.apache.uima.ruta.rule;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -188,4 +189,8 @@ public class RutaRule extends AbstractRu
return root;
}
+ public Collection<String> getLabels() {
+ return labels.keySet();
+ }
+
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java Mon Nov 18 12:19:31 2019
@@ -127,7 +127,7 @@ public class RutaRuleElement extends Abs
@Override
public List<RuleMatch> continueOwnMatch(boolean after, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
MatchContext context = new MatchContext(this, ruleMatch, after);
@@ -187,7 +187,7 @@ public class RutaRuleElement extends Abs
protected List<RuleMatch> continueMatchSomewhereElse(boolean after, boolean failed,
AnnotationFS eachAnchor, RuleMatch extendedMatch, RuleApply ruleApply,
- ComposedRuleElementMatch extendedContainerMatch, RutaRuleElement sideStepOrigin,
+ ComposedRuleElementMatch extendedContainerMatch, RuleElement sideStepOrigin,
RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
RuleElement nextRuleElement = getContainer().getNextElement(after, this);
@@ -204,9 +204,8 @@ public class RutaRuleElement extends Abs
@Override
public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd) {
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
// if() for really lazy quantifiers
MatchContext context = new MatchContext(this, ruleMatch, after);
@@ -271,7 +270,7 @@ public class RutaRuleElement extends Abs
protected List<RuleMatch> stepbackMatch(boolean after, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd,
+ RuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd,
RuleElement entryPoint) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
if (this.equals(entryPoint) && ruleApply == null) {
@@ -327,52 +326,8 @@ public class RutaRuleElement extends Abs
return result;
}
- public List<RuleMatch> continueSideStep(boolean after, RuleMatch ruleMatch, RuleApply ruleApply,
- ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd) {
- List<RuleMatch> result = new ArrayList<RuleMatch>();
- boolean newDirection = !after;
- List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOfElement(this);
- AnnotationFS annotation = null;
- if (!matchedAnnotationsOf.isEmpty()) {
- if (newDirection) {
- annotation = matchedAnnotationsOf.get(matchedAnnotationsOf.size() - 1);
- } else {
- annotation = matchedAnnotationsOf.get(0);
- }
- ComposedRuleElementMatch sideStepContainerMatch = containerMatch;
- if (!containerMatch.getRuleElement().equals(getContainer())) {
- List<List<RuleElementMatch>> matchInfo = ruleMatch
- .getMatchInfo((ComposedRuleElement) getContainer());
- if (newDirection) {
- List<RuleElementMatch> list = matchInfo.get(matchInfo.size() - 1);
- sideStepContainerMatch = (ComposedRuleElementMatch) list.get(list.size() - 1);
- } else {
- List<RuleElementMatch> list = matchInfo.get(0);
- sideStepContainerMatch = (ComposedRuleElementMatch) list.get(0);
- }
- }
- MatchContext context = new MatchContext(this, ruleMatch, newDirection);
- if (quantifier.continueMatch(newDirection, context, annotation, sideStepContainerMatch,
- stream, crowd)) {
- continueMatch(newDirection, annotation, ruleMatch, ruleApply, sideStepContainerMatch, null,
- entryPoint, stream, crowd);
- } else {
- RuleElement nextRuleElement = getContainer().getNextElement(newDirection, this);
- if (nextRuleElement != null) {
- result = nextRuleElement.continueMatch(newDirection, annotation, ruleMatch, ruleApply,
- sideStepContainerMatch, null, null, stream, crowd);
- } else if (getContainer() instanceof ComposedRuleElement) {
- ComposedRuleElement composed = (ComposedRuleElement) getContainer();
- result = composed.fallbackContinue(newDirection, false, annotation, ruleMatch, ruleApply,
- sideStepContainerMatch, null, entryPoint, stream, crowd);
- }
- }
- }
- return result;
- }
-
- protected void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ @Override
+ public void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
ComposedRuleElementMatch containerMatch, boolean ruleAnchor, RutaStream stream,
InferenceCrowd crowd) {
RuleElementMatch result = new RuleElementMatch(this, containerMatch);
@@ -404,11 +359,11 @@ public class RutaRuleElement extends Abs
}
result.setConditionInfo(base, evaluatedConditions);
if (result.matched()) {
- boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+ boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
result.setInlinedRulesMatched(inlinedRulesMatched);
} else {
// update label for failed match after evaluating conditions
- environment.addAnnotationsToVariable(null, getLabel(), context);
+ environment.removeVariableValue(getLabel(), context);
}
ruleMatch.setMatched(ruleMatch.matched() && result.matched());
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java Mon Nov 18 12:19:31 2019
@@ -24,6 +24,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.FSIterator;
@@ -60,11 +62,19 @@ public class WildCardRuleElement extends
@Override
public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
- RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
- InferenceCrowd crowd) {
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin,
+ RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
+ Pair<RuleElement, Integer> next = getNextRuleElement(after, this);
+ RuleElement nextElement = next.getLeft();
+ int nextDepth = next.getRight().intValue();
+ List<RuleMatch> result = tryWithNextRuleElement(nextElement, after, annotation, ruleMatch,
+ ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+ return result;
+ }
+
+ private Pair<RuleElement, Integer> getNextRuleElement(boolean after, RuleElement element) {
RuleElement nextElement = null;
- RuleElement current = this;
+ RuleElement current = element;
int nextDepth = -1;
while (nextElement == null && current != null && current.getContainer() != null) {
RuleElementContainer container = current.getContainer();
@@ -79,20 +89,18 @@ public class WildCardRuleElement extends
if (nextElement == null) {
nextDepth = 0;
}
- List<RuleMatch> result = tryWithNextRuleElement(nextElement, after, annotation, ruleMatch,
- ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
- return result;
+ return new ImmutablePair<RuleElement, Integer>(nextElement, Integer.valueOf(nextDepth));
}
private List<RuleMatch> tryWithNextRuleElement(RuleElement nextElement, boolean after,
AnnotationFS annotation, RuleMatch ruleMatch, RuleApply ruleApply,
- ComposedRuleElementMatch containerMatch, int nextDepth, RutaRuleElement sideStepOrigin,
+ ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin,
RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
// what is the next stuff that should match?
if (nextElement == null) {
AnnotationFS afs = getCoveredByWildCard(after, annotation, null, stream);
- doMatch(afs, ruleMatch, containerMatch, annotation == null, stream, crowd);
+ doMatch(after, afs, ruleMatch, containerMatch, annotation == null, stream, crowd);
ComposedRuleElement composed = (ComposedRuleElement) getContainer();
// [Peter] was ruleMatch.matched(), but it did not fail when matches?!
result = composed.fallbackContinue(after, !ruleMatch.matched(), afs, ruleMatch, ruleApply,
@@ -123,7 +131,7 @@ public class WildCardRuleElement extends
private List<RuleMatch> tryWithNextComposed(boolean after, AnnotationFS annotation,
ComposedRuleElement cre, RuleMatch ruleMatch, RuleApply ruleApply,
- ComposedRuleElementMatch containerMatch, int nextDepth, RutaRuleElement sideStepOrigin,
+ ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin,
RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
AnnotationFS nextOne = annotation;
@@ -135,8 +143,10 @@ public class WildCardRuleElement extends
ComposedRuleElementMatch extendedContainerMatch = containerMatch.copy();
RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, after);
AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, nextOne, stream);
- doMatch(coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null, stream,
- crowd);
+ doMatch(after, coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null,
+ stream, crowd);
+ doMatchPotentialParentElements(after, annotation, nextDepth, extendedMatch,
+ extendedContainerMatch, coveredByWildCard, stream, crowd);
if (extendedMatch.matched()) {
ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
extendedContainerMatch, nextDepth);
@@ -295,7 +305,7 @@ public class WildCardRuleElement extends
private List<RuleMatch> tryWithNextAnnotationType(boolean after, AnnotationFS annotation,
RuleElement nextElement, Type defaultType, RuleMatch ruleMatch, RuleApply ruleApply,
- ComposedRuleElementMatch containerMatch, int nextDepth, RutaRuleElement sideStepOrigin,
+ ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin,
RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
FSIterator<AnnotationFS> iterator = getIterator(after, annotation, nextElement, defaultType,
@@ -311,8 +321,13 @@ public class WildCardRuleElement extends
// optional did not match -> match complete window/document
// TODO refactor
- AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, null, stream);
- doMatch(coveredByWildCard, ruleMatch, containerMatch, annotation == null, stream, crowd);
+ AnnotationFS nextAnchor = getNextAnchor(after, annotation, nextElement, ruleMatch,
+ containerMatch, sideStepOrigin, stream, crowd);
+
+ AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, nextAnchor,
+ stream);
+ doMatch(after, coveredByWildCard, ruleMatch, containerMatch, annotation == null, stream,
+ crowd);
if (ruleMatch.matched()) {
ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
containerMatch, nextDepth);
@@ -330,8 +345,12 @@ public class WildCardRuleElement extends
}
} else {
- result = cre.fallbackContinue(after, true, annotation, ruleMatch, ruleApply,
- containerMatch, sideStepOrigin, entryPoint, stream, crowd);
+ if (ruleApply == null && entryPoint != null && entryPoint.equals(nextElement)) {
+ return result;
+ } else {
+ result = cre.fallbackContinue(after, true, annotation, ruleMatch, ruleApply,
+ containerMatch, sideStepOrigin, entryPoint, stream, crowd);
+ }
}
}
return result;
@@ -349,8 +368,12 @@ public class WildCardRuleElement extends
RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, after);
AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, nextOne, stream);
- doMatch(coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null, stream,
- crowd);
+ doMatch(after, coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null,
+ stream, crowd);
+
+ doMatchPotentialParentElements(after, annotation, nextDepth, extendedMatch,
+ extendedContainerMatch, coveredByWildCard, stream, crowd);
+
if (extendedMatch.matched()) {
ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
extendedContainerMatch, nextDepth);
@@ -379,6 +402,48 @@ public class WildCardRuleElement extends
return result;
}
+ private void doMatchPotentialParentElements(boolean after, AnnotationFS annotation, int nextDepth,
+ RuleMatch extendedMatch, ComposedRuleElementMatch extendedContainerMatch,
+ AnnotationFS coveredByWildCard, RutaStream stream, InferenceCrowd crowd) {
+ RuleElement element = this;
+ ComposedRuleElementMatch containerMatch = extendedContainerMatch.getContainerMatch();
+ for (int i = nextDepth; i > 0; i--) {
+ // UIMA-6041: also doMatch for container conditions like (A #){XYZ} B;
+ RuleElementContainer c = element.getContainer();
+ if (c instanceof ComposedRuleElement) {
+ ComposedRuleElement cre = (ComposedRuleElement) c;
+ cre.doMatch(after, coveredByWildCard, extendedMatch,
+ extendedContainerMatch.getContainerMatch(), annotation == null, stream, crowd);
+ element = cre;
+ containerMatch = containerMatch.getContainerMatch();
+ }
+ }
+ }
+
+ private AnnotationFS getNextAnchor(boolean after, AnnotationFS annotation,
+ RuleElement nextElement, RuleMatch ruleMatch, ComposedRuleElementMatch containerMatch,
+ RuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd) {
+ AnnotationFS nextAnchor = null;
+ Pair<RuleElement, Integer> nextNext = getNextRuleElement(after, nextElement);
+ if (nextNext != null && nextNext.getLeft() != null) {
+ List<RuleMatch> tryWithNextNextRuleElement = tryWithNextRuleElement(nextNext.getLeft(), after,
+ annotation, ruleMatch, null, containerMatch, nextNext.getRight().intValue(),
+ sideStepOrigin, nextNext.getLeft(), stream, crowd);
+ for (RuleMatch eachNextRuleMatch : tryWithNextNextRuleElement) {
+ if (eachNextRuleMatch.matched()) {
+ List<AnnotationFS> matchedAnnotationsOfElement = eachNextRuleMatch
+ .getMatchedAnnotationsOfElement(nextNext.getLeft());
+ if (matchedAnnotationsOfElement != null && !matchedAnnotationsOfElement.isEmpty()) {
+ nextAnchor = after ? matchedAnnotationsOfElement.get(0)
+ : matchedAnnotationsOfElement.get(matchedAnnotationsOfElement.size() - 1);
+ break;
+ }
+ }
+ }
+ }
+ return nextAnchor;
+ }
+
private boolean nextElementDidMatch(List<RuleMatch> result, RuleElement nextElement) {
if (result == null || result.isEmpty()) {
return false;
@@ -432,7 +497,7 @@ public class WildCardRuleElement extends
Type type = matcher.getType(parent, stream);
iterator = getIteratorOfType(after, type, annotation, stream);
}
- } else {
+ } else if (matcher != null) {
// fallback
Type type = matcher.getType(parent, stream);
iterator = getIteratorOfType(after, type, annotation, stream);
@@ -535,10 +600,13 @@ public class WildCardRuleElement extends
private List<RuleMatch> tryWithNextLiteral(boolean after, AnnotationFS annotation,
RutaRuleElement nextElement, RuleMatch ruleMatch, RuleApply ruleApply,
- ComposedRuleElementMatch containerMatch, int nextDepth, RutaRuleElement sideStepOrigin,
+ ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin,
RutaStream stream, InferenceCrowd crowd) {
List<RuleMatch> result = new ArrayList<RuleMatch>();
RutaLiteralMatcher matcher = (RutaLiteralMatcher) nextElement.getMatcher();
+ if (matcher == null) {
+ return result;
+ }
IStringExpression expression = matcher.getExpression();
MatchContext context = new MatchContext(this, ruleMatch, true);
String stringValue = expression.getStringValue(context, stream);
@@ -567,8 +635,10 @@ public class WildCardRuleElement extends
ComposedRuleElementMatch extendedContainerMatch = containerMatch.copy();
RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, after);
AnnotationFS coveredByWildCard = getCoveredByWildCard(after, annotation, anchor, stream);
- doMatch(coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null, stream,
- crowd);
+ doMatch(after, coveredByWildCard, extendedMatch, extendedContainerMatch, annotation == null,
+ stream, crowd);
+ doMatchPotentialParentElements(after, annotation, nextDepth, extendedMatch,
+ extendedContainerMatch, coveredByWildCard, stream, crowd);
if (extendedMatch.matched()) {
ComposedRuleElementMatch nextContainerMatch = getContainerMatchOfNextElement(
extendedContainerMatch, nextDepth);
@@ -668,9 +738,11 @@ public class WildCardRuleElement extends
return afs;
}
- private void doMatch(AnnotationFS annotation, RuleMatch ruleMatch,
+ @Override
+ public void doMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
ComposedRuleElementMatch containerMatch, boolean ruleAnchor, RutaStream stream,
InferenceCrowd crowd) {
+
RuleElementMatch result = new RuleElementMatch(this, containerMatch);
result.setRuleAnchor(ruleAnchor);
List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>(
@@ -699,11 +771,11 @@ public class WildCardRuleElement extends
}
result.setConditionInfo(base, evaluatedConditions);
if (result.matched()) {
- boolean inlinedRulesMatched = matchInnerRules(ruleMatch, stream, crowd);
+ boolean inlinedRulesMatched = matchInlinedRules(ruleMatch, result, stream, crowd);
result.setInlinedRulesMatched(inlinedRulesMatched);
} else {
// update label for failed match after evaluating conditions
- environment.addAnnotationsToVariable(null, getLabel(), context);
+ environment.removeVariableValue(getLabel(), context);
}
ruleMatch.setMatched(ruleMatch.matched() && result.matched());
}
@@ -711,7 +783,7 @@ public class WildCardRuleElement extends
@Override
public List<RuleMatch> continueOwnMatch(boolean after, AnnotationFS annotation,
RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
- RutaRuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
+ RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream,
InferenceCrowd crowd) {
// won't happen
return Collections.emptyList();
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/quantifier/AbstractRuleElementQuantifier.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/quantifier/AbstractRuleElementQuantifier.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/quantifier/AbstractRuleElementQuantifier.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/quantifier/AbstractRuleElementQuantifier.java Mon Nov 18 12:19:31 2019
@@ -75,7 +75,7 @@ public abstract class AbstractRuleElemen
String label = ruleElement.getLabel();
if (matches == null || matches.isEmpty()) {
- environment.addAnnotationsToVariable(null, label, context);
+ environment.removeVariableValue(label, context);
return;
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/seed/TextSeeder.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/seed/TextSeeder.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/seed/TextSeeder.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/seed/TextSeeder.java Mon Nov 18 12:19:31 2019
@@ -33,6 +33,7 @@ public class TextSeeder implements RutaA
public static final String seedType = "org.apache.uima.ruta.type.TokenSeed";
+ @Override
public Type seed(String text, CAS cas) {
Type result = null;
JCas jCas = null;
@@ -55,7 +56,8 @@ public class TextSeeder implements RutaA
try {
a = sourceLexer.yylex();
- } catch (Exception e) {
+ } catch (Throwable e) {
+ // ignore problems
}
while (a != null) {
cas.addFsToIndexes(a);
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/RutaVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/RutaVerbalizer.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/RutaVerbalizer.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/RutaVerbalizer.java Mon Nov 18 12:19:31 2019
@@ -26,6 +26,7 @@ import java.util.Map;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Type;
import org.apache.uima.ruta.RutaElement;
+import org.apache.uima.ruta.ScriptApply;
import org.apache.uima.ruta.action.AbstractRutaAction;
import org.apache.uima.ruta.block.RutaBlock;
import org.apache.uima.ruta.condition.AbstractRutaCondition;
@@ -67,21 +68,25 @@ public class RutaVerbalizer {
}
public String verbalize(Object element) {
- if (externalVerbalizers.keySet().contains(element.getClass()) && element instanceof RutaElement) {
- return externalVerbalizers.get(element.getClass()).verbalize((RutaElement) element, this);
- } else if (element instanceof AbstractRutaAction) {
- return actionVerbalizer.verbalize((AbstractRutaAction) element);
- } else if (element instanceof AbstractRutaCondition) {
- return conditionVerbalizer.verbalize((AbstractRutaCondition) element);
- } else if (element instanceof IRutaExpression) {
- return expressionVerbalizer.verbalize((RutaExpression) element);
- } else if (element instanceof RutaBlock) {
- return verbalize((RutaBlock) element, false);
- } else if (element instanceof RutaElement) {
- return scriptVerbalizer.verbalize((RutaElement) element);
- } else {
- return element.getClass().getSimpleName();
+ if (element != null) {
+ if (externalVerbalizers.keySet().contains(element.getClass())
+ && element instanceof RutaElement) {
+ return externalVerbalizers.get(element.getClass()).verbalize((RutaElement) element, this);
+ } else if (element instanceof AbstractRutaAction) {
+ return actionVerbalizer.verbalize((AbstractRutaAction) element);
+ } else if (element instanceof AbstractRutaCondition) {
+ return conditionVerbalizer.verbalize((AbstractRutaCondition) element);
+ } else if (element instanceof IRutaExpression) {
+ return expressionVerbalizer.verbalize((RutaExpression) element);
+ } else if (element instanceof RutaBlock) {
+ return verbalize((RutaBlock) element, false);
+ } else if (element instanceof RutaElement) {
+ return scriptVerbalizer.verbalize((RutaElement) element);
+ } else {
+ return element.getClass().getSimpleName();
+ }
}
+ return "";
}
public String verbalizeName(RutaElement element) {
@@ -103,6 +108,14 @@ public class RutaVerbalizer {
return scriptVerbalizer.verbalizeRuleElement(element);
}
+ public String verbalizeInlinedActionRuleBlock(List<ScriptApply> block) {
+ return scriptVerbalizer.verbalizeInlinedActionRuleApplyBlock(block);
+ }
+
+ public String verbalizeInlinedConditionRuleBlock(List<ScriptApply> block) {
+ return scriptVerbalizer.verbalizeInlinedConditionRuleApplyBlock(block);
+ }
+
public String verbalizeType(Type type) {
if (type.getName().equals(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)) {
return "Document";
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java Mon Nov 18 12:19:31 2019
@@ -23,10 +23,12 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.uima.ruta.RutaElement;
import org.apache.uima.ruta.RutaStatement;
+import org.apache.uima.ruta.ScriptApply;
import org.apache.uima.ruta.action.AbstractRutaAction;
import org.apache.uima.ruta.block.ForEachBlock;
import org.apache.uima.ruta.block.RutaBlock;
@@ -156,14 +158,19 @@ public class ScriptVerbalizer {
RutaRuleElement tmre = (RutaRuleElement) re;
RutaMatcher matcher = tmre.getMatcher();
// action-only rule
- IRutaExpression expression = matcher.getExpression();
- boolean actionOnlyRule = expression == null;
- if (expression != null) {
- String verbalize = verbalizer.verbalize(expression);
- if (StringUtils.isBlank(verbalize)) {
- actionOnlyRule = true;
- } else {
- result.append(verbalize);
+ boolean actionOnlyRule = false;
+ if (matcher == null) {
+ result.append("_");
+ } else {
+ IRutaExpression expression = matcher.getExpression();
+ actionOnlyRule = expression == null;
+ if (expression != null) {
+ String verbalize = verbalizer.verbalize(expression);
+ if (StringUtils.isBlank(verbalize)) {
+ actionOnlyRule = true;
+ } else {
+ result.append(verbalize);
+ }
}
}
if (actionOnlyRule) {
@@ -207,32 +214,61 @@ public class ScriptVerbalizer {
}
if (re instanceof AbstractRuleElement) {
AbstractRuleElement are = (AbstractRuleElement) re;
+
List<List<RutaStatement>> inlinedConditionRuleBlocks = are.getInlinedConditionRuleBlocks();
for (List<RutaStatement> inlinedConditionRules : inlinedConditionRuleBlocks) {
- if (inlinedConditionRules != null && !inlinedConditionRules.isEmpty()) {
- result.append(THEN2);
- result.append(CBOPEN);
- for (RutaStatement rutaStatement : inlinedConditionRules) {
- result.append(verbalize(rutaStatement));
- }
- result.append(CBCLOSE);
- }
+ result.append(verbalizeInlinedConditionRuleBlock(inlinedConditionRules));
}
List<List<RutaStatement>> inlinedActionRuleBlocks = are.getInlinedActionRuleBlocks();
for (List<RutaStatement> inlinedActionRules : inlinedActionRuleBlocks) {
- if (inlinedActionRules != null && !inlinedActionRules.isEmpty()) {
- result.append(THEN);
- result.append(CBOPEN);
- for (RutaStatement rutaStatement : inlinedActionRules) {
- result.append(verbalize(rutaStatement));
- }
- result.append(CBCLOSE);
- }
+ result.append(verbalizeInlinedActionRuleBlock(inlinedActionRules));
+ }
+ }
+ return result.toString();
+ }
+
+ public String verbalizeInlinedActionRuleBlock(List<RutaStatement> inlinedActionRules) {
+ StringBuilder result = new StringBuilder();
+ if (inlinedActionRules != null && !inlinedActionRules.isEmpty()) {
+ result.append(THEN);
+ result.append(CBOPEN);
+ for (RutaStatement rutaStatement : inlinedActionRules) {
+ result.append(verbalize(rutaStatement));
+ }
+ result.append(CBCLOSE);
+ }
+ return result.toString();
+ }
+
+ public String verbalizeInlinedConditionRuleBlock(List<RutaStatement> inlinedConditionRules) {
+ StringBuilder result = new StringBuilder();
+ if (inlinedConditionRules != null && !inlinedConditionRules.isEmpty()) {
+ result.append(THEN2);
+ result.append(CBOPEN);
+ for (RutaStatement rutaStatement : inlinedConditionRules) {
+ result.append(verbalize(rutaStatement));
}
+ result.append(CBCLOSE);
}
return result.toString();
}
+ public String verbalizeInlinedConditionRuleApplyBlock(List<ScriptApply> block) {
+
+ List<RutaStatement> list = block.stream().map(a -> a.getElement())
+ .filter(RutaStatement.class::isInstance).map(s -> (RutaStatement) s)
+ .collect(Collectors.toList());
+ return verbalizeInlinedConditionRuleBlock(list);
+ }
+
+ public String verbalizeInlinedActionRuleApplyBlock(List<ScriptApply> block) {
+
+ List<RutaStatement> list = block.stream().map(a -> a.getElement())
+ .filter(RutaStatement.class::isInstance).map(s -> (RutaStatement) s)
+ .collect(Collectors.toList());
+ return verbalizeInlinedActionRuleBlock(list);
+ }
+
private String verbalizeConjunct(ConjunctRulesRuleElement re) {
StringBuilder result = new StringBuilder();
String sep = " % ";
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoCollectorVisitor.java Mon Nov 18 12:19:31 2019
@@ -79,7 +79,7 @@ public class DebugInfoCollectorVisitor i
}
public boolean createDebugInfo(RutaRule rule) {
- return createDebugInfo || ids.contains("" + rule.getId());
+ return createDebugInfo || ids.contains(String.valueOf(rule.getId()));
}
@Override
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java Mon Nov 18 12:19:31 2019
@@ -26,6 +26,7 @@ import java.util.Map.Entry;
import java.util.Set;
import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.util.FSCollectionFactory;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.ruta.RutaElement;
@@ -34,7 +35,6 @@ import org.apache.uima.ruta.RutaStream;
import org.apache.uima.ruta.ScriptApply;
import org.apache.uima.ruta.block.BlockApply;
import org.apache.uima.ruta.rule.AbstractRule;
-import org.apache.uima.ruta.rule.AbstractRuleElement;
import org.apache.uima.ruta.rule.AbstractRuleMatch;
import org.apache.uima.ruta.rule.ComposedRuleElement;
import org.apache.uima.ruta.rule.ComposedRuleElementMatch;
@@ -49,6 +49,7 @@ import org.apache.uima.ruta.rule.RutaRul
import org.apache.uima.ruta.type.DebugBlockApply;
import org.apache.uima.ruta.type.DebugEvaluatedCondition;
import org.apache.uima.ruta.type.DebugFailedRuleMatch;
+import org.apache.uima.ruta.type.DebugInlinedBlock;
import org.apache.uima.ruta.type.DebugMatchedRuleMatch;
import org.apache.uima.ruta.type.DebugRuleApply;
import org.apache.uima.ruta.type.DebugRuleElementMatch;
@@ -192,9 +193,6 @@ public class DebugInfoFactory {
if (element instanceof RutaStatement) {
RutaStatement rs = (RutaStatement) element;
namespace = rs.getParent().getScript().getRootBlock().getNamespace();
- } else if (element instanceof AbstractRuleElement) {
- AbstractRuleElement are = (AbstractRuleElement) element;
- are.getRule().getParent().getScript().getRootBlock().getNamespace();
}
dra.setElement(verbalizer.verbalize(element));
dra.setApplied(ruleApply.getApplied());
@@ -227,7 +225,7 @@ public class DebugInfoFactory {
drm.setMatched(match.matchedCompletely());
if (match instanceof RuleMatch) {
ComposedRuleElementMatch rootMatch = ((RuleMatch) match).getRootMatch();
- setInnerMatches(stream, addToIndex, cas, drm, rootMatch);
+ setInnerMatches(stream, addToIndex, withMatches, timeInfo, drm, rootMatch);
// if (match.matched()) {
List<DebugScriptApply> delegates = new ArrayList<DebugScriptApply>();
for (ScriptApply rem : ((RuleMatch) match).getDelegateApply().values()) {
@@ -292,46 +290,57 @@ public class DebugInfoFactory {
return drm;
}
- private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas, DebugRuleMatch drm,
- ComposedRuleElementMatch rootMatch) {
+ private void setInnerMatches(RutaStream stream, boolean addToIndex, boolean withMatches,
+ Map<RutaElement, Long> timeInfo, DebugRuleMatch drm, ComposedRuleElementMatch rootMatch) {
Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = rootMatch.getInnerMatches()
.entrySet();
List<DebugRuleElementMatches> ruleElementMatches = new ArrayList<DebugRuleElementMatches>();
for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
RuleElement re = entry.getKey();
List<RuleElementMatch> rems = entry.getValue();
- ruleElementMatches.add(createDebugRuleElementMatches(re, rems, stream, addToIndex));
+ ruleElementMatches.add(
+ createDebugRuleElementMatches(re, rems, stream, addToIndex, withMatches, timeInfo));
}
- drm.setElements(UIMAUtils.toFSArray(cas, ruleElementMatches));
+ drm.setElements(UIMAUtils.toFSArray(stream.getJCas(), ruleElementMatches));
}
- private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas,
- DebugRuleElementMatch drm, ComposedRuleElementMatch rootMatch) {
+ private void setInnerMatches(RutaStream stream, boolean addToIndex, boolean withMatches,
+ Map<RutaElement, Long> timeInfo, DebugRuleElementMatch drm,
+ ComposedRuleElementMatch rootMatch) {
Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = rootMatch.getInnerMatches()
.entrySet();
List<DebugRuleElementMatches> ruleElementMatches = new ArrayList<DebugRuleElementMatches>();
for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
RuleElement re = entry.getKey();
List<RuleElementMatch> rems = entry.getValue();
- ruleElementMatches.add(createDebugRuleElementMatches(re, rems, stream, addToIndex));
+ ruleElementMatches.add(
+ createDebugRuleElementMatches(re, rems, stream, addToIndex, withMatches, timeInfo));
}
- drm.setElements(UIMAUtils.toFSArray(cas, ruleElementMatches));
+ drm.setElements(UIMAUtils.toFSArray(stream.getJCas(), ruleElementMatches));
}
public DebugRuleElementMatches createDebugRuleElementMatches(RuleElement re,
- List<RuleElementMatch> rems, RutaStream stream, boolean addToIndex) {
+ List<RuleElementMatch> rems, RutaStream stream, boolean addToIndex, boolean withMatches,
+ Map<RutaElement, Long> timeInfo) {
JCas cas = stream.getJCas();
DebugRuleElementMatches drems = new DebugRuleElementMatches(cas);
drems.setElement(verbalizer.verbalize(re));
List<DebugRuleElementMatch> remList = new ArrayList<DebugRuleElementMatch>();
if (rems != null) {
for (RuleElementMatch each : rems) {
+ DebugRuleElementMatch rem = null;
if (each instanceof ComposedRuleElementMatch) {
- remList.add(createDebugComposedRuleElementMatch((ComposedRuleElementMatch) each, stream,
- addToIndex));
+ rem = createDebugComposedRuleElementMatch((ComposedRuleElementMatch) each, stream,
+ addToIndex, withMatches, timeInfo);
} else {
- remList.add(createDebugRuleElementMatch(each, stream, addToIndex));
+ rem = createDebugRuleElementMatch(each, stream, addToIndex);
+ }
+ FSArray inlinedConditionBlocks = createInlinedBlocks(each.getInlinedConditionRules(),
+ stream, true, addToIndex, withMatches, timeInfo);
+ rem.setInlinedConditionBlocks(inlinedConditionBlocks);
+ if (rem != null) {
+ remList.add(rem);
}
}
}
@@ -339,20 +348,72 @@ public class DebugInfoFactory {
drems.setRuleAnchor(rems.get(0).isRuleAnchor());
}
drems.setMatches(UIMAUtils.toFSArray(cas, remList));
+
+ FSArray inlinedActionBlocks = createInlinedActionBlocks(rems, stream, addToIndex, withMatches,
+ timeInfo);
+ drems.setInlinedActionBlocks(inlinedActionBlocks);
+
if (addToIndex)
drems.addToIndexes();
return drems;
}
+ private FSArray createInlinedBlocks(List<List<ScriptApply>> blocks, RutaStream stream,
+ boolean asCondition, boolean addToIndex, boolean withMatches,
+ Map<RutaElement, Long> timeInfo) {
+ JCas jcas = stream.getJCas();
+ if (blocks == null || blocks.isEmpty()) {
+ return null;
+ }
+
+ List<DebugInlinedBlock> blockList = new ArrayList<>();
+ for (List<ScriptApply> block : blocks) {
+ List<DebugScriptApply> list = new ArrayList<>();
+ boolean oneRuleApplied = false;
+ for (ScriptApply ruleApply : block) {
+ if (ruleApply instanceof RuleApply) {
+ if (((RuleApply) ruleApply).getApplied() > 0) {
+ oneRuleApplied = true;
+ }
+ }
+ DebugScriptApply debugScriptApply = createDebugScriptApply(ruleApply, stream, addToIndex,
+ withMatches, timeInfo);
+ list.add(debugScriptApply);
+ }
+ DebugInlinedBlock debugInlinedBlock = new DebugInlinedBlock(jcas);
+ debugInlinedBlock.setInlinedRules(FSCollectionFactory.createFSArray(jcas, list));
+ debugInlinedBlock.setAsCondition(asCondition);
+ if (asCondition) {
+ debugInlinedBlock.setElement(verbalizer.verbalizeInlinedConditionRuleBlock(block));
+ debugInlinedBlock.setMatched(oneRuleApplied);
+ } else {
+ debugInlinedBlock.setElement(verbalizer.verbalizeInlinedActionRuleBlock(block));
+ }
+ blockList.add(debugInlinedBlock);
+ }
+ return FSCollectionFactory.createFSArray(jcas, blockList);
+ }
+
+ private FSArray createInlinedActionBlocks(List<RuleElementMatch> rems, RutaStream stream,
+ boolean addToIndex, boolean withMatches, Map<RutaElement, Long> timeInfo) {
+ if (rems == null || rems.isEmpty()) {
+ return null;
+ }
+
+ return createInlinedBlocks(rems.get(0).getInlinedActionRules(), stream, false, addToIndex,
+ withMatches, timeInfo);
+ }
+
public DebugRuleElementMatch createDebugComposedRuleElementMatch(ComposedRuleElementMatch rem,
- RutaStream stream, boolean addToIndex) {
+ RutaStream stream, boolean addToIndex, boolean withMatches,
+ Map<RutaElement, Long> timeInfo) {
JCas cas = stream.getJCas();
DebugRuleElementMatch drem = new DebugRuleElementMatch(cas);
DebugEvaluatedCondition base = new DebugEvaluatedCondition(cas);
base.setValue(rem.isBaseConditionMatched());
- setInnerMatches(stream, addToIndex, cas, drem, rem);
+ setInnerMatches(stream, addToIndex, withMatches, timeInfo, drem, rem);
String baseString = verbalizer.verbalize(rem.getRuleElement());
base.setElement(baseString);
@@ -399,8 +460,10 @@ public class DebugInfoFactory {
drem.setEnd(end);
}
}
- if (addToIndex)
+
+ if (addToIndex) {
drem.addToIndexes();
+ }
return drem;
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/resources/org/apache/uima/ruta/engine/BasicEngine.xml
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/resources/org/apache/uima/ruta/engine/BasicEngine.xml?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/resources/org/apache/uima/ruta/engine/BasicEngine.xml (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/resources/org/apache/uima/ruta/engine/BasicEngine.xml Mon Nov 18 12:19:31 2019
@@ -198,6 +198,13 @@
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
+ <configurationParameter>
+ <name>rulesScriptName</name>
+ <description/>
+ <type>String</type>
+ <multiValued>false</multiValued>
+ <mandatory>true</mandatory>
+ </configurationParameter>
<configurationParameter>
<name>dictRemoveWS</name>
<type>Boolean</type>
@@ -246,9 +253,36 @@
<multiValued>false</multiValued>
<mandatory>false</mandatory>
</configurationParameter>
+ <configurationParameter>
+ <name>inferenceVisitors</name>
+ <description/>
+ <type>String</type>
+ <multiValued>true</multiValued>
+ <mandatory>false</mandatory>
+ </configurationParameter>
+ <configurationParameter>
+ <name>maxRuleMatches</name>
+ <description/>
+ <type>Integer</type>
+ <multiValued>false</multiValued>
+ <mandatory>false</mandatory>
+ </configurationParameter>
+ <configurationParameter>
+ <name>maxRuleElementMatches</name>
+ <description/>
+ <type>Integer</type>
+ <multiValued>false</multiValued>
+ <mandatory>false</mandatory>
+ </configurationParameter>
</configurationParameters>
<configurationParameterSettings>
<nameValuePair>
+ <name>rulesScriptName</name>
+ <value>
+ <string>Anonymous</string>
+ </value>
+ </nameValuePair>
+ <nameValuePair>
<name>debug</name>
<value>
<boolean>false</boolean>
@@ -302,6 +336,30 @@
<boolean>true</boolean>
</value>
</nameValuePair>
+ <nameValuePair>
+ <name>dictRemoveWS</name>
+ <value>
+ <boolean>true</boolean>
+ </value>
+ </nameValuePair>
+ <nameValuePair>
+ <name>inferenceVisitors</name>
+ <value>
+ <array/>
+ </value>
+ </nameValuePair>
+ <nameValuePair>
+ <name>maxRuleMatches</name>
+ <value>
+ <integer>2147483647</integer>
+ </value>
+ </nameValuePair>
+ <nameValuePair>
+ <name>maxRuleElementMatches</name>
+ <value>
+ <integer>2147483647</integer>
+ </value>
+ </nameValuePair>
</configurationParameterSettings>
<typeSystemDescription>
<imports>
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java?rev=1869967&r1=1869966&r2=1869967&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java Mon Nov 18 12:19:31 2019
@@ -27,6 +27,7 @@ import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;
+import org.apache.uima.UIMAFramework;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
@@ -36,6 +37,7 @@ import org.apache.uima.fit.factory.Analy
import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
import org.apache.uima.fit.util.CasUtil;
import org.apache.uima.fit.util.JCasUtil;
+import org.apache.uima.resource.ResourceManager;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.uima.ruta.descriptor.RutaBuildOptions;
import org.apache.uima.ruta.descriptor.RutaDescriptorFactory;
@@ -76,7 +78,8 @@ public class ImportStatementsTest {
ruta.getAnalysisEngineMetaData().setTypeSystem(tsd);
- return AnalysisEngineFactory.createEngine(ruta);
+ ResourceManager resourceManager = UIMAFramework.newDefaultResourceManager();
+ return UIMAFramework.produceAnalysisEngine(ruta, resourceManager, null);
}
@Test