Author: rec Date: Mon May 11 16:47:27 2020 New Revision: 1877591 URL: http://svn.apache.org/viewvc?rev=1877591&view=rev Log: [UIMA-6231] Reducing memory pressure generated by UIMA Ruta
- Avoid creation of ArrayLists for individual elements - use asList() instead - Avoid unnecessary creation of ArrayLists - Try to choose initial size of ArrayLists more wisely in some cases - Avoid excessive creation of iterators in enhanced for-loops in InferenceCrowd (this is a hotspot, so it counts) - Use early-return instead of cascaded if-else and return-at-end Modified: uima/ruta/trunk/ (props changed) uima/ruta/trunk/ruta-core/ (props changed) uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/RutaExpression.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/EvaluatedCondition.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/InferenceCrowd.java Propchange: uima/ruta/trunk/ ------------------------------------------------------------------------------ Merged /uima/ruta/branches/UIMA-6231-memory-pressure:r1877514-1877589 Propchange: uima/ruta/trunk/ruta-core/ ------------------------------------------------------------------------------ Merged /uima/ruta/branches/UIMA-6231-memory-pressure/ruta-core:r1877514-1877589 Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -188,23 +188,23 @@ public class RutaEnvironment { super(); this.owner = owner; - types = new HashMap<String, Type>(); - namespaces = new HashMap<String, String>(); - ambiguousTypeAlias = new HashMap<String, Set<String>>(); - typesystems = new HashSet<String>(); - scripts = new HashSet<String>(); - typeImports = new HashMap<String, List<Alias>>(); - packageImports = new HashMap<String, List<String>>(); - declaredAnnotationTypes = new HashSet<String>(); - wordLists = new HashMap<String, RutaWordList>(); - tables = new HashMap<String, RutaTable>(); - variableValues = new HashMap<String, Object>(); - tempVariableValues = new HashMap<String, Object>(); - variableTypes = new HashMap<String, Class<?>>(); - variableGenericTypes = new HashMap<String, Class<?>>(); + types = new HashMap<>(); + namespaces = new HashMap<>(); + ambiguousTypeAlias = new HashMap<>(); + typesystems = new HashSet<>(); + scripts = new HashSet<>(); + typeImports = new HashMap<>(); + packageImports = new HashMap<>(); + declaredAnnotationTypes = new HashSet<>(); + wordLists = new HashMap<>(); + tables = new HashMap<>(); + variableValues = new HashMap<>(); + tempVariableValues = new HashMap<>(); + variableTypes = new HashMap<>(); + variableGenericTypes = new HashMap<>(); macroConditions = new HashMap<>(); macroActions = new HashMap<>(); - availableTypes = new HashMap<String, Class<?>>(); + availableTypes = new HashMap<>(); availableTypes.put(RutaConstants.RUTA_VARIABLE_ANNOTATION, AnnotationFS.class); availableTypes.put("INT", Integer.class); availableTypes.put("STRING", String.class); @@ -223,7 +223,7 @@ public class RutaEnvironment { availableTypes.put("FLOATLIST", List.class); availableTypes.put("STRINGLIST", List.class); availableTypes.put(RutaConstants.RUTA_VARIABLE_TYPE_LIST, List.class); - availableListTypes = new HashMap<String, Class<?>>(); + availableListTypes = new HashMap<>(); availableListTypes.put(RutaConstants.RUTA_VARIABLE_ANNOTATION_LIST, AnnotationFS.class); availableListTypes.put("BOOLEANLIST", Boolean.class); availableListTypes.put("INTLIST", Integer.class); @@ -232,7 +232,7 @@ public class RutaEnvironment { availableListTypes.put("STRINGLIST", String.class); availableListTypes.put(RutaConstants.RUTA_VARIABLE_TYPE_LIST, Type.class); resourcePaths = getResourcePaths(); - initializedVariables = new HashMap<String, Object>(); + initializedVariables = new HashMap<>(); variableAliases = new HashMap<>(); // Always import BasicTypeSystem @@ -527,7 +527,7 @@ public class RutaEnvironment { if (existing != null && !existing.equals(longName)) { // shortName can now be resolved to "existing" or "longName" - targets = new HashSet<String>(2); + targets = new HashSet<>(2); targets.add(existing); targets.add(longName); @@ -556,7 +556,7 @@ public class RutaEnvironment { List<Alias> aliases = typeImports.get(key); if (aliases == null) { - aliases = new ArrayList<Alias>(); + aliases = new ArrayList<>(); typeImports.put(key, aliases); } @@ -634,8 +634,9 @@ public class RutaEnvironment { */ public void importPackage(String packageName, String alias) { List<String> aliases = packageImports.get(packageName); + if (aliases == null) { - aliases = new ArrayList<String>(1); + aliases = new ArrayList<>(1); packageImports.put(packageName, aliases); } @@ -753,7 +754,7 @@ public class RutaEnvironment { Object init = initializedVariables.get(name); if (init != null) { if (init instanceof List) { - ArrayList<Object> list = new ArrayList<Object>(); + ArrayList<Object> list = new ArrayList<>(); list.addAll((Collection<? extends Object>) init); return list; } @@ -772,7 +773,7 @@ public class RutaEnvironment { } else if (Type.class.equals(type)) { return null; } else if (List.class.equals(type)) { - return new ArrayList<Object>(); + return new ArrayList<>(); } return null; } @@ -964,7 +965,7 @@ public class RutaEnvironment { public void setInitialVariableValue(String var, Object value) { if (ownsVariable(var)) { if (value instanceof List) { - List<Object> initValue = new ArrayList<Object>(); + List<Object> initValue = new ArrayList<>(); initValue.addAll((Collection<? extends Object>) value); initializedVariables.put(var, initValue); } else { @@ -1057,16 +1058,12 @@ public class RutaEnvironment { public void addMacroAction(String name, Map<String, String> def, Set<String> vars, List<AbstractRutaAction> actions) { - macroActions.put(name, - new ImmutableTriple<Map<String, String>, List<AbstractRutaAction>, Set<String>>(def, - actions, vars)); + macroActions.put(name, new ImmutableTriple<>(def, actions, vars)); } public void addMacroCondition(String name, Map<String, String> def, Set<String> vars, List<AbstractRutaCondition> conditions) { - macroConditions.put(name, - new ImmutableTriple<Map<String, String>, List<AbstractRutaCondition>, Set<String>>(def, - conditions, vars)); + macroConditions.put(name, new ImmutableTriple<>(def, conditions, vars)); } public boolean isMacroAction(String name) { Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,9 @@ package org.apache.uima.ruta; +import static java.util.Arrays.asList; +import static java.util.Collections.emptySet; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -102,9 +105,9 @@ public class RutaStream extends FSIterat private Type basicType; - private NavigableMap<Integer, RutaBasic> beginAnchors = new TreeMap<Integer, RutaBasic>(); + private NavigableMap<Integer, RutaBasic> beginAnchors = new TreeMap<>(); - private NavigableMap<Integer, RutaBasic> endAnchors = new TreeMap<Integer, RutaBasic>(); + private NavigableMap<Integer, RutaBasic> endAnchors = new TreeMap<>(); private FilterManager filter; @@ -201,9 +204,9 @@ public class RutaStream extends FSIterat private void updateIterators(CAS cas, Type basicType, FilterManager filter, AnnotationFS additionalWindow) { if (additionalWindow != null) { - this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow); + basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow); } else { - this.basicIt = cas.getAnnotationIndex(basicType).iterator(); + basicIt = cas.getAnnotationIndex(basicType).iterator(); } currentIt = filter.createFilteredIterator(cas, basicType); } @@ -218,7 +221,7 @@ public class RutaStream extends FSIterat reindexTypeList = removeSubsumedTypes(Arrays.asList(reindexOnly), cas.getTypeSystem()); } - final List<AnnotationFS> allAnnotations = new LinkedList<AnnotationFS>(); + final List<AnnotationFS> allAnnotations = new LinkedList<>(); for (Type type : reindexTypeList) { AnnotationIndex<AnnotationFS> annotationIndex = null; if (StringUtils.equals(type.getName(), CAS.TYPE_NAME_ANNOTATION)) { @@ -234,7 +237,7 @@ public class RutaStream extends FSIterat } if (basicIndex.size() == 0) { - TreeSet<Integer> anchors = new TreeSet<Integer>(); + TreeSet<Integer> anchors = new TreeSet<>(); for (AnnotationFS a : allAnnotations) { anchors.add(a.getBegin()); anchors.add(a.getEnd()); @@ -546,7 +549,7 @@ public class RutaStream extends FSIterat } public List<AnnotationFS> getOverappingAnnotations(AnnotationFS window, Type type) { - List<AnnotationFS> result = new ArrayList<AnnotationFS>(); + List<AnnotationFS> result = new ArrayList<>(); AnnotationFS newWindow = cas.createAnnotation(type, window.getBegin(), window.getEnd() - 1); FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator(newWindow); if (!iterator.isValid()) { @@ -566,7 +569,7 @@ public class RutaStream extends FSIterat } public List<Annotation> getAnnotationsFollowing(Annotation annotation) { - List<Annotation> result = new ArrayList<Annotation>(); + List<Annotation> result = new ArrayList<>(); moveTo(annotation); while (currentIt.isValid()) { currentIt.moveToNext(); @@ -596,7 +599,7 @@ public class RutaStream extends FSIterat } public List<AnnotationFS> getAllofType(Type type) { - List<AnnotationFS> result = new ArrayList<AnnotationFS>(); + List<AnnotationFS> result = new ArrayList<>(); FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).iterator(); while (iterator.isValid()) { FeatureStructure featureStructure = iterator.get(); @@ -611,34 +614,32 @@ public class RutaStream extends FSIterat if (windowAnnotation == null || type == null) { return Collections.emptyList(); } - TypeSystem typeSystem = this.getCas().getTypeSystem(); - List<AnnotationFS> result = new ArrayList<AnnotationFS>(); + TypeSystem typeSystem = getCas().getTypeSystem(); + List<AnnotationFS> result = new ArrayList<>(); if (typeSystem.subsumes(type, windowAnnotation.getType())) { result.add(windowAnnotation); } - result.addAll(CasUtil.selectCovered(this.cas, type, windowAnnotation)); + result.addAll(CasUtil.selectCovered(cas, type, windowAnnotation)); return result; } public Collection<RutaBasic> getAllBasicsInWindow(AnnotationFS windowAnnotation) { if (windowAnnotation.getBegin() >= windowAnnotation.getEnd()) { - return Collections.emptySet(); + return emptySet(); } + RutaBasic beginAnchor = getBeginAnchor(windowAnnotation.getBegin()); if (beginAnchor != null && beginAnchor.getEnd() == windowAnnotation.getEnd()) { - Collection<RutaBasic> result = new ArrayList<RutaBasic>(1); - result.add(beginAnchor); - return result; + return asList(beginAnchor); } - Collection<RutaBasic> subSet = null; + if (windowAnnotation.getEnd() == cas.getDocumentAnnotation().getEnd() && windowAnnotation.getBegin() == 0) { - subSet = beginAnchors.values(); - } else { - subSet = beginAnchors - .subMap(windowAnnotation.getBegin(), true, windowAnnotation.getEnd(), false).values(); + return beginAnchors.values(); } - return subSet; + + return beginAnchors.subMap(windowAnnotation.getBegin(), true, windowAnnotation.getEnd(), false) + .values(); } public RutaBasic getBasicNextTo(boolean before, AnnotationFS annotation) { @@ -685,7 +686,7 @@ public class RutaStream extends FSIterat } public List<RutaBasic> getBasicsInWindow(AnnotationFS windowAnnotation) { - List<RutaBasic> result = new ArrayList<RutaBasic>(); + List<RutaBasic> result = new ArrayList<>(); if (windowAnnotation instanceof RutaBasic) { result.add((RutaBasic) windowAnnotation); return result; @@ -721,7 +722,7 @@ public class RutaStream extends FSIterat if (annotation != null) { return getBasicsInWindow(annotation); } else { - return new ArrayList<RutaBasic>(); + return new ArrayList<>(); } } @@ -825,7 +826,7 @@ public class RutaStream extends FSIterat } public void setGreedyRuleElement(Boolean greedyAnchoring) { - this.greedyRuleElement = greedyAnchoring; + greedyRuleElement = greedyAnchoring; } public boolean isGreedyRule() { @@ -833,7 +834,7 @@ public class RutaStream extends FSIterat } public void setGreedyRule(Boolean greedyAnchoring) { - this.greedyRule = greedyAnchoring; + greedyRule = greedyAnchoring; } public void setIndexPenalty(double indexPenalty) { @@ -940,7 +941,7 @@ public class RutaStream extends FSIterat } public Collection<AnnotationFS> getAnnotations(Type type) { - Collection<AnnotationFS> result = new LinkedList<AnnotationFS>(); + Collection<AnnotationFS> result = new LinkedList<>(); AnnotationFS windowAnnotation = filter.getWindowAnnotation(); if (windowAnnotation != null && (windowAnnotation.getBegin() != cas.getDocumentAnnotation().getBegin() @@ -1117,7 +1118,7 @@ public class RutaStream extends FSIterat AnnotationTypeExpression ate = (AnnotationTypeExpression) value; if (range.isArray()) { List<AnnotationFS> annotations = ate.getAnnotationList(context, this); - annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), annotations)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), annotations)); } else { AnnotationFS a = ate.getAnnotation(context, this); annotation.setFeatureValue(feature, a); @@ -1136,7 +1137,7 @@ public class RutaStream extends FSIterat if (range.isArray()) { List<FeatureStructure> c = new ArrayList<>(); c.add(a); - annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), c)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), c)); } else { annotation.setFeatureValue(feature, a); } @@ -1145,7 +1146,7 @@ public class RutaStream extends FSIterat List<AnnotationFS> annotations = ale.getAnnotationList(context, this); if (annotations != null) { if (range.isArray()) { - annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), annotations)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), annotations)); } else { if (annotations.isEmpty()) { annotation.setFeatureValue(feature, null); @@ -1163,7 +1164,7 @@ public class RutaStream extends FSIterat } else if (value instanceof GenericFeatureExpression && !range.isPrimitive()) { FeatureExpression fe = ((GenericFeatureExpression) value).getFeatureExpression(); Type t = fe.getInitialType(context, this); - List<AnnotationFS> inWindow = this.getAnnotationsInWindow(context.getAnnotation(), t); + List<AnnotationFS> inWindow = getAnnotationsInWindow(context.getAnnotation(), t); if (fe instanceof SimpleFeatureExpression) { SimpleFeatureExpression sfe = (SimpleFeatureExpression) fe; List<? extends FeatureStructure> featureAnnotations = null; @@ -1174,15 +1175,14 @@ public class RutaStream extends FSIterat featureAnnotations = inWindow; } if (range.isArray()) { - annotation.setFeatureValue(feature, - UIMAUtils.toFSArray(this.getJCas(), featureAnnotations)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), featureAnnotations)); } else if (!featureAnnotations.isEmpty()) { FeatureStructure a = featureAnnotations.get(0); annotation.setFeatureValue(feature, a); } } else { if (range.isArray()) { - annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), inWindow)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), inWindow)); } else { AnnotationFS a = inWindow.get(0); annotation.setFeatureValue(feature, a); @@ -1194,9 +1194,9 @@ public class RutaStream extends FSIterat private void assignAnnotationByTypeInWindow(FeatureStructure annotation, Feature feature, MatchContext context, Type type) { - List<AnnotationFS> inWindow = this.getAnnotationsInWindow(context.getAnnotation(), type); + List<AnnotationFS> inWindow = getAnnotationsInWindow(context.getAnnotation(), type); if (feature.getRange().isArray()) { - annotation.setFeatureValue(feature, UIMAUtils.toFSArray(this.getJCas(), inWindow)); + annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), inWindow)); } else { if (inWindow != null && !inWindow.isEmpty()) { AnnotationFS a = inWindow.get(0); @@ -1307,7 +1307,7 @@ public class RutaStream extends FSIterat } public AnnotationFS getSingleAnnotationByTypeInContext(Type type, MatchContext context) { - List<AnnotationFS> inWindow = this.getAnnotationsInWindow(context.getAnnotation(), type); + List<AnnotationFS> inWindow = getAnnotationsInWindow(context.getAnnotation(), type); if (inWindow != null && !inWindow.isEmpty()) { return inWindow.get(0); } @@ -1315,12 +1315,12 @@ public class RutaStream extends FSIterat } public List<AnnotationFS> getAnnotationsByTypeInContext(Type type, MatchContext context) { - List<AnnotationFS> inWindow = this.getAnnotationsInWindow(context.getAnnotation(), type); + List<AnnotationFS> inWindow = getAnnotationsInWindow(context.getAnnotation(), type); return inWindow; } public List<AnnotationFS> getBestGuessedAnnotationsAt(AnnotationFS window, Type type) { - List<AnnotationFS> result = new ArrayList<AnnotationFS>(); + List<AnnotationFS> result = new ArrayList<>(); if (window == null || type == null) { return result; } @@ -1425,8 +1425,8 @@ public class RutaStream extends FSIterat public RutaAnnotation getRutaAnnotationFor(AnnotationFS annotation, boolean create, RutaStream stream) { - Type heuristicType = this.cas.getTypeSystem().getType(RutaAnnotation.class.getName()); - List<AnnotationFS> ras = CasUtil.selectAt(this.cas, heuristicType, annotation.getBegin(), + Type heuristicType = cas.getTypeSystem().getType(RutaAnnotation.class.getName()); + List<AnnotationFS> ras = CasUtil.selectAt(cas, heuristicType, annotation.getBegin(), annotation.getEnd()); for (AnnotationFS each : ras) { if (((RutaAnnotation) each).getAnnotation() == annotation) { @@ -1452,10 +1452,10 @@ public class RutaStream extends FSIterat } public long getMaxRuleMatches() { - return this.maxRuleMatches; + return maxRuleMatches; } public long getMaxRuleElementMatches() { - return this.maxRuleElementMatches; + return maxRuleElementMatches; } } Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,9 @@ package org.apache.uima.ruta.expression; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -74,10 +77,8 @@ public class AnnotationTypeExpression ex if (annotationExpression != null) { AnnotationFS annotation = annotationExpression.getAnnotation(context, stream); if (featureExpression != null) { - List<AnnotationFS> annotations = new ArrayList<>(1); - annotations.add(annotation); - Collection<? extends AnnotationFS> result = featureExpression.getAnnotations(annotations, - true, context, stream); + Collection<? extends AnnotationFS> result = featureExpression + .getAnnotations(asList(annotation), true, context, stream); if (result != null && !result.isEmpty()) { return result.iterator().next(); } @@ -110,11 +111,9 @@ public class AnnotationTypeExpression ex types = typeListExpression.getTypeList(context, stream); } else { Type type = getType(context, stream); - types = new ArrayList<>(1); - if (type != null) { - types.add(type); - } + types = type != null ? asList(type) : emptyList(); } + for (Type type : types) { if (type != null) { @@ -131,8 +130,8 @@ public class AnnotationTypeExpression ex bestGuessedAnnotations = stream.getBestGuessedAnnotationsAt(context.getAnnotation(), type); if (bestGuessedAnnotations.isEmpty()) { - bestGuessedAnnotations = new ArrayList<>(1); - bestGuessedAnnotations.add(stream.getSingleAnnotationByTypeInContext(type, context)); + bestGuessedAnnotations = asList( + stream.getSingleAnnotationByTypeInContext(type, context)); } } @@ -215,8 +214,7 @@ public class AnnotationTypeExpression ex } } else if (annotationExpression != null) { AnnotationFS annotation = annotationExpression.getAnnotation(context, stream); - List<AnnotationFS> result = new ArrayList<AnnotationFS>(1); - result.add(annotation); + List<AnnotationFS> result = asList(annotation); if (featureExpression != null) { return new ArrayList<>(featureExpression.getAnnotations(result, true, context, stream)); } else { @@ -228,9 +226,7 @@ public class AnnotationTypeExpression ex if (typeListExpression != null) { types = typeListExpression.getTypeList(context, stream); } else { - Type type = getType(context, stream); - types = new ArrayList<>(1); - types.add(type); + types = asList(getType(context, stream)); } List<AnnotationFS> annotations = new ArrayList<>(); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/RutaExpression.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/RutaExpression.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/RutaExpression.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/RutaExpression.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,7 +19,8 @@ package org.apache.uima.ruta.expression; -import java.util.ArrayList; +import static java.util.Arrays.asList; + import java.util.Collections; import java.util.List; @@ -45,9 +46,7 @@ public class RutaExpression extends Ruta IAnnotationListExpression annotationListExpression = sfe.getMatchReference() .getAnnotationListExpression(context, stream); if (annotationExpression != null) { - List<AnnotationFS> as = new ArrayList<>(1); - as.add(annotationExpression.getAnnotation(context, stream)); - return as; + return asList(annotationExpression.getAnnotation(context, stream)); } else if (annotationListExpression != null) { return annotationListExpression.getAnnotationList(context, stream); } @@ -65,19 +64,15 @@ public class RutaExpression extends Ruta AnnotationFS documentAnnotation = stream.getCas().getDocumentAnnotation(); Type docType = documentAnnotation.getType(); if (docType.equals(type)) { - List<AnnotationFS> result = new ArrayList<AnnotationFS>(1); AnnotationFS windowAnnotation = stream.getFilter().getWindowAnnotation(); + if (windowAnnotation == null) { - result.add(documentAnnotation); - return result; - } else { - result.add(windowAnnotation); - return result; + return asList(documentAnnotation); } + + return asList(windowAnnotation); } return stream.getBestGuessedAnnotationsAt(matchedAnnotation, type); - } - } Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,8 +19,9 @@ package org.apache.uima.ruta.expression.feature; +import static java.util.Arrays.asList; + import java.math.BigDecimal; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -148,10 +149,8 @@ public class FeatureMatchExpression exte FeatureStructure featureValue = fs.getFeatureValue(feature); if (!feature.getRange().isPrimitive() && getArg() instanceof FeatureExpression) { FeatureExpression fe = (FeatureExpression) getArg(); - List<FeatureStructure> list = new ArrayList<>(1); - list.add(fs); - Collection<? extends FeatureStructure> featureAnnotations = fe.getFeatureStructures(list, - false, context, stream); + Collection<? extends FeatureStructure> featureAnnotations = fe + .getFeatureStructures(asList(fs), false, context, stream); return compare(featureValue, featureAnnotations); } else if (!feature.getRange().isPrimitive() && getArg() instanceof IAnnotationExpression) { IAnnotationExpression ae = (IAnnotationExpression) getArg(); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AbstractRuleElement.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,8 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -68,10 +70,10 @@ public abstract class AbstractRuleElemen this.container = container; this.parent = parent; if (this.conditions == null) { - this.conditions = new ArrayList<AbstractRutaCondition>(); + this.conditions = new ArrayList<>(); } if (this.actions == null) { - this.actions = new ArrayList<AbstractRutaAction>(); + this.actions = new ArrayList<>(); } if (this.quantifier == null) { this.quantifier = new NormalQuantifier(); @@ -82,7 +84,7 @@ public abstract class AbstractRuleElemen public List<RuleMatch> continueSideStep(boolean after, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); boolean newDirection = !after; List<AnnotationFS> matchedAnnotationsOf = ruleMatch.getMatchedAnnotationsOfElement(this); AnnotationFS annotation = null; @@ -251,13 +253,11 @@ public abstract class AbstractRuleElemen @Override public List<Integer> getSelfIndexList() { - List<Integer> result = new ArrayList<Integer>(1); if (getContainer() == null) { return null; } - int indexOf = getContainer().getRuleElements().indexOf(this); - result.add(indexOf + 1); - return result; + + return asList(getContainer().getRuleElements().indexOf(this) + 1); } @Override @@ -364,7 +364,7 @@ public abstract class AbstractRuleElemen @Override public void setStartAnchor(boolean start) { - this.startAnchor = start; + startAnchor = start; } @Override @@ -384,7 +384,7 @@ public abstract class AbstractRuleElemen @Override public void addInlinedConditionRules(List<RutaStatement> innerRules) { - this.inlinedConditionRuleBlocks.add(innerRules); + inlinedConditionRuleBlocks.add(innerRules); } @Override @@ -394,7 +394,7 @@ public abstract class AbstractRuleElemen @Override public void addInlinedActionRules(List<RutaStatement> innerRules) { - this.inlinedActionRuleBlocks.add(innerRules); + inlinedActionRuleBlocks.add(innerRules); } @Override Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElement.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,8 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; + import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; @@ -56,7 +58,7 @@ public class ComposedRuleElement extends RuleElementContainer container, RutaBlock parent) { super(quantifier, conditions, actions, container, parent); this.elements = elements; - this.caretaker = new RuleElementCaretaker(this); + caretaker = new RuleElementCaretaker(this); } @Override @@ -69,7 +71,7 @@ public class ComposedRuleElement extends public List<RuleMatch> startMatch(RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); if (conjunct == null) { ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch, stream); @@ -78,7 +80,7 @@ public class ComposedRuleElement extends crowd); } else if (!conjunct) { // disjunctive - Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new LinkedHashMap<RuleMatch, ComposedRuleElementMatch>(); + Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new LinkedHashMap<>(); for (RuleElement each : elements) { ComposedRuleElementMatch extendedContainerMatch = containerMatch.copy(); RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, true); @@ -114,7 +116,7 @@ public class ComposedRuleElement extends } } else if (conjunct) { // conjunctive - Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new LinkedHashMap<RuleMatch, ComposedRuleElementMatch>(); + Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new LinkedHashMap<>(); RuleElement anchoringRuleElement = getAnchoringRuleElement(stream); ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch, @@ -188,7 +190,7 @@ public class ComposedRuleElement extends public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); if (conjunct == null) { // inner next sequential RuleElement nextElement = getNextElement(after, this); @@ -203,7 +205,7 @@ public class ComposedRuleElement extends } } else if (!conjunct) { // disjunctive - Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new HashMap<RuleMatch, ComposedRuleElementMatch>(); + Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new HashMap<>(); for (RuleElement each : elements) { ComposedRuleElementMatch extendedContainerMatch = containerMatch.copy(); RuleMatch extendedMatch = ruleMatch.copy(extendedContainerMatch, after); @@ -238,7 +240,7 @@ public class ComposedRuleElement extends } } else if (conjunct) { // conjunctive - Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new HashMap<RuleMatch, ComposedRuleElementMatch>(); + Map<RuleMatch, ComposedRuleElementMatch> ruleMatches = new HashMap<>(); RuleElement anchoringRuleElement = getAnchoringRuleElement(stream); ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch, @@ -325,7 +327,7 @@ public class ComposedRuleElement extends Map<RuleMatch, ComposedRuleElementMatch> ruleMatches, boolean direction, RutaStream stream) { // TODO hotfix: this needs a correct implementation - Map<RuleMatch, ComposedRuleElementMatch> result = new TreeMap<RuleMatch, ComposedRuleElementMatch>( + Map<RuleMatch, ComposedRuleElementMatch> result = new TreeMap<>( ruleMatchComparator); Set<Entry<RuleMatch, ComposedRuleElementMatch>> entrySet = ruleMatches.entrySet(); Entry<RuleMatch, ComposedRuleElementMatch> largestEntry = null; @@ -365,7 +367,7 @@ public class ComposedRuleElement extends RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); if (!stream.isSimpleGreedyForComposed()) { result = continueMatch(after, annotation, ruleMatch, ruleApply, containerMatch, sideStepOrigin, entryPoint, stream, crowd); @@ -415,10 +417,10 @@ public class ComposedRuleElement extends RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); RuleElementContainer container = getContainer(); doMatch(after, annotation, ruleMatch, containerMatch, isStartAnchor(), stream, crowd); - if (this.equals(entryPoint) && ruleApply == null) { + if (equals(entryPoint) && ruleApply == null) { result.add(ruleMatch); } else if (container == null) { result = fallback(after, failed, annotation, ruleMatch, ruleApply, containerMatch, @@ -456,7 +458,7 @@ public class ComposedRuleElement extends sideStepOrigin, entryPoint, stream, crowd); } } else { - if (this.equals(entryPoint)) { + if (equals(entryPoint)) { // hotfix for UIMA-3820 result.add(ruleMatch); } else { @@ -515,22 +517,22 @@ public class ComposedRuleElement extends RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); RuleElementContainer parentContainer = getContainer(); if (parentContainer instanceof ComposedRuleElement) { ComposedRuleElement parentElement = (ComposedRuleElement) parentContainer; - result = parentElement.fallbackContinue(after, failed, annotation, ruleMatch, ruleApply, + return parentElement.fallbackContinue(after, failed, annotation, ruleMatch, ruleApply, containerMatch, sideStepOrigin, entryPoint, stream, crowd); - } else if (sideStepOrigin != null && !failed) { - result = sideStepOrigin.continueSideStep(after, ruleMatch, ruleApply, containerMatch, + } + + if (sideStepOrigin != null && !failed) { + return sideStepOrigin.continueSideStep(after, ruleMatch, ruleApply, containerMatch, entryPoint, stream, crowd); - } else { - // take care that failed matches wont be applied - ruleMatch.setMatched(ruleMatch.matched && !failed); - result.add(ruleMatch); - doneMatching(ruleMatch, ruleApply, stream, crowd); } - return result; + + // take care that failed matches wont be applied + ruleMatch.setMatched(ruleMatch.matched && !failed); + doneMatching(ruleMatch, ruleApply, stream, crowd); + return asList(ruleMatch); } private void includeMatch(RuleMatch ruleMatch, ComposedRuleElementMatch containerMatch, @@ -563,7 +565,7 @@ public class ComposedRuleElement extends RutaEnvironment environment = context.getParent().getEnvironment(); environment.addMatchToVariable(ruleMatch, this, context, stream); - List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>( + List<EvaluatedCondition> evaluatedConditions = new ArrayList<>( conditions.size()); for (AbstractRutaCondition condition : conditions) { crowd.beginVisit(condition, null); @@ -652,7 +654,7 @@ public class ComposedRuleElement extends @Override public RuleElement getNextElement(boolean after, RuleElement ruleElement) { // return caretaker.getNextElement(after, ruleElement); - if (conjunct == null || this.equals(ruleElement)) { + if (conjunct == null || equals(ruleElement)) { return caretaker.getNextElement(after, ruleElement); } else { return null; Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/EvaluatedCondition.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/EvaluatedCondition.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/EvaluatedCondition.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/EvaluatedCondition.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,7 +19,9 @@ package org.apache.uima.ruta.rule; -import java.util.ArrayList; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.List; import org.apache.uima.ruta.condition.AbstractRutaCondition; @@ -33,8 +35,6 @@ public class EvaluatedCondition { private final List<EvaluatedCondition> conditions; - private final List<EvaluatedCondition> noConditions = new ArrayList<EvaluatedCondition>(0); - public EvaluatedCondition(AbstractRutaCondition condition, boolean value, List<EvaluatedCondition> conditions) { super(); @@ -47,15 +47,14 @@ public class EvaluatedCondition { super(); this.condition = condition; this.value = value; - this.conditions = noConditions; + conditions = emptyList(); } public EvaluatedCondition(NotCondition condition, boolean value, EvaluatedCondition eval) { super(); this.condition = condition; this.value = value; - this.conditions = new ArrayList<EvaluatedCondition>(); - conditions.add(eval); + conditions = asList(eval); } public AbstractRutaCondition getCondition() { @@ -69,5 +68,4 @@ public class EvaluatedCondition { public List<EvaluatedCondition> getConditions() { return conditions; } - } Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,7 +19,10 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; + import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,7 +47,7 @@ public class RuleMatch extends AbstractR public RuleMatch(RutaRule rule) { super(rule); - delegateApply = new HashMap<RutaElement, ScriptApply>(0); + delegateApply = new HashMap<>(0); } @Override @@ -94,7 +97,7 @@ public class RuleMatch extends AbstractR public List<AnnotationFS> getMatchedAnnotations(List<Integer> indexes, RuleElementContainer container) { - List<AnnotationFS> result = new ArrayList<AnnotationFS>(); + List<AnnotationFS> result = new ArrayList<>(); indexes = extendIndexes(indexes); if (container == null) { container = rule.getRoot(); @@ -103,7 +106,7 @@ public class RuleMatch extends AbstractR // TODO refactor this! if (indexes == null) { List<RuleElement> ruleElements = container.getRuleElements(); - indexes = new ArrayList<Integer>(); + indexes = new ArrayList<>(); for (RuleElement ruleElement : ruleElements) { indexes.add(ruleElements.indexOf(ruleElement) + 1); } @@ -130,7 +133,7 @@ public class RuleMatch extends AbstractR } // TODO rethink the reverse - List<List<List<RuleElementMatch>>> reverseList = new ArrayList<List<List<RuleElementMatch>>>(); + List<List<List<RuleElementMatch>>> reverseList = new ArrayList<>(); for (Integer index : indexes) { if (index > container.getRuleElements().size()) { continue; @@ -155,7 +158,7 @@ public class RuleMatch extends AbstractR boolean mergingRequired = list.size() > 1; for (List<RuleElementMatch> list2 : list) { if (list2 != null) { - if(list2.size()>1) { + if (list2.size() > 1) { mergingRequired = true; } if (list2.size() == 1 && !mergingRequired) { @@ -167,7 +170,7 @@ public class RuleMatch extends AbstractR mergingRequired = true; } } - if(mergingRequired) { + if (mergingRequired) { for (RuleElementMatch ruleElementMatch : list2) { List<AnnotationFS> textsMatched = ruleElementMatch.getTextsMatched(); if (textsMatched != null && !textsMatched.isEmpty()) { @@ -239,7 +242,7 @@ public class RuleMatch extends AbstractR if (indexes == null || indexes.size() <= 1) { return indexes; } - List<Integer> result = new ArrayList<Integer>(); + List<Integer> result = new ArrayList<>(); int pointer = indexes.get(0); for (Integer each : indexes) { while (pointer < each - 1) { @@ -281,8 +284,7 @@ public class RuleMatch extends AbstractR copy.setRootMatch(rootMatch.copy2(extendedContainerMatch, after)); } - Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<RutaElement, ScriptApply>( - delegateApply); + Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<>(delegateApply); copy.setDelegateApply(newDelegateApply); return copy; } @@ -291,8 +293,7 @@ public class RuleMatch extends AbstractR RuleMatch copy = new RuleMatch(rule); copy.setMatched(matched); copy.setRootMatch(rootMatch.copy()); - Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<RutaElement, ScriptApply>( - delegateApply); + Map<RutaElement, ScriptApply> newDelegateApply = new HashMap<>(delegateApply); copy.setDelegateApply(newDelegateApply); return copy; } @@ -331,29 +332,28 @@ public class RuleMatch extends AbstractR public List<List<RuleElementMatch>> getMatchInfo(RuleElementMatch rootMatch, RuleElement element) { - List<List<RuleElementMatch>> result = new ArrayList<List<RuleElementMatch>>(); - RuleElement root = rootMatch.getRuleElement(); - if (element.equals(root)) { - List<RuleElementMatch> list = new ArrayList<RuleElementMatch>(1); - list.add(rootMatch); - result.add(list); - } else if (rootMatch instanceof ComposedRuleElementMatch) { + if (element.equals(rootMatch.getRuleElement())) { + return asList(asList(rootMatch)); + } + + if (rootMatch instanceof ComposedRuleElementMatch) { + List<List<RuleElementMatch>> result = new ArrayList<>(); ComposedRuleElementMatch crem = (ComposedRuleElementMatch) rootMatch; Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = crem.getInnerMatches().entrySet(); for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) { List<RuleElementMatch> value = entry.getValue(); if (element.equals(entry.getKey())) { result.add(value); - } else { - if (value != null) { - for (RuleElementMatch eachMatch : value) { - result.addAll(getMatchInfo(eachMatch, element)); - } + } else if (value != null) { + for (RuleElementMatch eachMatch : value) { + result.addAll(getMatchInfo(eachMatch, element)); } } } + return result; } - return result; + + return Collections.emptyList(); } public RuleElementMatch getLastMatch(RuleElement ruleElement, boolean direction) { Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java Mon May 11 16:47:27 2020 @@ -19,10 +19,14 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.uima.cas.CAS; @@ -48,55 +52,54 @@ public class RutaAnnotationTypeMatcher i @Override public Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent, RutaStream stream) { - Collection<AnnotationFS> result = null; MatchContext context = new MatchContext(parent); // just for forcing expression top initialize // TODO this is maybe a bit expensive sometimes expression.getType(context, stream); 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)); + Collection<AnnotationFS> result = expression.getAnnotationList(context, stream); + // avoid null lists here + return result != null ? result : emptyList(); + } + + if (expression.getAnnotationExpression() != null) { + return asList(expression.getAnnotation(context, stream)); + } + + // TODO defer to getter of expression? + List<Type> types; + if (expression.getTypeListExpression() != null) { + types = expression.getTypeListExpression().getTypeList(context, stream); } else { - - // TODO defer to getter of expression? - List<Type> types = null; - if (expression.getTypeListExpression() != null) { - types = expression.getTypeListExpression().getTypeList(context, stream); + types = asList(getType(context.getParent(), stream)); + } + + Collection<AnnotationFS> result = new ArrayList<>(); + for (Type type : types) { + if (type == null) { + continue; + } + + Type overallDAType = stream.getCas().getDocumentAnnotation().getType(); + String name = type.getName(); + if (CAS.TYPE_NAME_DOCUMENT_ANNOTATION.equals(name) + || "org.apache.uima.ruta.type.Document".equals(name) + || overallDAType.equals(type)) { + // TODO what about dynamic windowing? + result.add(stream.getDocumentAnnotation()); } else { - Type type = getType(context.getParent(), stream); - types = new ArrayList<>(1); - if (type != null) { - types.add(type); - } + result.addAll(stream.getAnnotations(type)); } - result = new ArrayList<>(); - for (Type type : types) { - if (type != null) { - - Type overallDAType = stream.getCas().getDocumentAnnotation().getType(); - String name = type.getName(); - if (StringUtils.equals(CAS.TYPE_NAME_DOCUMENT_ANNOTATION, name) - || "org.apache.uima.ruta.type.Document".equals(name) - || overallDAType.equals(type)) { - // TODO what about dynamic windowing? - result.add(stream.getDocumentAnnotation()); - } else { - result.addAll(stream.getAnnotations(type)); - } - if (expression.getFeatureExpression() != null) { - result = new ArrayList<>(expression.getFeatureExpression().getAnnotations(result, - CHECK_ON_FEATURE, context, stream)); - } - } + + // TODO: Throwing away the result so far in this for loop seems odd + if (expression.getFeatureExpression() != null) { + @SuppressWarnings("unchecked") + Collection<AnnotationFS> r = (Collection<AnnotationFS>) expression.getFeatureExpression() + .getAnnotations(result, CHECK_ON_FEATURE, context, stream); + result = r; } - } + return result; } @@ -104,17 +107,19 @@ public class RutaAnnotationTypeMatcher i public Collection<? extends AnnotationFS> getAnnotationsAfter(RutaRuleElement ruleElement, AnnotationFS annotation, RutaBlock parent, RutaStream stream) { if (annotation.getEnd() == stream.getDocumentAnnotation().getEnd()) { - return Collections.emptyList(); + return emptyList(); } + RutaBasic lastBasic = stream.getEndAnchor(annotation.getEnd()); int end = 0; if (lastBasic == null) { if (annotation.getEnd() != 0) { - return Collections.emptyList(); + return emptyList(); } } else { end = lastBasic.getEnd(); } + if (end == stream.getDocumentAnnotation().getBegin()) { // non existing wildcard match stream.moveToFirst(); @@ -128,74 +133,73 @@ public class RutaAnnotationTypeMatcher i stream.moveToFirst(); } - if (stream.isValid()) { - RutaBasic nextBasic = (RutaBasic) stream.get(); - // TODO HOTFIX for annotation of length 0 - while (stream.isValid() && nextBasic.getBegin() < end) { - stream.moveToNext(); - if (stream.isValid()) { - nextBasic = (RutaBasic) stream.get(); - } + if (!stream.isValid()) { + return emptyList(); + } + + RutaBasic nextBasic = (RutaBasic) stream.get(); + // TODO HOTFIX for annotation of length 0 + while (stream.isValid() && nextBasic.getBegin() < end) { + stream.moveToNext(); + if (stream.isValid()) { + nextBasic = (RutaBasic) stream.get(); } + } - MatchContext context = new MatchContext(parent); - // just for forcing expression top initialize - expression.getType(context, stream); - if (expression.getAnnotationExpression() != null) { - - AnnotationFS ref = expression.getAnnotation(context, stream); - if (ref != null) { - boolean beginsWith = nextBasic.getBegin() == ref.getBegin(); - if (beginsWith) { - Collection<AnnotationFS> result = new ArrayList<>(1); - result.add(ref); - return result; - } - } - } else if (expression.getAnnotationListExpression() != null) { - List<AnnotationFS> annotations = expression.getAnnotationList(context, stream); - Collection<AnnotationFS> result = new ArrayList<>(); - for (AnnotationFS each : annotations) { - boolean beginsWith = nextBasic.getBegin() == each.getBegin(); - if (beginsWith) { - result.add(each); + MatchContext context = new MatchContext(parent); + // just for forcing expression top initialize + expression.getType(context, stream); + if (expression.getAnnotationExpression() != null) { + AnnotationFS ref = expression.getAnnotation(context, stream); + + if (ref == null) { + return emptyList(); + } + + if (nextBasic.getBegin() == ref.getBegin()) { + return asList(ref); + } + + return emptyList(); + } + + if (expression.getAnnotationListExpression() != null) { + RutaBasic referenceElement = nextBasic; + return expression.getAnnotationList(context, stream).stream() + .filter(each -> referenceElement.getBegin() == each.getBegin()) + .collect(Collectors.toList()); + } + + List<Type> types; + if (expression.getTypeListExpression() != null) { + types = expression.getTypeListExpression().getTypeList(context, stream); + } else { + types = asList(getType(context.getParent(), stream)); + } + + List<AnnotationFS> annotations = new ArrayList<>(); + for (Type type : types) { + Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(type); + Collection<AnnotationFS> anchors = new ArrayList<>(beginAnchors.size()); + + if (beginAnchors != null) { + for (AnnotationFS afs : beginAnchors) { + if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin() + && afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) { + anchors.add(afs); } } - return result; + } + + if (expression.getFeatureExpression() != null) { + annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors, + CHECK_ON_FEATURE, context, stream)); } else { - List<Type> types = null; - if (expression.getTypeListExpression() != null) { - types = expression.getTypeListExpression().getTypeList(context, stream); - } else { - Type type = getType(context.getParent(), stream); - types = new ArrayList<>(1); - types.add(type); - } - List<AnnotationFS> annotations = new ArrayList<>(); - for (Type type : types) { - Collection<AnnotationFS> anchors = new ArrayList<>(); - Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(type); - if (beginAnchors != null) { - for (AnnotationFS afs : beginAnchors) { - if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin() - && afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) { - anchors.add(afs); - } - } - } - if (expression.getFeatureExpression() != null) { - annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors, - CHECK_ON_FEATURE, context, stream)); - } else { - annotations.addAll(anchors); - } - - } - return annotations; + annotations.addAll(anchors); } - } - return Collections.emptyList(); + + return annotations; } @Override @@ -204,10 +208,12 @@ public class RutaAnnotationTypeMatcher i if (annotation.getBegin() == stream.getDocumentAnnotation().getBegin()) { return Collections.emptyList(); } + RutaBasic firstBasic = stream.getBeginAnchor(annotation.getBegin()); if (firstBasic == null) { return Collections.emptyList(); } + stream.moveTo(firstBasic); if (stream.isVisible(firstBasic)) { stream.moveToPrevious(); @@ -217,69 +223,67 @@ public class RutaAnnotationTypeMatcher i stream.moveToLast(); } - if (stream.isValid()) { - RutaBasic nextBasic = (RutaBasic) stream.get(); - // TODO HOTFIX for annotation of length 0 - while (stream.isValid() && nextBasic.getEnd() > firstBasic.getBegin()) { - stream.moveToPrevious(); - if (stream.isValid()) { - nextBasic = (RutaBasic) stream.get(); - } + if (!stream.isValid()) { + return emptyList(); + } + + RutaBasic nextBasic = (RutaBasic) stream.get(); + // TODO HOTFIX for annotation of length 0 + while (stream.isValid() && nextBasic.getEnd() > firstBasic.getBegin()) { + stream.moveToPrevious(); + if (stream.isValid()) { + nextBasic = (RutaBasic) stream.get(); } + } - MatchContext context = new MatchContext(parent); - // just for forcing expression top initialize - expression.getType(context, stream); - if (expression.getAnnotationExpression() != null) { - AnnotationFS ref = expression.getAnnotationExpression().getAnnotation(context, stream); - boolean endsWith = nextBasic.getEnd() == ref.getEnd(); - if (endsWith) { - Collection<AnnotationFS> result = new ArrayList<>(1); - result.add(ref); - return result; - } - } else if (expression.getAnnotationListExpression() != null) { - List<AnnotationFS> annotations = expression.getAnnotationListExpression() - .getAnnotationList(context, stream); - for (AnnotationFS each : annotations) { - boolean endsWith = nextBasic.getEnd() == each.getEnd(); - if (endsWith) { - Collection<AnnotationFS> result = new ArrayList<>(); - result.add(each); - return result; + MatchContext context = new MatchContext(parent); + // just for forcing expression top initialize + expression.getType(context, stream); + if (expression.getAnnotationExpression() != null) { + AnnotationFS ref = expression.getAnnotationExpression().getAnnotation(context, stream); + + if (nextBasic.getEnd() == ref.getEnd()) { + return asList(ref); + } + + return emptyList(); + } + + if (expression.getAnnotationListExpression() != null) { + RutaBasic referenceElement = nextBasic; + return expression.getAnnotationListExpression().getAnnotationList(context, stream).stream() + .filter(each -> referenceElement.getEnd() == each.getEnd()) + .collect(Collectors.toList()); + } + + List<Type> types; + if (expression.getTypeListExpression() != null) { + types = expression.getTypeListExpression().getTypeList(context, stream); + } else { + types = asList(getType(context.getParent(), stream)); + } + + List<AnnotationFS> annotations = new ArrayList<>(); + for (Type type : types) { + Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(type); + Collection<AnnotationFS> anchors = new ArrayList<>(endAnchors.size()); + if (endAnchors != null) { + for (AnnotationFS afs : endAnchors) { + if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) { + anchors.add(afs); } } + } + + if (expression.getFeatureExpression() != null) { + annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors, + CHECK_ON_FEATURE, context, stream)); } else { - List<Type> types = null; - if (expression.getTypeListExpression() != null) { - types = expression.getTypeListExpression().getTypeList(context, stream); - } else { - Type type = getType(context.getParent(), stream); - types = new ArrayList<>(1); - types.add(type); - } - List<AnnotationFS> annotations = new ArrayList<>(); - for (Type type : types) { - Collection<AnnotationFS> anchors = new ArrayList<>(); - Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(type); - if (endAnchors != null) { - for (AnnotationFS afs : endAnchors) { - if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) { - anchors.add(afs); - } - } - } - if (expression.getFeatureExpression() != null) { - annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors, - CHECK_ON_FEATURE, context, stream)); - } else { - annotations.addAll(anchors); - } - } - return annotations; + annotations.addAll(anchors); } } - return Collections.emptyList(); + + return annotations; } @Override @@ -341,5 +345,4 @@ public class RutaAnnotationTypeMatcher i return null; } - } Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java Mon May 11 16:47:27 2020 @@ -29,6 +29,10 @@ import org.apache.uima.ruta.expression.I public interface RutaMatcher { + /** + * @return a collection containing the matching annotations. Note that there is no + * guarantee that the returned collection is modifiable. + */ Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent, RutaStream stream); Type getType(RutaBlock parent, RutaStream stream); @@ -37,9 +41,17 @@ public interface RutaMatcher { long estimateAnchors(RutaBlock parent, RutaStream stream); + /** + * @return a collection containing the matching annotations. Note that there is no + * guarantee that the returned collection is modifiable. + */ Collection<? extends AnnotationFS> getAnnotationsAfter(RutaRuleElement ruleElement, AnnotationFS annotation, RutaBlock parent, RutaStream stream); + /** + * @return a collection containing the matching annotations. Note that there is no + * guarantee that the returned collection is modifiable. + */ Collection<? extends AnnotationFS> getAnnotationsBefore(RutaRuleElement ruleElement, AnnotationFS annotation, RutaBlock parent, RutaStream stream); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaOptionalRuleElement.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,9 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -62,15 +65,13 @@ public class RutaOptionalRuleElement ext InferenceCrowd crowd) { RuleElementMatch result = new RuleElementMatch(this, containerMatch); result.setRuleAnchor(ruleAnchor); - List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>( + List<EvaluatedCondition> evaluatedConditions = new ArrayList<>( conditions.size()); boolean base = true; MatchContext context = new MatchContext(annotation, this, ruleMatch, after); - List<AnnotationFS> textsMatched = new ArrayList<AnnotationFS>(1); - if (annotation != null) { - textsMatched.add(annotation); - } + List<AnnotationFS> textsMatched = annotation != null ? asList(annotation) : emptyList(); + // already set the matched text and inform others result.setMatchInfo(base, textsMatched, stream); RutaEnvironment environment = context.getParent().getEnvironment(); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,9 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -56,7 +59,7 @@ public class RutaRuleElement extends Abs public List<RuleMatch> startMatch(RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); Collection<? extends AnnotationFS> anchors = getAnchors(stream); boolean useAlternatives = anchors.size() != 1; for (AnnotationFS eachAnchor : anchors) { @@ -72,7 +75,7 @@ public class RutaRuleElement extends Abs extendedMatch = ruleMatch.copy(extendedContainerMatch, true); } doMatch(true, eachAnchor, extendedMatch, extendedContainerMatch, true, stream, crowd); - if (this.equals(entryPoint) && ruleApply == null) { + if (equals(entryPoint) && ruleApply == null) { result.add(extendedMatch); } else if (extendedMatch.matched()) { RuleElement after = getContainer().getNextElement(true, this); @@ -129,7 +132,7 @@ public class RutaRuleElement extends Abs RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); MatchContext context = new MatchContext(this, ruleMatch, after); if (quantifier.continueMatch(after, context, annotation, containerMatch, stream, crowd)) { boolean stopMatching = false; @@ -156,7 +159,7 @@ public class RutaRuleElement extends Abs lastAnchor = eachAnchor; eachAnchor = nextAnnotations.iterator().next(); doMatch(after, eachAnchor, extendedMatch, extendedContainerMatch, false, stream, crowd); - if (this.equals(entryPoint)) { + if (equals(entryPoint)) { result.add(extendedMatch); } else if (extendedMatch.matched()) { if (quantifier.continueMatch(after, context, eachAnchor, extendedContainerMatch, stream, @@ -189,7 +192,7 @@ public class RutaRuleElement extends Abs AnnotationFS eachAnchor, RuleMatch extendedMatch, RuleApply ruleApply, ComposedRuleElementMatch extendedContainerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); RuleElement nextRuleElement = getContainer().getNextElement(after, this); if (nextRuleElement != null) { result = nextRuleElement.continueMatch(after, eachAnchor, extendedMatch, ruleApply, @@ -206,7 +209,7 @@ public class RutaRuleElement extends Abs public List<RuleMatch> continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); // if() for really lazy quantifiers MatchContext context = new MatchContext(this, ruleMatch, after); if (quantifier.continueMatch(after, context, annotation, containerMatch, stream, crowd)) { @@ -231,7 +234,7 @@ public class RutaRuleElement extends Abs } doMatch(after, eachAnchor, extendedMatch, extendedContainerMatch, false, stream, crowd); - if (this.equals(entryPoint) && ruleApply == null) { + if (equals(entryPoint) && ruleApply == null) { result.add(extendedMatch); } else if (extendedMatch.matched()) { context = new MatchContext(this, extendedMatch, after); @@ -247,7 +250,7 @@ public class RutaRuleElement extends Abs result.addAll(continueMatchSomewhereElse); } } else { - if (this.equals(entryPoint)) { + if (equals(entryPoint)) { // hotfix for UIMA-3820 result.add(extendedMatch); } else { @@ -272,8 +275,8 @@ public class RutaRuleElement extends Abs RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd, RuleElement entryPoint) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); - if (this.equals(entryPoint) && ruleApply == null) { + List<RuleMatch> result = new ArrayList<>(); + if (equals(entryPoint) && ruleApply == null) { result.add(ruleMatch); return result; } @@ -332,16 +335,14 @@ public class RutaRuleElement extends Abs InferenceCrowd crowd) { RuleElementMatch result = new RuleElementMatch(this, containerMatch); result.setRuleAnchor(ruleAnchor); - List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>( + List<EvaluatedCondition> evaluatedConditions = new ArrayList<>( conditions.size()); // boolean base = matcher.match(annotation, stream, getParent()); boolean base = true; MatchContext context = new MatchContext(annotation, this, ruleMatch, after); - List<AnnotationFS> textsMatched = new ArrayList<AnnotationFS>(1); - if (annotation != null) { - textsMatched.add(annotation); - } + List<AnnotationFS> textsMatched = annotation != null ? asList(annotation) : emptyList(); + // already set the matched text and inform others result.setMatchInfo(base, textsMatched, stream); RutaEnvironment environment = context.getParent().getEnvironment(); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,9 @@ package org.apache.uima.ruta.rule; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -89,14 +92,14 @@ public class WildCardRuleElement extends if (nextElement == null) { nextDepth = 0; } - return new ImmutablePair<RuleElement, Integer>(nextElement, Integer.valueOf(nextDepth)); + return new ImmutablePair<>(nextElement, Integer.valueOf(nextDepth)); } private List<RuleMatch> tryWithNextRuleElement(RuleElement nextElement, boolean after, AnnotationFS annotation, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); // what is the next stuff that should match? if (nextElement == null) { AnnotationFS afs = getCoveredByWildCard(after, annotation, null, stream); @@ -133,7 +136,7 @@ public class WildCardRuleElement extends ComposedRuleElement cre, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); AnnotationFS nextOne = annotation; boolean doneHere = false; while (!doneHere @@ -210,7 +213,7 @@ public class WildCardRuleElement extends } else if (conjunct != null && !conjunct) { // disjunctive List<RuleElement> ruleElements = cre.getRuleElements(); - List<AnnotationFS> nextPostions = new ArrayList<AnnotationFS>(); + List<AnnotationFS> nextPostions = new ArrayList<>(); for (RuleElement ruleElement : ruleElements) { if (ruleElement instanceof ComposedRuleElement) { AnnotationFS nextPositionForComposed = getNextPositionForComposed( @@ -307,7 +310,7 @@ public class WildCardRuleElement extends RuleElement nextElement, Type defaultType, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin, RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); FSIterator<AnnotationFS> iterator = getIterator(after, annotation, nextElement, defaultType, stream); // already matched something maybe, but now at the end of the document @@ -602,7 +605,7 @@ public class WildCardRuleElement extends RutaRuleElement nextElement, RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch, int nextDepth, RuleElement sideStepOrigin, RutaStream stream, InferenceCrowd crowd) { - List<RuleMatch> result = new ArrayList<RuleMatch>(); + List<RuleMatch> result = new ArrayList<>(); RutaLiteralMatcher matcher = (RutaLiteralMatcher) nextElement.getMatcher(); if (matcher == null) { return result; @@ -745,16 +748,14 @@ public class WildCardRuleElement extends RuleElementMatch result = new RuleElementMatch(this, containerMatch); result.setRuleAnchor(ruleAnchor); - List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>( + List<EvaluatedCondition> evaluatedConditions = new ArrayList<>( conditions.size()); boolean base = true; MatchContext context = new MatchContext(annotation, this, ruleMatch, true); - List<AnnotationFS> textsMatched = new ArrayList<AnnotationFS>(1); - if (annotation != null) { - textsMatched.add(annotation); - } + List<AnnotationFS> textsMatched = annotation != null ? asList(annotation) : emptyList(); + result.setMatchInfo(base, textsMatched, stream); RutaEnvironment environment = context.getParent().getEnvironment(); environment.addMatchToVariable(ruleMatch, this, context, stream); Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/InferenceCrowd.java URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/InferenceCrowd.java?rev=1877591&r1=1877590&r2=1877591&view=diff ============================================================================== --- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/InferenceCrowd.java (original) +++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/InferenceCrowd.java Mon May 11 16:47:27 2020 @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -19,6 +19,8 @@ package org.apache.uima.ruta.visitor; +import static java.util.Arrays.asList; + import java.util.Collections; import java.util.List; @@ -31,13 +33,14 @@ import org.apache.uima.ruta.rule.Abstrac public class InferenceCrowd implements RutaInferenceVisitor { - public static InferenceCrowd emptyCrowd = new InferenceCrowd(Collections.<RutaInferenceVisitor>emptyList()); - - private final List<RutaInferenceVisitor> visitors; + public static InferenceCrowd emptyCrowd = new InferenceCrowd( + Collections.<RutaInferenceVisitor> emptyList()); + + private final RutaInferenceVisitor[] visitors; public InferenceCrowd(List<RutaInferenceVisitor> visitors) { super(); - this.visitors = visitors; + this.visitors = visitors.toArray(new RutaInferenceVisitor[visitors.size()]); } @Override @@ -55,8 +58,9 @@ public class InferenceCrowd implements R } public void finished(RutaStream stream) { + List<RutaInferenceVisitor> visitorList = asList(visitors); for (RutaInferenceVisitor each : visitors) { - each.finished(stream, visitors); + each.finished(stream, visitorList); } } @@ -68,13 +72,8 @@ public class InferenceCrowd implements R @Override public void annotationAdded(AnnotationFS annotation, AbstractRuleMatch<? extends AbstractRule> creator) { - if (visitors.isEmpty()) { - return; - } for (RutaInferenceVisitor each : visitors) { each.annotationAdded(annotation, creator); } - } - }