Search in sources :

Example 16 with ParameterValue

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());
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with ParameterValue

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;
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) MessageDigest(java.security.MessageDigest) Cell(eu.esdihumboldt.hale.common.align.model.Cell) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) CustomPropertyFunction(eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)

Example 18 with ParameterValue

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;
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) ParameterPage(eu.esdihumboldt.hale.ui.function.generic.pages.ParameterPage) GenericParameterPage(eu.esdihumboldt.hale.ui.function.generic.pages.GenericParameterPage) IWizardPage(org.eclipse.jface.wizard.IWizardPage) FunctionWizardPage(eu.esdihumboldt.hale.ui.function.generic.pages.FunctionWizardPage)

Example 19 with ParameterValue

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();
}
Also used : Group(org.eclipse.swt.widgets.Group) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) FunctionParameterDefinition(eu.esdihumboldt.hale.common.align.extension.function.FunctionParameterDefinition) GridData(org.eclipse.swt.layout.GridData) Label(org.eclipse.swt.widgets.Label) Point(org.eclipse.swt.graphics.Point) Pair(eu.esdihumboldt.util.Pair)

Example 20 with ParameterValue

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;
}
Also used : Binding(groovy.lang.Binding) ParameterBinding(eu.esdihumboldt.hale.common.align.model.impl.mdexpl.ParameterBinding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Aggregations

ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)80 Test (org.junit.Test)29 Cell (eu.esdihumboldt.hale.common.align.model.Cell)28 DefaultCell (eu.esdihumboldt.hale.common.align.model.impl.DefaultCell)21 AttributeMappingType (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType)14 AppSchemaMappingContext (eu.esdihumboldt.hale.io.appschema.writer.internal.mapping.AppSchemaMappingContext)13 ArrayList (java.util.ArrayList)13 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)9 Property (eu.esdihumboldt.hale.common.align.model.Property)9 ClientProperty (eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.AttributeMappingType.ClientProperty)9 Entity (eu.esdihumboldt.hale.common.align.model.Entity)8 DefaultProperty (eu.esdihumboldt.hale.common.align.model.impl.DefaultProperty)8 Value (eu.esdihumboldt.hale.common.core.io.Value)8 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)7 Type (eu.esdihumboldt.hale.common.align.model.Type)6 AssignHandler (eu.esdihumboldt.hale.io.appschema.writer.internal.AssignHandler)6 JoinParameter (eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter)5 HashSet (java.util.HashSet)5 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)4