use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class SpatialJoinHandler method partitionInstances.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_SPATIAL_JOIN) || transformationParameters.get(PARAMETER_SPATIAL_JOIN).isEmpty()) {
throw new TransformationException("No join parameter defined");
}
if (services == null) {
throw new IllegalStateException("ServiceProvider must be set before calling partitionInstances");
}
SpatialJoinParameter joinParameter = transformationParameters.get(PARAMETER_SPATIAL_JOIN).get(0).as(SpatialJoinParameter.class);
String validation = joinParameter.validate();
if (validation != null) {
throw new TransformationException("Spatial Join parameter invalid: " + validation);
}
List<TypeEntityDefinition> types = joinParameter.types;
// ChildType -> DirectParentType
int[] directParent = new int[joinParameter.types.size()];
// ChildType -> (ParentType -> Collection<JoinCondition>)
Map<Integer, Multimap<Integer, SpatialJoinCondition>> joinTable = new HashMap<>();
for (SpatialJoinCondition condition : joinParameter.conditions) {
int baseTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.baseProperty));
int joinTypeIndex = types.indexOf(AlignmentUtil.getTypeEntity(condition.joinProperty));
Multimap<Integer, SpatialJoinCondition> typeTable = joinTable.get(joinTypeIndex);
if (typeTable == null) {
typeTable = ArrayListMultimap.create(2, 2);
joinTable.put(joinTypeIndex, typeTable);
}
typeTable.put(baseTypeIndex, condition);
// update highest type if necessary
if (directParent[joinTypeIndex] < baseTypeIndex) {
directParent[joinTypeIndex] = baseTypeIndex;
}
}
// remember instances of first type to start join afterwards
Collection<InstanceReference> startInstances = new LinkedList<InstanceReference>();
// iterate once over all instances
ResourceIterator<Instance> iterator = instances.iterator();
try {
while (iterator.hasNext()) {
Instance next = iterator.next();
// remember instances of first type
if (next.getDefinition().equals(types.get(0).getDefinition())) {
startInstances.add(instances.getReference(next));
}
}
} finally {
iterator.close();
}
return new SpatialJoinIterator(instances, startInstances, directParent, services, joinTable);
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class DefaultTransformationTest method testGenerateUID.
/**
* Test for the generateduid. Since the uid is always different, just test
* for them being unique.
*
* @throws Exception if an error occurs executing the test
*/
@Test
public void testGenerateUID() throws Exception {
TransformationExample example = TransformationExamples.getExample(TransformationExamples.GENERATEUID);
List<Instance> transformedData = transformData(example);
TreeSet<String> uniqueIdSet = new TreeSet<String>();
for (Instance instance : transformedData) {
Iterable<QName> propertyNames = instance.getPropertyNames();
for (QName propertyName : propertyNames) {
Object[] property = instance.getProperty(propertyName);
for (Object object : property) {
boolean added = uniqueIdSet.add(object.toString());
if (!added) {
assertTrue("Found duplicated id when should be unique: " + object.toString(), added);
}
}
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class AbstractInstancePainter method update.
/**
* Do a complete update of the way-points. Existing way-points are
* discarded.
*
* @param selection the current selection
*/
public void update(ISelection selection) {
clearWaypoints();
// XXX only mappable type instances for source?!
InstanceCollection instances = instanceService.getInstances(dataSet);
if (selection != null) {
lastSelected = collectReferences(selection);
}
if (instances.isEmpty()) {
return;
}
final AtomicBoolean updateFinished = new AtomicBoolean(false);
IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
String taskName;
switch(getDataSet()) {
case SOURCE:
taskName = "Update source data in map";
break;
case TRANSFORMED:
default:
taskName = "Update transformed data in map";
break;
}
monitor.beginTask(taskName, IProgressMonitor.UNKNOWN);
// add way-points for instances
InstanceCollection instances = instanceService.getInstances(dataSet);
ResourceIterator<Instance> it = instances.iterator();
try {
while (it.hasNext()) {
Instance instance = it.next();
InstanceWaypoint wp = createWaypoint(instance, instanceService);
if (wp != null) {
if (lastSelected.contains(wp.getValue())) {
// refresh can be
wp.setSelected(true, null);
// ignored because
// it's done for
// addWaypoint
}
// no refresher, as
addWaypoint(wp, null);
// refreshAll is executed
}
}
} finally {
it.close();
monitor.done();
updateFinished.set(true);
}
}
};
try {
ThreadProgressMonitor.runWithProgressDialog(op, false);
} catch (Throwable e) {
log.error("Error running painter update", e);
}
HaleUI.waitFor(updateFinished);
refreshAll();
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class XsltTransformationTest method transformData.
@Override
protected List<Instance> transformData(TransformationExample example) throws Exception {
// export alignment to XSLT
XsltExport export = new XsltExport();
export.setAlignment(example.getAlignment());
export.setSourceSchema(new DefaultSchemaSpace().addSchema(example.getSourceSchema()));
export.setTargetSchema(new DefaultSchemaSpace().addSchema(example.getTargetSchema()));
export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAMESPACE, Value.of(example.getTargetContainerNamespace()));
export.setParameter(XsltExport.PARAM_ROOT_ELEMENT_NAME, Value.of(example.getTargetContainerName()));
File tempXsltFile = File.createTempFile("xsltest", ".xsl");
export.setTarget(new FileIOSupplier(tempXsltFile));
IOReport res = export.execute(new LogProgressIndicator());
assertTrue("XSLT export not successful", res.isSuccess());
assertTrue("Errors during XSLT export", res.getErrors().isEmpty());
// invoke XSLT on source file to produce target
File target = File.createTempFile("xsltest", ".xml");
executeXslt(example.getSourceDataInput(), tempXsltFile, target);
// load target and return instances
InstanceCollection instances = TestUtil.loadInstances(target.toURI(), example.getTargetSchema());
List<Instance> list = new ArrayList<Instance>();
ResourceIterator<Instance> it = instances.iterator();
try {
while (it.hasNext()) {
list.add(it.next());
}
} finally {
it.close();
}
// clean up
tempXsltFile.delete();
target.delete();
return list;
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceBuilder method getValue.
/**
* Get the value for a target node.
*
* @param node the target node
* @param typeLog the type transformation log
* @return the value or {@link NoObject#NONE} representing no value
*/
private Object getValue(TargetNode node, TransformationLog typeLog) {
if (node.getChildren(true).isEmpty()) {
// simple leaf
if (node.isDefined()) {
// XXX this is done in FunctionExecutor
return node.getResult();
} else {
return NoObject.NONE;
}
}
boolean isProperty = node.getDefinition().asProperty() != null;
boolean isGroup = node.getDefinition().asGroup() != null;
if (isProperty && node.isDefined()) {
// it's a property and we have a value/values
Object nodeValue = node.getResult();
if (!(nodeValue instanceof MultiValue)) {
// pack single value into multivalue
MultiValue nodeMultiValue = new MultiValue();
nodeMultiValue.add(nodeValue);
nodeValue = nodeMultiValue;
}
MultiValue nodeMultiValue = (MultiValue) nodeValue;
if (!nodeMultiValue.isEmpty()) {
// Create n instances
MultiValue resultMultiValue = new MultiValue(nodeMultiValue.size());
for (Object value : nodeMultiValue) {
// the value may have been wrapped in an Instance
if (value instanceof Instance) {
value = ((Instance) value).getValue();
}
MutableInstance instance = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
instance.setValue(value);
// XXX since this is the same for all instances maybe do
// this on a dummy and only copy properties for each?
// XXX MultiValue w/ target node children => strange results
populateGroup(instance, node, typeLog);
resultMultiValue.add(instance);
}
return resultMultiValue;
}
// if nodeMultiValue is empty fall through to below
// it the instance could still have children even without a value
}
// it's a property or group with no value
MutableGroup group;
if (isGroup) {
group = new DefaultGroup(node.getDefinition().asGroup());
} else if (isProperty) {
group = new DefaultInstance(node.getDefinition().asProperty().getPropertyType(), null);
} else {
throw new IllegalStateException("Illegal child definition");
}
// populate with children
if (populateGroup(group, node, typeLog)) {
return group;
} else {
return NoObject.NONE;
}
}
Aggregations