Search in sources :

Example 6 with InstanceBuilder

use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder in project hale by halestudio.

the class GroovyRetype method execute.

@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) throws TransformationException {
    // for each source instance create a target instance
    TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
    InstanceBuilder builder = new InstanceBuilder(false);
    Binding binding = createBinding(getSource(), cell, builder, log, getExecutionContext(), targetType);
    try {
        GroovyService service = getExecutionContext().getService(GroovyService.class);
        Script script = GroovyUtil.getScript(this, binding, service);
        Iterable<MutableInstance> targets = GroovyUtil.evaluateAll(script, builder, targetType, service, log);
        for (MutableInstance target : targets) {
            getPropertyTransformer().publish(getSource(), target, log, cell);
        }
    } catch (TransformationException e) {
        throw e;
    } catch (NoResultException e) {
        log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
    } catch (Exception e) {
        throw new TransformationException(e.getMessage(), e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 7 with InstanceBuilder

use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder in project hale by halestudio.

the class InstanceBuilderReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Run instance builder", ProgressIndicator.UNKNOWN);
    try {
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
        // Configure the GroovyShell and pass the compiler configuration.
        GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
        DelegatingScript script;
        try (InputStream in = getSource().getInput();
            InputStreamReader reader = new InputStreamReader(in, getCharset())) {
            script = (DelegatingScript) shell.parse(reader);
        }
        InstanceBuilder builder = new InstanceBuilder();
        // apply schema
        builder.setTypes(getSourceSchema());
        script.setDelegate(builder);
        Object res = script.run();
        if (res == null) {
            throw new IllegalStateException("Null returned by script");
        } else if (res instanceof InstanceCollection) {
            instances = (InstanceCollection) res;
        } else if (res instanceof Instance) {
            instances = new DefaultInstanceCollection(Collections.singleton((Instance) res));
        } else {
            throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
        }
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.setSuccess(false);
        reporter.error("Error running instance builder", e);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : Binding(groovy.lang.Binding) InputStreamReader(java.io.InputStreamReader) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InputStream(java.io.InputStream) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) DefaultInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstanceCollection) GroovyShell(groovy.lang.GroovyShell) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException) DelegatingScript(groovy.util.DelegatingScript) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 8 with InstanceBuilder

use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder in project hale by halestudio.

the class GroovyRetypePage method validate.

@Override
protected boolean validate(String document) {
    super.validate(document);
    Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    Type sourceType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getSource());
    if (sourceType == null || targetType == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    InstanceBuilder builder = new InstanceBuilder(false);
    Instance instance = testValues.get(sourceType.getDefinition());
    if (instance == null) {
        // use an empty instance as input for the script
        instance = new DefaultInstance(sourceType.getDefinition().getDefinition(), DataSet.SOURCE);
    }
    FamilyInstance source = new FamilyInstanceImpl(instance);
    Cell cell = getWizard().getUnfinishedCell();
    CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
    ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
    Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
    GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        script = service.parseScript(document, binding);
        GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
    } catch (final Exception e) {
        return handleValidationResult(script, e);
    }
    return handleValidationResult(script, null);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Type(eu.esdihumboldt.hale.common.align.model.Type) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 9 with InstanceBuilder

use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder 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 10 with InstanceBuilder

use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder in project hale by halestudio.

the class GroovyTransformation method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // determine if instances should be used in variables or their values
    boolean useInstanceVariables = getOptionalParameter(PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
    // instance builder
    InstanceBuilder builder = createBuilder(resultProperty);
    // create the script binding
    List<? extends Entity> varDefs = null;
    if (getCell().getSource() != null) {
        varDefs = getCell().getSource().get(ENTITY_VARIABLE);
    }
    Binding binding = createGroovyBinding(variables.get(ENTITY_VARIABLE), varDefs, getCell(), getTypeCell(), builder, useInstanceVariables, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
    Object result;
    try {
        GroovyService service = getExecutionContext().getService(GroovyService.class);
        Script groovyScript = GroovyUtil.getScript(this, binding, service);
        // evaluate the script
        result = evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
    } catch (TransformationException | NoResultException e) {
        throw e;
    } catch (Throwable e) {
        throw new TransformationException("Error evaluating the cell script", e);
    }
    if (result == null) {
        throw new NoResultException();
    }
    return result;
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Aggregations

InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)10 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)9 Binding (groovy.lang.Binding)9 Script (groovy.lang.Script)9 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)6 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 Cell (eu.esdihumboldt.hale.common.align.model.Cell)4 ExecutionContext (eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext)4 CellLog (eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog)4 DefaultTransformationReporter (eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter)4 Type (eu.esdihumboldt.hale.common.align.model.Type)3 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)3 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)2 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)2 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)2 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)2 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)2 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 Property (eu.esdihumboldt.hale.common.align.model.Property)1 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)1