use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.
the class GroovyJoinPage method validate.
@Override
protected boolean validate(String document) {
ParameterValue param = CellUtil.getFirstParameter(getWizard().getUnfinishedCell(), JoinFunction.PARAMETER_JOIN);
JoinParameter joinParameter = param.as(JoinParameter.class);
// check Join parameter
if (joinParameter == null) {
// setValidationError("Missing join configuration");
return false;
} else {
String error = joinParameter.validate();
if (!setValidationError(error)) {
return false;
}
}
// target type
Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (targetType == null) {
// not yet selected (NewRelationWizard)
return false;
}
/*
* FIXME use JoinParameter to fake joined instances!
*
* XXX for now just base instance
*/
TypeEntityDefinition sourceType = joinParameter.getTypes().get(0);
InstanceBuilder builder = new InstanceBuilder(false);
Instance instance = getTestValues().get(sourceType);
if (instance == null) {
// use an empty instance as input for the script
instance = new DefaultInstance(sourceType.getDefinition(), DataSet.SOURCE);
}
FamilyInstance source = new FamilyInstanceImpl(instance);
// prepare binding
Cell cell = getWizard().getUnfinishedCell();
CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
Script script = null;
try {
script = service.parseScript(document, binding);
GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
} catch (final Exception e) {
return handleValidationResult(script, e);
}
return handleValidationResult(script, null);
}
use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.
the class IndexJoinIterator method join.
/**
* Joins all direct children of the given type to currentInstances.
*/
@SuppressWarnings("javadoc")
private void join(FamilyInstance[] currentInstances, int currentType) {
// Join all types that are direct children of the last type.
for (int i = currentType + 1; i < joinDefinition.directParent.length; i++) {
if (joinDefinition.directParent[i] == currentType) {
// Get join condition for the direct child type.
Multimap<Integer, JoinCondition> joinConditions = joinDefinition.joinTable.get(i);
// Collect intersection of conditions. null marks beginning
// in contrast to an empty set.
Set<ResolvableInstanceReference> possibleInstances = null;
// ParentType -> JoinConditions
for (Map.Entry<Integer, JoinCondition> joinCondition : joinConditions.entries()) {
PropertyEntityDefinition baseProp = joinCondition.getValue().baseProperty;
QName baseTypeName = baseProp.getType().getName();
List<QName> basePropertyPath = baseProp.getPropertyPath().stream().map(pp -> pp.getChild().getName()).collect(Collectors.toList());
PropertyEntityDefinition joinProp = joinCondition.getValue().joinProperty;
QName joinTypeName = joinProp.getType().getName();
List<QName> joinPropertyPath = joinProp.getPropertyPath().stream().map(pp -> pp.getChild().getName()).collect(Collectors.toList());
List<IndexedPropertyValue> currentValues = index.getInstancePropertyValues(baseTypeName, basePropertyPath, currentInstances[joinCondition.getKey()].getId());
if (currentValues == null || currentValues.isEmpty()) {
possibleInstances = Collections.emptySet();
break;
}
HashSet<ResolvableInstanceReference> matches = new HashSet<ResolvableInstanceReference>();
for (IndexedPropertyValue currentValue : currentValues) {
if (currentValue.getValues() == null || currentValue.getValues().isEmpty()) {
continue;
}
// Find instances that have the current property value
Collection<ResolvableInstanceReference> instancesWithValues = index.getInstancesByValue(joinTypeName, joinPropertyPath, currentValue.getValues());
matches.addAll(instancesWithValues);
}
if (possibleInstances == null) {
possibleInstances = matches;
} else {
// Remove candidates that don't have the current
// property value
Iterator<ResolvableInstanceReference> it = possibleInstances.iterator();
while (it.hasNext()) {
ResolvableInstanceReference cand = it.next();
if (!matches.contains(cand)) {
it.remove();
}
}
}
if (possibleInstances.isEmpty()) {
break;
}
}
if (possibleInstances != null && !possibleInstances.isEmpty()) {
FamilyInstance parent = currentInstances[currentType];
for (ResolvableInstanceReference ref : possibleInstances) {
FamilyInstance child;
child = new FamilyInstanceImpl(ref.resolve());
parent.addChild(child);
currentInstances[i] = child;
join(currentInstances, i);
}
currentInstances[i] = null;
}
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.
the class JoinIterator method convert.
/**
* @see eu.esdihumboldt.hale.common.instance.model.impl.GenericResourceIteratorAdapter#convert(java.lang.Object)
*/
@Override
protected FamilyInstance convert(InstanceReference next) {
FamilyInstance base = new FamilyInstanceImpl(instances.getInstance(next));
FamilyInstance[] currentInstances = new FamilyInstance[parent.length];
currentInstances[0] = base;
join(currentInstances, 0);
return base;
}
use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance in project hale by halestudio.
the class JoinIterator method join.
// Joins all direct children of the given type to currentInstances.
private void join(FamilyInstance[] currentInstances, int currentType) {
// Join all types that are direct children of the last type.
for (int i = currentType + 1; i < parent.length; i++) {
if (parent[i] == currentType) {
// Get join condition for the direct child type.
Multimap<Integer, JoinCondition> joinConditions = joinTable.get(i);
// Collect intersection of conditions. null marks beginning
// in contrast to an empty set.
Set<InstanceReference> possibleInstances = null;
// ParentType -> JoinConditions
for (Map.Entry<Integer, JoinCondition> joinCondition : joinConditions.entries()) {
Collection<Object> currentValues = AlignmentUtil.getValues(currentInstances[joinCondition.getKey()], joinCondition.getValue().baseProperty, true);
if (currentValues == null) {
possibleInstances = Collections.emptySet();
break;
}
// Allow targets with any of the property values.
HashSet<InstanceReference> matches = new HashSet<InstanceReference>();
for (Object currentValue : currentValues) {
Object keyValue = currentValue;
if (valueProcessor != null) {
keyValue = valueProcessor.processValue(currentValue, joinCondition.getValue().baseProperty);
}
matches.addAll(index.get(joinCondition.getValue().joinProperty).get(keyValue));
}
if (possibleInstances == null)
possibleInstances = matches;
else {
// Intersect!
Iterator<InstanceReference> iter = possibleInstances.iterator();
while (iter.hasNext()) {
InstanceReference ref = iter.next();
if (!matches.contains(ref))
iter.remove();
}
}
// Break if set is empty.
if (possibleInstances.isEmpty())
break;
}
if (possibleInstances != null && !possibleInstances.isEmpty()) {
FamilyInstance parent = currentInstances[currentType];
for (InstanceReference ref : possibleInstances) {
FamilyInstance child;
if (ref instanceof ResolvableInstanceReference) {
child = new FamilyInstanceImpl(((ResolvableInstanceReference) ref).resolve());
} else {
child = new FamilyInstanceImpl(instances.getInstance(ref));
}
parent.addChild(child);
currentInstances[i] = child;
join(currentInstances, i);
}
currentInstances[i] = null;
}
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.FamilyInstance 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);
}
Aggregations