use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder 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.hale.common.instance.groovy.InstanceBuilder in project hale by halestudio.
the class InstanceBuilderReader method execute.
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Run instance builder", ProgressIndicator.UNKNOWN);
try {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setScriptBaseClass(DelegatingScript.class.getName());
// Configure the GroovyShell and pass the compiler configuration.
GroovyShell shell = new GroovyShell(getClass().getClassLoader(), new Binding(), compilerConfiguration);
DelegatingScript script;
try (InputStream in = getSource().getInput();
InputStreamReader reader = new InputStreamReader(in, getCharset())) {
script = (DelegatingScript) shell.parse(reader);
}
InstanceBuilder builder = new InstanceBuilder();
// apply schema
builder.setTypes(getSourceSchema());
script.setDelegate(builder);
Object res = script.run();
if (res == null) {
throw new IllegalStateException("Null returned by script");
} else if (res instanceof InstanceCollection) {
instances = (InstanceCollection) res;
} else if (res instanceof Instance) {
instances = new DefaultInstanceCollection(Collections.singleton((Instance) res));
} else {
throw new IllegalStateException("Unrecognised return type: " + res.getClass().getName());
}
reporter.setSuccess(true);
} catch (Exception e) {
reporter.setSuccess(false);
reporter.error("Error running instance builder", e);
} finally {
progress.end();
}
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder 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);
}
use of eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder 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.hale.common.instance.groovy.InstanceBuilder 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;
}
Aggregations