Search in sources :

Example 1 with GroupNode

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

the class TargetContext method duplicateTree.

/**
 * Duplicates the transformation tree for the given target node to the given
 * duplicate target node.
 *
 * @param target the original target node
 * @param info the duplication info object
 * @param log the transformation log
 * @return a collection of newly created target nodes
 */
private static TargetNodeImpl duplicateTree(TargetNode target, DuplicationInformation info, TransformationLog log) {
    GroupNode parent = null;
    // Check if the parent node exists in the given context already.
    if (target.getParent() instanceof TargetNode) {
        Iterator<TargetNode> possibleParents = info.getAllTargetNodes(((TargetNode) target.getParent()).getEntityDefinition()).iterator();
        while (possibleParents.hasNext()) {
            TargetNode possibleParent = possibleParents.next();
            if (info.getContextTargets().contains(target) || !possibleParent.getChildren(false).contains(target)) {
                parent = possibleParent;
                break;
            }
        }
    } else if (target.getParent() instanceof TransformationTree && info.getContextTargets().contains(target)) {
        // Reached root, but this is a possible target.
        parent = target.getParent();
    } else {
        // Reached root, but this is no possible target or the parent is
        // null!
        // XXX instead log and return null or not connected TargetNode? See
        // T O D O below
        log.warn(log.createMessage("DuplicationContext present, but no matching target found.", null));
        return null;
    // throw new IllegalStateException(
    // "DuplicationContext present, but no matching target found.");
    }
    if (parent == null) {
        // Does not exist: recursion.
        TargetNodeImpl duplicatedTarget = duplicateTree((TargetNode) target.getParent(), info, log);
        if (duplicatedTarget == null) {
            return null;
        }
        TargetNodeImpl newTarget = new TargetNodeImpl(target.getEntityDefinition(), duplicatedTarget);
        info.addNewTargetNode(newTarget.getEntityDefinition(), newTarget);
        duplicatedTarget.addChild(newTarget);
        return newTarget;
    } else {
        // Exists: add as child.
        TargetNodeImpl newTarget = new TargetNodeImpl(target.getEntityDefinition(), parent);
        info.addNewTargetNode(newTarget.getEntityDefinition(), newTarget);
        // as annotated child.
        if (parent instanceof TargetNodeImpl && !parent.getChildren(false).contains(newTarget))
            ((TargetNodeImpl) parent).addChild(newTarget);
        else
            parent.addAnnotatedChild(newTarget);
        return newTarget;
    }
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) GroupNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode) TargetNodeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)

Example 2 with GroupNode

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

the class TargetContext method duplicateTarget.

/**
 * Duplicate a target node.
 *
 * @param target the original target node
 * @param relation the relation to associated to the target node
 * @param duplicationContext the duplication context
 * @return the duplicated target node or <code>null</code> if duplication
 *         was prohibited
 */
private TargetNode duplicateTarget(TargetNode target, CellNode relation, DuplicationContext duplicationContext) {
    TargetNodeImpl duplicatedTarget = duplicationContext.getNode(target.getEntityDefinition());
    if (duplicatedTarget == null) {
        // target node not created yet
        boolean duplicateParent = true;
        if (contextTargets.contains(target)) {
            // this is an endpoint, as such this is the last node to be
            // duplicated
            duplicateParent = false;
        }
        GroupNode duplicatedParent;
        if (duplicateParent) {
            GroupNode parent = target.getParent();
            if (parent instanceof TargetNode) {
                // create a duplicated parent
                duplicatedParent = duplicateTarget((TargetNode) parent, null, duplicationContext);
                if (duplicatedParent == null) {
                    return null;
                }
            } else {
                // thus this is not a valid path for duplication
                return null;
            }
        } else {
            duplicatedParent = target.getParent();
        }
        // create duplicate
        duplicatedTarget = new TargetNodeImpl(target.getEntityDefinition(), duplicatedParent);
        // add as annotated child to parent
        duplicatedParent.addAnnotatedChild(duplicatedTarget);
        // add to duplication context
        duplicationContext.addNode(duplicatedTarget, target);
    }
    if (relation != null) {
        // assign relation
        duplicatedTarget.addAssignment(target.getAssignmentNames(relation), relation);
    }
    return duplicatedTarget;
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) GroupNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode) TargetNodeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)

Example 3 with GroupNode

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

the class TGraphFactory method create.

/**
 * Create a transformation graph from a transformation tree.
 *
 * @param ttree the transformation tree
 * @param functionService the function service
 * @return an in-memory graph created from the transformation tree
 */
public static Graph create(TransformationTree ttree, FunctionService functionService) {
    TreeToGraphVisitor graphVisitor = new TreeToGraphVisitor(functionService);
    ttree.accept(graphVisitor);
    SetMultimap<String, String> connections = graphVisitor.getAllConnections();
    Set<String> ids = graphVisitor.getAllIds();
    Graph graph = new TinkerGraph();
    // add nodes to the graph
    for (String key : ids) {
        // create a vertex for each transformation node
        TransformationNode node = graphVisitor.getNode(key);
        Vertex vertex = graph.addVertex(key);
        setVertexProperties(vertex, node);
    }
    for (String key : connections.keySet()) {
        for (String value : connections.get(key)) {
            Vertex targetSide = graph.getVertex(key);
            Vertex sourceSide = graph.getVertex(value);
            TransformationNode targetSideNode = graphVisitor.getNode(key);
            TransformationNode sourceSideNode = graphVisitor.getNode(value);
            String edgeLabel;
            if (sourceSideNode instanceof SourceNode && targetSideNode instanceof SourceNode) {
                edgeLabel = EDGE_CHILD;
            } else if (sourceSideNode instanceof SourceNode && targetSideNode instanceof CellNode) {
                edgeLabel = EDGE_VARIABLE;
            } else if (sourceSideNode instanceof CellNode && targetSideNode instanceof GroupNode) {
                edgeLabel = EDGE_RESULT;
            } else if (sourceSideNode instanceof GroupNode && targetSideNode instanceof GroupNode) {
                edgeLabel = EDGE_PARENT;
            } else {
                throw new IllegalStateException("Invalid relation in transformation tree");
            }
            Edge edge = graph.addEdge(null, sourceSide, targetSide, edgeLabel);
            setEdgeProperties(edge, sourceSideNode, targetSideNode);
        }
    }
    return graph;
}
Also used : TransformationNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationNode) Vertex(com.tinkerpop.blueprints.Vertex) CellNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) TreeToGraphVisitor(eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.TreeToGraphVisitor) GroupNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode) SourceNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) Edge(com.tinkerpop.blueprints.Edge)

Aggregations

GroupNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.GroupNode)3 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)2 TargetNodeImpl (eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TargetNodeImpl)2 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 CellNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)1 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)1 TransformationNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationNode)1 TransformationTree (eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree)1 TreeToGraphVisitor (eu.esdihumboldt.hale.common.align.model.transformation.tree.visitor.TreeToGraphVisitor)1