Search in sources :

Example 6 with ExpressionEvaluationContext

use of com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext in project midpoint by Evolveum.

the class Mapping method evaluateExpression.

private void evaluateExpression(Task task, OperationResult result, boolean conditionResultOld, boolean conditionResultNew) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ExpressionType expressionType = null;
    if (mappingType != null) {
        expressionType = mappingType.getExpression();
    }
    expression = expressionFactory.makeExpression(expressionType, outputDefinition, "expression in " + getMappingContextDescription(), task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(sources, variables, "expression in " + getMappingContextDescription(), task, result);
    context.setDefaultSource(defaultSource);
    context.setSkipEvaluationMinus(!conditionResultOld);
    context.setSkipEvaluationPlus(!conditionResultNew);
    context.setStringPolicyResolver(stringPolicyResolver);
    context.setExpressionFactory(expressionFactory);
    context.setDefaultTargetContext(getTargetContext());
    context.setRefinedObjectClassDefinition(getRefinedObjectClassDefinition());
    context.setMappingQName(mappingQName);
    outputTriple = expression.evaluate(context);
    if (outputTriple == null) {
        if (conditionResultNew) {
            // We need to return empty triple instead of null.
            // The condition was true (or there was not condition at all)
            // so the mapping is applicable.
            // Returning null would mean that the mapping is not applicable
            // at all.
            outputTriple = new PrismValueDeltaSetTriple<>();
        }
    } else {
        // reflect condition change
        if (!conditionResultOld && conditionResultNew) {
            // Condition change false -> true
            outputTriple.addAllToPlusSet(outputTriple.getZeroSet());
            outputTriple.clearZeroSet();
            outputTriple.clearMinusSet();
        }
        if (conditionResultOld && !conditionResultNew) {
            // Condition change true -> false
            outputTriple.addAllToMinusSet(outputTriple.getZeroSet());
            outputTriple.clearZeroSet();
            outputTriple.clearPlusSet();
        }
    }
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)

Example 7 with ExpressionEvaluationContext

use of com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext in project midpoint by Evolveum.

the class Mapping method evaluateCondition.

private void evaluateCondition(Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ExpressionType conditionExpressionType = mappingType.getCondition();
    if (conditionExpressionType == null) {
        // True -> True
        conditionOutputTriple = new PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>>();
        conditionOutputTriple.addToZeroSet(new PrismPropertyValue<Boolean>(Boolean.TRUE));
        return;
    }
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = ExpressionUtil.createCondition(conditionExpressionType, expressionFactory, "condition in " + getMappingContextDescription(), task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(sources, variables, "condition in " + getMappingContextDescription(), task, result);
    context.setStringPolicyResolver(stringPolicyResolver);
    context.setExpressionFactory(expressionFactory);
    context.setDefaultSource(defaultSource);
    context.setDefaultTargetContext(getTargetContext());
    context.setRefinedObjectClassDefinition(getRefinedObjectClassDefinition());
    context.setMappingQName(mappingQName);
    conditionOutputTriple = expression.evaluate(context);
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)

Example 8 with ExpressionEvaluationContext

use of com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext in project midpoint by Evolveum.

the class AccCertExpressionHelper method evaluateExpression.

private <T> List<T> evaluateExpression(Class<T> resultClass, ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName xsdType = XsdTypeMapper.toXsdType(resultClass);
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<T> resultDef = new PrismPropertyDefinitionImpl<>(resultName, xsdType, prismContext);
    Expression<PrismPropertyValue<T>, PrismPropertyDefinition<T>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<T>> exprResult = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    List<T> retval = new ArrayList<>();
    for (PrismPropertyValue<T> item : exprResult.getZeroSet()) {
        retval.add(item.getValue());
    }
    return retval;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList)

Example 9 with ExpressionEvaluationContext

use of com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext in project midpoint by Evolveum.

the class PrimaryChangeAspectHelper method evaluateApplicabilityCondition.

//endregion
//region ========================================================================== Expression evaluation
public boolean evaluateApplicabilityCondition(PcpAspectConfigurationType config, ModelContext modelContext, Serializable itemToApprove, ExpressionVariables additionalVariables, PrimaryChangeAspect aspect, Task task, OperationResult result) {
    if (config == null || config.getApplicabilityCondition() == null) {
        return true;
    }
    ExpressionType expressionType = config.getApplicabilityCondition();
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
    ExpressionVariables expressionVariables = new ExpressionVariables();
    expressionVariables.addVariableDefinition(SchemaConstants.C_MODEL_CONTEXT, modelContext);
    expressionVariables.addVariableDefinition(SchemaConstants.C_ITEM_TO_APPROVE, itemToApprove);
    if (additionalVariables != null) {
        expressionVariables.addVariableDefinitions(additionalVariables);
    }
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple;
    try {
        Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, "applicability condition expression", task, result);
        ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, "applicability condition expression", task, result);
        exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    } catch (SchemaException | ExpressionEvaluationException | ObjectNotFoundException | RuntimeException e) {
        // TODO report as a specific exception?
        throw new SystemException("Couldn't evaluate applicability condition in aspect " + aspect.getClass().getSimpleName() + ": " + e.getMessage(), e);
    }
    Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
    if (exprResult.size() == 0) {
        return false;
    } else if (exprResult.size() > 1) {
        throw new IllegalStateException("Applicability condition expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
    }
    Boolean boolResult = exprResult.iterator().next().getValue();
    return boolResult != null ? boolResult : false;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 10 with ExpressionEvaluationContext

use of com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext in project midpoint by Evolveum.

the class WfExpressionEvaluationHelper method evaluateExpression.

@SuppressWarnings("unchecked")
@NotNull
public <T> List<T> evaluateExpression(ExpressionType expressionType, ExpressionVariables variables, String contextDescription, Class<T> clazz, QName typeName, Function<Object, Object> additionalConvertor, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    ExpressionFactory expressionFactory = getExpressionFactory();
    PrismContext prismContext = expressionFactory.getPrismContext();
    PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(new QName(SchemaConstants.NS_C, "result"), typeName, prismContext);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, contextDescription, task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDescription, task, result);
    context.setAdditionalConvertor(additionalConvertor);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, context, task, result);
    return exprResultTriple.getZeroSet().stream().map(ppv -> (T) ppv.getRealValue()).collect(Collectors.toList());
}
Also used : SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) SpringApplicationContextHolder.getMiscDataUtil(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getMiscDataUtil) MiscDataUtil(com.evolveum.midpoint.wf.impl.util.MiscDataUtil) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DOMUtil(com.evolveum.midpoint.util.DOMUtil) QNameUtil(com.evolveum.midpoint.util.QNameUtil) PrismContext(com.evolveum.midpoint.prism.PrismContext) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) ModelExpressionThreadLocalHolder(com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task) ApprovalUtils(com.evolveum.midpoint.wf.util.ApprovalUtils) JAXBException(javax.xml.bind.JAXBException) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Component(org.springframework.stereotype.Component) List(java.util.List) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) DelegateExecution(org.activiti.engine.delegate.DelegateExecution) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ApprovalLevelOutcomeType(com.evolveum.midpoint.xml.ns._public.common.common_3.ApprovalLevelOutcomeType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)25 QName (javax.xml.namespace.QName)14 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)12 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)11 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)11 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)9 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)6 ArrayList (java.util.ArrayList)6 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 PrismValue (com.evolveum.midpoint.prism.PrismValue)2 PrismValueDeltaSetTriple (com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)2 ItemDeltaItem (com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)2 Source (com.evolveum.midpoint.repo.common.expression.Source)2