Search in sources :

Example 6 with GroovyService

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

the class GroovyGreedyTransformation 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 = GroovyTransformation.createBuilder(resultProperty);
    // create the script binding
    Binding binding = createGroovyBinding(variables.get(ENTITY_VARIABLE), getCell().getSource().get(ENTITY_VARIABLE), 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 = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
    } catch (NoResultException | TransformationException 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 7 with GroovyService

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

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

the class GroovyFilter method getGroovyService.

/**
 * @return the Groovy service to use for building and evaluating the filter
 *         script.
 */
protected GroovyService getGroovyService() {
    synchronized (this) {
        if (groovyService != null) {
            return groovyService;
        }
        GroovyService result = HalePlatform.getService(GroovyService.class);
        // fall back to restrictive Groovy service
        if (result == null) {
            DefaultGroovyService def = new DefaultGroovyService();
            def.setRestrictionActive(true);
            result = def;
            log.warn("No GroovyService found, using restricted execution for filter");
        }
        groovyService = result;
        return result;
    }
}
Also used : DefaultGroovyService(eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) DefaultGroovyService(eu.esdihumboldt.util.groovy.sandbox.DefaultGroovyService)

Example 9 with GroovyService

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

the class GroovyScript method validate.

/**
 * @see Script#validate(String, Iterable, ServiceProvider)
 */
@Override
public String validate(String script, Iterable<PropertyValue> variables, ServiceProvider provider) {
    Binding binding = createGroovyBinding(variables, false);
    GroovyService service = provider.getService(GroovyService.class);
    try {
        service.evaluate(service.parseScript(script, binding), null);
    } catch (Exception e) {
        return e.getLocalizedMessage();
    }
    return null;
}
Also used : Binding(groovy.lang.Binding) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) ConversionException(org.springframework.core.convert.ConversionException) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException)

Example 10 with GroovyService

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

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