Search in sources :

Example 76 with EntityDefinition

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

the class FormattedStringMigrator method convertPattern.

private String convertPattern(String pattern, ListMultimap<String, ? extends Entity> oldSource, AlignmentMigration migration, SimpleLog log) {
    List<PropertyEntityDefinition> oldVars = new ArrayList<>();
    if (oldSource != null && oldSource.get(FormattedStringFunction.ENTITY_VARIABLE) != null) {
        oldVars = oldSource.get(FormattedStringFunction.ENTITY_VARIABLE).stream().filter(e -> e.getDefinition() instanceof PropertyEntityDefinition).map(e -> (PropertyEntityDefinition) e.getDefinition()).collect(Collectors.toList());
    }
    Map<String, Object> replacements = new HashMap<>();
    for (PropertyEntityDefinition var : oldVars) {
        Optional<EntityDefinition> replacement = migration.entityReplacement(var, log);
        replacement.ifPresent(repl -> {
            String newName = repl.getDefinition().getName().getLocalPart();
            // XXX there might be name conflicts - check for those or use
            // long names?
            FormattedStringFunction.addValue(replacements, newName, var);
        });
    }
    for (Entry<String, Object> replacement : replacements.entrySet()) {
        // replace variables
        pattern = pattern.replaceAll(Pattern.quote("{" + replacement.getKey() + "}"), "{" + replacement.getValue() + "}");
    }
    return pattern;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) SimpleLog(eu.esdihumboldt.hale.common.core.report.SimpleLog) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) ListMultimap(com.google.common.collect.ListMultimap) HashMap(java.util.HashMap) Entity(eu.esdihumboldt.hale.common.align.model.Entity) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) AlignmentMigration(eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration) Collectors(java.util.stream.Collectors) DefaultCellMigrator(eu.esdihumboldt.hale.common.align.migrate.impl.DefaultCellMigrator) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) Entry(java.util.Map.Entry) Optional(java.util.Optional) MigrationOptions(eu.esdihumboldt.hale.common.align.migrate.MigrationOptions) Pattern(java.util.regex.Pattern) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) CellLog(eu.esdihumboldt.hale.common.align.model.annotations.messages.CellLog) Value(eu.esdihumboldt.hale.common.core.io.Value) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) PropertyEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 77 with EntityDefinition

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

the class AbstractCellExplanation method getEntityNameWithoutCondition.

/**
 * Returns an entity name without condition strings (e.g. "part1.part2").
 *
 * @param entity the entity
 * @return the entity name
 */
protected String getEntityNameWithoutCondition(Entity entity) {
    EntityDefinition entityDef = entity.getDefinition();
    if (entityDef.getPropertyPath() != null && !entityDef.getPropertyPath().isEmpty()) {
        List<String> names = new ArrayList<String>();
        for (ChildContext context : entityDef.getPropertyPath()) {
            names.add(context.getChild().getName().getLocalPart());
        }
        String longName = Joiner.on('.').join(names);
        return longName;
    } else
        return entityDef.getDefinition().getDisplayName();
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) ArrayList(java.util.ArrayList) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 78 with EntityDefinition

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

the class DefaultAlignment method getCells.

/**
 * @see Alignment#getCells(EntityDefinition, boolean)
 */
@Override
public Collection<? extends Cell> getCells(EntityDefinition entityDefinition, boolean includeInherited) {
    if (!includeInherited)
        return Collections.unmodifiableCollection(cellsPerEntity.get(entityDefinition));
    else {
        // Set for safety to return each cell only once.
        // Duplicates shouldn't happen in usual cases, though.
        Collection<Cell> cells = new HashSet<Cell>();
        EntityDefinition e = entityDefinition;
        do {
            cells.addAll(cellsPerEntity.get(e));
            if (e.getFilter() != null) {
                cells.addAll(cellsPerEntity.get(AlignmentUtil.createEntity(e.getType(), e.getPropertyPath(), e.getSchemaSpace(), null)));
            }
            TypeDefinition superType = e.getType().getSuperType();
            e = superType == null ? null : AlignmentUtil.createEntity(superType, e.getPropertyPath(), e.getSchemaSpace(), e.getFilter());
        } while (e != null);
        return cells;
    }
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) Cell(eu.esdihumboldt.hale.common.align.model.Cell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) HashSet(java.util.HashSet) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 79 with EntityDefinition

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

