Search in sources :

Example 6 with DefaultCustomPropertyFunction

use of eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction in project hale by halestudio.

the class PropertyFunctionScriptPage method addActions.

@Override
protected void addActions(ToolBar toolbar, CompilingSourceViewer<GroovyAST> viewer) {
    super.addActions(toolbar, viewer);
    PageHelp.createToolItem(toolbar, this);
    TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.SOURCE, new TypeProvider() {

        @Override
        public Collection<? extends TypeDefinition> getTypes() {
            // create a dummy type with the variables as children
            DefaultTypeDefinition dummy = new DefaultTypeDefinition(TypeStructureTray.VARIABLES_TYPE_NAME);
            DefaultCustomPropertyFunction cf = getWizard().getUnfinishedFunction();
            int index = 0;
            for (EntityDefinition variable : getVariables()) {
                DefaultCustomPropertyFunctionEntity source = cf.getSources().get(index);
                if (variable.getDefinition() instanceof PropertyDefinition) {
                    PropertyDefinition prop = (PropertyDefinition) variable.getDefinition();
                    TypeDefinition propertyType;
                    boolean useInstanceValue = CustomGroovyTransformation.useInstanceVariableForSource(source);
                    if (useInstanceValue) {
                        // use instance type
                        propertyType = prop.getPropertyType();
                    } else {
                        // use dummy type with only the
                        // binding/HasValueFlag copied
                        DefaultTypeDefinition crippledType = new DefaultTypeDefinition(prop.getPropertyType().getName());
                        crippledType.setConstraint(prop.getPropertyType().getConstraint(Binding.class));
                        crippledType.setConstraint(prop.getPropertyType().getConstraint(HasValueFlag.class));
                        propertyType = crippledType;
                    }
                    DefaultPropertyDefinition dummyProp = new DefaultPropertyDefinition(new QName(source.getName()), dummy, propertyType);
                    // number of times
                    if (source.isEager())
                        dummyProp.setConstraint(Cardinality.CC_ANY_NUMBER);
                }
                index++;
            }
            return Collections.singleton(dummy);
        }
    });
    TypeStructureTray.createToolItem(toolbar, this, SchemaSpaceID.TARGET, new TypeProvider() {

        @Override
        public Collection<? extends TypeDefinition> getTypes() {
            DefaultCustomPropertyFunctionEntity target = getWizard().getUnfinishedFunction().getTarget();
            if (target != null) {
                return Collections.singleton(createDummyType(target));
            }
            return Collections.emptyList();
        }
    });
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) QName(javax.xml.namespace.QName) TypeProvider(eu.esdihumboldt.hale.ui.functions.groovy.internal.TypeStructureTray.TypeProvider) Collection(java.util.Collection) DefaultCustomPropertyFunctionEntity(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 7 with DefaultCustomPropertyFunction

use of eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction in project hale by halestudio.

the class PropertyFunctionScriptPage method apply.

@Override
public void apply() {
    DefaultCustomPropertyFunction cf = getWizard().getResultFunction();
    if (cf == null)
        return;
    cf.setFunctionType(CustomPropertyFunctionType.GROOVY);
    List<ParameterValue> script = getConfiguration().get(PARAMETER_SCRIPT);
    if (script != null && !script.isEmpty()) {
        cf.setFunctionDefinition(script.get(0));
    } else {
        cf.setFunctionDefinition(Value.NULL);
    }
}
Also used : DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue)

Example 8 with DefaultCustomPropertyFunction

use of eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction 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 9 with DefaultCustomPropertyFunction

use of eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction in project hale by halestudio.

the class DefaultCustomPropertyFunctionTypeTest method testWriteRead.

/**
 * Test if a simple lookup table containing only string values is the same
 * when converted to DOM and back again.
 */
