use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class OMLReaderTest method testAssign2.
/**
* Test for assign function in alignment2
*/
@Test
@Ignore
public // because now NilReasonFunction also produces assign cells
void testAssign2() {
Collection<? extends Cell> cells = alignment2.getCells();
Iterator<? extends Cell> it = cells.iterator();
List<Cell> assignCells = new ArrayList<Cell>();
while (it.hasNext()) {
Cell temp = it.next();
if (temp.getTransformationIdentifier().equals("eu.esdihumboldt.hale.align.assign")) {
assignCells.add(temp);
}
}
// test all cells that have an assign function
for (int i = 0; i < assignCells.size(); i++) {
Cell cell = assignCells.get(i);
ListMultimap<String, ParameterValue> params = cell.getTransformationParameters();
List<ParameterValue> values = params.get("value");
assertEquals(1, values.size());
// size is always 1
String temp = values.get(0).as(String.class);
// test cell #1
if (i == 0) {
assertEquals("manMade", temp);
}
// test cell #2
if (i == 1) {
assertEquals("false", temp);
}
// test cell #3
if (i == 2) {
assertEquals("2009-12-23 12:13:14", temp);
}
// test cell #4
if (i == 3) {
assertEquals("m", temp);
}
}
// check if all cells with an assign function were tested
assertEquals(4, assignCells.size());
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class PreferencesGroovyService method getScriptHash.
/**
* Calculates the current alignments script hash.
*
* @return the current alignments script hash
*/
private synchronized String getScriptHash() {
if (scriptHash == null) {
List<String> scripts = new ArrayList<>();
// get all Groovy scripts
for (Cell cell : alignmentService.getAlignment().getCells()) {
ListMultimap<String, ParameterValue> parameters = cell.getTransformationParameters();
if (parameters == null)
continue;
// Groovy transformations
if (cell.getTransformationIdentifier().contains("groovy")) {
List<ParameterValue> val = parameters.get(GroovyConstants.PARAMETER_SCRIPT);
if (!val.isEmpty()) {
String script = getScriptString(val.get(0));
if (script != null) {
scripts.add(script);
}
}
}
// GroovyScript function parameters
for (ParameterValue value : parameters.values()) {
if (GroovyScript.GROOVY_SCRIPT_ID.equals(value.getType())) {
String script = getScriptString(value);
if (script != null) {
scripts.add(script);
}
}
}
}
// Groovy scripts of custom property functions
for (CustomPropertyFunction customFunction : alignmentService.getAlignment().getAllCustomPropertyFunctions().values()) {
if (customFunction instanceof DefaultCustomPropertyFunction) {
DefaultCustomPropertyFunction cf = (DefaultCustomPropertyFunction) customFunction;
if (CustomPropertyFunctionType.GROOVY.equals(cf.getFunctionType())) {
Value functionDef = cf.getFunctionDefinition();
if (functionDef != null && !functionDef.isEmpty()) {
String script = getScriptString(functionDef);
if (script != null) {
scripts.add(script);
}
}
}
}
}
// order scripts (for consistent hash)
Collections.sort(scripts);
// modify the script in a undetectable way
try {
MessageDigest md = MessageDigest.getInstance("MD5");
for (String script : scripts) md.update(script.getBytes("UTF-8"));
byte[] hash = md.digest();
StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
sb.append(String.format("%02x", b & 0xff));
}
scriptHash = sb.toString();
// Both exceptions cannot happen in a valid Java platform.
// Anyways, if they happen, execution should stop here!
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("No MD5 MessageDigest!");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("No UTF-8 Charset!");
}
}
return scriptHash;
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class AbstractGenericFunctionWizard method performFinish.
/**
* @see Wizard#performFinish()
*/
@Override
public boolean performFinish() {
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
resultCell.setTransformationParameters(parameters);
// configure cell with all pages
for (IWizardPage page : getPages()) if (page instanceof FunctionWizardPage)
((FunctionWizardPage) page).configureCell(resultCell);
else if (page instanceof ParameterPage)
parameters.putAll(((ParameterPage) page).getConfiguration());
return true;
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class GenericParameterPage method createContent.
/**
* @see HaleWizardPage#createContent(Composite)
*/
@Override
protected void createContent(Composite page) {
page.setLayout(GridLayoutFactory.swtDefaults().create());
// create section for each function parameter
for (final FunctionParameterDefinition fp : params) {
boolean fixed = fp.getMinOccurrence() == fp.getMaxOccurrence();
boolean unbounded = fp.getMaxOccurrence() == AbstractParameter.UNBOUNDED;
Group group = new Group(page, SWT.NONE);
group.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
group.setText(fp.getDisplayName());
// only one column if the amount is fixed (-> no remove buttons)
group.setLayout(GridLayoutFactory.swtDefaults().numColumns(fixed ? 1 : 2).create());
if (fp.getDescription() != null) {
Label description = new Label(group, SWT.WRAP);
description.setText(fp.getDescription());
description.setLayoutData(GridDataFactory.swtDefaults().span(fixed ? 1 : 2, 1).align(SWT.FILL, SWT.CENTER).grab(true, false).hint(250, SWT.DEFAULT).create());
}
// walk over data of initial cell while creating input fields
List<ParameterValue> initialData = initialValues.get(fp.getName());
Iterator<ParameterValue> initialDataIter = initialData.iterator();
// create a minimum number of input fields
int i;
for (i = 0; i < fp.getMinOccurrence(); i++) if (initialDataIter.hasNext())
createField(group, fp, initialDataIter.next(), fixed);
else
createField(group, fp, null, fixed);
// create further fields if initial cell has more
for (; initialDataIter.hasNext() && (unbounded || i < fp.getMaxOccurrence()); i++) createField(group, fp, initialDataIter.next(), false);
// create control buttons if max occurrence != min occurrence
if (!fixed)
createAddButton(group, fp, unbounded || i < fp.getMaxOccurrence());
// required
if (i > fp.getMinOccurrence())
for (Pair<AttributeEditor<?>, Button> pair : inputFields.get(fp)) pair.getSecond().setEnabled(true);
}
// update state now that all texts (with validators) are generated
updateState();
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.
the class CustomGroovyTransformation 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 {
// store script as parameter
if (!CustomPropertyFunctionType.GROOVY.equals(customFunction.getFunctionType())) {
throw new TransformationException("Custom function is not of type groovy");
}
Value scriptValue = customFunction.getFunctionDefinition();
if (scriptValue.isEmpty()) {
throw new NoResultException("Script not defined");
}
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
params.put(GroovyTransformation.PARAMETER_SCRIPT, new ParameterValue(scriptValue));
setParameters(params);
// instance builder
InstanceBuilder builder = GroovyTransformation.createBuilder(resultProperty);
// create the script binding
Binding binding = createGroovyBinding(variables, getCell(), getTypeCell(), builder, log, getExecutionContext(), resultProperty.getDefinition().getPropertyType());
Object result;
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script groovyScript = GroovyUtil.getScript(this, binding, service, true);
// evaluate the script
result = GroovyTransformation.evaluate(groovyScript, builder, resultProperty.getDefinition().getPropertyType(), service, log);
} catch (TransformationException | NoResultException e) {
throw e;
} catch (Throwable e) {
throw new TransformationException("Error evaluating the custom function script", e);
}
if (result == null) {
throw new NoResultException();
}
return result;
}
Aggregations