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