@Test
public void testWriteRead() {
    DefaultCustomPropertyFunction f = new DefaultCustomPropertyFunction();
    f.setIdentifier("ident");
    f.setFunctionType("groovy");
    f.setName("My function");
    f.setFunctionDefinition(Value.of(new Text("a + b")));
    List<DefaultCustomPropertyFunctionEntity> sources = new ArrayList<>();
    sources.add(createEntity("a", 1, 1, false));
    sources.add(createEntity("b", 0, 1, false));
    f.setSources(sources);
    f.setTarget(createEntity(null, 1, 1, false));
    List<DefaultCustomPropertyFunctionParameter> parameters = new ArrayList<>();
    DefaultCustomPropertyFunctionParameter param1 = new DefaultCustomPropertyFunctionParameter();
    param1.setName("gender");
    Set<String> enumeration = new TreeSet<>();
    enumeration.add("male");
    enumeration.add("female");
    param1.setMinOccurrence(1);
    param1.setMaxOccurrence(1);
    param1.setEnumeration(enumeration);
    parameters.add(param1);
    DefaultCustomPropertyFunctionParameter param2 = new DefaultCustomPropertyFunctionParameter();
    param2.setName("name");
    param2.setBindingClass(String.class);
    param2.setMinOccurrence(0);
    param2.setMaxOccurrence(1);
    parameters.add(param2);
    DefaultCustomPropertyFunctionParameter param3 = new DefaultCustomPropertyFunctionParameter();
    param3.setName("flag");
    param3.setBindingClass(Boolean.class);
    param3.setMinOccurrence(1);
    param3.setMaxOccurrence(1);
    param3.setDefaultValue(Value.of(false));
    String p3display = "Awesome flag";
    param3.setDisplayName(p3display);
    String p3desc = "Awesome flag estimated in 2016.\nAll rights conserved.";
    param3.setDescription(p3desc);
    parameters.add(param3);
    f.setParameters(parameters);
    // explanation
    Map<Locale, Value> templates = new HashMap<>();
    templates.put(Locale.ROOT, Value.of(new Text("Hello")));
    templates.put(Locale.GERMAN, Value.of(new Text("Hallo")));
    templates.put(Locale.FRANCE, Value.of(new Text("Salut")));
    DefaultCustomFunctionExplanation explanation = new DefaultCustomFunctionExplanation(templates, null);
    f.setExplanation(explanation);
    // convert to DOM
    Element fragment = HaleIO.getComplexElement(f);
    // DEBUG
    System.out.println(XmlUtil.serialize(fragment));
    // convert back
    DefaultCustomPropertyFunction conv = HaleIO.getComplexValue(fragment, DefaultCustomPropertyFunction.class, null);
    // checks
    assertNotNull(conv);
    assertEquals(f.getIdentifier(), conv.getIdentifier());
    assertEquals(f.getName(), conv.getName());
    assertEquals(f.getFunctionType(), conv.getFunctionType());
    // function definition
    Text text = conv.getFunctionDefinition().as(Text.class);
    assertNotNull(text);
    assertEquals("a + b", text.getText());
    // sources
    assertEquals(2, conv.getSources().size());
    DefaultCustomPropertyFunctionEntity source1 = conv.getSources().get(0);
    assertEquals("a", source1.getName());
    assertEquals(1, source1.getMinOccurrence());
    assertEquals(1, source1.getMaxOccurrence());
    DefaultCustomPropertyFunctionEntity source2 = conv.getSources().get(1);
    assertEquals("b", source2.getName());
    assertEquals(0, source2.getMinOccurrence());
    // target
    assertNotNull(conv.getTarget());
    assertEquals(null, conv.getTarget().getName());
    // parameters
    assertEquals(3, conv.getParameters().size());
    DefaultCustomPropertyFunctionParameter cp1 = conv.getParameters().get(0);
    assertEquals("gender", cp1.getName());
    assertEquals(2, cp1.getEnumeration().size());
    DefaultCustomPropertyFunctionParameter cp2 = conv.getParameters().get(1);
    assertEquals("name", cp2.getName());
    assertEquals(0, cp2.getMinOccurrence());
    assertEquals(1, cp2.getMaxOccurrence());
    assertEquals(String.class, cp2.getBindingClass());
    DefaultCustomPropertyFunctionParameter cp3 = conv.getParameters().get(2);
    assertEquals("flag", cp3.getName());
    assertEquals(1, cp3.getMinOccurrence());
    assertEquals(1, cp3.getMaxOccurrence());
    assertEquals(Boolean.class, cp3.getBindingClass());
    assertEquals(false, cp3.getDefaultValue().as(Boolean.class));
    assertEquals(p3display, cp3.getDisplayName());
    assertEquals(p3desc, cp3.getDescription());
    // explanation
    assertNotNull(conv.getExplanation());
    Map<Locale, Value> tempConv = conv.getExplanation().getTemplates();
    assertEquals(3, tempConv.size());
    Value tempRoot = tempConv.get(Locale.ROOT);
    assertNotNull(tempRoot);
    assertEquals("Hello", tempRoot.as(Text.class).getText());
    Value tempGerman = tempConv.get(Locale.GERMAN);
    assertNotNull(tempGerman);
    assertEquals("Hallo", tempGerman.as(Text.class).getText());
    Value tempFrance = tempConv.get(Locale.FRANCE);
    assertNotNull(tempFrance);
    assertEquals("Salut", tempFrance.as(Text.class).getText());
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) DefaultCustomPropertyFunctionParameter(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionParameter) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Text(eu.esdihumboldt.hale.common.core.io.Text) DefaultCustomPropertyFunctionEntity(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) TreeSet(java.util.TreeSet) Value(eu.esdihumboldt.hale.common.core.io.Value) DefaultCustomFunctionExplanation(eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation) Test(org.junit.Test)

Example 10 with DefaultCustomPropertyFunction

use of eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction in project hale by halestudio.

the class CustomFunctionExplanationPage method createContent.

@Override
protected void createContent(Composite page) {
    DefaultCustomPropertyFunction cf = getWizard().getResultFunction();
    DefaultCustomFunctionExplanation expl = cf.getExplanation();
    Map<Locale, Value> initialContent = null;
    if (expl != null) {
        initialContent = expl.getTemplates();
    }
    GridLayoutFactory.fillDefaults().numColumns(1).applyTo(page);
    // top part - editor and locale controls
    createMainPart(page, initialContent);
// bottom part - explanation preview
// createPreviewPart(page);
}
Also used : Locale(java.util.Locale) DefaultCustomPropertyFunction(eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction) Value(eu.esdihumboldt.hale.common.core.io.Value) DefaultCustomFunctionExplanation(eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation)

Aggregations

DefaultCustomPropertyFunction (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunction)13 Value (eu.esdihumboldt.hale.common.core.io.Value)4 DefaultCustomFunctionExplanation (eu.esdihumboldt.hale.common.align.custom.DefaultCustomFunctionExplanation)3 CustomPropertyFunction (eu.esdihumboldt.hale.common.align.extension.function.custom.CustomPropertyFunction)3 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)3 Locale (java.util.Locale)3 DefaultCustomPropertyFunctionEntity (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionEntity)2 AlignmentService (eu.esdihumboldt.hale.ui.service.align.AlignmentService)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ActionContributionItem (org.eclipse.jface.action.ActionContributionItem)2 IContributionItem (org.eclipse.jface.action.IContributionItem)2 Text (org.eclipse.swt.widgets.Text)2 DefaultCustomPropertyFunctionParameter (eu.esdihumboldt.hale.common.align.custom.DefaultCustomPropertyFunctionParameter)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 Text (eu.esdihumboldt.hale.common.core.io.Text)1 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1