use of com.evolveum.midpoint.repo.common.expression.ExpressionFactory in project midpoint by Evolveum.
the class MappingTestEvaluator method init.
public void init() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
PrismTestUtil.resetPrismContext(MidPointPrismContextFactory.FACTORY);
prismContext = PrismTestUtil.createInitializedPrismContext();
ObjectResolver resolver = new DirectoryFileObjectResolver(MidPointTestConstants.OBJECTS_DIR);
protector = ExpressionTestUtil.createInitializedProtector(prismContext);
ExpressionFactory expressionFactory = ExpressionTestUtil.createInitializedExpressionFactory(resolver, protector, prismContext, null);
mappingFactory = new MappingFactory();
mappingFactory.setExpressionFactory(expressionFactory);
mappingFactory.setObjectResolver(resolver);
mappingFactory.setPrismContext(prismContext);
mappingFactory.setProfiling(true);
mappingFactory.setProtector(protector);
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionFactory 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());
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionFactory in project midpoint by Evolveum.
the class AbstractSearchExpressionEvaluator method evaluatePopulateExpression.
private <IV extends PrismValue, ID extends ItemDefinition, C extends Containerable> ItemDelta<IV, ID> evaluatePopulateExpression(PopulateItemType populateItem, ExpressionVariables variables, ExpressionEvaluationContext params, PrismContainerDefinition<C> objectDefinition, String contextDescription, boolean evaluateMinus, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
ExpressionType expressionType = populateItem.getExpression();
if (expressionType == null) {
LOGGER.warn("No expression in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
return null;
}
VariableBindingDefinitionType targetType = populateItem.getTarget();
if (targetType == null) {
LOGGER.warn("No target in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
return null;
}
ItemPathType itemPathType = targetType.getPath();
if (itemPathType == null) {
throw new SchemaException("No path in target definition in " + contextDescription);
}
ItemPath targetPath = itemPathType.getItemPath();
ID propOutputDefinition = ExpressionUtil.resolveDefinitionPath(targetPath, variables, objectDefinition, "target definition in " + contextDescription);
if (propOutputDefinition == null) {
throw new SchemaException("No target item that would conform to the path " + targetPath + " in " + contextDescription);
}
String expressionDesc = "expression in assignment expression in " + contextDescription;
ExpressionFactory expressionFactory = params.getExpressionFactory();
Expression<IV, ID> expression = expressionFactory.makeExpression(expressionType, propOutputDefinition, expressionDesc, task, result);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, expressionDesc, task, result);
context.setExpressionFactory(expressionFactory);
context.setStringPolicyResolver(params.getStringPolicyResolver());
context.setDefaultTargetContext(params.getDefaultTargetContext());
context.setSkipEvaluationMinus(true);
context.setSkipEvaluationPlus(false);
PrismValueDeltaSetTriple<IV> outputTriple = expression.evaluate(context);
LOGGER.trace("output triple: {}", outputTriple.debugDump());
Collection<IV> pvalues = outputTriple.getNonNegativeValues();
// Maybe not really clean but it works. TODO: refactor later
NameItemPathSegment first = (NameItemPathSegment) targetPath.first();
if (first.isVariable()) {
targetPath = targetPath.rest();
}
ItemDelta<IV, ID> itemDelta = propOutputDefinition.createEmptyDelta(targetPath);
itemDelta.addValuesToAdd(PrismValue.cloneCollection(pvalues));
LOGGER.trace("Item delta:\n{}", itemDelta.debugDump());
return itemDelta;
}
use of com.evolveum.midpoint.repo.common.expression.ExpressionFactory in project midpoint by Evolveum.
the class ExpressionTestUtil method createInitializedExpressionFactory.
public static ExpressionFactory createInitializedExpressionFactory(ObjectResolver resolver, ProtectorImpl protector, PrismContext prismContext, SecurityEnforcer securityEnforcer) {
ExpressionFactory expressionFactory = new ExpressionFactory(resolver, securityEnforcer, prismContext);
// asIs
AsIsExpressionEvaluatorFactory asIsFactory = new AsIsExpressionEvaluatorFactory(prismContext, protector);
expressionFactory.addEvaluatorFactory(asIsFactory);
expressionFactory.setDefaultEvaluatorFactory(asIsFactory);
// value
LiteralExpressionEvaluatorFactory valueFactory = new LiteralExpressionEvaluatorFactory(prismContext);
expressionFactory.addEvaluatorFactory(valueFactory);
// const
ConstantsManager constManager = new ConstantsManager(createConfiguration());
ConstExpressionEvaluatorFactory constFactory = new ConstExpressionEvaluatorFactory(protector, constManager, prismContext);
expressionFactory.addEvaluatorFactory(constFactory);
// path
PathExpressionEvaluatorFactory pathFactory = new PathExpressionEvaluatorFactory(prismContext, resolver, protector);
expressionFactory.addEvaluatorFactory(pathFactory);
// generate
ValuePolicyProcessor valuePolicyGenerator = new ValuePolicyProcessor();
valuePolicyGenerator.setExpressionFactory(expressionFactory);
GenerateExpressionEvaluatorFactory generateFactory = new GenerateExpressionEvaluatorFactory(protector, resolver, valuePolicyGenerator, prismContext);
expressionFactory.addEvaluatorFactory(generateFactory);
// script
Collection<FunctionLibrary> functions = new ArrayList<FunctionLibrary>();
functions.add(FunctionLibraryUtil.createBasicFunctionLibrary(prismContext, protector));
functions.add(FunctionLibraryUtil.createLogFunctionLibrary(prismContext));
ScriptExpressionFactory scriptExpressionFactory = new ScriptExpressionFactory(resolver, prismContext, protector);
scriptExpressionFactory.setFunctions(functions);
XPathScriptEvaluator xpathEvaluator = new XPathScriptEvaluator(prismContext);
scriptExpressionFactory.registerEvaluator(XPathScriptEvaluator.XPATH_LANGUAGE_URL, xpathEvaluator);
Jsr223ScriptEvaluator groovyEvaluator = new Jsr223ScriptEvaluator("Groovy", prismContext, protector);
scriptExpressionFactory.registerEvaluator(groovyEvaluator.getLanguageUrl(), groovyEvaluator);
ScriptExpressionEvaluatorFactory scriptExpressionEvaluatorFactory = new ScriptExpressionEvaluatorFactory(scriptExpressionFactory, securityEnforcer);
expressionFactory.addEvaluatorFactory(scriptExpressionEvaluatorFactory);
return expressionFactory;
}
Aggregations