Search in sources :

Example 6 with CellNode

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

the class TargetContext method configureSourceDuplicate.

/**
 * Configure a duplicated source node.
 *
 * @param originalSource the original source node
 * @param duplicate the duplicated source node
 * @param parent the parent for the duplicated source node
 * @param duplicationContext the duplication context
 * @param addToParent if the duplicated source node should be added as child
 *            to its parent
 * @return the duplicated source node or <code>null</code> if it has no
 *         further connections
 */
private SourceNode configureSourceDuplicate(SourceNode originalSource, SourceNode duplicate, SourceNode parent, DuplicationContext duplicationContext, boolean addToParent) {
    // duplicate relations
    List<CellNode> relations = new ArrayList<CellNode>(originalSource.getRelations(false).size());
    // though each cell node is only duplicated once per duplication context
    for (CellNode relation : originalSource.getRelations(false)) {
        // XXX
        // should
        // the
        // annotated
        // relations
        // be
        // included?
        CellNode duplicatedRelation = duplicateCell(relation, duplicate, duplicationContext);
        if (duplicatedRelation != null) {
            relations.add(duplicatedRelation);
        }
    }
    // duplicate children
    List<SourceNode> children = new ArrayList<SourceNode>(originalSource.getChildren(false).size());
    for (SourceNode child : originalSource.getChildren(false)) {
        // XXX
        // should
        // the
        // annotated
        // children
        // be
        // included?
        SourceNode duplicatedChild = duplicateSource(child, duplicate, true, duplicationContext);
        if (duplicatedChild != null) {
            children.add(duplicatedChild);
        }
    }
    if (children.isEmpty() && relations.isEmpty()) {
        // abort
        return null;
    }
    // add duplicated relations
    for (CellNode relation : relations) {
        duplicate.addRelation(relation);
    }
    // add duplicated children
    for (SourceNode child : children) {
        duplicate.addChild(child);
    }
    if (addToParent) {
        parent.addChild(duplicate);
    }
    return duplicate;
}
Also used : CellNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) ArrayList(java.util.ArrayList)

Example 7 with CellNode

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

CellNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)7 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)6 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)4 TransformationTree (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree)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 IdentityWrapper (eu.esdihumboldt.util.IdentityWrapper)2 ArrayList (java.util.ArrayList)2 Joiner (com.google.common.base.Joiner)1 Edge (com.tinkerpop.blueprints.Edge)1 Graph (com.tinkerpop.blueprints.Graph)1 Vertex (com.tinkerpop.blueprints.Vertex)1 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 GroupNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode)1 TransformationNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationNode)1 SourceNodeImpl (eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.SourceNodeImpl)1 TargetNodeImpl (eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)1 AbstractTargetToSourceVisitor (eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.AbstractTargetToSourceVisitor)1