in ruta-core/src/main/java/org/apache/uima/ruta/condition/ContainsCondition.java [82:187]
public EvaluatedCondition eval(MatchContext context, RutaStream stream, InferenceCrowd crowd) {
AnnotationFS annotation = context.getAnnotation();
boolean usePercentage = percent.getBooleanValue(context, stream);
int minIntValue = min.getIntegerValue(context, stream);
int maxIntValue = max.getIntegerValue(context, stream);
int basicCount = 0;
int anchorCount = 0;
int totalCount = 0;
boolean usePredefinedBoundaries = minIntValue == 1 && maxIntValue == Integer.MAX_VALUE ? false
: true;
if (type != null) {
Type t = type.getType(context, stream);
if (annotation != null && t != null) {
if (!usePredefinedBoundaries && !usePercentage) {
// shortcut for simple CONTAINS(Type)
boolean annotationExsits = checkExistingAnnotation(t, annotation, stream);
return new EvaluatedCondition(this, annotationExsits);
} else {
List<RutaBasic> annotations = stream.getBasicsInWindow(annotation);
for (RutaBasic each : annotations) {
totalCount++;
if (each.beginsWith(t) || stream.getCas().getTypeSystem().subsumes(t, each.getType())) {
Collection<AnnotationFS> beginAnchors = each.getBeginAnchors(t);
anchorCount = incrementAnchorsWithinStrictBoundaries(annotation, anchorCount,
beginAnchors);
basicCount++;
} else if (each.isPartOf(t)) {
basicCount++;
}
}
}
}
} else {
List<?> list = argList.getList(context, stream);
totalCount = list.size();
Object sniff = null;
if (totalCount > 0) {
sniff = list.get(0);
}
if (check(sniff, Boolean.class) && arg instanceof IBooleanExpression
&& argList instanceof IBooleanListExpression) {
IBooleanExpression e = (IBooleanExpression) arg;
IBooleanListExpression le = (IBooleanListExpression) argList;
boolean v = e.getBooleanValue(context, stream);
List<Boolean> l = new ArrayList<Boolean>(le.getBooleanList(context, stream));
while (l.remove(v)) {
basicCount++;
}
} else if (check(sniff, Number.class) && arg instanceof INumberExpression
&& argList instanceof INumberListExpression) {
INumberExpression e = (INumberExpression) arg;
INumberListExpression le = (INumberListExpression) argList;
Double v = e.getDoubleValue(context, stream);
// TODO: this wont work for numbers yet!
List<Number> l = new ArrayList<Number>(le.getNumberList(context, stream));
while (l.remove(v)) {
basicCount++;
}
} else if (check(sniff, String.class) && arg instanceof IStringExpression
&& argList instanceof IStringListExpression) {
IStringExpression e = (IStringExpression) arg;
IStringListExpression le = (IStringListExpression) argList;
String v = e.getStringValue(context, stream);
List<String> l = new ArrayList<String>(le.getStringList(context, stream));
while (l.remove(v)) {
basicCount++;
}
} else if (check(sniff, Type.class) && arg instanceof ITypeExpression
&& argList instanceof ITypeListExpression) {
ITypeExpression e = (ITypeExpression) arg;
ITypeListExpression le = (ITypeListExpression) argList;
Type v = e.getType(context, stream);
List<Type> l = new ArrayList<Type>(le.getTypeList(context, stream));
while (l.remove(v)) {
basicCount++;
}
} else if (check(sniff, AnnotationFS.class) && arg instanceof IAnnotationExpression
&& argList instanceof IAnnotationListExpression) {
IAnnotationExpression e = (IAnnotationExpression) arg;
IAnnotationListExpression le = (IAnnotationListExpression) argList;
AnnotationFS v = e.getAnnotation(context, stream);
List<AnnotationFS> l = new ArrayList<AnnotationFS>(le.getAnnotationList(context, stream));
while (l.remove(v)) {
basicCount++;
}
}
anchorCount = basicCount;
}
if (usePercentage) {
double percentValue = 0;
if (totalCount != 0) {
percentValue = (((double) basicCount) / ((double) totalCount)) * 100;
}
boolean value = percentValue >= min.getDoubleValue(context, stream)
&& percentValue <= max.getDoubleValue(context, stream);
return new EvaluatedCondition(this, value);
} else {
boolean value = anchorCount >= minIntValue && anchorCount <= maxIntValue;
return new EvaluatedCondition(this, value);
}
}