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;
}
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;
}
});
}
Aggregations