Search in sources :

Example 1 with PropertyValueImpl

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl in project hale by halestudio.

the class MathEditor method createPropertyValues.

/**
 * Returns an {@link Iterable} for the current variables for use with the
 * {@link Script}.
 *
 * @return an {@link Iterable} for the current variables for use with the
 *         {@link Script}
 */
protected Iterable<PropertyValue> createPropertyValues() {
    Collection<PropertyValue> result = new ArrayList<PropertyValue>(variables.size());
    // using double results in no /0 exceptions because of stuff like
    // 1/(a-b)
    Double one = Double.valueOf(1);
    for (PropertyEntityDefinition property : variables) result.add(new PropertyValueImpl(one, property));
    return result;
}
Also used : PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ArrayList(java.util.ArrayList) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)

Example 2 with PropertyValueImpl

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl in project hale by halestudio.

the class GroovyTransformationPage method validate.

@Override
protected boolean validate(String document) {
    super.validate(document);
    List<PropertyValue> values = new ArrayList<PropertyValue>();
    for (EntityDefinition var : getVariables()) {
        if (var instanceof PropertyEntityDefinition) {
            PropertyEntityDefinition property = (PropertyEntityDefinition) var;
            values.add(new PropertyValueImpl(testValues.get(property), property));
        }
    }
    Property targetProperty = (Property) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    if (targetProperty == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    InstanceBuilder builder = GroovyTransformation.createBuilder(targetProperty.getDefinition());
    Cell cell = getWizard().getUnfinishedCell();
    boolean useInstanceValues = CellUtil.getOptionalParameter(cell, GroovyTransformation.PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
    AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
    GroovyService gs = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        Collection<? extends Cell> typeCells = as.getAlignment().getTypeCells(cell);
        // select one matching type cell, the script has to run for all
        // matching cells
        // if there is no matching cell it may produce a npe, which is okay
        Cell typeCell = null;
        if (!typeCells.isEmpty()) {
            typeCell = typeCells.iterator().next();
        }
        CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
        ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
        groovy.lang.Binding binding;
        if (cell.getTransformationIdentifier().equals(GroovyGreedyTransformation.ID)) {
            binding = GroovyGreedyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
        } else {
            binding = GroovyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
        }
        script = gs.parseScript(document, binding);
        GroovyTransformation.evaluate(script, builder, targetProperty.getDefinition().getDefinition().getPropertyType(), gs, log);
    } catch (NoResultException e) {
    // continue
    } catch (final Exception e) {
        return handleValidationResult(script, e);
    }
    return handleValidationResult(script, null);
}
Also used : Script(groovy.lang.Script) ArrayList(java.util.ArrayList) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) AlignmentService(eu.esdihumboldt.hale.ui.service.align.AlignmentService) Property(eu.esdihumboldt.hale.common.align.model.Property) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 3 with PropertyValueImpl

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl in project hale by halestudio.

the class FunctionExecutor method executeTransformation.

