Search in sources :

Example 1 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class CustomCheck method evaluate.

@Override
public ValidationResultFlag evaluate(Attribute<?, ?> node) {
    try {
        ExpressionEvaluator expressionEvaluator = node.getSurvey().getContext().getExpressionEvaluator();
        boolean valid = expressionEvaluator.evaluateBoolean(node.getParent(), node, getExpression());
        return ValidationResultFlag.valueOf(valid, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating custom check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 2 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class DistanceCheck method evaluateAttributeValueExpression.

private <T extends Object> T evaluateAttributeValueExpression(String expression, CoordinateAttribute thisNode) {
    if (expression == null) {
        return null;
    }
    try {
        ExpressionEvaluator expressionEvaluator = getExpressionEvaluator(thisNode);
        T result = expressionEvaluator.evaluateAttributeValue(thisNode.getParent(), thisNode, thisNode.getDefinition(), expression);
        return result;
    } catch (InvalidExpressionException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(String.format("[survey %s: coordinate attribute: %s] Unable to evaluate expression %s" + thisNode.getSurvey().getName(), thisNode.getPath(), expression), e);
        }
        return null;
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 3 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class UniquenessCheck method evaluate.

@Override
public ValidationResultFlag evaluate(final Attribute<?, ?> attribute) {
    try {
        SurveyContext recordContext = attribute.getRecord().getSurveyContext();
        ExpressionEvaluator expressionEvaluator = recordContext.getExpressionEvaluator();
        Node<?> duplicateNode = expressionEvaluator.findNode(attribute.getParent(), attribute, expression, new Predicate<Node<?>>() {

            public boolean evaluate(Node<?> node) {
                if (node instanceof Attribute && node != attribute) {
                    Value value = ((Attribute<?, ?>) node).getValue();
                    if (value != null && value.equals(attribute.getValue())) {
                        return true;
                    }
                }
                return false;
            }
        });
        boolean unique = duplicateNode == null;
        return ValidationResultFlag.valueOf(unique, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating uniqueness check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) Attribute(org.openforis.idm.model.Attribute) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) Value(org.openforis.idm.model.Value) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 4 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class AttributeDefault method evaluateExpression.

private <V extends Value> V evaluateExpression(Attribute<?, V> attrib) throws InvalidExpressionException {
    ExpressionEvaluator expressionEvaluator = getExpressionEvaluator(attrib);
    V val = expressionEvaluator.evaluateAttributeValue(attrib.getParent(), attrib, attrib.getDefinition(), expression);
    return val;
}
Also used : ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 5 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class RecordUpdater method calculateMaxCount.

private int calculateMaxCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMaxCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return defn.isMultiple() ? Integer.MAX_VALUE : 1;
    }
    if (defn.getFixedMaxCount() != null) {
        return defn.getFixedMaxCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? Integer.MAX_VALUE : value.intValue();
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating required expression", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Aggregations

ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)14 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)10 SurveyContext (org.openforis.idm.metamodel.SurveyContext)9 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)5 Node (org.openforis.idm.model.Node)3 Record (org.openforis.idm.model.Record)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 Entity (org.openforis.idm.model.Entity)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 CodeListImportException (org.openforis.collect.io.exception.CodeListImportException)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CoordinateOperations (org.openforis.idm.geospatial.CoordinateOperations)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 Schema (org.openforis.idm.metamodel.Schema)1 IdmlParseException (org.openforis.idm.metamodel.xml.IdmlParseException)1 Attribute (org.openforis.idm.model.Attribute)1 CodeAttribute (org.openforis.idm.model.CodeAttribute)1