use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell in project hale by halestudio.
the class CellBean method createCell.
/**
* Create a cell based on the information in the cell bean if possible.
* Otherwise a corresponding error message should be added to the report.
*
* @param reporter the I/O reporter to report any errors to, may be
* <code>null</code>
* @param sourceTypes the source types to use for resolving definition
* references
* @param targetTypes the target types to use for resolving definition
* references
* @return the created cell or <code>null</code>
*/
public MutableCell createCell(IOReporter reporter, TypeIndex sourceTypes, TypeIndex targetTypes) {
MutableCell cell = new DefaultCell();
cell.setTransformationIdentifier(getTransformationIdentifier());
if (transformationParameters != null && !transformationParameters.isEmpty()) {
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
for (ParameterValueBean param : transformationParameters) {
parameters.put(param.getName(), param.createParameterValue());
}
cell.setTransformationParameters(parameters);
}
cell.setId(id);
try {
cell.setSource(createEntities(source, sourceTypes, SchemaSpaceID.SOURCE));
cell.setTarget(createEntities(target, targetTypes, SchemaSpaceID.TARGET));
} catch (Throwable e) {
reporter.error(new IOMessageImpl("Could not create cell", e));
return null;
}
return cell;
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell 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);
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell in project hale by halestudio.
the class InlineTransformation method evaluate.
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
List<PropertyValue> sources = variables.get(null);
if (sources.isEmpty()) {
throw new NoResultException("No source available to transform");
}
PropertyValue source = sources.get(0);
Object sourceValue = source.getValue();
if (sourceValue == null) {
throw new NoResultException("Source value is null");
}
if (!(sourceValue instanceof Instance)) {
throw new TransformationException("Sources for inline transformation must be instances");
}
Instance sourceInstance = (Instance) sourceValue;
TypeDefinition sourceType = sourceInstance.getDefinition();
// get the original alignment
Alignment orgAlignment = getExecutionContext().getAlignment();
MutableAlignment alignment = new DefaultAlignment(orgAlignment);
// identify relevant type cell(s)
MutableCell queryCell = new DefaultCell();
ListMultimap<String, Type> sourceEntities = ArrayListMultimap.create();
sourceEntities.put(null, new DefaultType(new TypeEntityDefinition(sourceType, SchemaSpaceID.SOURCE, null)));
queryCell.setSource(sourceEntities);
ListMultimap<String, Type> targetEntities = ArrayListMultimap.create();
targetEntities.put(null, new DefaultType(new TypeEntityDefinition(resultProperty.getDefinition().getPropertyType(), SchemaSpaceID.TARGET, null)));
queryCell.setTarget(targetEntities);
Collection<? extends Cell> candidates = alignment.getTypeCells(queryCell);
if (candidates.isEmpty()) {
log.error(log.createMessage("No type transformations found for inline transformation", null));
throw new NoResultException();
}
// filter alignment -> only keep relevant type relations
List<Cell> allTypeCells = new ArrayList<>(alignment.getTypeCells());
for (Cell cell : allTypeCells) {
// remove cell
alignment.removeCell(cell);
if (!cell.getTransformationMode().equals(TransformationMode.disabled)) {
// only readd if not disabled
MutableCell copy = new DefaultCell(cell);
if (candidates.contains(cell)) {
// readd as active
copy.setTransformationMode(TransformationMode.active);
} else {
// readd as passive
copy.setTransformationMode(TransformationMode.passive);
}
alignment.addCell(copy);
}
}
// prepare transformation input/output
DefaultInstanceCollection sourceInstances = new DefaultInstanceCollection();
sourceInstances.add(sourceInstance);
DefaultInstanceSink target = new DefaultInstanceSink();
// run transformation
TransformationService ts = getExecutionContext().getService(TransformationService.class);
if (ts == null) {
throw new TransformationException("Transformation service not available for inline transformation");
}
ProgressIndicator progressIndicator = new LogProgressIndicator();
TransformationReport report = ts.transform(alignment, sourceInstances, new ThreadSafeInstanceSink<InstanceSink>(target), getExecutionContext(), progressIndicator);
// copy report messages
log.importMessages(report);
if (!report.isSuccess()) {
// copy report messages
log.importMessages(report);
throw new TransformationException("Inline transformation failed");
}
// extract result
List<Instance> targetList = target.getInstances();
if (targetList.isEmpty()) {
log.error(log.createMessage("Inline transformation yielded no result", null));
throw new NoResultException("No result from inline transformation");
}
if (targetList.size() > 1) {
log.error(log.createMessage("Inline transformation yielded multiple results, only first result is used", null));
}
return targetList.get(0);
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell in project hale by halestudio.
the class SourceTargetTypeSelector method getSelectedCell.
/**
* Returns the selected cell. If no cell is selected a dummy cell with the
* selected source and target is returned. Both source and target may or may
* not be empty in that case.
*
* @return the selected cell (or a dummy)
*/
public Cell getSelectedCell() {
if (selectedCell != null)
return selectedCell;
DefaultCell cell = new DefaultCell();
if (sourceTypeSelector.getSelectedObject() != null) {
ListMultimap<String, Type> sources = ArrayListMultimap.create(1, 1);
sources.put(null, new DefaultType((TypeEntityDefinition) sourceTypeSelector.getSelectedObject()));
cell.setSource(sources);
}
if (targetTypeSelector.getSelectedObject() != null) {
ListMultimap<String, Type> targets = ArrayListMultimap.create(1, 1);
targets.put(null, new DefaultType((TypeEntityDefinition) targetTypeSelector.getSelectedObject()));
cell.setTarget(targets);
}
return cell;
}
use of eu.esdihumboldt.hale.common.align.model.impl.DefaultCell in project hale by halestudio.
the class AbstractGenericFunctionWizard method getUnfinishedCell.
/**
* Returns the cell that would be created if the wizard would be finished
* now.
*
* @return the cell
*/
public Cell getUnfinishedCell() {
MutableCell current = new DefaultCell();
current.setTransformationIdentifier(getFunctionId());
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
current.setTransformationParameters(parameters);
for (IWizardPage page : getPages()) {
// stop at first uncompleted page
if (!page.isPageComplete())
break;
if (page instanceof FunctionWizardPage)
((FunctionWizardPage) page).configureCell(current);
else if (page instanceof ParameterPage)
parameters.putAll(((ParameterPage) page).getConfiguration());
}
return current;
}
Aggregations