/**
 * Execute a property transformation.
 *
 * @param transformation the transformation factory
 * @param cell the alignment cell
 * @param sources the named source entities and nodes
 * @param targets the named target entities and nodes
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void executeTransformation(PropertyTransformationFactory transformation, Cell cell, ListMultimap<String, Pair<SourceNode, Entity>> sources, ListMultimap<String, Pair<TargetNode, Entity>> targets) {
    TransformationLog cellLog = new CellLog(reporter, cell);
    PropertyTransformation<?> function;
    try {
        // TODO cache function objects?
        function = transformation.createExtensionObject();
    } catch (Exception e) {
        cellLog.error(cellLog.createMessage("Error creating transformation function.", e));
        return;
    }
    TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
    if (engine == null) {
        // TODO instead try another transformation
        cellLog.error(cellLog.createMessage("Skipping property transformation: No matching transformation engine found", null));
        return;
    }
    // configure function
    // set expected result
    ListMultimap<String, PropertyEntityDefinition> expectedResult = ArrayListMultimap.create(targets.keySet().size(), 1);
    for (Entry<String, Pair<TargetNode, Entity>> targetEntry : targets.entries()) {
        EntityDefinition def = targetEntry.getValue().getSecond().getDefinition();
        expectedResult.put(targetEntry.getKey(), toPropertyEntityDefinition(def));
    }
    function.setExpectedResult(expectedResult);
    // set source variables
    ListMultimap<String, PropertyValue> variables = ArrayListMultimap.create();
    for (Entry<String, Pair<SourceNode, Entity>> sourceEntry : sources.entries()) {
        EntityDefinition def = sourceEntry.getValue().getSecond().getDefinition();
        SourceNode sourceNode = sourceEntry.getValue().getFirst();
        if (TransformationTreeUtil.isEager(cell, sourceNode, cellLog, context.getServiceProvider())) {
            // eager source - all values
            Object[] values = sourceNode.getAllValues();
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    PropertyValue propertyValue = new PropertyValueImpl(values[i], toPropertyEntityDefinition(def));
                    variables.put(sourceEntry.getKey(), propertyValue);
                }
            }
        } else {
            // non-eager source - one value
            Object value = sourceNode.getValue();
            PropertyValue propertyValue = new PropertyValueImpl(value, toPropertyEntityDefinition(def));
            variables.put(sourceEntry.getKey(), propertyValue);
        }
    }
    function.setVariables(variables);
    // set parameters
    function.setParameters(cell.getTransformationParameters());
    // set context
    function.setExecutionContext(context.getCellContext(cell));
    // set target type
    TypeDefinition targetType = null;
    if (!targets.isEmpty()) {
        TargetNode target = targets.values().iterator().next().getFirst();
        targetType = target.getEntityDefinition().getType();
    }
    function.setTargetType(targetType);
    function.setTypeCell(typeCell.get());
    // execute function
    try {
        ((PropertyTransformation) function).execute(transformation.getIdentifier(), engine, transformation.getExecutionParameters(), cellLog, cell);
    } catch (Throwable e) {
        // TODO instead try another transformation?
        cellLog.error(cellLog.createMessage("Skipping property transformation: Executing property transformation failed.", e));
        return;
    }
    // apply function results
    ListMultimap<String, Object> results = function.getResults();
    if (results != null) {
        for (String name : results.keySet()) {
            List<Object> values = results.get(name);
            List<Pair<TargetNode, Entity>> nodes = targets.get(name);
            if (nodes.size() > values.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("Transformation result misses values for result with name {0}", name), null));
            }
            if (values.size() > nodes.size()) {
                cellLog.warn(cellLog.createMessage(MessageFormat.format("More transformation results than target nodes for result with name {0}", name), null));
            }
            int count = Math.min(values.size(), nodes.size());
            // node...
            for (int i = 0; i < count; i++) {
                Object value = values.get(i);
                TargetNode node = nodes.get(i).getFirst();
                if (value instanceof MultiValue) {
                    MultiValue originalValue = (MultiValue) value;
                    MultiValue processedValue = new MultiValue(originalValue.size());
                    for (Object o : originalValue) {
                        processedValue.add(processValue(cellLog, function, o, node));
                    }
                    value = processedValue;
                } else {
                    value = processValue(cellLog, function, value, node);
                }
                /*
					 * TODO
					 * 
					 * set node value only if no result has already been set. If
					 * a value is already there and we are in a lower priority
					 * executor, we do not overwrite.
					 */
                if (!node.isDefined()) {
                    node.setResult(value);
                }
            }
        }
    }
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) PropertyTransformation(eu.esdihumboldt.hale.common.align.transformation.function.PropertyTransformation) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) MultiValue(eu.esdihumboldt.cst.MultiValue) Pair(eu.esdihumboldt.util.Pair) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) ConversionException(org.springframework.core.convert.ConversionException) TransformationEngine(eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TransformationLog(eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)

Example 4 with PropertyValueImpl

use of eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl in project hale by halestudio.

the class GroovyTransformation method createGroovyBinding.

/**
 * Create a Groovy binding from the list of variables.
 *
 * @param vars the variable values
 * @param varDefs definition of the assigned variables, in case some
 *            variable values are not set, may be <code>null</code>
 * @param cell the cell the binding is created for
 * @param typeCell the type cell the binding is created for, may be
 *            <code>null</code>
 * @param builder the instance builder for creating target instances, or
 *            <code>null</code> if not applicable
 * @param useInstanceVariables if instances should be used as variables for
 *            the binding instead of extracting the instance values
 * @param log the transformation log
 * @param context the execution context
 * @param targetInstanceType the type of the target instance
 * @return the binding for use with {@link GroovyShell}
 */
public static Binding createGroovyBinding(List<PropertyValue> vars, List<? extends Entity> varDefs, Cell cell, Cell typeCell, InstanceBuilder builder, boolean useInstanceVariables, TransformationLog log, ExecutionContext context, TypeDefinition targetInstanceType) {
    Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, context, targetInstanceType);
    // collect definitions to check if all were provided
    Set<EntityDefinition> notDefined = new HashSet<>();
    if (varDefs != null) {
        for (Entity entity : varDefs) {
            notDefined.add(entity.getDefinition());
        }
    }
    // keep only defs where no value is provided
    if (!notDefined.isEmpty()) {
        for (PropertyValue var : vars) {
            notDefined.remove(var.getProperty());
        }
    }
    // add null value for missing variables
    if (!notDefined.isEmpty()) {
        vars = new ArrayList<>(vars);
        for (EntityDefinition entityDef : notDefined) {
            if (entityDef instanceof PropertyEntityDefinition) {
                vars.add(new PropertyValueImpl(null, (PropertyEntityDefinition) entityDef));
            }
        }
    }
    for (PropertyValue var : vars) {
        // add the variable to the environment
        addToBinding(binding, var.getProperty(), getUseValue(var.getValue(), useInstanceVariables));
    }
    return binding;
}
Also used : Binding(groovy.lang.Binding) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Entity(eu.esdihumboldt.hale.common.align.model.Entity) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) HashSet(java.util.HashSet) PropertyValueImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)

Aggregations

PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 PropertyValueImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.PropertyValueImpl)4 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)3 CellLog (eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog)2 ArrayList (java.util.ArrayList)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 Entity (eu.esdihumboldt.hale.common.align.model.Entity)1 Property (eu.esdihumboldt.hale.common.align.model.Property)1 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)1 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)1 TransformationEngine (eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine)1 ExecutionContext (eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext)1 PropertyTransformation (eu.esdihumboldt.hale.common.align.transformation.function.PropertyTransformation)1 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)1 TransformationLog (eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)1 DefaultTransformationReporter (eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter)1 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1