Author: pkluegl Date: Fri May 10 14:34:09 2013 New Revision: 1481029 URL: http://svn.apache.org/r1481029 Log: UIMA-2758 - started work on matching conditions based an feature values
Added: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureExpression.java uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaFeatureMatcher.java Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/FeatureMatchExpression.java uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaTypeChecker.java Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g (original) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g Fri May 10 14:34:09 2013 @@ -62,6 +62,8 @@ import org.apache.uima.ruta.RutaAutomata import org.apache.uima.ruta.RutaStatement; import org.apache.uima.ruta.expression.ExpressionFactory; import org.apache.uima.ruta.expression.RutaExpression; +import org.apache.uima.ruta.expression.feature.FeatureExpression; +import org.apache.uima.ruta.expression.feature.FeatureMatchExpression; import org.apache.uima.ruta.expression.bool.BooleanExpression; import org.apache.uima.ruta.expression.list.BooleanListExpression; import org.apache.uima.ruta.expression.list.ListExpression; @@ -481,7 +483,7 @@ level--; ruleElementWithCA[RuleElementContainer container] returns [RutaRuleElement re = null] : - idRef=typeExpression + idRef=typeMatchExpression {re = factory.createRuleElement(idRef,null,null,null, container, $blockDeclaration::env);} q = quantifierPart? LCURLY c = conditions? (THEN a = actions)? RCURLY @@ -606,12 +608,12 @@ ruleElementDisjunctive [RuleElementConta } : LPAREN - ((typeExpression | simpleStringExpression) VBAR)=> (e11 =typeExpression | e12 =simpleStringExpression) + ((typeMatchExpression | simpleStringExpression) VBAR)=> (e11 =typeMatchExpression | e12 =simpleStringExpression) {if(e11 != null) exprs.add(e11);if(e12 != null) exprs.add(e12);} - VBAR (e21 =typeExpression | e22 =simpleStringExpression) + VBAR (e21 =typeMatchExpression | e22 =simpleStringExpression) {if(e21 != null) exprs.add(e21);if(e22 != null) exprs.add(e22);} ( - VBAR (e31 =typeExpression | e32 =simpleStringExpression) + VBAR (e31 =typeMatchExpression | e32 =simpleStringExpression) {if(e31 != null) exprs.add(e31);if(e32 != null) exprs.add(e32);} )* RPAREN @@ -673,7 +675,7 @@ scope { ruleElementType [RuleElementContainer container] returns [RutaRuleElement re = null] : - (typeExpression)=>typeExpr = typeExpression + (typeMatchExpression)=>typeExpr = typeMatchExpression {re = factory.createRuleElement(typeExpr, null, null, null, container, $blockDeclaration::env);} q = quantifierPart? (LCURLY c = conditions? (THEN a = actions)? RCURLY)? @@ -841,6 +843,19 @@ simpleTypeListExpression returns [TypeLi {expr = ExpressionFactory.createReferenceTypeListExpression(var);} ; +typeMatchExpression returns [RutaExpression expr = null] +options { + backtrack = true; +} + : + (featureMatchExpression)=> ft = featureMatchExpression {expr = ft;} + | + tf = typeFunction {expr = tf;} + | + st = simpleTypeExpression {expr = st;} + + ; + typeExpression returns [TypeExpression type = null] options { @@ -878,6 +893,22 @@ simpleTypeExpression returns [TypeExpres {type = ExpressionFactory.createSimpleTypeExpression(at, $blockDeclaration::env);} ; +featureExpression returns [FeatureExpression feat = null] +@init{ +List<Token> fs = new ArrayList<Token>(); +} + : + at = Identifier {te = ExpressionFactory.createSimpleTypeExpression(at, $blockDeclaration::env);} + (DOT f = Identifier {fs.add(f);})+ + {feat = ExpressionFactory.createFeatureExpression(te, fs, $blockDeclaration::env);} + ; + +featureMatchExpression returns [FeatureMatchExpression fme = null] + : + f = featureExpression (EQUAL arg = primitiveArgument)? + {fme = ExpressionFactory.createFeatureMatchExpression(f, arg, $blockDeclaration::env);} + ; + variable returns [Token var = null] : @@ -1711,7 +1742,24 @@ options { //| (a4 = stringExpression)=> a4 = stringExpression {expr = a4;} //| (a1 = typeExpression)=> a1 = typeExpression {expr = a1;} ; - + +primitiveArgument returns [RutaExpression expr = null] +options { + backtrack = true; +} + : + a4 = simpleStringExpression {expr = a4;} + | a2 = simpleBooleanExpression {expr = a2;} + | a3 = simpleNumberExpression {expr = a3;} + | a1 = simpleTypeExpression {expr = a1;} + //token = ( + //(booleanExpression[par]) => booleanExpression[par] + //| (numberExpression[par]) => numberExpression[par] + //| (stringExpression[par]) => stringExpression[par] + //| (typeExpression[par]) => typeExpression[par] + //) + //{arg = token;} + ; dottedIdentifier returns [String idString = ""] : Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java (original) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java Fri May 10 14:34:09 2013 @@ -27,6 +27,7 @@ import org.apache.uima.resource.metadata import org.apache.uima.ruta.action.AbstractRutaAction; import org.apache.uima.ruta.condition.AbstractRutaCondition; import org.apache.uima.ruta.expression.RutaExpression; +import org.apache.uima.ruta.expression.feature.FeatureExpression; import org.apache.uima.ruta.expression.number.NumberExpression; import org.apache.uima.ruta.expression.string.StringExpression; import org.apache.uima.ruta.expression.type.SimpleTypeExpression; @@ -38,7 +39,9 @@ import org.apache.uima.ruta.rule.RuleEle import org.apache.uima.ruta.rule.RuleElementContainer; import org.apache.uima.ruta.rule.RuleElementIsolator; import org.apache.uima.ruta.rule.RutaDisjunctiveMatcher; +import org.apache.uima.ruta.rule.RutaFeatureMatcher; import org.apache.uima.ruta.rule.RutaLiteralMatcher; +import org.apache.uima.ruta.rule.RutaMatcher; import org.apache.uima.ruta.rule.RutaRule; import org.apache.uima.ruta.rule.RutaRuleElement; import org.apache.uima.ruta.rule.RutaTypeMatcher; @@ -113,41 +116,35 @@ public class RutaScriptFactory { return new RutaRule(elements, parent, idCounter++); } - public RutaRuleElement createRuleElement(TypeExpression typeExpression, + public RutaRuleElement createRuleElement(RutaExpression expression, RuleElementQuantifier quantifier, List<AbstractRutaCondition> conditions, - List<AbstractRutaAction> actions, RuleElementContainer container, - RutaBlock parent) { - RutaTypeMatcher matcher = new RutaTypeMatcher(typeExpression); + List<AbstractRutaAction> actions, RuleElementContainer container, RutaBlock parent) { + RutaMatcher matcher = null; + if (expression instanceof TypeExpression) { + matcher = new RutaTypeMatcher((TypeExpression) expression); + } else if (expression instanceof FeatureExpression) { + matcher = new RutaFeatureMatcher((FeatureExpression) expression); + } else if (expression instanceof StringExpression) { + matcher = new RutaLiteralMatcher((StringExpression) expression); + } return new RutaRuleElement(matcher, quantifier, conditions, actions, container, parent); } public AbstractRuleElement createWildCardRuleElement(List<AbstractRutaCondition> conditions, - List<AbstractRutaAction> actions, RuleElementContainer container, - RutaBlock parent) { + List<AbstractRutaAction> actions, RuleElementContainer container, RutaBlock parent) { return new WildCardRuleElement(conditions, actions, container, parent); } - - + public RutaRuleElement createRuleElement(List<RutaExpression> exprs, RuleElementQuantifier quantifier, List<AbstractRutaCondition> conditions, - List<AbstractRutaAction> actions, RuleElementContainer container, - RutaBlock parent) { + List<AbstractRutaAction> actions, RuleElementContainer container, RutaBlock parent) { RutaDisjunctiveMatcher matcher = new RutaDisjunctiveMatcher(exprs); return new RutaRuleElement(matcher, quantifier, conditions, actions, container, parent); } - public RutaRuleElement createRuleElement(StringExpression stringExpression, - RuleElementQuantifier quantifier, List<AbstractRutaCondition> conditions, - List<AbstractRutaAction> actions, RuleElementContainer container, - RutaBlock parent) { - RutaLiteralMatcher matcher = new RutaLiteralMatcher(stringExpression); - return new RutaRuleElement(matcher, quantifier, conditions, actions, container, parent); - } - public ComposedRuleElement createComposedRuleElement(List<RuleElement> res, RuleElementQuantifier quantifier, List<AbstractRutaCondition> conditions, - List<AbstractRutaAction> actions, RuleElementContainer container, - RutaBlock parent) { + List<AbstractRutaAction> actions, RuleElementContainer container, RutaBlock parent) { return new ComposedRuleElement(res, quantifier, conditions, actions, container, parent); } @@ -191,8 +188,8 @@ public class RutaScriptFactory { return new QuestionGreedy(); } - public RutaBlock createAutomataBlock(Token id, RutaRuleElement re, - List<RutaStatement> body, RutaBlock env) { + public RutaBlock createAutomataBlock(Token id, RutaRuleElement re, List<RutaStatement> body, + RutaBlock env) { return createScriptBlock(id, re, body, env); } @@ -200,6 +197,4 @@ public class RutaScriptFactory { return new RegExpRule(null, null, idCounter++, env); } - - } Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java (original) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java Fri May 10 14:34:09 2013 @@ -30,6 +30,9 @@ import org.apache.uima.ruta.expression.b import org.apache.uima.ruta.expression.bool.ReferenceBooleanExpression; import org.apache.uima.ruta.expression.bool.SimpleBooleanExpression; import org.apache.uima.ruta.expression.bool.SimpleBooleanFunction; +import org.apache.uima.ruta.expression.feature.FeatureExpression; +import org.apache.uima.ruta.expression.feature.SimpleFeatureExpression; +import org.apache.uima.ruta.expression.feature.FeatureMatchExpression; import org.apache.uima.ruta.expression.list.BooleanListExpression; import org.apache.uima.ruta.expression.list.NumberListExpression; import org.apache.uima.ruta.expression.list.ReferenceBooleanListExpression; @@ -220,6 +223,20 @@ public class ExpressionFactory { return new SimpleStringListExpression(list); } + public static SimpleFeatureExpression createFeatureExpression(TypeExpression te, List<Token> fs, + RutaBlock env) { + List<String> featureReferences = new ArrayList<String>(); + for (Token token : fs) { + featureReferences.add(token.getText()); + } + return new SimpleFeatureExpression(te, featureReferences); + } + + public static FeatureMatchExpression createFeatureMatchExpression(FeatureExpression f, + RutaExpression arg, RutaBlock env) { + return new FeatureMatchExpression(f, arg); + } + } Added: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureExpression.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureExpression.java?rev=1481029&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureExpression.java (added) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureExpression.java Fri May 10 14:34:09 2013 @@ -0,0 +1,21 @@ +package org.apache.uima.ruta.expression.feature; + +import java.util.List; + +import org.apache.uima.cas.Feature; +import org.apache.uima.ruta.RutaStatement; +import org.apache.uima.ruta.expression.RutaExpression; +import org.apache.uima.ruta.expression.type.TypeExpression; + +public abstract class FeatureExpression extends RutaExpression { + + public abstract Feature getFeature(RutaStatement parent); + + public abstract List<Feature> getFeatures(RutaStatement parent); + + public abstract List<String> getFeatureStringList(); + + public abstract TypeExpression getTypeExpr(); + + +} Added: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java?rev=1481029&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java (added) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java Fri May 10 14:34:09 2013 @@ -0,0 +1,45 @@ +/* + * 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.feature; + +import org.apache.uima.ruta.expression.RutaExpression; + + +public class FeatureMatchExpression extends SimpleFeatureExpression { + + private RutaExpression arg; + + public FeatureMatchExpression(FeatureExpression f, RutaExpression arg) { + super(f.getTypeExpr(), f.getFeatureStringList()); + this.arg = arg; + } + + public RutaExpression getArg() { + return arg; + } + + public void setArg(RutaExpression arg) { + this.arg = arg; + } + + + + +} Added: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java?rev=1481029&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java (added) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java Fri May 10 14:34:09 2013 @@ -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.feature; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.uima.cas.Feature; +import org.apache.uima.cas.Type; +import org.apache.uima.ruta.RutaStatement; +import org.apache.uima.ruta.expression.type.TypeExpression; + +public class SimpleFeatureExpression extends FeatureExpression { + + private TypeExpression typeExpr; + private List<String> features; + + public SimpleFeatureExpression(TypeExpression te, List<String> featureReferences) { + super(); + this.typeExpr = te; + this.features = featureReferences; + } + + @Override + public Feature getFeature(RutaStatement parent) { + return getFeatures(parent).get(0); + } + + @Override + public List<Feature> getFeatures(RutaStatement parent) { + List<Feature> result = new ArrayList<Feature>(); + Type type = typeExpr.getType(parent); + Feature feature = null; + for (String each : features) { + feature = type.getFeatureByBaseName(each); + result.add(feature); + type = feature.getRange(); + } + return result; + } + + public TypeExpression getTypeExpr() { + return typeExpr; + } + + public void setTypeExpr(TypeExpression typeExpr) { + this.typeExpr = typeExpr; + } + + public List<String> getFeatureStringList() { + return features; + } + + public void setFeatures(List<String> features) { + this.features = features; + } + + + +} Added: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaFeatureMatcher.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaFeatureMatcher.java?rev=1481029&view=auto ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaFeatureMatcher.java (added) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaFeatureMatcher.java Fri May 10 14:34:09 2013 @@ -0,0 +1,135 @@ +package org.apache.uima.ruta.rule; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.TreeSet; + +import org.apache.uima.cas.Feature; +import org.apache.uima.cas.FeatureStructure; +import org.apache.uima.cas.Type; +import org.apache.uima.cas.text.AnnotationFS; +import org.apache.uima.ruta.RutaBlock; +import org.apache.uima.ruta.RutaStream; +import org.apache.uima.ruta.UIMAConstants; +import org.apache.uima.ruta.expression.RutaExpression; +import org.apache.uima.ruta.expression.bool.BooleanExpression; +import org.apache.uima.ruta.expression.feature.FeatureExpression; +import org.apache.uima.ruta.expression.feature.FeatureMatchExpression; +import org.apache.uima.ruta.expression.number.NumberExpression; +import org.apache.uima.ruta.expression.string.StringExpression; + +public class RutaFeatureMatcher extends RutaTypeMatcher { + + private FeatureExpression featExpr; + + public RutaFeatureMatcher(FeatureExpression expression) { + super(expression.getTypeExpr()); + this.featExpr = expression; + } + + public Collection<AnnotationFS> getMatchingAnnotations(RutaStream stream, RutaBlock parent) { + Collection<AnnotationFS> baseAnnotations = super.getMatchingAnnotations(stream, parent); + Collection<AnnotationFS> result = new TreeSet<AnnotationFS>(comparator); + List<Feature> features = featExpr.getFeatures(parent); + for (AnnotationFS eachBase : baseAnnotations) { + AnnotationFS afs = eachBase; + for (Feature feature : features) { + if(feature.getRange().isPrimitive()&& featExpr instanceof FeatureMatchExpression) { + FeatureMatchExpression fme = (FeatureMatchExpression) featExpr; + RutaExpression arg = fme.getArg(); + if(checkFeatureValue(afs, feature, arg, stream, parent)) { + result.add(afs); + } + break; + } else { + FeatureStructure value = afs.getFeatureValue(feature); + afs = (AnnotationFS) value; + } + } + if(!(featExpr instanceof FeatureMatchExpression)) { + result.add(afs); + } + } + return result; + } + + private boolean checkFeatureValue(AnnotationFS afs, Feature feature, RutaExpression arg, + RutaStream stream, RutaBlock parent) { + String rn = feature.getRange().getName(); + if(rn.equals(UIMAConstants.TYPE_BOOLEAN)) { + boolean v1 = afs.getBooleanValue(feature); + if(arg instanceof BooleanExpression) { + BooleanExpression expr = (BooleanExpression) arg; + boolean v2 = expr.getBooleanValue(parent); + return v1 == v2; + } + } else if(rn.equals(UIMAConstants.TYPE_INTEGER) || rn.equals(UIMAConstants.TYPE_BYTE) || rn.equals(UIMAConstants.TYPE_SHORT)|| rn.equals(UIMAConstants.TYPE_LONG)) { + Integer v1 = afs.getIntValue(feature); + if(arg instanceof NumberExpression) { + NumberExpression expr = (NumberExpression) arg; + Integer v2 = expr.getIntegerValue(parent); + return v1.equals(v2); + } + } else if(rn.equals(UIMAConstants.TYPE_DOUBLE)) { + Double v1 = afs.getDoubleValue(feature); + if(arg instanceof NumberExpression) { + NumberExpression expr = (NumberExpression) arg; + Double v2 = expr.getDoubleValue(parent); + return v1.equals(v2); + } + } else if(rn.equals(UIMAConstants.TYPE_FLOAT)) { + Float v1 = afs.getFloatValue(feature); + if(arg instanceof NumberExpression) { + NumberExpression expr = (NumberExpression) arg; + Float v2 = expr.getFloatValue(parent); + return v1.equals(v2); + } + } else if(rn.equals(UIMAConstants.TYPE_STRING)) { + String v1 = afs.getStringValue(feature); + if(arg instanceof StringExpression) { + StringExpression expr = (StringExpression) arg; + String v2 = expr.getStringValue(parent); + return v1.equals(v2); + } + } + return false; + } + + public boolean match(AnnotationFS annotation, RutaStream stream, RutaBlock parent) { + Type baseType = getType(expression, parent, stream); + return false; + } + + public List<Type> getTypes(RutaBlock parent, RutaStream stream) { + List<Type> result = new ArrayList<Type>(1); + Type baseType = getType(expression, parent, stream); + result.add(baseType); + return result; + } + + public RutaExpression getExpression() { + return expression; + } + + public Collection<AnnotationFS> getAnnotationsAfter(RutaRuleElement ruleElement, + AnnotationFS annotation, RutaStream stream, RutaBlock parent) { + // TODO Auto-generated method stub + return null; + } + + public Collection<AnnotationFS> getAnnotationsBefore(RutaRuleElement ruleElement, + AnnotationFS annotation, RutaStream stream, RutaBlock parent) { + // TODO Auto-generated method stub + return null; + } + + public FeatureExpression getFeatExpr() { + return featExpr; + } + + public void setFeatExpr(FeatureExpression featExpr) { + this.featExpr = featExpr; + } + +} Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java (original) +++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java Fri May 10 14:34:09 2013 @@ -38,9 +38,9 @@ import org.apache.uima.ruta.type.RutaBas public class RutaTypeMatcher implements RutaMatcher { - private final TypeExpression expression; + protected final TypeExpression expression; - private AnnotationComparator comparator; + protected AnnotationComparator comparator; public RutaTypeMatcher(TypeExpression expression) { super(); Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g (original) +++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g Fri May 10 14:34:09 2013 @@ -551,7 +551,7 @@ level--; ruleElementWithCA returns [RutaRuleElement re = null] : - idRef=typeExpression quantifier = quantifierPart? {re = scriptFactory.createRuleElement(idRef,quantifier,c,a, end);} + idRef=typeMatchExpression quantifier = quantifierPart? {re = scriptFactory.createRuleElement(idRef,quantifier,c,a, end);} LCURLY c = conditions? (THEN a = actions)? end = RCURLY {re = scriptFactory.createRuleElement(idRef,quantifier,c,a, end);} ; @@ -559,7 +559,7 @@ ruleElementWithCA returns [RutaRuleEleme ruleElementWithoutCA returns [RutaRuleElement re = null] : - idRef=typeExpression quantifier = quantifierPart? + idRef=typeMatchExpression quantifier = quantifierPart? {re = scriptFactory.createRuleElement(idRef,quantifier,null,null, null);} ; @@ -681,7 +681,7 @@ ruleElementType returns [RutaRuleElement List<RutaCondition> dummyConds = new ArrayList<RutaCondition>(); } : - (typeExpression)=>idRef=typeExpression quantifier = quantifierPart? + (typeMatchExpression)=>idRef=typeMatchExpression quantifier = quantifierPart? (LCURLY { @@ -855,7 +855,8 @@ simpleTypeListExpression returns [Expres {expr = ExpressionFactory.createListExpression(var, RutaTypeConstants.RUTA_TYPE_TL);} ; -typeExpression returns [Expression expr = null] + +typeMatchExpression returns [Expression expr = null] options { backtrack = true; } @@ -863,7 +864,22 @@ options { expr = ExpressionFactory.createEmptyTypeExpression(input.LT(1)); } : + (featureTypeExpression)=> ft = featureTypeExpression {expr = ft;} + | tf = typeFunction {expr = tf;} + | + st = simpleTypeExpression + {expr = ExpressionFactory.createTypeExpression(st); + } + + ; + +typeExpression returns [Expression expr = null] +@init { +expr = ExpressionFactory.createEmptyTypeExpression(input.LT(1)); +} + : + (typeFunction)=> tf = typeFunction {expr = tf;} | st = simpleTypeExpression {expr = ExpressionFactory.createTypeExpression(st); } @@ -887,6 +903,11 @@ externalTypeFunction returns [Expression } ; +featureTypeExpression returns [Expression expr = null] + : + feature = dottedId (comp = EQUAL | comp = NOTEQUAL) value = primitiveArgument {expr = ExpressionFactory.createFeatureMatch(feature, comp, value);} + ; + simpleTypeExpression returns [Expression type = null] : at = annotationType {type = at;} @@ -1892,6 +1913,24 @@ options { //{arg = token;} ; +primitiveArgument returns [Expression expr = null] +options { + backtrack = true; +} + : + a4 = simpleStringExpression {expr = a4;} + | a2 = simpleBooleanExpression {expr = a2;} + | a3 = simpleNumberExpression {expr = a3;} + | a1 = simpleTypeExpression {expr = a1;} + //token = ( + //(booleanExpression[par]) => booleanExpression[par] + //| (numberExpression[par]) => numberExpression[par] + //| (stringExpression[par]) => stringExpression[par] + //| (typeExpression[par]) => typeExpression[par] + //) + //{arg = token;} + ; + //snooze dottedIdentifier returns [String idString = ""] : Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java (original) +++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java Fri May 10 14:34:09 2013 @@ -342,9 +342,9 @@ public class ExpressionFactory extends A } - public static Expression createFeatureMatch(Token feature, Expression value) { + public static Expression createFeatureMatch(Token feature, Token comp, Expression value) { int bounds[] = getBounds(feature); - return new FeatureMatchExpression(bounds[0], value.sourceEnd(), feature, value); + return new FeatureMatchExpression(bounds[0], value.sourceEnd(), feature, comp, value); } public static Expression createBooleanFunction(Token id, List<Expression> args) { Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/FeatureMatchExpression.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/FeatureMatchExpression.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/FeatureMatchExpression.java (original) +++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/FeatureMatchExpression.java Fri May 10 14:34:09 2013 @@ -22,19 +22,24 @@ package org.apache.uima.ruta.ide.parser. import org.antlr.runtime.Token; import org.eclipse.dltk.ast.ASTVisitor; import org.eclipse.dltk.ast.expressions.Expression; +import org.eclipse.dltk.ast.expressions.StringLiteral; public class FeatureMatchExpression extends Expression { - + private final Token feature; + private final Expression value; - public FeatureMatchExpression(int sourceStart, int sourceEnd, Token feature, Expression value) { + private Token comp; + + public FeatureMatchExpression(int sourceStart, int sourceEnd, Token feature, Token comp, + Expression value) { super(sourceStart, sourceEnd); this.feature = feature; + this.comp = comp; this.value = value; } - @Override public void traverse(ASTVisitor visitor) throws Exception { if (visitor.visit(this)) { @@ -45,20 +50,22 @@ public class FeatureMatchExpression exte } } - @Override public int getKind() { - return 0; + return value.getKind(); } - public Token getFeature() { return feature; } - public Expression getValue() { return value; } + @Override + public String toString() { + return feature.getText() + comp.getText() + value.toString(); + } + } Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaTypeChecker.java URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaTypeChecker.java?rev=1481029&r1=1481028&r2=1481029&view=diff ============================================================================== --- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaTypeChecker.java (original) +++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaTypeChecker.java Fri May 10 14:34:09 2013 @@ -24,8 +24,11 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.Stack; import java.util.TreeSet; @@ -35,9 +38,11 @@ import org.apache.uima.resource.Resource import org.apache.uima.resource.metadata.FeatureDescription; import org.apache.uima.resource.metadata.TypeDescription; import org.apache.uima.resource.metadata.TypeSystemDescription; +import org.apache.uima.ruta.UIMAConstants; import org.apache.uima.ruta.ide.RutaIdePlugin; import org.apache.uima.ruta.ide.core.RutaCorePreferences; import org.apache.uima.ruta.ide.core.builder.RutaProjectUtils; +import org.apache.uima.ruta.ide.parser.ast.FeatureMatchExpression; import org.apache.uima.ruta.ide.parser.ast.RutaStatementConstants; import org.apache.uima.ruta.ide.parser.ast.RutaTypeConstants; import org.apache.uima.ruta.ide.parser.ast.RutaBlock; @@ -61,6 +66,7 @@ import org.eclipse.dltk.ast.ASTVisitor; import org.eclipse.dltk.ast.declarations.MethodDeclaration; import org.eclipse.dltk.ast.declarations.ModuleDeclaration; import org.eclipse.dltk.ast.expressions.Expression; +import org.eclipse.dltk.ast.expressions.StringLiteral; import org.eclipse.dltk.ast.references.SimpleReference; import org.eclipse.dltk.ast.statements.Statement; import org.eclipse.dltk.compiler.problem.IProblem; @@ -107,8 +113,7 @@ public class RutaTypeChecker implements public TypeCheckerVisitor(IProblemReporter rep, ISourceLineTracker linetracker, ISourceModule curFile, Set<String> completeTypes, Set<String> shortTypes) { - this.problemFactory = new RutaCheckerProblemFactory(curFile.getElementName(), - linetracker); + this.problemFactory = new RutaCheckerProblemFactory(curFile.getElementName(), linetracker); this.linetracker = linetracker; this.rep = rep; this.typeVariables = new HashSet<String>(); @@ -155,7 +160,7 @@ public class RutaTypeChecker implements } catch (Exception e) { RutaIdePlugin.error(e); } - + Preferences store = RutaIdePlugin.getDefault().getPluginPreferences(); reportWarningOnShortNames = !store .getBoolean(RutaCorePreferences.BUILDER_IGNORE_DUPLICATE_SHORTNAMES); @@ -300,8 +305,7 @@ public class RutaTypeChecker implements // HOTFIX Peter add also the imported types of the imported type system! try { - boolean checkScriptImport = RutaCheckerUtils - .checkScriptImport(localpath, project); + boolean checkScriptImport = RutaCheckerUtils.checkScriptImport(localpath, project); if (!checkScriptImport) { rep.reportProblem(problemFactory.createFileNotFoundProblem(sRef, localpath)); } @@ -324,8 +328,8 @@ public class RutaTypeChecker implements // old stuff Set<String> importedTypes = new HashSet<String>(); try { - importedTypes = RutaCheckerUtils.importScript(localpath, IModelElement.FIELD, - project, true); + importedTypes = RutaCheckerUtils.importScript(localpath, IModelElement.FIELD, project, + true); } catch (IOException e) { rep.reportProblem(problemFactory.createFileNotFoundProblem(sRef, localpath)); } @@ -384,8 +388,8 @@ public class RutaTypeChecker implements } private TypeSystemDescription getTypeSystemOfScript() throws InvalidXMLException, IOException { - IPath descriptorPath = RutaProjectUtils.getTypeSystemDescriptorPath(currentFile - .getResource().getLocation(), project.getProject()); + IPath descriptorPath = RutaProjectUtils.getTypeSystemDescriptorPath(currentFile.getResource() + .getLocation(), project.getProject()); TypeSystemDescription typeSysDescr = null; if (descriptorPath.toFile().exists()) { typeSysDescr = UIMAFramework.getXMLParser().parseTypeSystemDescription( @@ -409,32 +413,147 @@ public class RutaTypeChecker implements } if (typeVariables.contains(ref.getName()) || completeTypes.contains(ref.getName()) || shortTypes.contains(ref.getName()) || otherTypes.contains(ref.getName()) - || isLongLocalATRef(ref.getName()) || isLongExternalATRef(ref.getName()) - ) { + || isLongLocalATRef(ref.getName()) || isLongExternalATRef(ref.getName())) { return false; } - if(isFeatureMatch(ref)) { + if (isFeatureMatch(ref.getName()) != null) { return false; } rep.reportProblem(problemFactory.createTypeProblem(ref, currentFile)); return false; + } else if (s instanceof FeatureMatchExpression) { + FeatureMatchExpression fme = (FeatureMatchExpression) s; + String featText = fme.getFeature().getText(); + checkTypeOfFeatureMatch(featText, fme); + return true; } return true; } - private boolean isFeatureMatch(RutaVariableReference ref) { - String name = ref.getName(); + private void checkTypeOfFeatureMatch(String featText, FeatureMatchExpression fme) { + int lastIndexOf = featText.lastIndexOf("."); + String aref = featText.substring(0, lastIndexOf); + String fref = featText.substring(lastIndexOf+1, featText.length()); + String match = isFeatureMatch(aref); + if (match != null) { + int kind = fme.getValue().getKind(); + if(fme.getValue() instanceof StringLiteral) { + kind = RutaTypeConstants.RUTA_TYPE_S; + } + boolean findFeature = findFeature(match, fref, kind); + if (findFeature) { + + } else { + rep.reportProblem(problemFactory.createUnknownFeatureProblem(fme, aref)); + } + } else { + rep.reportProblem(problemFactory.createUnknownFeatureProblem(fme, aref)); + } + } + + private String isFeatureMatch(String text) { for (String each : shortTypes) { - if(checkFeatureMatch(name, each)) return true; + String t = checkFeatureMatch(text, each); + if (t != null) { + return t; + } } for (String each : completeTypes) { - if(checkFeatureMatch(name, each)) return true; + String t = checkFeatureMatch(text, each); + if (t != null) { + return t; + } + } + return null; + } + + private boolean findFeature(String structure, String feat, int kind) { + if (description == null) { + return true; + } + if (structure == null) { + return false; + } + + // TODO HOTFIX + if (structure.equals("Document") || structure.equals("DocumentAnnotation") + || structure.equals("uima.tcas.DocumentAnnotation")) { + if (feat.equals("language")) { + return true; + } + } + + boolean featureFound = false; + TypeDescription[] descriptions = description.getTypes(); + Map<String, TypeDescription> typeMap = new HashMap<String, TypeDescription>(); + for (TypeDescription typeDescription : descriptions) { + String typeName = typeDescription.getName(); + typeMap.put(typeName, typeDescription); + } + + for (TypeDescription typeDescription : descriptions) { + String typeName = typeDescription.getName(); + String shortName = getShortName(typeName); + if (typeName.equals(structure) || shortName.equals(structure)) { + Collection<FeatureDescription> allFeatures = getAllDeclaredFeatures(typeDescription, + typeMap); + for (FeatureDescription featureDescription : allFeatures) { + String featureName = featureDescription.getName(); + if (featureName.equals(feat) && checkFeatureKind(featureDescription, kind)) { + featureFound = true; + break; + } + } + } + + if (featureFound) { + break; + } + } + return featureFound; + } + + private boolean checkFeatureKind(FeatureDescription f, int kind) { + String t = f.getRangeTypeName(); + if (t.equals(UIMAConstants.TYPE_BOOLEAN) && RutaTypeConstants.RUTA_TYPE_B == kind) { + return true; + } else if (t.equals(UIMAConstants.TYPE_STRING) && RutaTypeConstants.RUTA_TYPE_S == kind) { + return true; + } else if ((t.equals(UIMAConstants.TYPE_BYTE) || t.equals(UIMAConstants.TYPE_DOUBLE) + || t.equals(UIMAConstants.TYPE_FLOAT) || t.equals(UIMAConstants.TYPE_INTEGER) + || t.equals(UIMAConstants.TYPE_LONG) || t.equals(UIMAConstants.TYPE_SHORT)) + && RutaTypeConstants.RUTA_TYPE_N == kind) { + return true; } return false; } - private boolean checkFeatureMatch(String name, String type) { - if(name.startsWith(type)) { + private String getShortName(String typeName) { + String[] nameSpace = typeName.split("[.]"); + return nameSpace[nameSpace.length - 1]; + } + + private Collection<FeatureDescription> getAllDeclaredFeatures(TypeDescription typeDescription, + Map<String, TypeDescription> typeMap) { + Collection<FeatureDescription> result = new HashSet<FeatureDescription>(); + if (typeDescription == null) { + return result; + } + FeatureDescription[] features = typeDescription.getFeatures(); + if (features == null) { + return result; + } + result.addAll(Arrays.asList(features)); + String supertypeName = typeDescription.getSupertypeName(); + if (supertypeName != null) { + TypeDescription parent = typeMap.get(supertypeName); + result.addAll(getAllDeclaredFeatures(parent, typeMap)); + } + return result; + } + + private String checkFeatureMatch(String name, String type) { + if (name.startsWith(type)) { boolean foundAll = true; String tail = name.substring(type.length() + 1); String[] split = tail.split("[.]"); @@ -442,34 +561,35 @@ public class RutaTypeChecker implements for (String feat : split) { typeToCheck = checkFSFeatureOfType(feat, typeToCheck); foundAll &= (typeToCheck != null); - if(!foundAll) { - return false; + if (!foundAll) { + return null; } } - return true; + return typeToCheck; } else { - return false; + return null; } } private String checkFSFeatureOfType(String feat, String type) { - if(type.indexOf(".") == -1) { + if (type.indexOf(".") == -1) { for (String each : completeTypes) { String[] split = each.split("[.]"); - if(split[split.length-1].equals(type)) { + if (split[split.length - 1].equals(type)) { type = each; break; } } } TypeDescription t = description.getType(type); - if(t == null) return null; + if (t == null) + return null; FeatureDescription[] features = t.getFeatures(); for (FeatureDescription featureDescription : features) { String name = featureDescription.getName(); String rangeTypeName = featureDescription.getRangeTypeName(); boolean isFS = isFeatureStructure(rangeTypeName); - if(name.equals(feat) && isFS) { + if (name.equals(feat) && isFS) { return rangeTypeName; } } @@ -477,15 +597,15 @@ public class RutaTypeChecker implements } private boolean isFeatureStructure(String rangeTypeName) { - if(rangeTypeName.equals("uima.tcas.Annotation") || rangeTypeName.equals("uima.cas.TOP")) { + if (rangeTypeName.equals("uima.tcas.Annotation") || rangeTypeName.equals("uima.cas.TOP")) { return true; } TypeDescription type = description.getType(rangeTypeName); - if(type == null) { + if (type == null) { return false; } String supertypeName = type.getSupertypeName(); - if(supertypeName != null) { + if (supertypeName != null) { return isFeatureStructure(supertypeName); } return false;