use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class MappingView method getDefinitionsFromSelection.
private Pair<Set<EntityDefinition>, Set<EntityDefinition>> getDefinitionsFromSelection(SchemaSelection selection) {
Set<EntityDefinition> sourceItems;
Set<EntityDefinition> targetItems;
if (selection instanceof IStructuredSelection) {
// prefer getting information from the IStructuredSelection, which
// from the Schema Explorer only contains the recently selected
// elements on one side
sourceItems = new HashSet<EntityDefinition>();
targetItems = new HashSet<EntityDefinition>();
for (Object object : ((IStructuredSelection) selection).toArray()) {
if (object instanceof EntityDefinition) {
EntityDefinition def = (EntityDefinition) object;
switch(def.getSchemaSpace()) {
case TARGET:
targetItems.add(def);
break;
case SOURCE:
default:
sourceItems.add(def);
}
}
}
} else {
sourceItems = selection.getSourceItems();
targetItems = selection.getTargetItems();
}
return new Pair<Set<EntityDefinition>, Set<EntityDefinition>>(sourceItems, targetItems);
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class FunctionExecutor method executeTransformation.
/**
* Execute a property transformation.
*
* @param transformation the transformation factory
* @param cell the alignment cell
* @param sources the named source entities and nodes
* @param targets the named target entities and nodes
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void executeTransformation(PropertyTransformationFactory transformation, Cell cell, ListMultimap<String, Pair<SourceNode, Entity>> sources, ListMultimap<String, Pair<TargetNode, Entity>> targets) {
TransformationLog cellLog = new CellLog(reporter, cell);
PropertyTransformation<?> function;
try {
// TODO cache function objects?
function = transformation.createExtensionObject();
} catch (Exception e) {
cellLog.error(cellLog.createMessage("Error creating transformation function.", e));
return;
}
TransformationEngine engine = engines.get(transformation.getEngineId(), cellLog);
if (engine == null) {
// TODO instead try another transformation
cellLog.error(cellLog.createMessage("Skipping property transformation: No matching transformation engine found", null));
return;
}
// configure function
// set expected result
ListMultimap<String, PropertyEntityDefinition> expectedResult = ArrayListMultimap.create(targets.keySet().size(), 1);
for (Entry<String, Pair<TargetNode, Entity>> targetEntry : targets.entries()) {
EntityDefinition def = targetEntry.getValue().getSecond().getDefinition();
expectedResult.put(targetEntry.getKey(), toPropertyEntityDefinition(def));
}
function.setExpectedResult(expectedResult);
// set source variables
ListMultimap<String, PropertyValue> variables = ArrayListMultimap.create();
for (Entry<String, Pair<SourceNode, Entity>> sourceEntry : sources.entries()) {
EntityDefinition def = sourceEntry.getValue().getSecond().getDefinition();
SourceNode sourceNode = sourceEntry.getValue().getFirst();
if (TransformationTreeUtil.isEager(cell, sourceNode, cellLog, context.getServiceProvider())) {
// eager source - all values
Object[] values = sourceNode.getAllValues();
if (values != null) {
for (int i = 0; i < values.length; i++) {
PropertyValue propertyValue = new PropertyValueImpl(values[i], toPropertyEntityDefinition(def));
variables.put(sourceEntry.getKey(), propertyValue);
}
}
} else {
// non-eager source - one value
Object value = sourceNode.getValue();
PropertyValue propertyValue = new PropertyValueImpl(value, toPropertyEntityDefinition(def));
variables.put(sourceEntry.getKey(), propertyValue);
}
}
function.setVariables(variables);
// set parameters
function.setParameters(cell.getTransformationParameters());
// set context
function.setExecutionContext(context.getCellContext(cell));
// set target type
TypeDefinition targetType = null;
if (!targets.isEmpty()) {
TargetNode target = targets.values().iterator().next().getFirst();
targetType = target.getEntityDefinition().getType();
}
function.setTargetType(targetType);
function.setTypeCell(typeCell.get());
// execute function
try {
((PropertyTransformation) function).execute(transformation.getIdentifier(), engine, transformation.getExecutionParameters(), cellLog, cell);
} catch (Throwable e) {
// TODO instead try another transformation?
cellLog.error(cellLog.createMessage("Skipping property transformation: Executing property transformation failed.", e));
return;
}
// apply function results
ListMultimap<String, Object> results = function.getResults();
if (results != null) {
for (String name : results.keySet()) {
List<Object> values = results.get(name);
List<Pair<TargetNode, Entity>> nodes = targets.get(name);
if (nodes.size() > values.size()) {
cellLog.warn(cellLog.createMessage(MessageFormat.format("Transformation result misses values for result with name {0}", name), null));
}
if (values.size() > nodes.size()) {
cellLog.warn(cellLog.createMessage(MessageFormat.format("More transformation results than target nodes for result with name {0}", name), null));
}
int count = Math.min(values.size(), nodes.size());
// node...
for (int i = 0; i < count; i++) {
Object value = values.get(i);
TargetNode node = nodes.get(i).getFirst();
if (value instanceof MultiValue) {
MultiValue originalValue = (MultiValue) value;
MultiValue processedValue = new MultiValue(originalValue.size());
for (Object o : originalValue) {
processedValue.add(processValue(cellLog, function, o, node));
}
value = processedValue;
} else {
value = processValue(cellLog, function, value, node);
}
/*
* TODO
*
* set node value only if no result has already been set. If
* a value is already there and we are in a lower priority
* executor, we do not overwrite.
*/
if (!node.isDefined()) {
node.setResult(value);
}
}
}
}
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class HaleIO method findIOProviderAndId.
/**
* Find an I/O provider instance for the given input
*
* @param <T> the provider interface type
*
* @param providerType the provider type, usually an interface
* @param in the input supplier to use for testing, may be <code>null</code>
* if the file name is not <code>null</code>
* @param filename the file name, may be <code>null</code> if the input
* supplier is not <code>null</code>
* @return a pair with the I/O provider and the corresponding identifier,
* both are <code>null</code> if no matching I/O provider was found
*/
@SuppressWarnings("unchecked")
public static <T extends IOProvider> Pair<T, String> findIOProviderAndId(Class<T> providerType, InputSupplier<? extends InputStream> in, String filename) {
T reader = null;
String providerId = null;
IContentType contentType = HaleIO.findContentType(providerType, in, filename);
if (contentType != null) {
IOProviderDescriptor factory = HaleIO.findIOProviderFactory(providerType, contentType, null);
try {
reader = (T) factory.createExtensionObject();
providerId = factory.getIdentifier();
} catch (Exception e) {
throw new RuntimeException("Could not create I/O provider", e);
}
if (reader != null) {
reader.setContentType(contentType);
}
}
return new Pair<T, String>(reader, providerId);
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class CellNodeValidator method visit.
/**
* @see AbstractTargetToSourceVisitor#visit(CellNode)
*/
@Override
public boolean visit(CellNode node) {
// evaluate if for the cell all needed inputs are set
Cell cell = node.getCell();
// collect source and target nodes per entity name
ListMultimap<String, Pair<SourceNode, Entity>> sources = ArrayListMultimap.create();
if (cell.getSource() != null) {
for (Entry<String, ? extends Entity> sourceEntry : cell.getSource().entries()) {
String name = sourceEntry.getKey();
Entity entity = sourceEntry.getValue();
SourceNode sourceNode = findSourceNode(node, entity);
if (sourceNode != null) {
if (sourceNode.isDefined()) {
sources.put(name, new Pair<SourceNode, Entity>(sourceNode, entity));
}
} else {
log.error("Source node for entity not found.");
}
}
}
ListMultimap<String, Pair<TargetNode, Entity>> targets = ArrayListMultimap.create();
for (Entry<String, ? extends Entity> targetEntry : cell.getTarget().entries()) {
String name = targetEntry.getKey();
Entity entity = targetEntry.getValue();
TargetNode targetNode = findTargetNode(node, entity);
if (targetNode != null) {
targets.put(name, new Pair<TargetNode, Entity>(targetNode, entity));
} else {
log.error("Target node for entity not found.");
}
}
boolean valid = validate(node, sources, targets);
if (valid) {
processValid(cell, sources, targets);
}
node.setValid(valid);
// don't visit source nodes
return false;
}
use of eu.esdihumboldt.util.Pair in project hale by halestudio.
the class JoinContext method apply.
/**
* Apply information collected in the context to the cell.
*
* @param newCell the merged cell
* @param log the cell log
* @param migration the alignment migration
*/
public void apply(MutableCell newCell, AlignmentMigration migration, SimpleLog log) {
ListMultimap<String, ParameterValue> params = ArrayListMultimap.create();
/*
* Order: Keep original order but replace entities w/ all matches
*/
Set<TypeEntityDefinition> types = new LinkedHashSet<>();
for (TypeEntityDefinition type : orgParameter.getTypes()) {
List<TypeEntityDefinition> repl = replacements.get(type);
if (repl.isEmpty()) {
log.error("Could not find replacement for type {0} in join order", type);
types.add(type);
} else {
types.addAll(repl);
}
}
/*
* Conditions: (1) add conditions from matches and (2) add conditions
* from original cell translated to new schema (via property mapping),
* if they are not duplicates
*/
Set<Pair<PropertyEntityDefinition, PropertyEntityDefinition>> cons = new LinkedHashSet<>();
// add conditions from matches
for (Cell match : joinMatches) {
JoinParameter matchParameter = CellUtil.getFirstParameter(match, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
for (JoinCondition condition : matchParameter.getConditions()) {
cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
}
}
// migrate original conditions
Set<JoinCondition> migrated = orgParameter.getConditions().stream().map(condition -> {
PropertyEntityDefinition baseProperty = processOriginalConditionProperty(condition.baseProperty, migration, log);
PropertyEntityDefinition joinProperty = processOriginalConditionProperty(condition.joinProperty, migration, log);
JoinCondition result = new JoinCondition(baseProperty, joinProperty);
return result;
}).collect(Collectors.toSet());
for (JoinCondition condition : migrated) {
if (!condition.baseProperty.equals(condition.joinProperty)) {
// migrated condition may contain "loop" condition
cons.add(new Pair<>(condition.baseProperty, condition.joinProperty));
}
}
// add messages on dropped filter/conditions
for (EntityDefinition stripped : strippedSources) {
if (!AlignmentUtil.isDefaultEntity(stripped)) {
String msg = "Conditions/contexts for an original source could not be transfered and were dropped: " + MergeUtil.getContextInfoString(stripped);
log.warn(msg);
}
}
// all conditions
Set<JoinCondition> conditions = new HashSet<>();
for (Pair<PropertyEntityDefinition, PropertyEntityDefinition> condition : cons) {
conditions.add(new JoinCondition(condition.getFirst(), condition.getSecond()));
}
JoinParameter newParam = new JoinParameter(new ArrayList<>(types), conditions);
params.replaceValues(JoinFunction.PARAMETER_JOIN, Collections.singleton(new ParameterValue(Value.of(newParam))));
// Use Groovy Join if original or match uses a script
if (!scripts.isEmpty()) {
boolean originalScript = scripts.size() == 1 && GroovyJoin.ID.equals(newCell.getTransformationIdentifier());
Text script;
if (originalScript) {
// use original script
script = new Text(scripts.get(0).getSecond());
// create annotation
log.warn("The Groovy script from the original cell was reused, logic and references to sources are very likely not valid anymore.");
} else {
// dummy script with all original scripts
newCell.setTransformationIdentifier(GroovyJoin.ID);
script = buildScript(scripts);
// create annotation
log.warn("At least one source mapping used a Groovy script, the script could not be combined automatically and was replaced with a dummy script (old scripts are commented out). Please check how you can migrate the old functionality.");
}
params.replaceValues(GroovyConstants.PARAMETER_SCRIPT, Collections.singleton(new ParameterValue(Value.of(script))));
}
newCell.setTransformationParameters(params);
}
Aggregations