use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree in project hale by halestudio.
the class TransformationTreeContentProvider method getElements.
/**
* @see ArrayContentProvider#getElements(Object)
*/
@SuppressWarnings("unchecked")
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof TransformationTree)
return collectNodes((TransformationTree) inputElement).toArray();
Collection<Instance> instances = null;
if (inputElement instanceof Pair<?, ?>) {
Pair<?, ?> pair = (Pair<?, ?>) inputElement;
inputElement = pair.getFirst();
if (pair.getSecond() instanceof Collection<?>) {
instances = (Collection<Instance>) pair.getSecond();
}
}
if (inputElement instanceof Alignment) {
Alignment alignment = (Alignment) inputElement;
// input contained specific instances
if (instances != null && !instances.isEmpty()) {
Collection<Object> result = new ArrayList<Object>();
// create transformation trees for each instance
for (Instance instance : instances) {
// find type cells matching the instance
Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
Collection<Cell> associatedTypeCells = new LinkedList<Cell>();
for (Cell typeCell : typeCells) for (Entity entity : typeCell.getSource().values()) {
TypeEntityDefinition type = (TypeEntityDefinition) entity.getDefinition();
if (type.getDefinition().equals(instance.getDefinition()) && (type.getFilter() == null || type.getFilter().match(instance))) {
associatedTypeCells.add(typeCell);
break;
}
}
// for each type cell one tree
for (Cell cell : associatedTypeCells) {
TransformationTree tree = createInstanceTree(instance, cell, alignment);
if (tree != null)
result.addAll(collectNodes(tree));
}
}
return result.toArray();
}
// input was alignment only, show trees for all type cells
Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
Collection<Object> result = new ArrayList<Object>(typeCells.size());
for (Cell typeCell : typeCells) {
// create tree and add nodes for each cell
result.addAll(collectNodes(new TransformationTreeImpl(alignment, typeCell)));
}
return result.toArray();
}
return super.getElements(inputElement);
}
use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree in project hale by halestudio.
the class TransformationTreeContentProvider method getChilddren.
/**
* Get the children of a node
*
* @param node the node
* @return the node's children
*/
private Collection<? extends Object> getChilddren(Object node) {
if (node instanceof IdentityWrapper<?>) {
node = ((IdentityWrapper<?>) node).getValue();
}
if (node instanceof TransformationTree) {
return wrapNodes(((TransformationTree) node).getChildren(true));
}
if (node instanceof TargetNode) {
List<Object> children = new ArrayList<Object>();
children.addAll(((TargetNode) node).getChildren(true));
children.addAll(((TargetNode) node).getAssignments());
return wrapNodes(children);
}
if (node instanceof CellNode) {
return wrapNodes(((CellNode) node).getSources());
}
if (node instanceof SourceNode) {
SourceNode parent = ((SourceNode) node).getParent();
if (parent != null) {
return Collections.singleton(new IdentityWrapper<Object>(parent));
}
}
return Collections.emptyList();
}
use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree in project hale by halestudio.
the class TransformationTreeLabelProvider method getFigure.
/**
* @see GraphLabelProvider#getFigure(Object)
*/
@Override
public IFigure getFigure(Object element) {
if (element instanceof IdentityWrapper<?>) {
element = ((IdentityWrapper<?>) element).getValue();
}
ShapePainter shape = null;
String contextText = null;
String cardText = null;
if (element instanceof TransformationTree) {
shape = new TransformationNodeShape(10, SWT.NONE);
} else if (element instanceof TargetNode) {
TargetNode node = (TargetNode) element;
contextText = AlignmentUtil.getContextText(node.getEntityDefinition());
if (!hasTransformationAnnotations(element)) {
cardText = getCardinalityText(node.getEntityDefinition().getDefinition());
}
if (node.getAssignments().isEmpty()) {
shape = new TransformationNodeShape(10, SWT.NONE);
} else {
// shape = new TransformationNodeShape(10, SWT.LEFT);
shape = new FingerPost(10, SWT.LEFT);
}
} else if (element instanceof SourceNode) {
SourceNode node = (SourceNode) element;
contextText = AlignmentUtil.getContextText(node.getEntityDefinition());
if (!hasTransformationAnnotations(element)) {
cardText = getCardinalityText(node.getEntityDefinition().getDefinition());
}
if (node.getParent() == null) {
shape = new TransformationNodeShape(10, SWT.NONE);
} else {
shape = new FingerPost(10, SWT.RIGHT);
}
}
if (shape != null) {
CustomShapeFigure figure;
if (contextText != null || cardText != null) {
figure = new EntityFigure(shape, contextText, cardText, getCustomFigureFont());
} else {
figure = new CustomShapeLabel(shape, getCustomFigureFont());
}
figure.setMaximumWidth(MAX_FIGURE_WIDTH);
return figure;
}
element = TransformationTreeUtil.extractObject(element);
return super.getFigure(element);
}
use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree in project hale by halestudio.
the class XslRetype method createPropertiesFragment.
/**
* Create a XSL fragment for populating the properties of a target instance.
*
* @param typeCell the associated type cell
* @return the XSL fragment
* @throws TransformationException if creating the fragment failed
*/
protected String createPropertiesFragment(final Cell typeCell) throws TransformationException {
final TransformationTree tree = new TransformationTreeImpl(context().getAlignment(), typeCell);
FunctionService functionService = new AlignmentFunctionService(context().getAlignment());
/*
* Create the transformation graph derived from the transformation tree
* and perform context matching.
*/
/*
* XXX proxying multi-result nodes omitted for now, see
* proxyMultiResultNodes imlementation
*/
// final TGraph graph = new TGraphImpl(tree).proxyMultiResultNodes().performContextMatching();
final TGraph graph = new TGraphImpl(tree, functionService).performContextMatching();
// TODO tree as GraphML as informative annotation into XSLT?
try {
StringWriter propsOut = new StringWriter();
try {
AbstractTransformationTraverser trav = new RetypeTraverser(context(), propsOut, typeCell);
trav.traverse(graph);
} finally {
propsOut.close();
}
return propsOut.toString();
} catch (Exception e) {
throw new TransformationException("Failed to create property transformations", e);
}
}
use of eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree in project hale by halestudio.
the class TreePropertyTransformer method publish.
/**
* @see PropertyTransformer#publish(FamilyInstance, MutableInstance,
* TransformationLog, Cell)
*/
@Override
public void publish(final FamilyInstance source, final MutableInstance target, final TransformationLog typeLog, final Cell typeCell) {
instanceCounter.adjustOrPutValue(typeCell, 1, 1);
// increase output type counter
reporter.stats().at("createdPerType").at(target.getDefinition().getName().toString()).next();
Runnable job = new Runnable() {
@Override
public void run() {
try {
SimpleLogContext.withLog(typeLog, () -> {
// Add the meta data ID of the source as SourceID to the
// target
Collection<Instance> sources = InstanceUtil.getInstanceOutOfFamily(source);
Set<Object> ids = new HashSet<Object>();
for (Instance inst : sources) {
// Merge instances may have multiple IDs
List<Object> sourceIDs = inst.getMetaData(InstanceMetadata.METADATA_ID);
if (sourceIDs != null) {
ids.addAll(sourceIDs);
}
}
InstanceMetadata.setSourceID(target, ids.toArray());
// identify transformations to be executed on given
// instances
// create/get a transformation tree
TransformationTree tree = treePool.getTree(typeCell);
// State: base tree
HooksUtil.executeTreeHooks(treeHooks, TreeState.MINIMAL, tree, target);
// apply instance value to transformation tree
InstanceVisitor instanceVisitor = new InstanceVisitor(source, tree, typeLog);
tree.accept(instanceVisitor);
// State: basic source populated tree
// duplicate subtree as necessary
DuplicationVisitor duplicationVisitor = new DuplicationVisitor(tree, typeLog);
tree.accept(duplicationVisitor);
duplicationVisitor.doAugmentationTrackback();
// State: source populated tree (duplication complete)
HooksUtil.executeTreeHooks(treeHooks, TreeState.SOURCE_POPULATED, tree, target);
// apply functions
for (FunctionExecutor functionExecutor : executors) {
functionExecutor.setTypeCell(typeCell);
tree.accept(functionExecutor);
}
// State: full tree (target populated)
HooksUtil.executeTreeHooks(treeHooks, TreeState.FULL, tree, target);
// fill instance
builder.populate(target, tree, typeLog);
// generate the rest of the metadatas
metaworkerthread.get().generate(target);
// XXX ok to add to sink in any thread?!
// XXX addInstance and close were made synchronized in
// OrientInstanceSink
// XXX instead collect instances and write them in only
// one
// thread?
// after property transformations, publish target
// instance
sink.addInstance(target);
// and release the tree for further use
treePool.releaseTree(tree);
});
} catch (Throwable e) {
/*
* Catch any error, as exceptions in the executor service
* will only result in a message on the console.
*/
typeLog.error(typeLog.createMessage("Error performing property transformations", e));
}
}
};
if (executorService != null) {
executorService.execute(job);
} else {
job.run();
}
}
Aggregations