use of eu.esdihumboldt.hale.common.align.model.MutableCell 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.MutableCell in project hale by halestudio.
the class AbstractBaseAlignmentLoader method migrateCells.
private List<MutableCell> migrateCells(List<MutableCell> cells, SimpleLog log) {
List<MutableCell> result = new ArrayList<>();
// Collect mappings from all UnmigratedCells
Map<EntityDefinition, EntityDefinition> allMappings = new HashMap<>();
cells.stream().filter(c -> c instanceof UnmigratedCell).map(c -> (UnmigratedCell) c).forEach(uc -> allMappings.putAll(uc.getEntityMappings()));
// Add cells to the alignment, migrate UnmigratedCells
for (MutableCell cell : cells) {
if (cell instanceof UnmigratedCell) {
result.add(((UnmigratedCell) cell).migrate(allMappings, log));
} else {
result.add(cell);
}
}
return result;
}
use of eu.esdihumboldt.hale.common.align.model.MutableCell 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.MutableCell 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.MutableCell in project hale by halestudio.
the class FunctionWizardUtil method createNewWizard.
/**
* Open a wizard that creates a new relation with the given ID and adds the
* result to the alignment.
*
* @param functionId the function identifier
* @param elements the schema selection defining the initial selection or
* <code>null</code>
* @return the created cell or <code>null</code>
*/
public static Cell createNewWizard(String functionId, SchemaSelection elements) {
FunctionWizardDescriptor<?> desc = FunctionWizardExtension.getInstance().getWizardDescriptor(functionId);
FunctionWizard wizard = desc.createNewWizard(elements);
if (wizard != null) {
// initialize the wizard
wizard.init();
HaleWizardDialog dialog = new HaleWizardDialog(Display.getCurrent().getActiveShell(), wizard);
if (dialog.open() == WizardDialog.OK) {
MutableCell cell = wizard.getResult();
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
as.addCell(cell);
return cell;
}
}
return null;
}
Aggregations