the class TargetContext method duplicateContext.

// /**
// * @see TransformationContext#duplicateContext(SourceNode, Object)
// */
// @Override
// public void duplicateContext(SourceNode contextSource, Object value) {
// // create a new duplication context
// DuplicationContext duplicationContext = new DuplicationContext();
// 
// // duplicate context source
// SourceNode source = duplicateSource(contextSource,
// contextSource.getParent(), false, duplicationContext);
// 
// if (source != null) {
// // add duplicated source as annotation to context source parent
// contextSource.getParent().addAnnotatedChild(source);
// 
// // apply value to source
// if (value instanceof Group) {
// // value is a group, duplication may again be necessary for its properties
// InstanceVisitor visitor = new InstanceVisitor((Group) value);
// source.accept(visitor);
// }
// else {
// // value is a simple value
// source.setValue(value);
// }
// }
// }
/**
 * @see TransformationContext#duplicateContext(SourceNode, SourceNode, Set,
 *      TransformationLog)
 */
@Override
public void duplicateContext(SourceNode originalSource, final SourceNode duplicate, Set<Cell> ignoreCells, TransformationLog log) {
    DuplicationInformation info = new DuplicationInformation(duplicate, ignoreCells, contextTargets);
    SourceNode parent = duplicate.getParent();
    if (parent == null)
        parent = duplicate.getAnnotatedParent();
    if (parent != null) {
        // Find existing cell/target nodes over children of the parent node.
        Map<EntityDefinition, SourceNode> contextPath = new HashMap<EntityDefinition, SourceNode>();
        SourceNode pathNode = duplicate;
        SourceNode root;
        do {
            contextPath.put(pathNode.getEntityDefinition(), pathNode);
            root = pathNode;
            if (pathNode.getParent() != null)
                pathNode = pathNode.getParent();
            else
                pathNode = pathNode.getAnnotatedParent();
        } while (pathNode != null);
        collectExistingNodes(root, contextPath, info, false);
        // add target nodes reachable through original source node
        // XXX not all target nodes are needed, collection of some of them
        // could even lead to wrong results
        collectExistingNodes(originalSource, Collections.<EntityDefinition, SourceNode>emptyMap(), info, true);
        duplicateTree(originalSource, duplicate, info, log, serviceProvider);
    } else
        throw new IllegalStateException("Duplicate node neither got a parent, nor an annotated parent.");
// Code matching the old stuff at bottom!
// // create a new duplication context
// DuplicationContext duplicationContext = new DuplicationContext(ignoreCells);
// 
// // configure duplicate, but don't add to parent (as it is already added as annotated child)
// // at this point duplicate may have a parent, even if the original source hasn't
// configureSourceDuplicate(originalSource, duplicate,
// duplicate.getParent(), duplicationContext, false);
// 
// // track back to sources from cells where sources are missing
// for (Pair<CellNodeImpl, CellNode> cellPair : duplicationContext.getIncompleteCellNodes()) {
// CellNodeImpl cellNode = cellPair.getFirst();
// CellNode originalCell = cellPair.getSecond();
// 
// cellTrackback(cellNode, originalCell);
// }
// 
// // track back from targets where augmentations are missing
// for (Pair<TargetNodeImpl, TargetNode> targetPair : duplicationContext.getIncompleteTargetNodes()) {
// //			TargetNodeImpl targetNode = targetPair.getFirst();
// TargetNode originalTarget = targetPair.getSecond();
// 
// augmentationTrackback(originalTarget, duplicationContext);
// }
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) HashMap(java.util.HashMap)

Example 80 with EntityDefinition

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

the class TargetContext method augmentationTrackback.

/**
 * Track back target nodes and duplicate any augmentation cells.
 *
 * @param tree the tree to work on
 */
