diff --git a/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g b/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g index 4e5f7fb7c..4627ba17d 100644 --- a/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g +++ b/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g @@ -2431,7 +2431,7 @@ options { | match = dottedIdWithIndex2 (comp = LESS | comp = GREATER | comp = GREATEREQUAL | comp = LESSEQUAL |comp = EQUAL | comp = NOTEQUAL) arg = argument {MatchReference mr = expressionFactory.createMatchReference(match, comp, arg); expr = expressionFactory.createAnnotationTypeExpression(mr);} - + | (complexStringExpression) => cse = complexStringExpression {expr = cse;} | (featureExpression)=> fe = featureExpression {expr = expressionFactory.createGenericFeatureExpression(fe);} | a2 = booleanExpression {expr = a2;} | a3 = numberExpression {expr = a3;} @@ -2455,8 +2455,8 @@ options { } : (featureExpression)=> fe = featureExpression {expr = expressionFactory.createGenericFeatureExpression(fe);} - | a2 = booleanExpression {expr = a2;} - | a3 = numberExpression {expr = a3;} + | a2 = simpleBooleanExpression {expr = a2;} + | a3 = simpleNumberExpression {expr = a3;} | a4 = stringExpression {expr = a4;} | (listExpression)=> l = listExpression {expr = l;} | a5 = nullExpression {expr = a5;} @@ -2778,6 +2778,18 @@ List exprs = new ArrayList(); |(e = stringFunction)=> e = stringFunction{expr = e;} ; +complexStringExpression returns [IStringExpression expr = null] +options { + backtrack = true; +} +@init {List list = new ArrayList();} + : + a1 = simpleArgument {list.add(a1);} + ((PLUS)=>PLUS an = simpleArgument {list.add(an);})+ + {expr = expressionFactory.createGenericComposedStringExpression(list);} + ; + + // not checked stringFunction returns [IStringExpression expr = null] @init {List list = new ArrayList();} diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java b/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java index 8f56d83d8..0a7310872 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java @@ -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 @@ -106,7 +106,7 @@ public RutaScriptBlock createScriptBlock(String text, RutaRuleElement ruleElemen if (ruleElement != null) { rule = createRule(ruleElement, parent); } - List elements = new ArrayList(); + List elements = new ArrayList<>(); if (body != null) { for (RutaStatement each : body) { if (each != null) { @@ -121,7 +121,7 @@ public RutaScriptBlock createRootScriptBlock(String module, String pack) { String defaultNamespace = getDefaultNamespace(module, pack); RutaScriptBlock result = createScriptBlock(module, null, null, null, defaultNamespace); - List ruleElements = new ArrayList(); + List ruleElements = new ArrayList<>(); RuleElementIsolator container = new RuleElementIsolator(); ITypeExpression documentExpression = expressionFactory .createSimpleTypeExpression(CAS.TYPE_NAME_DOCUMENT_ANNOTATION, null); @@ -167,13 +167,13 @@ public RutaBlock createForEachBlock(Token varToken, IBooleanExpression direction } public RutaRule createRule(RuleElement element, RutaBlock parent) { - List elements = new ArrayList(); + List elements = new ArrayList<>(); elements.add(element); return createRule(elements, parent); } public RutaStatement createImplicitRule(List actions, RutaBlock parent) { - List elements = new ArrayList(); + List elements = new ArrayList<>(); ITypeExpression documentExpression = expressionFactory.createSimpleTypeExpression("Document", parent); MatchReference mr = expressionFactory.createMatchReference(documentExpression); @@ -293,15 +293,15 @@ public List processConjunctRules(List reList, List> map = new TreeMap>(); - List connectors = new ArrayList(); + Map> map = new TreeMap<>(); + List connectors = new ArrayList<>(); int reCounter = 0; int conCounter = 0; for (Token token : conList) { if (token == null) { List list = map.get(conCounter); if (list == null) { - list = new ArrayList(); + list = new ArrayList<>(); map.put(conCounter, list); } RuleElement e = reList.get(reCounter); @@ -312,7 +312,7 @@ public List processConjunctRules(List reList, List elements = new ArrayList(); + List elements = new ArrayList<>(); ConjunctRulesRuleElement cr = new ConjunctRulesRuleElement(null, container, env); for (List each : map.values()) { @@ -324,7 +324,7 @@ public List processConjunctRules(List reList, List result = new ArrayList(); + List result = new ArrayList<>(); result.add(cr); return result; } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java b/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java index 7a5c6126e..bd2ec85af 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java @@ -204,11 +204,10 @@ private void updateIterators(CAS cas, Type basicType, FilterManager filter, AnnotationFS additionalWindow) { if (additionalWindow != null) { // TODO UIMA-6281 replace select - this.basicIt = cas.getAnnotationIndex(basicType).select().coveredBy(additionalWindow) - .fsIterator(); + basicIt = cas.getAnnotationIndex(basicType).select().coveredBy(additionalWindow).fsIterator(); // was: this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow); } else { - this.basicIt = cas.getAnnotationIndex(basicType).iterator(); + basicIt = cas.getAnnotationIndex(basicType).iterator(); } currentIt = filter.createFilteredIterator(cas, basicType); } @@ -346,8 +345,9 @@ private void createBasicsForAnchors(List anchors) { createRutaBasic(0, 0); } else if (anchors.size() == 1) { Integer first = anchors.get(0); - if (first >= 0 && first <= cas.getDocumentText().length()) + if (first >= 0 && first <= cas.getDocumentText().length()) { createRutaBasic(first, first); + } } else { for (int i = 0; i < anchors.size() - 1; i++) { Integer first = anchors.get(i); @@ -1231,7 +1231,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, } else if (value instanceof IStringExpression) { IStringExpression stringExpr = (IStringExpression) value; String string = stringExpr.getStringValue(context, this); - StringArrayFS array = FSCollectionFactory.createStringArrayFS(cas, new String[] { string }); + StringArrayFS array = FSCollectionFactory.createStringArrayFS(cas, string); annotation.setFeatureValue(feature, array); } } else if (rangeName.equals(CAS.TYPE_NAME_INTEGER) || rangeName.equals(CAS.TYPE_NAME_LONG) @@ -1253,7 +1253,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, if (value instanceof INumberExpression) { INumberExpression numberExpr = (INumberExpression) value; int v = numberExpr.getIntegerValue(context, this); - IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas, new int[] { v }); + IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas, v); annotation.setFeatureValue(feature, array); } else if (value instanceof INumberListExpression) { INumberListExpression expr = (INumberListExpression) value; @@ -1272,7 +1272,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, if (value instanceof INumberExpression) { INumberExpression numberExpr = (INumberExpression) value; double v = numberExpr.getDoubleValue(context, this); - DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas, new double[] { v }); + DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas, v); annotation.setFeatureValue(feature, array); } else if (value instanceof INumberListExpression) { INumberListExpression expr = (INumberListExpression) value; @@ -1291,7 +1291,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, if (value instanceof INumberExpression) { INumberExpression numberExpr = (INumberExpression) value; float v = numberExpr.getFloatValue(context, this); - FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas, new float[] { v }); + FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas, v); annotation.setFeatureValue(feature, array); } else if (value instanceof INumberListExpression) { INumberListExpression expr = (INumberListExpression) value; @@ -1305,6 +1305,9 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, IBooleanExpression expr = (IBooleanExpression) value; Boolean v = expr.getBooleanValue(context, this); annotation.setBooleanValue(feature, v); + } else if (value instanceof IStringExpression) { + String stringValue = ((IStringExpression) value).getStringValue(context, this); + annotation.setBooleanValue(feature, Boolean.parseBoolean(stringValue)); } } else if (rangeName.equals(CAS.TYPE_NAME_BOOLEAN_ARRAY)) { if (value instanceof IBooleanListExpression) { @@ -1315,7 +1318,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature, } else if (value instanceof IBooleanExpression) { IBooleanExpression expr = (IBooleanExpression) value; Boolean v = expr.getBooleanValue(context, this); - BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, new boolean[] { v }); + BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, v); annotation.setFeatureValue(feature, array); } } else if (value instanceof AnnotationTypeExpression && !range.isPrimitive()) { diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/action/AbstractMarkAction.java b/ruta-core/src/main/java/org/apache/uima/ruta/action/AbstractMarkAction.java index 7ec80235c..4cfc7e69b 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/action/AbstractMarkAction.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/action/AbstractMarkAction.java @@ -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 @@ -66,7 +66,7 @@ public String toString() { boolean getDictWSParamValue(MatchContext context) { Object configParameterValue = context.getParent().getContext() - .getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS); + .getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS); if (configParameterValue instanceof Boolean) { return (Boolean) configParameterValue; } @@ -76,7 +76,7 @@ boolean getDictWSParamValue(MatchContext context) { protected List getIndexList(MatchContext context, List list, RutaStream stream) { RuleElement element = context.getElement(); - List indexList = new ArrayList(); + List indexList = new ArrayList<>(); if (list == null || list.isEmpty()) { int self = element.getContainer().getRuleElements().indexOf(element) + 1; indexList.add(self); diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/action/ColorAction.java b/ruta-core/src/main/java/org/apache/uima/ruta/action/ColorAction.java index 33045c5c5..c6c2bb2c4 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/action/ColorAction.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/action/ColorAction.java @@ -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 diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkFastReloadAction.java b/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkFastReloadAction.java index 1f90f190d..b2a93e534 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkFastReloadAction.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/action/MarkFastReloadAction.java @@ -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 @@ -49,113 +49,123 @@ public class MarkFastReloadAction extends AbstractMarkAction { - private static ConcurrentHashMap twlCache = new ConcurrentHashMap(); + private static ConcurrentHashMap twlCache = new ConcurrentHashMap<>(); - private IStringExpression listName; + private IStringExpression listName; - private IStringListExpression stringList; + private IStringListExpression stringList; - private IBooleanExpression ignore; + private IBooleanExpression ignore; - private INumberExpression ignoreLength; + private INumberExpression ignoreLength; - private IBooleanExpression ignoreWS; + private IBooleanExpression ignoreWS; - private class TwlCacheEntry { - private TreeWordList twl; - private long lastModified; + private class TwlCacheEntry { + private TreeWordList twl; - public TwlCacheEntry(TreeWordList twl, long lastModified) { - this.lastModified = lastModified; - this.twl = twl; - } - } + private long lastModified; - public MarkFastReloadAction(ITypeExpression type, IStringExpression listName, IBooleanExpression ignore, INumberExpression ignoreLength, IBooleanExpression ignoreWS) { - super(type); - this.listName = listName; - this.ignore = ignore == null ? new SimpleBooleanExpression(false) : ignore; - this.ignoreLength = ignoreLength == null ? new SimpleNumberExpression(Integer.valueOf(0)) : ignoreLength; - this.ignoreWS = ignoreWS == null ? new SimpleBooleanExpression(true) : ignoreWS; + public TwlCacheEntry(TreeWordList twl, long lastModified) { + this.lastModified = lastModified; + this.twl = twl; } - - @Override - public void execute(MatchContext context, RutaStream stream, InferenceCrowd crowd) { - - TreeWordList wl = null; - - RuleMatch match = context.getRuleMatch(); - RuleElement element = context.getElement(); - String listNameValue = listName.getStringValue(context, stream); - - ResourceLoader resourceLoader = new RutaResourceLoader(element.getParent().getEnvironment().getResourcePaths()); - Resource resource = resourceLoader.getResource(listNameValue); - if (resource.exists()) { - File resourceFile = null; - try { - resourceFile = resource.getFile(); - } catch (IOException e1) { - Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unable to obtain file from resource: " + listNameValue, e1); - } - - TwlCacheEntry cacheEntry = twlCache.get(listNameValue); - if (cacheEntry == null || cacheEntry != null && resourceFile.lastModified() > cacheEntry.lastModified) { - Logger.getLogger(this.getClass().getName()).info("Creating Tree Word List from resource: " + listNameValue); - - UimaContext uimaContext = element.getParent().getContext(); - Boolean dictRemoveWS = false; - if (uimaContext != null) { - dictRemoveWS = (Boolean) uimaContext.getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS); - if (dictRemoveWS == null) { - dictRemoveWS = false; - } - } - - try { - wl = new TreeWordList(resource, dictRemoveWS); - } catch (IOException e) { - Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Unable to create TWL: " + listNameValue, e); - } - - twlCache.put(listNameValue, new TwlCacheEntry(wl, resourceFile.lastModified())); - } else { - wl = cacheEntry.twl; - } - - } else { - Logger.getLogger(this.getClass().getName()).severe("Can't find resource: " + listNameValue); + } + + public MarkFastReloadAction(ITypeExpression type, IStringExpression listName, + IBooleanExpression ignore, INumberExpression ignoreLength, IBooleanExpression ignoreWS) { + super(type); + this.listName = listName; + this.ignore = ignore == null ? new SimpleBooleanExpression(false) : ignore; + this.ignoreLength = ignoreLength == null ? new SimpleNumberExpression(Integer.valueOf(0)) + : ignoreLength; + this.ignoreWS = ignoreWS == null ? new SimpleBooleanExpression(true) : ignoreWS; + } + + @Override + public void execute(MatchContext context, RutaStream stream, InferenceCrowd crowd) { + + TreeWordList wl = null; + + RuleMatch match = context.getRuleMatch(); + RuleElement element = context.getElement(); + String listNameValue = listName.getStringValue(context, stream); + + ResourceLoader resourceLoader = new RutaResourceLoader( + element.getParent().getEnvironment().getResourcePaths()); + Resource resource = resourceLoader.getResource(listNameValue); + if (resource.exists()) { + File resourceFile = null; + try { + resourceFile = resource.getFile(); + } catch (IOException e1) { + Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, + "Unable to obtain file from resource: " + listNameValue, e1); + } + + TwlCacheEntry cacheEntry = twlCache.get(listNameValue); + if (cacheEntry == null + || cacheEntry != null && resourceFile.lastModified() > cacheEntry.lastModified) { + Logger.getLogger(this.getClass().getName()) + .info("Creating Tree Word List from resource: " + listNameValue); + + UimaContext uimaContext = element.getParent().getContext(); + Boolean dictRemoveWS = false; + if (uimaContext != null) { + dictRemoveWS = (Boolean) uimaContext + .getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS); + if (dictRemoveWS == null) { + dictRemoveWS = false; + } } - List matchedAnnotationsOf = match.getMatchedAnnotationsOfElement(element); - for (AnnotationFS annotationFS : matchedAnnotationsOf) { - RutaStream windowStream = stream.getWindowStream(annotationFS, annotationFS.getType()); - Collection found = wl.find(windowStream, this.getIgnore().getBooleanValue(context, stream), - this.getIgnoreLength().getIntegerValue(context, stream), null, 0, this.getIgnoreWS().getBooleanValue(context, stream)); - for (AnnotationFS annotation : found) { - createAnnotation(annotation, context, windowStream); - } + try { + wl = new TreeWordList(resource, dictRemoveWS); + } catch (IOException e) { + Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, + "Unable to create TWL: " + listNameValue, e); } - } - public IStringExpression getListName() { - return listName; - } + twlCache.put(listNameValue, new TwlCacheEntry(wl, resourceFile.lastModified())); + } else { + wl = cacheEntry.twl; + } - public IStringListExpression getStringList() { - return stringList; + } else { + Logger.getLogger(this.getClass().getName()).severe("Can't find resource: " + listNameValue); } - public IBooleanExpression getIgnore() { - return ignore; + List matchedAnnotationsOf = match.getMatchedAnnotationsOfElement(element); + for (AnnotationFS annotationFS : matchedAnnotationsOf) { + RutaStream windowStream = stream.getWindowStream(annotationFS, annotationFS.getType()); + Collection found = wl.find(windowStream, + getIgnore().getBooleanValue(context, stream), + getIgnoreLength().getIntegerValue(context, stream), null, 0, + getIgnoreWS().getBooleanValue(context, stream)); + for (AnnotationFS annotation : found) { + createAnnotation(annotation, context, windowStream); + } } + } - public INumberExpression getIgnoreLength() { - return ignoreLength; - } + public IStringExpression getListName() { + return listName; + } - public IBooleanExpression getIgnoreWS() { - return ignoreWS; - } + public IStringListExpression getStringList() { + return stringList; + } + + public IBooleanExpression getIgnore() { + return ignore; + } + + public INumberExpression getIgnoreLength() { + return ignoreLength; + } + + public IBooleanExpression getIgnoreWS() { + return ignoreWS; + } - } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java b/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java index 58e73d56a..320a32e88 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java @@ -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 @@ -97,14 +97,14 @@ public TypeSystemDescription createTypeSystemDescription(RutaDescriptorInformati } rm.setDataPath(dataPath); } - Map typeNameMap = new HashMap(); + Map typeNameMap = new HashMap<>(); TypeSystemDescription initialTypeSystem = UIMAFramework.getXMLParser() .parseTypeSystemDescription(new XMLInputSource(defaultTypeSystem)); CAS cas = CasCreationUtils.createCas(initialTypeSystem, null, new FsIndexDescription[0]); fillTypeNameMap(typeNameMap, cas.getTypeSystem()); cas.release(); - List toInclude = new ArrayList(); - List importList = new ArrayList(); + List toInclude = new ArrayList<>(); + List importList = new ArrayList<>(); Import_impl import_impl = new Import_impl(); if (options.isImportByName()) { String name = initialTypeSystem.getName(); @@ -229,12 +229,12 @@ public TypeSystemDescription createTypeSystemDescription(RutaDescriptorInformati } // TODO hotfixes: where do I get the final types?? - Set finalTypes = new HashSet(); - finalTypes.addAll(Arrays.asList(new String[] { "uima.cas.Boolean", "uima.cas.Byte", - "uima.cas.Short", "uima.cas.Integer", "uima.cas.Long", "uima.cas.Float", "uima.cas.Double", - "uima.cas.BooleanArray", "uima.cas.ByteArray", "uima.cas.ShortArray", - "uima.cas.IntegerArray", "uima.cas.LongArray", "uima.cas.FloatArray", - "uima.cas.DoubleArray", "uima.cas.StringArray", "uima.cas.FSArray" })); + Set finalTypes = new HashSet<>(); + finalTypes.addAll(Arrays.asList("uima.cas.Boolean", "uima.cas.Byte", "uima.cas.Short", + "uima.cas.Integer", "uima.cas.Long", "uima.cas.Float", "uima.cas.Double", + "uima.cas.BooleanArray", "uima.cas.ByteArray", "uima.cas.ShortArray", + "uima.cas.IntegerArray", "uima.cas.LongArray", "uima.cas.FloatArray", + "uima.cas.DoubleArray", "uima.cas.StringArray", "uima.cas.FSArray")); int typeIndex = 0; for (String eachType : desc.getTypeShortNames()) { @@ -261,8 +261,8 @@ public TypeSystemDescription createTypeSystemDescription(RutaDescriptorInformati typeIndex++; } - Set names = new HashSet(); - Collection types = new HashSet(); + Set names = new HashSet<>(); + Collection types = new HashSet<>(); for (TypeDescription each : typeSystemDescription.getTypes()) { String name = each.getName(); if (!names.contains(name)) { @@ -489,7 +489,7 @@ private AnalysisEngineDescription configureEngine(RutaDescriptorInformation desc } String[] parameterValue = (String[]) analysisEngineDescription.getAnalysisEngineMetaData() .getConfigurationParameterSettings().getParameterValue(RutaEngine.PARAM_RESOURCE_PATHS); - Set resourceLocations = new HashSet(); + Set resourceLocations = new HashSet<>(); if (parameterValue != null && parameterValue.length != 0) { resourceLocations.addAll(Arrays.asList(parameterValue)); @@ -557,7 +557,7 @@ private void configureExtensions(AnalysisEngineDescription analysisEngineDescrip String[] extensions = (String[]) configurationParameterSettings .getParameterValue(RutaEngine.PARAM_ADDITIONAL_EXTENSIONS); - List es = new ArrayList(); + List es = new ArrayList<>(); if (extensions != null) { es.addAll(Arrays.asList(extensions)); } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java b/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java index cefd57756..f697ec319 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/engine/Ruta.java @@ -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 @@ -68,7 +68,7 @@ public class Ruta { /** * This applies the given rule on the given JCas object - * + * * @param jcas * - the current document * @param rule @@ -181,7 +181,7 @@ private static void collectRuleMatches(Annotation annotation, List r /** * Removes all debug annotations from the index. - * + * * @param jcas * - the current document */ @@ -230,7 +230,7 @@ private static int[] getAddresses(FeatureStructure[] annotations) { /** * This method returns the spans of successful rule applies. - * + * * @param jcas * - the current document * @param rule @@ -255,7 +255,7 @@ public static List select(JCas jcas, String rule, Object... configur /** * This method returns true if the rule (or one of the rules) was able to match. - * + * * @param jcas * - the current document * @param rule @@ -341,7 +341,7 @@ public static AnalysisEngineDescription createAnalysisEngineDescription(String s String name = scriptFile.getName().substring(0, scriptFile.getName().length() - 5); settings.setParameterValue(RutaEngine.PARAM_MAIN_SCRIPT, name); if (tsds != null) { - List tsdList = new ArrayList(); + List tsdList = new ArrayList<>(); tsdList.add(metaData.getTypeSystem()); tsdList.addAll(Arrays.asList(tsds)); TypeSystemDescription typeSystemDescription = CasCreationUtils.mergeTypeSystems(tsdList); diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java b/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java index d5230a4f0..c5f4c9bf6 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/engine/RutaTestUtils.java @@ -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 @@ -134,7 +134,7 @@ public static CAS process(String ruleFileName, String textFileName, int amount, String resourceDirName, CAS cas) throws URISyntaxException, IOException, InvalidXMLException, ResourceInitializationException, AnalysisEngineProcessException, ResourceConfigurationException { - final Map parameters = new HashMap(); + final Map parameters = new HashMap<>(); parameters.put(RutaEngine.PARAM_DYNAMIC_ANCHORING, dynamicAnchoring); parameters.put(RutaEngine.PARAM_SIMPLE_GREEDY_FOR_COMPOSED, simpleGreedyForComposed); @@ -172,7 +172,7 @@ public static CAS process(String ruleFileName, String textFileName, addTestTypes(basicTypeSystem); addAdditionalTypes(complexTypes, features, basicTypeSystem); - Collection tsds = new ArrayList(); + Collection tsds = new ArrayList<>(); tsds.add(basicTypeSystem); TypeSystemDescription mergeTypeSystems = CasCreationUtils.mergeTypeSystems(tsds); aed.getAnalysisEngineMetaData().setTypeSystem(mergeTypeSystems); @@ -211,7 +211,7 @@ public static CAS process(String ruleFileName, String textFileName, /** * Helper to get the test type, e.g. org.apache.uima.T1, org.apache.uima.T2, ... - * + * * @param cas * - The CAS object containing the type system * @param i @@ -219,8 +219,9 @@ public static CAS process(String ruleFileName, String textFileName, * @return the test type object with the given counter */ public static Type getTestType(CAS cas, int i) { - if (cas == null) + if (cas == null) { return null; + } return cas.getTypeSystem().getType(TYPE + i); } @@ -252,7 +253,7 @@ public static CAS getCAS(String document, Map complexTypes, TypeSystemDescription basicTypeSystem = aed.getAnalysisEngineMetaData().getTypeSystem(); addTestTypes(basicTypeSystem); addAdditionalTypes(complexTypes, features, basicTypeSystem); - Collection tsds = new ArrayList(); + Collection tsds = new ArrayList<>(); tsds.add(basicTypeSystem); TypeSystemDescription mergeTypeSystems = CasCreationUtils.mergeTypeSystems(tsds); @@ -304,7 +305,7 @@ public static void printAnnotations(CAS cas, int typeId) { /** * Helper for common assertion in JUnit tests - * + * * @param cas * - The CAS object containing the type system * @param typeId @@ -336,7 +337,7 @@ public static void assertAnnotationsEquals(CAS cas, int typeId, int expectedCnt, /** * Helper to run Ruta on a tests script - * + * * @param testClass * - the class of the unit test * @return the annotated {@link CAS} diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java b/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java index cc437cf2c..22f1be767 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/engine/ViewWriter.java @@ -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 @@ -42,7 +42,7 @@ * This Analysis Engine is able to serialize the processed CAS to an XMI file whereas the the source * and destination view can be specified A descriptor file for this Analysis Engine is located in * the folder descriptor/utils of a UIMA Ruta project. - * + * */ public class ViewWriter extends JCasMultiplier_ImplBase { diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java b/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java index 4ec446d07..d2fe1f097 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/engine/XMIWriter.java @@ -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 @@ -42,7 +42,7 @@ * folder, dependent on the execution of the rules, e.g., whether a pattern of annotations occurs or * not. A descriptor file for this Analysis Engine is located in the folder * descriptor/utils of a UIMA Ruta project. - * + * */ public class XMIWriter extends JCasAnnotator_ImplBase { @@ -70,7 +70,7 @@ public void initialize(UimaContext aContext) throws ResourceInitializationExcept } if (aContext != null) { output = (String) aContext.getConfigParameterValue(PARAM_OUTPUT); - this.context = aContext; + context = aContext; } } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java b/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java index de85b03a9..cf752cfc3 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java @@ -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 @@ -116,9 +116,8 @@ public INumberExpression createIntegerExpression(Token number, Token minus) { SimpleNumberExpression simpleNumberExpression = new SimpleNumberExpression(valueOf); if (minus != null) { return new NegativeNumberExpression(simpleNumberExpression); - } else { - return simpleNumberExpression; } + return simpleNumberExpression; } public INumberExpression createDoubleExpression(Token number, Token minus) { @@ -126,23 +125,21 @@ public INumberExpression createDoubleExpression(Token number, Token minus) { SimpleNumberExpression simpleNumberExpression = new SimpleNumberExpression(valueOf); if (minus != null) { return new NegativeNumberExpression(simpleNumberExpression); - } else { - return simpleNumberExpression; } + return simpleNumberExpression; } public INumberExpression createReferenceNumberExpression(Token var, Token minus) { NumberVariableExpression simpleNumberExpression = new NumberVariableExpression(var.getText()); if (minus != null) { return new NegativeNumberExpression(simpleNumberExpression); - } else { - return simpleNumberExpression; } + return simpleNumberExpression; } public INumberExpression createComposedNumberExpression(List expressions, List opTokens) { - List ops = new ArrayList(); + List ops = new ArrayList<>(); for (Token token : opTokens) { ops.add(token.getText()); } @@ -151,8 +148,8 @@ public INumberExpression createComposedNumberExpression(List public INumberExpression createComposedNumberExpression(INumberExpression expression, Token opToken) { - List ops = new ArrayList(); - List exprList = new ArrayList(); + List ops = new ArrayList<>(); + List exprList = new ArrayList<>(); ops.add(opToken.getText()); exprList.add(expression); return new ComposedNumberExpression(exprList, ops); @@ -160,8 +157,8 @@ public INumberExpression createComposedNumberExpression(INumberExpression expres public INumberExpression createComposedNumberExpression(INumberExpression expression1, Token opToken, INumberExpression expression2) { - List ops = new ArrayList(); - List exprList = new ArrayList(); + List ops = new ArrayList<>(); + List exprList = new ArrayList<>(); ops.add(opToken.getText()); exprList.add(expression1); exprList.add(expression2); @@ -178,6 +175,19 @@ public IStringExpression createComposedStringExpression(List return new ComposedStringExpression(expressions); } + public IStringExpression createGenericComposedStringExpression( + List expressions) { + List stringExpression = new ArrayList<>(); + for (IRutaExpression each : expressions) { + if (each instanceof IStringExpression) { + stringExpression.add((IStringExpression) each); + } else { + System.out.println(); + } + } + return new ComposedStringExpression(stringExpression); + } + public AbstractStringExpression createReferenceStringExpression(Token var) { return new StringVariableExpression(var.getText()); } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/expression/annotation/AbstractAnnotationExpression.java b/ruta-core/src/main/java/org/apache/uima/ruta/expression/annotation/AbstractAnnotationExpression.java index 04a8535fe..d9c992b72 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/expression/annotation/AbstractAnnotationExpression.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/expression/annotation/AbstractAnnotationExpression.java @@ -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 @@ -25,21 +25,21 @@ import org.apache.uima.ruta.expression.RutaExpression; import org.apache.uima.ruta.rule.MatchContext; -public abstract class AbstractAnnotationExpression extends RutaExpression implements IAnnotationExpression { +public abstract class AbstractAnnotationExpression extends RutaExpression + implements IAnnotationExpression { @Override public FeatureStructure getFeatureStructure(MatchContext context, RutaStream stream) { return getAnnotation(context, stream); } - + @Override public String getStringValue(MatchContext context, RutaStream stream) { AnnotationFS annotation = getAnnotation(context, stream); - if(annotation != null) { + if (annotation != null) { return annotation.getCoveredText(); - } else { - return "null"; } + return "null"; } - + } diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/expression/bool/BooleanFeatureExpression.java b/ruta-core/src/main/java/org/apache/uima/ruta/expression/bool/BooleanFeatureExpression.java index 415a634d4..627f79ada 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/expression/bool/BooleanFeatureExpression.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/expression/bool/BooleanFeatureExpression.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.List; +import org.apache.uima.cas.CAS; import org.apache.uima.cas.Feature; import org.apache.uima.cas.FeatureStructure; import org.apache.uima.cas.text.AnnotationFS; @@ -43,10 +44,10 @@ public BooleanFeatureExpression(FeatureExpression fe) { public boolean getBooleanValue(MatchContext context, RutaStream stream) { AnnotationFS annotation = context.getAnnotation(); - Feature feature = this.fe.getFeature(context, stream); - List list = this.getTargetAnnotation(annotation, this.fe, context, stream); - Collection featureStructures = this.fe.getFeatureStructures(list, - false, context, stream); + Feature feature = fe.getFeature(context, stream); + List list = getTargetAnnotation(annotation, fe, context, stream); + Collection featureStructures = fe.getFeatureStructures(list, false, + context, stream); if (!featureStructures.isEmpty()) { FeatureStructure next = featureStructures.iterator().next(); // if (next instanceof AnnotationFS && next != annotation) { @@ -57,6 +58,11 @@ public boolean getBooleanValue(MatchContext context, RutaStream stream) { LazyFeature lazyFeature = (LazyFeature) feature; feature = lazyFeature.initialize(next); } + if (feature != null && feature.getRange().getName().equals(CAS.TYPE_NAME_STRING)) { + String stringValue = next.getStringValue(feature); + return Boolean.parseBoolean(stringValue); + } + return next.getBooleanValue(feature); } return false; @@ -65,12 +71,12 @@ public boolean getBooleanValue(MatchContext context, RutaStream stream) { @Override public String getStringValue(MatchContext context, RutaStream stream) { - return String.valueOf(this.getBooleanValue(context, stream)); + return String.valueOf(getBooleanValue(context, stream)); } public FeatureExpression getFe() { - return this.fe; + return fe; } public void setFe(FeatureExpression fe) { diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/expression/string/StringFeatureExpression.java b/ruta-core/src/main/java/org/apache/uima/ruta/expression/string/StringFeatureExpression.java index b838a59f2..82f49e096 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/expression/string/StringFeatureExpression.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/expression/string/StringFeatureExpression.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.List; +import org.apache.uima.cas.CAS; import org.apache.uima.cas.Feature; import org.apache.uima.cas.FeatureStructure; import org.apache.uima.cas.text.AnnotationFS; @@ -45,10 +46,10 @@ public StringFeatureExpression(FeatureExpression fe) { public String getStringValue(MatchContext context, RutaStream stream) { AnnotationFS annotation = context.getAnnotation(); - Feature feature = this.fe.getFeature(context, stream); - List list = this.getTargetAnnotation(annotation, this.fe, context, stream); - Collection featureStructures = this.fe.getFeatureStructures(list, - false, context, stream); + Feature feature = fe.getFeature(context, stream); + List list = getTargetAnnotation(annotation, fe, context, stream); + Collection featureStructures = fe.getFeatureStructures(list, false, + context, stream); if (!featureStructures.isEmpty()) { FeatureStructure next = featureStructures.iterator().next(); // if (next instanceof AnnotationFS && !next.getType().equals(annotation.getType())) { @@ -61,16 +62,18 @@ public String getStringValue(MatchContext context, RutaStream stream) { } if (next instanceof AnnotationFS && feature instanceof CoveredTextFeature) { return ((AnnotationFS) next).getCoveredText(); - } else { + } + if (feature.getRange().getName().equals(CAS.TYPE_NAME_STRING)) { return next.getStringValue(feature); } + return next.getFeatureValueAsString(feature); } return null; } public FeatureExpression getFe() { - return this.fe; + return fe; } public void setFe(FeatureExpression fe) { diff --git a/ruta-core/src/main/java/org/apache/uima/ruta/visitor/StatisticsVisitor.java b/ruta-core/src/main/java/org/apache/uima/ruta/visitor/StatisticsVisitor.java index 141a327fb..aa3a57c47 100644 --- a/ruta-core/src/main/java/org/apache/uima/ruta/visitor/StatisticsVisitor.java +++ b/ruta-core/src/main/java/org/apache/uima/ruta/visitor/StatisticsVisitor.java @@ -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 @@ -62,12 +62,12 @@ public class StatisticsVisitor implements RutaInferenceVisitor { public StatisticsVisitor(RutaVerbalizer verbalizer) { super(); this.verbalizer = verbalizer; - conditionTime = new HashMap(); - actionTime = new HashMap(); - conditionAmount = new HashMap(); - actionAmount = new HashMap(); - conditionDelta = new HashMap(); - actionDelta = new HashMap(); + conditionTime = new HashMap<>(); + actionTime = new HashMap<>(); + conditionAmount = new HashMap<>(); + actionAmount = new HashMap<>(); + conditionDelta = new HashMap<>(); + actionDelta = new HashMap<>(); } @Override @@ -76,8 +76,9 @@ public void beginVisit(RutaElement element, ScriptApply result) { AbstractRutaCondition c = (AbstractRutaCondition) element; String name = verbalizer.verbalizeName(c); Integer amount = conditionAmount.get(name); - if (amount == null) + if (amount == null) { amount = 0; + } amount++; conditionAmount.put(name, amount); conditionDelta.put(name, System.currentTimeMillis()); @@ -85,8 +86,9 @@ public void beginVisit(RutaElement element, ScriptApply result) { AbstractRutaAction a = (AbstractRutaAction) element; String name = verbalizer.verbalizeName(a); Integer amount = actionAmount.get(name); - if (amount == null) + if (amount == null) { amount = 0; + } amount++; actionAmount.put(name, amount); actionDelta.put(name, System.currentTimeMillis()); @@ -101,8 +103,9 @@ public void endVisit(RutaElement element, ScriptApply result) { Long start = conditionDelta.get(name); long delta = System.currentTimeMillis() - start; Long total = conditionTime.get(name); - if (total == null) + if (total == null) { total = 0L; + } total += delta; conditionTime.put(name, total); } else if (element instanceof AbstractRutaAction) { @@ -111,8 +114,9 @@ public void endVisit(RutaElement element, ScriptApply result) { Long start = actionDelta.get(name); long delta = System.currentTimeMillis() - start; Long total = actionTime.get(name); - if (total == null) + if (total == null) { total = 0L; + } total += delta; actionTime.put(name, total); } @@ -120,10 +124,10 @@ public void endVisit(RutaElement element, ScriptApply result) { @Override public void finished(RutaStream stream, List visitors) { - List names = new ArrayList(); - List totals = new ArrayList(); - List amounts = new ArrayList(); - List parts = new ArrayList(); + List names = new ArrayList<>(); + List totals = new ArrayList<>(); + List amounts = new ArrayList<>(); + List parts = new ArrayList<>(); for (String each : conditionTime.keySet()) { double total = conditionTime.get(each); double amount = conditionAmount.get(each); diff --git a/ruta-core/src/test/java/org/apache/uima/ruta/expression/bool/BooleanExpressionTest.java b/ruta-core/src/test/java/org/apache/uima/ruta/expression/bool/BooleanExpressionTest.java new file mode 100644 index 000000000..5a16d727c --- /dev/null +++ b/ruta-core/src/test/java/org/apache/uima/ruta/expression/bool/BooleanExpressionTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.uima.ruta.expression.bool; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import org.apache.uima.cas.CAS; +import org.apache.uima.ruta.engine.Ruta; +import org.apache.uima.ruta.engine.RutaTestUtils; +import org.apache.uima.ruta.engine.RutaTestUtils.TestFeature; +import org.junit.jupiter.api.Test; + +public class BooleanExpressionTest { + + @Test + public void testBooleanAssignment() throws Exception { + + Map typeMap = new TreeMap<>(); + typeMap.put("Struct", "uima.tcas.Annotation"); + typeMap.put("Temp", "uima.tcas.Annotation"); + + Map> featureMap = new TreeMap<>(); + featureMap.put("Struct", Arrays.asList(new TestFeature("bf", "", "uima.cas.Boolean"))); + featureMap.put("Temp", Arrays.asList(new TestFeature("sf", "", "uima.cas.String"))); + + String document = "This is a test."; + + String script = ""; + script += "STRING vsTrue = \"true\";\n"; + script += "STRING vsFalse = \"FALSE\";\n"; + script += "Document{->Struct, t:Temp, t.sf=\"true\"};\n"; + + script += "s:Struct{-> s.bf = false};\n"; + script += "s:Struct{-> s.bf = true};\n"; + script += "s:Struct{s.bf-> T1};\n"; + + script += "s:Struct{-> s.bf = false};\n"; + script += "s:Struct{-> s.bf = vsTrue};\n"; + script += "s:Struct{s.bf-> T2};\n"; + + script += "s:Struct{-> s.bf = false};\n"; + script += "s:Struct{-> s.bf = Temp.sf};\n"; + script += "s:Struct{s.bf-> T3};\n"; + + script += "s:Struct{-> s.bf = false};\n"; + script += "s:Struct{-> s.bf = \"tr\" + \"ue\"};\n"; + script += "s:Struct{s.bf-> T4};\n"; + + CAS cas = RutaTestUtils.getCAS(document, typeMap, featureMap); + Ruta.apply(cas, script); + + RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "This is a test."); + } + +} diff --git a/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringExpressionTest.java b/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringExpressionTest.java new file mode 100644 index 000000000..8855493eb --- /dev/null +++ b/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringExpressionTest.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * 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 + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.uima.ruta.expression.string; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import org.apache.uima.cas.CAS; +import org.apache.uima.ruta.engine.Ruta; +import org.apache.uima.ruta.engine.RutaTestUtils; +import org.apache.uima.ruta.engine.RutaTestUtils.TestFeature; +import org.junit.jupiter.api.Test; + +public class StringExpressionTest { + + @Test + public void testValidValue() throws Exception { + + String document = "Any - 0 test."; + + String script = "DOUBLE d = 5; BOOLEAN b = true; TYPE t = org.apache.uima.ruta.type.TruePositive;INT i = 4;\n"; + script += "CW{-> CREATE(Struct, \"s\" = d)};"; + script += "SPECIAL{-> CREATE(Struct, \"s\" = b)};"; + script += "NUM{-> CREATE(Struct, \"s\" = t)};"; + script += "SW{-> Struct};"; + script += "s:Struct{PARTOF(SW)-> s.s = i};"; + + script += "Struct.s==\"5.0\"{-> T1};"; + script += "Struct.s==\"true\"{-> T2};"; + script += "Struct.s==\"org.apache.uima.ruta.type.TruePositive\"{-> T3};"; + script += "Struct.s==\"4\"{-> T4};"; + + Map typeMap = new TreeMap<>(); + String typeName = "Struct"; + typeMap.put(typeName, "uima.tcas.Annotation"); + + Map> featureMap = new TreeMap<>(); + List list = new ArrayList<>(); + featureMap.put(typeName, list); + String fn = "s"; + list.add(new TestFeature(fn, "", "uima.cas.String")); + + CAS cas = RutaTestUtils.getCAS(document, typeMap, featureMap); + Ruta.apply(cas, script); + + RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "Any"); + RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "-"); + RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "0"); + RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "test"); + + } + + @Test + public void testStringFeatureAssignment() throws Exception { + + Map typeMap = new TreeMap<>(); + typeMap.put("Struct", "uima.tcas.Annotation"); + typeMap.put("Temp", "uima.tcas.Annotation"); + + Map> featureMap = new TreeMap<>(); + featureMap.put("Struct", Arrays.asList(new TestFeature("sf", "", "uima.cas.String"))); + featureMap.put("Temp", Arrays.asList(new TestFeature("if", "", "uima.cas.Integer"))); + + String document = "This is a test."; + + String script = ""; + script += "INT vi = 2;\n"; + script += "STRING vs = \"T\";\n"; + script += "Document{->Struct, Temp};\n"; + + script += "s:Struct{-> s.sf = \"T\" + 1};\n"; + script += "s:Struct{s.sf == \"T1\"-> T1};\n"; + + script += "s:Struct{-> s.sf = \"T\" + vi};\n"; + script += "s:Struct{s.sf == \"T2\"-> T2};\n"; + + script += "s:Struct{->s.sf = \"T\", s.sf = s.sf + 3};\n"; + script += "s:Struct{s.sf == \"T3\"-> T3};\n"; + + script += "Temp{->t:Temp, t.if = 4};\n"; + script += "s:Struct{->s.sf = \"T\", s.sf = \"T\" + Temp.if};\n"; + script += "s:Struct{s.sf == \"T4\"-> T4};\n"; + + script += "Temp{->t:Temp, t.if = 5};\n"; + script += "s:Struct{->s.sf = \"T\" + t.if}<-{t:Temp;};\n"; + script += "s:Struct{s.sf == \"T5\"-> T5};\n"; + + script += "Temp{->t:Temp, t.if = 6};\n"; + script += "s:Struct{->s.sf = vs + t.if}<-{t:Temp;};\n"; + script += "s:Struct{s.sf == \"T6\"-> T6};\n"; + + script += "s:Struct{->s.sf = vi};\n"; + script += "s:Struct{s.sf == \"2\"-> T7};\n"; + + CAS cas = RutaTestUtils.getCAS(document, typeMap, featureMap); + Ruta.apply(cas, script); + + RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 5, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 6, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 7, 1, "This is a test."); + } + + @Test + public void testStringVariableAssignment() throws Exception { + + Map typeMap = new TreeMap<>(); + typeMap.put("Struct", "uima.tcas.Annotation"); + typeMap.put("Temp", "uima.tcas.Annotation"); + + Map> featureMap = new TreeMap<>(); + featureMap.put("Struct", Arrays.asList(new TestFeature("sf", "", "uima.cas.String"))); + featureMap.put("Temp", Arrays.asList(new TestFeature("if", "", "uima.cas.Integer"))); + + String document = "This is a test."; + + String script = ""; + script += "INT vi = 2;\n"; + script += "STRING result = \"\";\n"; + script += "STRING st = \"T\";\n"; + script += "Document{->s:Struct, t:Temp, s.sf=\"a\", t.if=9};\n"; + + script += "Document{-> result = \"T\" + 1};\n"; + script += "Document{result == \"T1\"-> T1};\n"; + + script += "Document{-> result = \"T\" + vi};\n"; + script += "Document{result == \"T2\"-> T2};\n"; + + script += "Document{-> result = st +3};\n"; + script += "Document{result == \"T3\"-> T3};\n"; + + script += "Document{-> result = 4};\n"; + script += "Document{result == \"4\"-> T4};\n"; + + script += "Document{-> result = false};\n"; + script += "Document{result == \"false\"-> T5};\n"; + + script += "Document{-> result = Struct.sf + Temp.if};\n"; + script += "Document{result == \"a9\"-> T6};\n"; + + CAS cas = RutaTestUtils.getCAS(document, typeMap, featureMap); + Ruta.apply(cas, script); + + RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 5, 1, "This is a test."); + RutaTestUtils.assertAnnotationsEquals(cas, 6, 1, "This is a test."); + } + +} diff --git a/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringFeatureTest.java b/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringFeatureTest.java deleted file mode 100644 index 98c3a979a..000000000 --- a/ruta-core/src/test/java/org/apache/uima/ruta/expression/string/StringFeatureTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * 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 - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.uima.ruta.expression.string; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -import org.apache.uima.cas.CAS; -import org.apache.uima.ruta.engine.Ruta; -import org.apache.uima.ruta.engine.RutaTestUtils; -import org.apache.uima.ruta.engine.RutaTestUtils.TestFeature; -import org.junit.jupiter.api.Test; - -public class StringFeatureTest { - - @Test - public void testValidValue() throws Exception { - - String document = "Any - 0 test."; - - String script = "DOUBLE d = 5; BOOLEAN b = true; TYPE t = org.apache.uima.ruta.type.TruePositive;INT i = 4;\n"; - script += "CW{-> CREATE(Struct, \"s\" = d)};"; - script += "SPECIAL{-> CREATE(Struct, \"s\" = b)};"; - script += "NUM{-> CREATE(Struct, \"s\" = t)};"; - script += "SW{-> Struct};"; - script += "s:Struct{PARTOF(SW)-> s.s = i};"; - - script += "Struct.s==\"5.0\"{-> T1};"; - script += "Struct.s==\"true\"{-> T2};"; - script += "Struct.s==\"org.apache.uima.ruta.type.TruePositive\"{-> T3};"; - script += "Struct.s==\"4\"{-> T4};"; - - Map typeMap = new TreeMap(); - String typeName = "Struct"; - typeMap.put(typeName, "uima.tcas.Annotation"); - - Map> featureMap = new TreeMap>(); - List list = new ArrayList(); - featureMap.put(typeName, list); - String fn = "s"; - list.add(new TestFeature(fn, "", "uima.cas.String")); - - CAS cas = RutaTestUtils.getCAS(document, typeMap, featureMap); - Ruta.apply(cas, script); - - RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "Any"); - RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "-"); - RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "0"); - RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "test"); - - } - -} diff --git a/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g b/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g index 040114db9..83db6a50b 100644 --- a/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g +++ b/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g @@ -2284,6 +2284,7 @@ argument returns [Expression expr = null] : (conditionedAnnotationType)=>cat = conditionedAnnotationType{expr = cat; } | (nullExpression) => a5 = nullExpression {expr = a5;} + | (complexStringExpression) => cse = complexStringExpression {expr = cse;} | (featureExpression)=> fe = featureExpression {expr = fe;} | (booleanExpression)=> a2 = booleanExpression {expr = a2;} | (numberExpression)=> a3 = numberExpression {expr = a3;} @@ -2310,8 +2311,8 @@ simpleArgument returns [Expression expr = null] : (nullExpression) => a5 = nullExpression {expr = a5;} | (featureExpression)=> fe = featureExpression {expr = fe;} - | (booleanExpression)=> a2 = booleanExpression {expr = a2;} - | (numberExpression)=> a3 = numberExpression {expr = a3;} + | (simpleBooleanExpression)=> a2 = simpleBooleanExpression {expr = a2;} + | (simpleNumberExpression)=> a3 = simpleNumberExpression {expr = a3;} | (stringExpression)=> a4 = stringExpression {expr = a4;} | (listExpression)=> l = listExpression {expr = l;} | a1 = typeExpression {expr = a1;} @@ -2616,6 +2617,19 @@ List exprList = new ArrayList(); {expr = ExpressionFactory.createStringExpression(exprList);} ; +complexStringExpression returns [Expression expr = null] +options { + backtrack = true; +} +@init { +List exprList = new ArrayList(); +{expr = ExpressionFactory.createEmptyStringExpression(input.LT(1));} +} + : + a1 = simpleArgument {exprList.add(a1);} + ((PLUS)=>PLUS an = simpleArgument {exprList.add(an);})+ + {expr = ExpressionFactory.createStringExpression(exprList);} + ; // not checked stringFunction returns [Expression expr = null]