Search in sources :

Example 11 with TargetNode

use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode in project hale by halestudio.

the class TargetContext method duplicateTree.

/**
 * Duplicates the transformation tree for the given cell node to the given
 * duplicate cell node.
 *
 * @param cell the original cell node
 * @param duplicateCell the duplication target
 * @param info the duplication info object
 * @param log the transformation log
 */
private static void duplicateTree(CellNode cell, CellNode duplicateCell, DuplicationInformation info, TransformationLog log) {
    // Duplicate targets.
    for (TargetNode target : cell.getTargets()) {
        TargetNodeImpl duplicatedTarget = duplicateTree(target, info, log);
        if (duplicatedTarget != null) {
            duplicateCell.addTarget(duplicatedTarget);
            duplicatedTarget.addAssignment(target.getAssignmentNames(cell), duplicateCell);
        }
    }
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) TargetNodeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)

Example 12 with TargetNode

use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode 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)

Example 13 with TargetNode

use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode in project hale by halestudio.

the class CellNodeValidator method visit.

/**
 * @see AbstractTargetToSourceVisitor#visit(CellNode)
 */
@Override
public boolean visit(CellNode node) {
    // evaluate if for the cell all needed inputs are set
    Cell cell = node.getCell();
    // collect source and target nodes per entity name
    ListMultimap<String, Pair<SourceNode, Entity>> sources = ArrayListMultimap.create();
    if (cell.getSource() != null) {
        for (Entry<String, ? extends Entity> sourceEntry : cell.getSource().entries()) {
            String name = sourceEntry.getKey();
            Entity entity = sourceEntry.getValue();
            SourceNode sourceNode = findSourceNode(node, entity);
            if (sourceNode != null) {
                if (sourceNode.isDefined()) {
                    sources.put(name, new Pair<SourceNode, Entity>(sourceNode, entity));
                }
            } else {
                log.error("Source node for entity not found.");
            }
        }
    }
    ListMultimap<String, Pair<TargetNode, Entity>> targets = ArrayListMultimap.create();
    for (Entry<String, ? extends Entity> targetEntry : cell.getTarget().entries()) {
        String name = targetEntry.getKey();
        Entity entity = targetEntry.getValue();
        TargetNode targetNode = findTargetNode(node, entity);
        if (targetNode != null) {
            targets.put(name, new Pair<TargetNode, Entity>(targetNode, entity));
        } else {
            log.error("Target node for entity not found.");
        }
    }
    boolean valid = validate(node, sources, targets);
    if (valid) {
        processValid(cell, sources, targets);
    }
    node.setValid(valid);
    // don't visit source nodes
    return false;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Pair(eu.esdihumboldt.util.Pair)

Aggregations

TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)13 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)6 CellNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)4 TransformationTree (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree)4 TargetNodeImpl (eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)4 IdentityWrapper (eu.esdihumboldt.util.IdentityWrapper)3 MultiValue (eu.esdihumboldt.cst.MultiValue)2 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)2 GroupNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode)2 CellNodeImpl (eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.CellNodeImpl)2 Group (eu.esdihumboldt.hale.common.instance.model.Group)2 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)2 Pair (eu.esdihumboldt.util.Pair)2 ArrayList (java.util.ArrayList)2 Joiner (com.google.common.base.Joiner)1 PropertyTransformationFactory (eu.esdihumboldt.hale.common.align.extension.transformation.PropertyTransformationFactory)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 Entity (eu.esdihumboldt.hale.common.align.model.Entity)1 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)1 AbstractTargetToSourceVisitor (eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTargetToSourceVisitor)1