public static void augmentationTrackback(TransformationTree tree) {
    final Map<EntityDefinition, TargetNode> targetNodesWithAugmentations = new HashMap<EntityDefinition, TargetNode>();
    // Search for original target nodes
    tree.accept(new AbstractTargetToSourceVisitor() {

        Deque<Boolean> hasAugmentation = new ArrayDeque<Boolean>();

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTransformationNodeVisitor#visit(eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)
         */
        @Override
        public boolean visit(CellNode cell) {
            // hasAugmentation to true.
            if (cell.getSources().isEmpty()) {
                if (!hasAugmentation.getLast()) {
                    hasAugmentation.pop();
                    hasAugmentation.push(true);
                }
            }
            return false;
        }

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTransformationNodeVisitor#visit(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)
         */
        @Override
        public boolean visit(TargetNode target) {
            // Simply add a new level to hasAugmentation starting with
            // false.
            hasAugmentation.push(false);
            return true;
        }

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTransformationNodeVisitor#leave(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)
         */
        @Override
        public void leave(TargetNode target) {
            // If this nodes level in hasAugmentation is true...
            if (hasAugmentation.pop()) {
                // ... add it to targetNodesWithAugmentations and set
                // parents level to true, too.
                targetNodesWithAugmentations.put(target.getEntityDefinition(), target);
                if (!hasAugmentation.isEmpty() && !hasAugmentation.getLast()) {
                    hasAugmentation.pop();
                    hasAugmentation.push(true);
                }
            }
        }

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationNodeVisitor#includeAnnotatedNodes()
         */
        @Override
        public boolean includeAnnotatedNodes() {
            // Only look for original target nodes.
            return false;
        }
    });
    // Add augmentations to all target nodes (no copied target node got them
    // yet)
    tree.accept(new AbstractTargetToSourceVisitor() {

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTransformationNodeVisitor#visit(eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)
         */
        @Override
        public boolean visit(CellNode cell) {
            return false;
        }

        /**
         * @see eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTransformationNodeVisitor#visit(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)
         */
        @Override
        public boolean visit(TargetNode target) {
            // TODO more intelligent behavior of when NOT to create the
            // augmentation.
            // For example if the augmentation belongs to a complex
            // structure that can
            // occur 0..n times, it should not be created for the first time
            // due to an
            // augmentation.
            TargetNode originalTarget = targetNodesWithAugmentations.get(target.getEntityDefinition());
            // Only have to do something if the node is present in the map.
            if (originalTarget != null && originalTarget != target) {
                // sources are missing).
                for (CellNode originalAssignment : originalTarget.getAssignments()) {
                    if (originalAssignment.getSources().isEmpty()) {
                        CellNodeImpl duplicatedAssignment = new CellNodeImpl(originalAssignment.getCell());
                        duplicatedAssignment.addTarget(target);
                        ((TargetNodeImpl) target).addAssignment(originalTarget.getAssignmentNames(originalAssignment), duplicatedAssignment);
                    }
                }
                // Check for missing children.
                for (TargetNode child : originalTarget.getChildren(false)) {
                    // Only add missing children that need an augmentation.
                    if (targetNodesWithAugmentations.containsKey(child.getEntityDefinition()) && !target.getChildren(false).contains(child)) {
                        TargetNodeImpl duplicatedChild = new TargetNodeImpl(child.getEntityDefinition(), target);
                        ((TargetNodeImpl) target).addChild(duplicatedChild);
                    // The child will be handled by this visior later.
                    }
                }
                return true;
            } else
                return false;
        }

        @Override
        public boolean includeAnnotatedNodes() {
            return true;
        }
    });
}
Also used : EntityDefinition(eu.esdihumboldt.hale.common.align.model.EntityDefinition) CellNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode) TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) AbstractTargetToSourceVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTargetToSourceVisitor) HashMap(java.util.HashMap) CellNodeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.CellNodeImpl) ArrayDeque(java.util.ArrayDeque) TargetNodeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)

Aggregations

EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)99 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)39 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)37 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)23 ArrayList (java.util.ArrayList)22 Entity (eu.esdihumboldt.hale.common.align.model.Entity)21 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)20 Cell (eu.esdihumboldt.hale.common.align.model.Cell)18 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)13 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)11 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)10 HashSet (java.util.HashSet)10 QName (javax.xml.namespace.QName)10 HashMap (java.util.HashMap)9 Condition (eu.esdihumboldt.hale.common.align.model.Condition)8 SimpleLog (eu.esdihumboldt.hale.common.core.report.SimpleLog)7 ISelection (org.eclipse.jface.viewers.ISelection)7 List (java.util.List)6 TreePath (org.eclipse.jface.viewers.TreePath)6 AlignmentMigration (eu.esdihumboldt.hale.common.align.migrate.AlignmentMigration)5