Search in sources :

Example 51 with Script

use of groovy.lang.Script 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 52 with Script

use of groovy.lang.Script 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 53 with Script

use of groovy.lang.Script in project hale by halestudio.

the class GroovySnippets method loadScript.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Script loadScript(String id, Map<?, ?> moreVariables) throws Exception {
    Optional<Snippet> snippet = snippets.getSnippet(id);
    if (snippet.isPresent()) {
        Script script = snippet.get().getScript(services);
        // "clone" the script
        script = script.getClass().newInstance();
        Map variables = new HashMap<>();
        variables.put(BINDING_HALE_MARKER, true);
        if (parentBinding != null) {
            variables.putAll(parentBinding.getVariables());
        }
        if (moreVariables != null) {
            variables.putAll(moreVariables);
        }
        Binding binding = new Binding(variables);
        script.setBinding(binding);
        return script;
    } else {
        return null;
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 54 with Script

use of groovy.lang.Script in project hale by halestudio.

the class GroovyCreatePage method validate.

@Override
protected boolean validate(String document) {
    super.validate(document);
    Type typeEntity = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    if (typeEntity == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    InstanceBuilder builder = new InstanceBuilder(false);
    Cell cell = getWizard().getUnfinishedCell();
    CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
    ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
    Binding binding = GroovyUtil.createBinding(builder, cell, cell, log, context, typeEntity.getDefinition().getDefinition());
    binding.setProperty(BINDING_INDEX, 0);
    GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        script = service.parseScript(document, binding);
        GroovyUtil.evaluateAll(script, builder, typeEntity.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) Type(eu.esdihumboldt.hale.common.align.model.Type) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 55 with Script

use of groovy.lang.Script in project hale by halestudio.

the class GroovyJoinPage method validate.

@Override
protected boolean validate(String document) {
    ParameterValue param = CellUtil.getFirstParameter(getWizard().getUnfinishedCell(), JoinFunction.PARAMETER_JOIN);
    JoinParameter joinParameter = param.as(JoinParameter.class);
    // check Join parameter
    if (joinParameter == null) {
        // setValidationError("Missing join configuration");
        return false;
    } else {
        String error = joinParameter.validate();
        if (!setValidationError(error)) {
            return false;
        }
    }
    // target type
    Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    if (targetType == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    /*
		 * FIXME use JoinParameter to fake joined instances!
		 * 
		 * XXX for now just base instance
		 */
    TypeEntityDefinition sourceType = joinParameter.getTypes().get(0);
    InstanceBuilder builder = new InstanceBuilder(false);
    Instance instance = getTestValues().get(sourceType);
    if (instance == null) {
        // use an empty instance as input for the script
        instance = new DefaultInstance(sourceType.getDefinition(), DataSet.SOURCE);
    }
    FamilyInstance source = new FamilyInstanceImpl(instance);
    // prepare binding
    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) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) 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) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) 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) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) 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

Script (groovy.lang.Script)123 Binding (groovy.lang.Binding)59 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 File (java.io.File)12 Map (java.util.Map)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 Closure (groovy.lang.Closure)8 PrintWriter (java.io.PrintWriter)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 MissingMethodException (groovy.lang.MissingMethodException)7