Search in sources :

Example 11 with GroovyService

use of eu.esdihumboldt.util.groovy.sandbox.GroovyService 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 12 with GroovyService

use of eu.esdihumboldt.util.groovy.sandbox.GroovyService 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)

Example 13 with GroovyService

use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.

the class GroovyFilter method match.

@Override
public boolean match(Instance instance, Map<Object, Object> context) {
    GroovyService service = getGroovyService();
    Binding binding = new Binding();
    // helper functions
    binding.setVariable(GroovyConstants.BINDING_HELPER_FUNCTIONS, HelperFunctions.createDefault(null));
    // instance
    binding.setVariable("instance", instance);
    // context
    binding.setVariable("withContext", SynchronizedContextProvider.getContextClosure(context));
    // log
    binding.setVariable(GroovyConstants.BINDING_LOG, new LogWrapper(log));
    Script script = getScript(service, binding);
    try {
        return service.evaluate(script, new ResultProcessor<Boolean>() {

            @Override
            public Boolean process(Script script, Object returnValue) throws Exception {
                if (returnValue != null && returnValue.equals(true)) {
                    return true;
                }
                return false;
            }
        });
    } catch (Exception e) {
        throw new IllegalStateException("Error evaluating Groovy filter", e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) DefaultGroovyService(eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService)

Example 14 with GroovyService

use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.

the class ExecTransformation method transform.

private void transform() throws InterruptedException, ExecutionException {
    status("Running hale transformation...");
    // configure transformation environment
    // override/set Groovy service
    GroovyService gs = new DefaultGroovyService();
    gs.setRestrictionActive(context.isRestrictGroovy());
    env.addService(GroovyService.class, gs);
    // run transformation
    ListenableFuture<Boolean> res = Transformation.transform(sources, target, env, reportHandler, id, validators, context.getFilters());
    if (res.get()) {
        info("Transformation completed. Please check the reports for more details.");
    } else {
        // complete and file their report (otherwise error may get lost)
        try {
            Thread.sleep(3000);
        } catch (Throwable e) {
        // ignore
        }
        throw fail("Transformation failed, please check the reports for details.");
    }
}
Also used : DefaultGroovyService(eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) DefaultGroovyService(eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService)

Example 15 with GroovyService

use of eu.esdihumboldt.util.groovy.sandbox.GroovyService in project hale by halestudio.

the class GroovyScript method evaluate.

/**
 * @see Script#evaluate(String, Iterable, ServiceProvider)
 */
@Override
public Object evaluate(String script, Iterable<PropertyValue> variables, ServiceProvider provider) throws ScriptException {
    Binding binding = createGroovyBinding(variables, true);
    GroovyService service = provider.getService(GroovyService.class);
    Object result;
    try {
        result = service.evaluate(service.parseScript(script, binding), new ResultProcessor<Object>() {

            @Override
            public Object process(groovy.lang.Script script, Object returnValue) throws Exception {
                return returnValue;
            }
        });
    } catch (Exception e) {
        throw new ScriptException(e);
    } catch (Throwable t) {
        throw new ScriptException(t.toString());
    }
    if (result == null) {
        // XXX throw new NoResultException(); ? as cause for SE?
        throw new ScriptException("no result");
    }
    return result;
}
Also used : Binding(groovy.lang.Binding) Script(eu.esdihumboldt.hale.common.scripting.Script) ScriptException(javax.script.ScriptException) ResultProcessor(eu.esdihumboldt.util.groovy.sandbox.GroovyService.ResultProcessor) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) ConversionException(org.springframework.core.convert.ConversionException) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException)

Aggregations

GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)17 Binding (groovy.lang.Binding)11 Script (groovy.lang.Script)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)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 DefaultGroovyService (eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService)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 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)2 MissingPropertyException (groovy.lang.MissingPropertyException)2 ScriptException (javax.script.ScriptException)2