use of eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl 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.align.transformation.function.impl.FamilyInstanceImpl 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.align.transformation.function.impl.FamilyInstanceImpl 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.align.transformation.function.impl.FamilyInstanceImpl 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.align.transformation.function.impl.FamilyInstanceImpl in project hale by halestudio.
the class GroovyRetypePage method validate.
@Override
protected boolean validate(String document) {
super.validate(document);
Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
Type sourceType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getSource());
if (sourceType == null || targetType == null) {
// not yet selected (NewRelationWizard)
return false;
}
InstanceBuilder builder = new InstanceBuilder(false);
Instance instance = testValues.get(sourceType.getDefinition());
if (instance == null) {
// use an empty instance as input for the script
instance = new DefaultInstance(sourceType.getDefinition().getDefinition(), DataSet.SOURCE);
}
FamilyInstance source = new FamilyInstanceImpl(instance);
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);
}
Aggregations