use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceValueLabelProvider method update.
/**
* @see CellLabelProvider#update(ViewerCell)
*/
@Override
public void update(ViewerCell cell) {
TreePath treePath = cell.getViewerRow().getTreePath();
Object element = treePath.getLastSegment();
Definition<?> definition = null;
Object value = ((Pair<?, ?>) element).getSecond();
if (((Pair<?, ?>) element).getFirst() instanceof Definition)
definition = (Definition<?>) ((Pair<?, ?>) element).getFirst();
InstanceValidationReport report = null;
if (definition instanceof ChildDefinition<?>) {
report = validator.validate(value, (ChildDefinition<?>) ((Pair<?, ?>) element).getFirst());
} else if (definition instanceof TypeDefinition) {
report = validator.validate((Instance) value);
}
boolean hasValue = false;
if (value instanceof Instance) {
hasValue = ((Instance) value).getValue() != null;
}
StyledString styledString;
if (value == null) {
styledString = new StyledString("no value", StyledString.DECORATIONS_STYLER);
} else if (value instanceof Group && !hasValue) {
styledString = new StyledString("+", StyledString.QUALIFIER_STYLER);
} else {
if (value instanceof Instance) {
value = ((Instance) value).getValue();
}
// TODO some kind of conversion?
String stringValue = value.toString();
/*
* Values that are very large, e.g. string representations of very
* complex geometries lead to
* StyledCellLabelProvider.updateTextLayout taking a very long time,
* rendering the application unresponsive when the data views are
* displayed. As such, we reduce the string to a maximum size.
*/
if (stringValue.length() > MAX_STRING_LENGTH) {
stringValue = stringValue.substring(0, MAX_STRING_LENGTH) + "...";
}
styledString = new StyledString(stringValue, null);
}
cell.setText(styledString.toString());
cell.setStyleRanges(styledString.getStyleRanges());
if (report != null && !report.getWarnings().isEmpty()) {
cell.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK));
}
super.update(cell);
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class InstanceImportAdvisor method handleResults.
/**
* @see IOAdvisor#handleResults(IOProvider)
*/
@Override
public void handleResults(InstanceReader provider) {
// add instances to instance service
InstanceService is = getService(InstanceService.class);
InstanceCollection instances = provider.getInstances();
ResourceIterator<Instance> it = instances.iterator();
try {
if (!it.hasNext()) {
URI loc = provider.getSource().getLocation();
if (loc != null) {
log.warn(MessageFormat.format("No instances could be imported with the given configuration from {0}", loc.toString()));
} else {
log.warn("No instances could be imported with the given configuration.");
}
}
} finally {
it.close();
}
// apply sampling before adding to the instance service
InstanceViewService ivs = PlatformUI.getWorkbench().getService(InstanceViewService.class);
if (ivs != null) {
instances = ivs.sample(instances);
}
is.addSourceInstances(instances);
super.handleResults(provider);
}
use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.
the class TypeStyleHandler method collectTypesFromSelection.
/**
* Collect all type definitions and data sets from the current selection of
* {@link TypeDefinition}s, {@link EntityDefinition}s, {@link Instance}s and
* {@link InstanceReference}s.
*
* @param event the handler execution event
* @return the collected type definitions
*/
public static SetMultimap<DataSet, TypeDefinition> collectTypesFromSelection(ExecutionEvent event) {
SetMultimap<DataSet, TypeDefinition> types = HashMultimap.create();
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
for (Object object : ((IStructuredSelection) selection).toArray()) {
if (object instanceof TypeDefinition) {
TypeDefinition type = (TypeDefinition) object;
if (!types.containsValue(type)) {
DataSet dataSet = findDataSet(type);
types.put(dataSet, type);
}
}
if (object instanceof EntityDefinition) {
EntityDefinition entityDef = (EntityDefinition) object;
if (entityDef.getPropertyPath().isEmpty()) {
DataSet dataSet = (entityDef.getSchemaSpace() == SchemaSpaceID.SOURCE) ? (DataSet.SOURCE) : (DataSet.TRANSFORMED);
types.put(dataSet, entityDef.getType());
}
}
if (object instanceof InstanceReference) {
InstanceService is = HandlerUtil.getActiveWorkbenchWindow(event).getWorkbench().getService(InstanceService.class);
object = is.getInstance((InstanceReference) object);
}
if (object instanceof Instance) {
Instance instance = (Instance) object;
types.put(instance.getDataSet(), instance.getDefinition());
}
}
}
return types;
}
use of eu.esdihumboldt.hale.common.instance.model.Instance 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.instance.model.Instance 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