use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class EditMappableTypesPage method createControl.
/**
* @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createControl(Composite parent) {
parent.setLayout(new GridLayout());
// create filtered tree
PatternFilter patternFilter = new PatternFilter();
patternFilter.setIncludeLeadingWildcard(true);
FilteredTree tree = new FilteredTree(parent, SWT.BORDER | SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL, patternFilter, true) {
@Override
protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
return new CheckboxTreeViewer(parent, style);
}
};
// configure viewer
viewer = (CheckboxTreeViewer) tree.getViewer();
contentProvider = new NSTypeTreeContentProvider();
viewer.setContentProvider(contentProvider);
viewer.setComparator(new DefinitionComparator());
viewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object doubleClicked = selection.getFirstElement();
if (doubleClicked instanceof String)
viewer.setExpandedState(doubleClicked, !viewer.getExpandedState(doubleClicked));
else {
boolean newState = !checkStateProvider.isChecked(doubleClicked);
viewer.setChecked(doubleClicked, newState);
checkStateOfTypeChanged((TypeDefinition) doubleClicked, newState);
}
}
});
viewer.setLabelProvider(definitionLabels);
// because elements filtered by FilteredTree lose their checked state:
checkStateProvider = new ICheckStateProvider() {
@Override
public boolean isGrayed(Object element) {
if (element instanceof String) {
Object[] children = contentProvider.getChildren(element);
boolean containsChecked = false;
boolean containsUnchecked = false;
for (Object child : children) {
if (isChecked(child))
containsChecked = true;
else
containsUnchecked = true;
if (containsChecked && containsUnchecked)
return true;
}
}
return false;
}
@Override
public boolean isChecked(Object element) {
if (element instanceof String) {
for (Object child : contentProvider.getChildren(element)) if (isChecked(child))
return true;
return false;
}
return ((TypeDefinition) element).getConstraint(MappingRelevantFlag.class).isEnabled() != changedTypes.contains(element);
}
};
viewer.setCheckStateProvider(checkStateProvider);
viewer.addCheckStateListener(new ICheckStateListener() {
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
if (event.getElement() instanceof String) {
// update children
viewer.setGrayed(event.getElement(), false);
for (Object child : contentProvider.getChildren(event.getElement())) {
// if (mappedTypeFilter.select(viewer, event.getElement(), child)) {
if (checkStateProvider.isChecked(child) != event.getChecked()) {
viewer.setChecked(child, event.getChecked());
checkStateOfTypeChanged((TypeDefinition) child, event.getChecked());
}
// }
}
viewer.setGrayed(event.getElement(), checkStateProvider.isGrayed(event.getElement()));
// only two levels, no need to update any parents or
// children's children
} else {
// if (mappedTypeFilter.select(viewer, null, event.getElement()))
checkStateOfTypeChanged((TypeDefinition) event.getElement(), event.getChecked());
// else if (!event.getChecked())
// viewer.setChecked(event.getElement(), true);
}
}
});
// // filter types which are used in the current alignment
// viewer.addFilter(mappedTypeFilter);
// set input to all types in the given index
viewer.setInput(typeIndex.getTypes());
// expand all except XMLSchema
viewer.expandAll();
viewer.setExpandedState("http://www.w3.org/2001/XMLSchema", false);
// set control
setControl(viewer.getControl());
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class NSTypeTreeContentProvider method getElements.
/**
* @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
*/
@Override
public Object[] getElements(Object inputElement) {
if (inputElement == null) {
nameSpaces = null;
return new Object[0];
}
@SuppressWarnings("unchecked") Collection<? extends TypeDefinition> types = (Collection<? extends TypeDefinition>) inputElement;
nameSpaces = ArrayListMultimap.create();
for (TypeDefinition type : types) {
// only show types with mappable flag
if (!type.getConstraint(MappableFlag.class).isEnabled())
continue;
String ns = type.getName().getNamespaceURI();
if (XMLConstants.NULL_NS_URI.equals(ns))
ns = "(no namespace)";
nameSpaces.put(ns, type);
}
return nameSpaces.keySet().toArray();
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class Retype method execute.
@Override
public void execute(String transformationIdentifier, TransformationEngine engine, Map<String, String> executionParameters, TransformationLog log, Cell cell) {
// for each source instance create a target instance
TypeDefinition targetType = getTarget().values().iterator().next().getDefinition().getDefinition();
MutableInstance target = null;
// structural rename
boolean structuralRename = getOptionalParameter(RenameFunction.PARAMETER_STRUCTURAL_RENAME, Value.of(false)).as(Boolean.class, false);
if (structuralRename) {
boolean ignoreNamespaces = getOptionalParameter(RenameFunction.PARAMETER_IGNORE_NAMESPACES, Value.of(false)).as(Boolean.class, false);
boolean copyGeometries = getOptionalParameter(RenameFunction.PARAMETER_COPY_GEOMETRIES, Value.of(true)).as(Boolean.class);
target = doStructuralRename(getSource(), targetType, ignoreNamespaces, copyGeometries, log);
}
if (target == null) {
target = getInstanceFactory().createInstance(targetType);
}
getPropertyTransformer().publish(getSource(), target, log, cell);
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class IndexMergeHandler method merge.
private Instance merge(InstanceCollection instances, IndexMergeConfig mergeConfig) {
TypeDefinition type;
try (ResourceIterator<Instance> it = instances.iterator()) {
if (instances.hasSize() && instances.size() == 1) {
// early exit if only one instance to merge
return it.next();
} else {
type = it.next().getDefinition();
}
}
MutableInstance result = getInstanceFactory().createInstance(type);
/*
* FIXME This a first VERY basic implementation, where only the first
* item in each property path is regarded, and that whole tree is added
* only once (from the first instance). XXX This especially will be a
* problem, if a path contains a choice. XXX For more advanced stuff we
* need more advanced test cases.
*/
Set<QName> rootNames = new HashSet<QName>();
Set<QName> nonKeyRootNames = new HashSet<QName>();
// collect path roots
for (List<QName> path : mergeConfig.keyProperties) {
rootNames.add(path.get(0));
}
for (List<QName> path : mergeConfig.additionalProperties) {
nonKeyRootNames.add(path.get(0));
}
// XXX what about metadata?!
// XXX for now only retain IDs
Set<Object> ids = new HashSet<Object>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance instance = it.next();
for (QName name : instance.getPropertyNames()) {
if (rootNames.contains(name)) {
/*
* Property is merge key -> only use first occurrence
* (as all entries need to be the same)
*
* TODO adapt if multiple keys are possible per instance
*/
addFirstOccurrence(result, instance, name);
} else if (nonKeyRootNames.contains(name)) {
/*
* Property is additional merge property.
*
* Traditional behavior: Only keep unique values.
*
* XXX should this be configurable?
*/
addUnique(result, instance, name);
} else if (mergeConfig.autoDetect) {
/*
* Auto-detection is enabled.
*
* Only keep unique values.
*
* XXX This differs from the traditional behavior in
* that there only the first value would be used, but
* only if all values were equal. That cannot be easily
* checked in an iterative approach.
*/
addUnique(result, instance, name);
} else {
/*
* Property is not to be merged.
*
* XXX but we could do some kind of aggregation
*
* XXX for now just add all values
*/
addValues(result, instance, name);
}
}
List<Object> instanceIDs = instance.getMetaData(InstanceMetadata.METADATA_ID);
for (Object id : instanceIDs) {
ids.add(id);
}
}
}
// store metadata IDs
result.setMetaData(InstanceMetadata.METADATA_ID, ids.toArray());
return result;
}
use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.
the class NetworkExpansion method evaluate.
/**
* @see AbstractSingleTargetScriptedPropertyTransformation#evaluate(String,
* TransformationEngine, ListMultimap, String,
* PropertyEntityDefinition, Map, 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 {
// get the buffer width parameter
String bufferWidthString = getTransformedParameterChecked(PARAMETER_BUFFER_WIDTH).as(String.class);
double bufferWidth;
try {
bufferWidth = Double.parseDouble(bufferWidthString);
} catch (NumberFormatException e) {
// For backwards compatibility try to run the string as script.
MathScript mathScript = new MathScript();
try {
Object result = mathScript.evaluate(bufferWidthString, variables.get(ENTITY_VARIABLE), getExecutionContext());
bufferWidth = ConversionUtil.getAs(result, Double.class);
} catch (ScriptException e1) {
throw new TransformationException("Failed to evaluate buffer width expression.", e1);
} catch (ConversionException e2) {
throw new TransformationException("Failed to convert buffer width expression result to double.", e2);
}
}
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
if (inputValue instanceof Instance) {
inputValue = ((Instance) inputValue).getValue();
}
GeometryProperty<Geometry> result = calculateBuffer(inputValue, bufferWidth, log);
// try to yield a result compatible to the target
if (result != null) {
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// TODO check element type?
Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getGeometry().getClass())) {
return result.getGeometry();
} else {
return result;
}
}
throw new TransformationException("Geometry for network expansion could not be retrieved.");
}
Aggregations