Search in sources :

Example 71 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class JoinMigrator method updateCell.

@Override
public MutableCell updateCell(Cell originalCell, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
    MutableCell result = super.updateCell(originalCell, migration, options, log);
    SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
    if (options.updateSource()) {
        ListMultimap<String, ParameterValue> modParams = ArrayListMultimap.create(result.getTransformationParameters());
        List<ParameterValue> joinParams = modParams.get(JoinFunction.PARAMETER_JOIN);
        if (!joinParams.isEmpty()) {
            JoinParameter joinParam = joinParams.get(0).as(JoinParameter.class);
            if (joinParam != null) {
                joinParams.clear();
                joinParams.add(new ParameterValue(Value.complex(convertJoinParameter(joinParam, migration, options, cellLog))));
            }
        }
        result.setTransformationParameters(modParams);
    }
    return result;
}
Also used : SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog)

Example 72 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class GroovyRetypeMergeMigrator method mergeSource.

@Override
protected void mergeSource(MutableCell cell, String sourceName, EntityDefinition source, Cell match, Cell originalCell, SimpleLog log, Void context, AlignmentMigration migration, MergeIndex mergeIndex) {
    // combination always possible
    // but Groovy script cannot be handled automatically
    String matchFunction = match.getTransformationIdentifier();
    // function that the migrated cell should have
    String translateTo = matchFunction;
    // if the script must be dropped
    boolean dropScript = true;
    // if there is an existing script in the match
    boolean addToScript = false;
    switch(matchFunction) {
        // cases where "normal" match is replaced by Groovy equivalent
        case JoinFunction.ID:
            translateTo = GroovyJoin.ID;
            dropScript = false;
            break;
        case RetypeFunction.ID:
            translateTo = GroovyRetype.ID;
            dropScript = false;
            break;
        case MergeFunction.ID:
            translateTo = GroovyMerge.ID;
            dropScript = false;
            break;
        case CreateFunction.ID:
            translateTo = GroovyCreate.ID;
            dropScript = false;
            break;
        // cases where a groovy match is annotated with the script
        case GroovyJoin.ID:
        case GroovyCreate.ID:
        case GroovyRetype.ID:
        case GroovyMerge.ID:
            dropScript = false;
            addToScript = true;
            break;
        default:
    }
    // cell function
    cell.setTransformationIdentifier(translateTo);
    // source from match
    cell.setSource(ArrayListMultimap.create(match.getSource()));
    if (source.getFilter() != null) {
        // note about dropped filter
        String msg = "The filter on the original source has been dropped because transfering it was not possible automatically.";
        String filterString = FilterDefinitionManager.getInstance().asString(source.getFilter());
        if (filterString != null) {
            msg = msg + " The original filter was: \"" + filterString + "\".";
        }
        log.warn(msg);
    }
    // parameters from match
    ListMultimap<String, ParameterValue> params = ArrayListMultimap.create(match.getTransformationParameters());
    String script = null;
    ParameterValue scriptValue = CellUtil.getFirstParameter(originalCell, GroovyConstants.PARAMETER_SCRIPT);
    if (scriptValue != null) {
        // try retrieving as text
        Text text = scriptValue.as(Text.class);
        if (text != null) {
            script = text.getText();
        } else {
            // fall back to string value
            script = scriptValue.as(String.class);
        }
    }
    if (script != null) {
        if (dropScript) {
            // script is dropped and not added to parameters
            String msg = "Script from Groovy Retype could not be transfered to migrated cell:\n\n" + script;
            log.warn(msg);
        } else {
            if (addToScript) {
                // add to existing script
                String matchScript = null;
                ParameterValue matchScriptValue = CellUtil.getFirstParameter(match, GroovyConstants.PARAMETER_SCRIPT);
                if (matchScriptValue != null) {
                    // try retrieving as text
                    Text text = matchScriptValue.as(Text.class);
                    if (text != null) {
                        matchScript = text.getText();
                    } else {
                        // fall back to string value
                        matchScript = matchScriptValue.as(String.class);
                    }
                }
                if (matchScript != null) {
                    script = matchScript + "\n\n\n// FIXME Script from merged Groovy Retype\n\n" + comment(script);
                }
                log.warn("A script was associated to the merged Groovy Retype this cell is derived from, it was added commented out to this cell's script. Please check how the functionality can be combined.");
            } else {
                // set as new script
                if (!RetypeFunction.ID.equals(match.getTransformationIdentifier())) {
                    script = "// FIXME Script taken from merged Groovy Retype:\n\n" + script;
                    log.warn("A script was associated to the merged Groovy Retype this cell is derived from, it was used as script for this cell's script and needs to be verified/adapted.");
                }
            }
            params.put(GroovyConstants.PARAMETER_SCRIPT, new ParameterValue(Value.of(new Text(script))));
        }
    }
    cell.setTransformationParameters(params);
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 73 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class JoinContext method apply.

/**
 * Apply information collected in the context to the cell.
 *
 * @param newCell the merged cell
 * @param log the cell log
 * @param migration the alignment migration
 */
public void apply(MutableCell newCell, AlignmentMigration migration, SimpleLog log) {
    ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
    /*
		 * Order: Keep original order but replace entities w/ all matches
		 */
    Set<TypeEntityDefinition> types = new LinkedHashSet<>();
    for (TypeEntityDefinition type : orgParameter.getTypes()) {
        List<TypeEntityDefinition> repl = replacements.get(type);
        if (repl.isEmpty()) {
            log.error("Could not find replacement for type {0} in join order", type);
            types.add(type);
        } else {
            types.addAll(repl);
        }
    }
    /*
		 * Conditions: (1) add conditions from matches and (2) add conditions
		 * from original cell translated to new schema (via property mapping),
		 * if they are not duplicates
		 */
    Set<Pair<PropertyEntityDefinition, PropertyEntityDefinition>> cons = new LinkedHashSet<>();
    // add conditions from matches
    for (Cell match : joinMatches) {
        JoinParameter matchParameter = CellUtil.getFirstParameter(match, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
        for (JoinCondition condition : matchParameter.getConditions()) {
            cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
        }
    }
    // migrate original conditions
    Set<JoinCondition> migrated = orgParameter.getConditions().stream().map(condition -> {
        PropertyEntityDefinition baseProperty = processOriginalConditionProperty(condition.baseProperty, migration, log);
        PropertyEntityDefinition joinProperty = processOriginalConditionProperty(condition.joinProperty, migration, log);
        JoinCondition result = new JoinCondition(baseProperty, joinProperty);
        return result;
    }).collect(Collectors.toSet());
    for (JoinCondition condition : migrated) {
        if (!condition.baseProperty.equals(condition.joinProperty)) {
            // migrated condition may contain "loop" condition
            cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
        }
    }
    // add messages on dropped filter/conditions
    for (EntityDefinition stripped : strippedSources) {
        if (!AlignmentUtil.isDefaultEntity(stripped)) {
            String msg = "Conditions/contexts for an original source could not be transfered and were dropped: " + MergeUtil.getContextInfoString(stripped);
            log.warn(msg);
        }
    }
    // all conditions
    Set<JoinCondition> conditions = new HashSet<>();
    for (Pair<PropertyEntityDefinition, PropertyEntityDefinition> condition : cons) {
        conditions.add(new JoinCondition(condition.getFirst(), condition.getSecond()));
    }
    JoinParameter newParam = new JoinParameter(new ArrayList<>(types), conditions);
    params.replaceValues(JoinFunction.PARAMETER_JOIN, Collections.singleton(new ParameterValue(Value.of(newParam))));
    // Use Groovy Join if original or match uses a script
    if (!scripts.isEmpty()) {
        boolean originalScript = scripts.size() == 1 && GroovyJoin.ID.equals(newCell.getTransformationIdentifier());
        Text script;
        if (originalScript) {
            // use original script
            script = new Text(scripts.get(0).getSecond());
            // create annotation
            log.warn("The Groovy script from the original cell was reused, logic and references to sources are very likely not valid anymore.");
        } else {
            // dummy script with all original scripts
            newCell.setTransformationIdentifier(GroovyJoin.ID);
            script = buildScript(scripts);
            // create annotation
            log.warn("At least one source mapping used a Groovy script, the script could not be combined automatically and was replaced with a dummy script (old scripts are commented out). Please check how you can migrate the old functionality.");
        }
        params.replaceValues(GroovyConstants.PARAMETER_SCRIPT, Collections.singleton(new ParameterValue(Value.of(script))));
    }
    newCell.setTransformationParameters(params);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Text(eu.esdihumboldt.hale.common.core.io.Text) ListMultimap(com.google.common.collect.ListMultimap) GroovyConstants(eu.esdihumboldt.cst.functions.groovy.GroovyConstants) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) AlignmentUtil(eu.esdihumboldt.hale.common.align.model.AlignmentUtil) MergeUtil(eu.esdihumboldt.hale.common.align.merge.MergeUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JoinFunction(eu.esdihumboldt.hale.common.align.model.functions.JoinFunction) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) LinkedHashSet(java.util.LinkedHashSet) Value(eu.esdihumboldt.hale.common.core.io.Value) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Set(java.util.Set) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Collectors(java.util.stream.Collectors) CellUtil(eu.esdihumboldt.hale.common.align.model.CellUtil) GroovyJoin(eu.esdihumboldt.cst.functions.groovy.GroovyJoin) List(java.util.List) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Collections(java.util.Collections) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) JoinCondition(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter.JoinCondition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Pair(eu.esdihumboldt.util.Pair) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 74 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class JoinMergeMigrator method addScript.

private void addScript(Cell match, JoinContext context) {
    ParameterValue scriptValue = CellUtil.getFirstParameter(match, GroovyConstants.PARAMETER_SCRIPT);
    if (scriptValue != null) {
        String script;
        // try retrieving as text
        Text text = scriptValue.as(Text.class);
        if (text != null) {
            script = text.getText();
        } else {
            // fall back to string value
            script = scriptValue.as(String.class);
        }
        context.addGroovyScript(match, script);
    }
}
Also used : ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) Text(eu.esdihumboldt.hale.common.core.io.Text)

Example 75 with ParameterValue

use of eu.esdihumboldt.hale.common.align.model.ParameterValue in project hale by halestudio.

the class DefaultAlignmentIOTest method testSaveLoad.

/**
 * Test saving and loading an example alignment
 *
 * @throws Exception if an error occurs
 */
@Test
public void testSaveLoad() throws Exception {
    // populate alignment
    MutableAlignment align = new DefaultAlignment();
    DefaultSchema source = new DefaultSchema("", null);
    DefaultSchema target = new DefaultSchema("", null);
    // cell 1
    MutableCell cell1 = new DefaultCell();
    String id1;
    cell1.setTransformationIdentifier(id1 = "trans1");
    ListMultimap<String, ParameterValue> parameters1 = LinkedListMultimap.create();
    parameters1.put("test", new ParameterValue("1"));
    parameters1.put("test", new ParameterValue("2"));
    parameters1.put("t", new ParameterValue("3"));
    cell1.setTransformationParameters(parameters1);
    if (supportsTransformationModes()) {
        cell1.setTransformationMode(TransformationMode.passive);
    }
    ListMultimap<String, Type> source1 = ArrayListMultimap.create();
    QName source1TypeName;
    String source1EntityName;
    TypeDefinition sourceType1 = new DefaultTypeDefinition(source1TypeName = new QName("source1Type"));
    String filterText = "someproperty > 12";
    Filter filter = new FilterGeoCqlImpl(filterText);
    source1.put(source1EntityName = null, new DefaultType(new TypeEntityDefinition(sourceType1, SchemaSpaceID.SOURCE, filter)));
    cell1.setSource(source1);
    source.addType(sourceType1);
    ListMultimap<String, Type> target1 = ArrayListMultimap.create();
    QName target1TypeName;
    String target1EntityName;
    TypeDefinition targetType1 = new DefaultTypeDefinition(target1TypeName = new QName("http://some.name.space/t1", "target1Type"));
    target1.put(target1EntityName = "Some name", new DefaultType(new TypeEntityDefinition(targetType1, SchemaSpaceID.TARGET, null)));
    cell1.setTarget(target1);
    target.addType(targetType1);
    align.addCell(cell1);
    // cell 2
    MutableCell cell2 = new DefaultCell();
    String id2;
    cell2.setTransformationIdentifier(id2 = "trans2");
    ListMultimap<String, ParameterValue> parameters2 = LinkedListMultimap.create();
    parameters2.put("test", new ParameterValue("4"));
    parameters2.put("tx", new ParameterValue("5"));
    parameters2.put("tx", new ParameterValue("6"));
    // complex parameter value
    if (supportsComplexParameters()) {
        TestAnnotation commentParam = new TestAnnotation();
        commentParam.setAuthor("Gerd");
        commentParam.setComment("Should a comment really be used as parameter?");
        parameters2.put("comment", new ParameterValue(Value.complex(commentParam)));
    }
    cell2.setTransformationParameters(parameters2);
    ListMultimap<String, Type> target2 = ArrayListMultimap.create();
    TypeDefinition targetType2 = new DefaultTypeDefinition(new QName("target2Type"));
    target2.put("Some other name", new DefaultType(new TypeEntityDefinition(targetType2, SchemaSpaceID.TARGET, null)));
    cell2.setTarget(target2);
    target.addType(targetType2);
    align.addCell(cell2);
    TestAnnotation ann1 = null;
    TestAnnotation ann2 = null;
    if (supportsAnnotations()) {
        // add some annotations
        ann1 = (TestAnnotation) cell2.addAnnotation("test");
        ann1.setAuthor("Simon");
        ann1.setComment("I have really no idea what I did here");
        ann2 = (TestAnnotation) cell2.addAnnotation("test");
        ann2.setAuthor("Hans");
        ann2.setComment("Me neither");
    }
    String doc1 = "This cell was created in memory of...\nSorry, forgotten.";
    String tag1 = "This is a tag";
    String tag2 = "awesome";
    if (supportsDocumentation()) {
        cell1.getDocumentation().put(null, doc1);
        cell1.getDocumentation().put("tag", tag1);
        cell1.getDocumentation().put("tag", tag2);
    }
    // write alignment
    File alignmentFile = tmp.newFile("alignment.xml");
    System.out.println(alignmentFile.getAbsolutePath());
    saveAlignment(align, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
    // load alignment
    // TODO use and check reporter?
    MutableAlignment align2 = loadAlignment(new FileInputStream(alignmentFile), source, target);
    // compare loaded alignment
    Collection<? extends Cell> cells = align2.getCells();
    assertFalse(cells.isEmpty());
    Iterator<? extends Cell> it = cells.iterator();
    // cell 1
    Cell ncell1 = it.next();
    assertNotNull(ncell1);
    assertEquals(id1, ncell1.getTransformationIdentifier());
    // documentation
    if (supportsDocumentation()) {
        assertEquals(3, ncell1.getDocumentation().size());
        assertEquals(doc1, ncell1.getDocumentation().get(null).get(0));
        assertEquals(tag1, ncell1.getDocumentation().get("tag").get(0));
        assertEquals(tag2, ncell1.getDocumentation().get("tag").get(1));
    }
    if (supportsTransformationModes()) {
        assertEquals(TransformationMode.passive, ncell1.getTransformationMode());
    }
    // source 1
    ListMultimap<String, ? extends Entity> source1Entities = ncell1.getSource();
    assertEquals(1, source1Entities.size());
    List<? extends Entity> s1list = source1Entities.get(source1EntityName);
    assertFalse(s1list.isEmpty());
    assertEquals(source1TypeName, s1list.get(0).getDefinition().getDefinition().getName());
    // filter
    assertEquals(filter, s1list.get(0).getDefinition().getFilter());
    // target 1
    ListMultimap<String, ? extends Entity> target1Entities = ncell1.getTarget();
    assertEquals(1, target1Entities.size());
    List<? extends Entity> t1list = target1Entities.get(target1EntityName);
    assertFalse(t1list.isEmpty());
    assertEquals(target1TypeName, t1list.get(0).getDefinition().getDefinition().getName());
    // cell 2
    Cell ncell2 = it.next();
    assertNotNull(ncell2);
    assertEquals(id2, ncell2.getTransformationIdentifier());
    // parameters
    ListMultimap<String, ParameterValue> param2 = ncell2.getTransformationParameters();
    if (!supportsComplexParameters()) {
        assertEquals(2, param2.keySet().size());
        assertEquals(3, param2.values().size());
    } else {
        assertEquals(3, param2.keySet().size());
        assertEquals(4, param2.values().size());
        ParameterValue complexParam = param2.get("comment").get(0);
        assertTrue(complexParam.getValue() instanceof TestAnnotation);
    }
    // annotations
    if (supportsAnnotations()) {
        List<?> annotations = ncell2.getAnnotations("test");
        assertEquals(2, annotations.size());
        TestAnnotation nann1 = (TestAnnotation) annotations.get(0);
        assertEquals(ann1, nann1);
        TestAnnotation nann2 = (TestAnnotation) annotations.get(1);
        assertEquals(ann2, nann2);
    }
}
Also used : MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) FilterGeoCqlImpl(eu.esdihumboldt.hale.common.filter.FilterGeoCqlImpl) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) DefaultSchema(eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchema) DefaultAlignment(eu.esdihumboldt.hale.common.align.model.impl.DefaultAlignment) BufferedOutputStream(java.io.BufferedOutputStream) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) QName(javax.xml.namespace.QName) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) FileInputStream(java.io.FileInputStream) Type(eu.esdihumboldt.hale.common.align.model.Type) DefaultType(eu.esdihumboldt.hale.common.align.model.impl.DefaultType) DefaultCell(eu.esdihumboldt.hale.common.align.model.impl.DefaultCell) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Test(org.junit.Test)

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