use of eu.esdihumboldt.hale.common.align.migrate.CellMigrator in project hale by halestudio.
the class AbstractMergeCellMigratorTest method mergeWithMigrator.
/**
* Perform merging using a specific merge migrator.
*
* @param migrator the migrator to test, <code>null</code> if the migrator
* configured in the system should be used
* @param cellToMigrate the cell to migrate
* @param matchingProject the project providing the matching information
* @return the merge result
*/
protected List<MutableCell> mergeWithMigrator(MergeCellMigrator migrator, Cell cellToMigrate, ProjectTransformationEnvironment matchingProject) {
MergeIndex mergeIndex = new TargetIndex(matchingProject.getAlignment());
AlignmentMigration migration = new MatchingMigration(matchingProject, true);
List<MutableCell> cells = new ArrayList<>();
if (migrator == null) {
try {
migrator = MigratorExtension.getInstance().getMigrator(cellToMigrate.getTransformationIdentifier()).orElse(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (migrator == null) {
CellMigrator mig = getCellMigrator(cellToMigrate.getTransformationIdentifier());
if (mig instanceof MergeCellMigrator) {
migrator = (MergeCellMigrator) mig;
} else if (mig == null) {
throw new IllegalStateException("No cell migrator could be retrieved");
} else {
// perform migration with "ordinary" CellMigrator
MigrationOptions options = new MigrationOptions() {
@Override
public boolean updateTarget() {
return false;
}
@Override
public boolean updateSource() {
return true;
}
@Override
public boolean transferBase() {
return false;
}
};
cells.add(mig.updateCell(cellToMigrate, migration, options, SimpleLog.CONSOLE_LOG));
}
}
if (migrator != null) {
// perform merge with MergeCellMigrator
Iterable<MutableCell> result = migrator.mergeCell(cellToMigrate, mergeIndex, migration, this::getCellMigrator, SimpleLog.CONSOLE_LOG);
Iterables.addAll(cells, result);
}
return cells;
}
use of eu.esdihumboldt.hale.common.align.migrate.CellMigrator in project hale by halestudio.
the class DefaultAlignmentMigrator method updateAligmment.
@Override
public MutableAlignment updateAligmment(Alignment originalAlignment, AlignmentMigration migration, MigrationOptions options, SimpleLog log) {
MutableAlignment result = new DefaultAlignment(originalAlignment);
// XXX TODO adapt custom functions?!
// result.getCustomPropertyFunctions();
Collection<? extends Cell> cellList = new ArrayList<>(result.getCells());
for (Cell cell : cellList) {
// XXX
if (cell instanceof MutableCell) {
CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
MutableCell newCell = cm.updateCell(cell, migration, options, log);
MigrationUtil.removeIdPrefix(newCell, options.transferBase(), options.transferBase());
result.removeCell(cell);
if (newCell != null) {
result.addCell(newCell);
}
} else {
// XXX can we deal with other cases? (Base alignment cells)
if (options.transferBase()) {
// include base alignment cell as mutable mapping cell
CellMigrator cm = getCellMigrator(cell.getTransformationIdentifier());
MutableCell newCell = cm.updateCell(cell, migration, options, log);
MigrationUtil.removeIdPrefix(newCell, true, true);
result.removeCell(cell);
if (newCell != null) {
result.addCell(newCell);
}
}
}
}
if (options.transferBase()) {
MigrationUtil.removeBaseCells(result);
} else {
// does something need to be done to correctly retain base
// alignments?
}
return result;
}
use of eu.esdihumboldt.hale.common.align.migrate.CellMigrator in project hale by halestudio.
the class AbstractFunction method getCustomMigrator.
@Override
public Optional<CellMigrator> getCustomMigrator() {
if (customMigrator != null) {
return customMigrator;
}
if (conf.getAttribute("cellMigrator") == null || conf.getAttribute("cellMigrator").isEmpty()) {
customMigrator = Optional.empty();
return customMigrator;
}
try {
CellMigrator migrator = (CellMigrator) conf.createExecutableExtension("cellMigrator");
customMigrator = Optional.ofNullable(migrator);
} catch (CoreException e) {
customMigrator = Optional.empty();
log.error("Could not create custom cell migrator for function", e);
}
return customMigrator;
}
use of eu.esdihumboldt.hale.common.align.migrate.CellMigrator in project hale by halestudio.
the class JaxbToAlignment method createUnmigratedCell.
private static UnmigratedCell createUnmigratedCell(CellType cell, LoadAlignmentContext context, IOReporter reporter, EntityResolver resolver, ServiceProvider serviceProvider) {
// The sourceCell represents the cell as it was imported from the
// XML alignment. The conversion to the resolved cell must be performed
// later by migrating the UnmigratedCell returned from this function.
final DefaultCell sourceCell = new DefaultCell();
sourceCell.setTransformationIdentifier(cell.getRelation());
final FunctionDefinition<?> cellFunction = FunctionUtil.getFunction(sourceCell.getTransformationIdentifier(), serviceProvider);
final CellMigrator migrator;
if (cellFunction != null) {
migrator = cellFunction.getCustomMigrator().orElse(new DefaultCellMigrator());
} else {
migrator = new DefaultCellMigrator();
}
Map<EntityDefinition, EntityDefinition> mappings = new HashMap<>();
try {
// The returned Entity pair consists of
// (1st) a dummy entity representing the entity read from JAXB
// (2nd) the resolved entity
ListMultimap<String, Pair<Entity, Entity>> convertedSourceEntities = convertEntities(cell.getSource(), context.getSourceTypes(), SchemaSpaceID.SOURCE, resolver);
if (convertedSourceEntities == null) {
sourceCell.setSource(null);
} else {
sourceCell.setSource(Multimaps.transformValues(convertedSourceEntities, pair -> pair.getFirst()));
for (Pair<Entity, Entity> pair : convertedSourceEntities.values()) {
mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
}
}
ListMultimap<String, Pair<Entity, Entity>> convertedTargetEntities = convertEntities(cell.getTarget(), context.getTargetTypes(), SchemaSpaceID.TARGET, resolver);
if (convertedTargetEntities == null) {
sourceCell.setTarget(null);
} else {
sourceCell.setTarget(Multimaps.transformValues(convertedTargetEntities, pair -> pair.getFirst()));
for (Pair<Entity, Entity> pair : convertedTargetEntities.values()) {
mappings.put(pair.getFirst().getDefinition(), pair.getSecond().getDefinition());
}
}
if (sourceCell.getTarget() == null || sourceCell.getTarget().isEmpty()) {
// target is mandatory for cells!
throw new IllegalStateException("Cannot create cell without target");
}
} catch (Exception e) {
if (reporter != null) {
reporter.error(new IOMessageImpl("Could not create cell", e));
}
return null;
}
if (!cell.getAbstractParameter().isEmpty()) {
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
for (JAXBElement<? extends AbstractParameterType> param : cell.getAbstractParameter()) {
AbstractParameterType apt = param.getValue();
if (apt instanceof ParameterType) {
// treat string parameters or null parameters
ParameterType pt = (ParameterType) apt;
ParameterValue pv = new ParameterValue(pt.getType(), Value.of(pt.getValue()));
parameters.put(pt.getName(), pv);
} else if (apt instanceof ComplexParameterType) {
// complex parameters
ComplexParameterType cpt = (ComplexParameterType) apt;
parameters.put(cpt.getName(), new ParameterValue(new ElementValue(cpt.getAny(), context)));
} else
throw new IllegalStateException("Illegal parameter type");
}
sourceCell.setTransformationParameters(parameters);
}
// annotations & documentation
for (Object element : cell.getDocumentationOrAnnotation()) {
if (element instanceof AnnotationType) {
// add annotation to the cell
AnnotationType annot = (AnnotationType) element;
// but first load it from the DOM
AnnotationDescriptor<?> desc = AnnotationExtension.getInstance().get(annot.getType());
if (desc != null) {
try {
Object value = desc.fromDOM(annot.getAny(), null);
sourceCell.addAnnotation(annot.getType(), value);
} catch (Exception e) {
if (reporter != null) {
reporter.error(new IOMessageImpl("Error loading cell annotation", e));
} else
throw new IllegalStateException("Error loading cell annotation", e);
}
} else
reporter.error(new IOMessageImpl("Cell annotation of type {0} unknown, cannot load the annotation object", null, -1, -1, annot.getType()));
} else if (element instanceof DocumentationType) {
// add documentation to the cell
DocumentationType doc = (DocumentationType) element;
sourceCell.getDocumentation().put(doc.getType(), doc.getValue());
}
}
sourceCell.setId(cell.getId());
// a default value is assured for priority
String priorityStr = cell.getPriority().value();
Priority priority = Priority.fromValue(priorityStr);
if (priority != null) {
sourceCell.setPriority(priority);
} else {
// used.
throw new IllegalArgumentException();
}
return new UnmigratedCell(sourceCell, migrator, mappings);
}
Aggregations