use of eu.esdihumboldt.hale.common.align.model.ParameterValue 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.ParameterValue 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.ParameterValue 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.ParameterValue in project hale by halestudio.
the class AssignFromCollector method evaluate.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.lang.String,
* eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
* java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@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 {
// XXX check anchor?
final Collector mainCollector = (Collector) getExecutionContext().getTransformationContext().get(ContextHelpers.KEY_COLLECTOR);
if (mainCollector == null) {
throw new TransformationException("Fatal: No collector has been created yet. Check function priority.");
}
final ParameterValue collectorName = getParameterChecked(PARAMETER_COLLECTOR);
if (collectorName == null || collectorName.isEmpty()) {
throw new TransformationException("Fatal: No collector name was specified.");
}
final Collector collector = mainCollector.getAt(collectorName.getValue().toString());
if (collector == null) {
throw new TransformationException(MessageFormat.format("Error retrieving collector \"{0}\"", collectorName.getValue().toString()));
} else if (collector.values().isEmpty()) {
log.warn(new TransformationMessageImpl(getCell(), MessageFormat.format("Collector \"{0}\" contains no values. If this is unexpected, check the spelling of the collector name and the priority of the transformation function.", collectorName.getStringRepresentation()), null));
}
// Determine where to assign the collected values
final TypeDefinition resultPropertyType = resultProperty.getDefinition().getPropertyType();
final PropertyDefinition targetProperty;
final ResultStrategy resultStrategy;
if (resultPropertyType.getConstraint(HasValueFlag.class).isEnabled()) {
// The result property can take values, therefore assign directly to
// property
targetProperty = resultProperty.getDefinition();
// No instance creation is required in this case
resultStrategy = ResultStrategy.USE_VALUE;
} else {
// Find child element/attribute that can be assigned the reference
targetProperty = Optional.ofNullable(findReferenceChildProperty(resultPropertyType)).orElseThrow(() -> new TransformationException("Fatal: No child property could be found to assign a reference to."));
resultStrategy = ResultStrategy.BUILD_INSTANCE;
}
List<Object> collectedReferences = helper.extractCollectedValues(collector);
// Process collected values if target property is a reference, otherwise
// use plain values
final Function<Object, Object> referenceStrategy;
if (targetProperty.getConstraint(Reference.class).isReference()) {
final Reference referenceConstraint = targetProperty.getConstraint(Reference.class);
// Use the idToReference method to construct the reference
referenceStrategy = referenceConstraint::idToReference;
} else {
referenceStrategy = Function.identity();
}
MultiValue result = new MultiValue();
collectedReferences.forEach(ref -> result.add(resultStrategy.createResult(resultPropertyType, targetProperty, referenceStrategy.apply(ref))));
return result;
}
use of eu.esdihumboldt.hale.common.align.model.ParameterValue 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