use of eu.esdihumboldt.hale.common.align.model.Entity in project hale by halestudio.
the class GroovyTransformation method createGroovyBinding.
/**
* Create a Groovy binding from the list of variables.
*
* @param vars the variable values
* @param varDefs definition of the assigned variables, in case some
* variable values are not set, may be <code>null</code>
* @param cell the cell the binding is created for
* @param typeCell the type cell the binding is created for, may be
* <code>null</code>
* @param builder the instance builder for creating target instances, or
* <code>null</code> if not applicable
* @param useInstanceVariables if instances should be used as variables for
* the binding instead of extracting the instance values
* @param log the transformation log
* @param context the execution context
* @param targetInstanceType the type of the target instance
* @return the binding for use with {@link GroovyShell}
*/
public static Binding createGroovyBinding(List<PropertyValue> vars, List<? extends Entity> varDefs, Cell cell, Cell typeCell, InstanceBuilder builder, boolean useInstanceVariables, TransformationLog log, ExecutionContext context, TypeDefinition targetInstanceType) {
Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, context, targetInstanceType);
// collect definitions to check if all were provided
Set<EntityDefinition> notDefined = new HashSet<>();
if (varDefs != null) {
for (Entity entity : varDefs) {
notDefined.add(entity.getDefinition());
}
}
// keep only defs where no value is provided
if (!notDefined.isEmpty()) {
for (PropertyValue var : vars) {
notDefined.remove(var.getProperty());
}
}
// add null value for missing variables
if (!notDefined.isEmpty()) {
vars = new ArrayList<>(vars);
for (EntityDefinition entityDef : notDefined) {
if (entityDef instanceof PropertyEntityDefinition) {
vars.add(new PropertyValueImpl(null, (PropertyEntityDefinition) entityDef));
}
}
}
for (PropertyValue var : vars) {
// add the variable to the environment
addToBinding(binding, var.getProperty(), getUseValue(var.getValue(), useInstanceVariables));
}
return binding;
}
use of eu.esdihumboldt.hale.common.align.model.Entity in project hale by halestudio.
the class MathematicalExpressionExplanation method getExplanation.
@Override
protected String getExplanation(Cell cell, boolean html, ServiceProvider services, Locale locale) {
Entity target = CellUtil.getFirstEntity(cell.getTarget());
String expression = CellUtil.getFirstParameter(cell, MathematicalExpression.PARAMETER_EXPRESSION).as(String.class);
List<? extends Entity> sources = cell.getSource().get(MathematicalExpression.ENTITY_VARIABLE);
if (target != null && expression != null) {
if (html)
expression = "<pre>" + expression + "</pre>";
String explanation = MessageFormat.format(getMessage("main", locale), formatEntity(target, html, true, locale), expression);
if (html)
explanation = explanation.replaceAll("\n", "<br />");
if (html) {
Map<String, String> varToProperty = sources.stream().collect(Collectors.toMap(entity -> {
return getEntityNameWithoutCondition(entity);
}, entity -> {
return formatEntity(entity, true, false, locale);
}));
explanation += buildReplacementTable(varToProperty, locale);
}
return explanation;
}
return null;
}
use of eu.esdihumboldt.hale.common.align.model.Entity in project hale by halestudio.
the class DefaultCellMigrator method updateCell.
@Override
public MutableCell updateCell(final Cell originalCell, final AlignmentMigration migration, final MigrationOptions options, SimpleLog log) {
MutableCell result = new DefaultCell(originalCell);
SimpleLog cellLog = SimpleLog.all(log, new CellLog(result, CELL_LOG_CATEGORY));
final AtomicBoolean replacedEntities = new AtomicBoolean(false);
EntryTransformer<String, Entity, Entity> entityTransformer = new EntryTransformer<String, Entity, Entity>() {
@Override
public Entity transformEntry(String key, Entity value) {
EntityDefinition org = value.getDefinition();
Optional<EntityDefinition> replace = migration.entityReplacement(org, cellLog);
EntityDefinition entity = replace.orElse(org);
if (!Objects.equal(entity, org)) {
replacedEntities.set(true);
}
if (entity instanceof PropertyEntityDefinition) {
return new DefaultProperty((PropertyEntityDefinition) entity);
} else if (entity instanceof TypeEntityDefinition) {
return new DefaultType((TypeEntityDefinition) entity);
} else {
throw new IllegalStateException("Invalid entity definition for creating entity");
}
}
};
// update source entities
if (options.updateSource() && result.getSource() != null && !result.getSource().isEmpty()) {
result.setSource(ArrayListMultimap.create(Multimaps.transformEntries(result.getSource(), entityTransformer)));
}
// update target entities
if (options.updateTarget() && result.getTarget() != null && !result.getTarget().isEmpty()) {
result.setTarget(ArrayListMultimap.create(Multimaps.transformEntries(result.getTarget(), entityTransformer)));
}
// TODO anything else?
postUpdateCell(result, options, replacedEntities.get(), cellLog);
return result;
}
use of eu.esdihumboldt.hale.common.align.model.Entity in project hale by halestudio.
the class FormattedStringMigrator method convertPattern.
private String convertPattern(String pattern, ListMultimap<String, ? extends Entity> oldSource, AlignmentMigration migration, SimpleLog log) {
List<PropertyEntityDefinition> oldVars = new ArrayList<>();
if (oldSource != null && oldSource.get(FormattedStringFunction.ENTITY_VARIABLE) != null) {
oldVars = oldSource.get(FormattedStringFunction.ENTITY_VARIABLE).stream().filter(e -> e.getDefinition() instanceof PropertyEntityDefinition).map(e -> (PropertyEntityDefinition) e.getDefinition()).collect(Collectors.toList());
}
Map<String, Object> replacements = new HashMap<>();
for (PropertyEntityDefinition var : oldVars) {
Optional<EntityDefinition> replacement = migration.entityReplacement(var, log);
replacement.ifPresent(repl -> {
String newName = repl.getDefinition().getName().getLocalPart();
// XXX there might be name conflicts - check for those or use
// long names?
FormattedStringFunction.addValue(replacements, newName, var);
});
}
for (Entry<String, Object> replacement : replacements.entrySet()) {
// replace variables
pattern = pattern.replaceAll(Pattern.quote("{" + replacement.getKey() + "}"), "{" + replacement.getValue() + "}");
}
return pattern;
}
use of eu.esdihumboldt.hale.common.align.model.Entity in project hale by halestudio.
the class ClassificationMappingExplanation method getExplanation.
@Override
public String getExplanation(Cell cell, ServiceProvider provider, Locale locale) {
Entity target = CellUtil.getFirstEntity(cell.getTarget());
Entity source = CellUtil.getFirstEntity(cell.getSource());
LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(cell.getTransformationParameters(), provider);
ListMultimap<Value, Value> revLookup = lookup.reverse();
String notClassifiedAction = CellUtil.getFirstParameter(cell, PARAMETER_NOT_CLASSIFIED_ACTION).as(String.class);
if (target != null && source != null) {
StringBuilder mappingString = new StringBuilder();
for (Value targetValue : revLookup.keySet()) {
mappingString.append(quoteValue(targetValue.as(String.class), false));
mappingString.append(' ');
mappingString.append(getMessage("oneOf", locale));
mappingString.append(' ');
int i = 1;
for (Value sourceValue : revLookup.get(targetValue)) {
if (i != 1) {
mappingString.append(", ");
}
mappingString.append(quoteValue(sourceValue.as(String.class), false));
i++;
}
mappingString.append(".\n");
}
String notClassifiedResult = "null";
if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
notClassifiedResult = getMessage("useSource", locale);
} else if (notClassifiedAction != null && notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
notClassifiedResult = quoteText(notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1), false);
}
return MessageFormat.format(getMessage("main", locale), formatEntity(target, false, true, locale), formatEntity(source, false, true, locale), mappingString.toString(), notClassifiedResult);
}
return null;
}
Aggregations