Search in sources :

Example 11 with InvalidExpressionException

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

the class XPathDataQueryEvaluator method evaluate.

@Override
public List<Node<?>> evaluate(CollectRecord record) {
    List<Node<?>> result = new ArrayList<Node<?>>();
    String condition = query.getConditions();
    AttributeDefinition attrDef = query.getAttributeDefinition();
    SurveyContext surveyContext = record.getSurveyContext();
    ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
    List<Node<?>> nodes = record.findNodesByPath(attrDef.getPath());
    for (Node<?> node : nodes) {
        try {
            if (expressionEvaluator.evaluateBoolean(node.getParent(), node, condition)) {
                result.add(node);
            }
        } catch (InvalidExpressionException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 12 with InvalidExpressionException

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

the class ComparisonCheck method evaluate.

@Override
public ValidationResultFlag evaluate(Attribute<?, ?> node) {
    Record record = node.getRecord();
    SurveyContext recordContext = record.getSurveyContext();
    ExpressionEvaluator expressionEvaluator = recordContext.getExpressionEvaluator();
    String expr = getExpression();
    try {
        boolean valid = expressionEvaluator.evaluateBoolean(node.getParent(), node, expr, true);
        return ValidationResultFlag.valueOf(valid, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating comparison check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Record(org.openforis.idm.model.Record) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 13 with InvalidExpressionException

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

the class DistanceCheck method evaluate.

@Override
public ValidationResultFlag evaluate(CoordinateAttribute coordinateAttr) {
    CoordinateOperations coordinateOperations = getCoordinateOperations(coordinateAttr);
    if (coordinateOperations == null) {
        return ValidationResultFlag.OK;
    }
    try {
        ExpressionEvaluator expressionEvaluator = getExpressionEvaluator(coordinateAttr);
        boolean valid = expressionEvaluator.evaluateBoolean(coordinateAttr.getParent(), coordinateAttr, getExpression());
        return ValidationResultFlag.valueOf(valid, this.getFlag());
    } catch (InvalidExpressionException e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Unable to evaluate distance check ", e);
        }
        return ValidationResultFlag.OK;
    }
}
Also used : CoordinateOperations(org.openforis.idm.geospatial.CoordinateOperations) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 14 with InvalidExpressionException

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

the class DistanceCheck method evaluateValueExpression.

private <T extends Object> T evaluateValueExpression(String expression, CoordinateAttribute thisNode) {
    if (expression == null) {
        return null;
    }
    try {
        ExpressionEvaluator expressionEvaluator = getExpressionEvaluator(thisNode);
        @SuppressWarnings("unchecked") T result = (T) expressionEvaluator.evaluateValue(thisNode.getParent(), thisNode, 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 15 with InvalidExpressionException

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

the class RecordUpdater method recalculateValue.

private Value recalculateValue(Attribute<?, ?> attribute) {
    try {
        AttributeDefinition defn = attribute.getDefinition();
        List<AttributeDefault> attributeDefaults = defn.getAttributeDefaults();
        for (AttributeDefault attributeDefault : attributeDefaults) {
            if (attributeDefault.evaluateCondition(attribute)) {
                Value value = attributeDefault.evaluate(attribute);
                return value;
            }
        }
        return null;
    } catch (InvalidExpressionException e) {
        throw new IllegalStateException(String.format("Invalid expression for calculated attribute %s", attribute.getPath()));
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue) BooleanAttributeDefinition(org.openforis.idm.metamodel.BooleanAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition)

Aggregations

InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)16 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)10 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)6 SurveyContext (org.openforis.idm.metamodel.SurveyContext)6 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3 Node (org.openforis.idm.model.Node)3 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 Record (org.openforis.idm.model.Record)2 Value (org.openforis.idm.model.Value)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 DataTransformation (org.openforis.collect.io.data.csv.DataTransformation)1 ModelCsvWriter (org.openforis.collect.io.data.csv.ModelCsvWriter)1 CollectRecord (org.openforis.collect.model.CollectRecord)1