use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class MarkdownCellExplanationTest method createTestCell.
@SuppressWarnings("unused")
private Cell createTestCell() {
// cell 1
MutableCell cell1 = new DefaultCell();
String id1;
// must be an existing function
cell1.setTransformationIdentifier(id1 = "eu.esdihumboldt.hale.align.formattedstring");
ListMultimap<String, ParameterValue> parameters1 = LinkedListMultimap.create();
// parameter that does not exist (for parameter list testing)
parameters1.put("test", new ParameterValue("1"));
parameters1.put("test", new ParameterValue("2"));
// existing parameter
parameters1.put("pattern", new ParameterValue("3"));
cell1.setTransformationParameters(parameters1);
ListMultimap<String, Type> source1 = ArrayListMultimap.create();
QName source1TypeName;
String source1EntityName;
TypeDefinition sourceType1 = new DefaultTypeDefinition(source1TypeName = new QName("source1Type"));
Filter filter = null;
source1.put(source1EntityName = "var", new DefaultType(new TypeEntityDefinition(sourceType1, SchemaSpaceID.SOURCE, filter)));
cell1.setSource(source1);
ListMultimap<String, Type> target1 = ArrayListMultimap.create();
QName target1TypeName;
String target1EntityName;
TypeDefinition targetType1 = new DefaultTypeDefinition(target1TypeName = new QName("http://some.name.space/t1", "target1Type"));
target1.put(target1EntityName = null, new DefaultType(new TypeEntityDefinition(targetType1, SchemaSpaceID.TARGET, null)));
cell1.setTarget(target1);
return cell1;
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition 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 {
for (TypeEntityDefinition replacement : repl) {
Filter filter = typeFilters.get(replacement.getDefinition());
if (filter != null) {
// apply filter
types.add(new TypeEntityDefinition(replacement.getDefinition(), replacement.getSchemaSpace(), filter));
} else {
types.add(replacement);
}
}
}
}
/*
* 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.getType().equals(condition.joinProperty.getType())) {
// 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(applyFilter(condition.getFirst()), applyFilter(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);
}
use of eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition in project hale by halestudio.
the class AbstractMergeCellMigrator method applySourceContextsToJoinFocus.
/**
* Handle special case of applying source contexts to the entity that is the
* Join focus.
*
* @param newCell the new cell to update the sources
* @param originalSource the original source
* @param migration the alignment migration
* @param log the operation log
* @return if the method handled the context transfer
*/
private boolean applySourceContextsToJoinFocus(MutableCell newCell, Entity originalSource, AlignmentMigration migration, SimpleLog log) {
if (!MergeSettings.isTransferContextsToJoinFocus()) {
return false;
}
String function = newCell.getTransformationIdentifier();
switch(function) {
case GroovyJoin.ID:
case JoinFunction.ID:
break;
default:
return false;
}
JoinParameter joinConfig = CellUtil.getFirstParameter(newCell, JoinFunction.PARAMETER_JOIN).as(JoinParameter.class);
if (joinConfig == null || joinConfig.getTypes() == null || joinConfig.getTypes().isEmpty()) {
return false;
}
TypeEntityDefinition focus = joinConfig.getTypes().iterator().next();
AtomicReference<Filter> focusFilter = new AtomicReference<>();
// transfer context
newCell.setSource(ArrayListMultimap.create(Multimaps.transformValues(newCell.getSource(), new com.google.common.base.Function<Entity, Entity>() {
@Override
public Entity apply(Entity input) {
if (input.getDefinition().getPropertyPath().isEmpty() && input.getDefinition().getType().equals(focus.getType())) {
EntityDefinition transferedSource = AbstractMigration.translateContexts(originalSource.getDefinition(), input.getDefinition(), migration, log);
focusFilter.set(transferedSource.getFilter());
return AlignmentUtil.createEntity(transferedSource);
} else {
return input;
}
}
})));
if (focusFilter.get() != null) {
// order
List<TypeEntityDefinition> types = new ArrayList<>();
for (int i = 0; i < joinConfig.getTypes().size(); i++) {
TypeEntityDefinition type = joinConfig.getTypes().get(i);
if (i == 0) {
type = new TypeEntityDefinition(type.getDefinition(), type.getSchemaSpace(), focusFilter.get());
}
types.add(type);
}
// conditions
Set<JoinCondition> conditions = joinConfig.getConditions().stream().map(c -> {
if (c.baseProperty.getType().equals(focus.getType())) {
return new JoinCondition(applyFilter(c.baseProperty, focusFilter.get()), c.joinProperty);
} else {
return c;
}
}).collect(Collectors.toSet());
JoinParameter newConfig = new JoinParameter(types, conditions);
ListMultimap<String, ParameterValue> modParams = ArrayListMultimap.create(newCell.getTransformationParameters());
List<ParameterValue> joinParams = modParams.get(JoinFunction.PARAMETER_JOIN);
if (!joinParams.isEmpty()) {
JoinParameter joinParam = joinParams.get(0).as(JoinParameter.class);
if (joinParam != null) {
joinParams.clear();
joinParams.add(new ParameterValue(Value.complex(newConfig)));
}
}
newCell.setTransformationParameters(modParams);
}
return true;
}
Aggregations