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;
}
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);
}
}
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;
}
}
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;
}
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);
}
Aggregations