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