use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GeographicalName method evaluate.
/**
* @see AbstractSingleTargetPropertyTransformation#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 {
// list of all source properties
List<PropertyValue> inputs = variables.get(null);
if (inputs.isEmpty()) {
// no input, so don't create any structure
throw new NoResultException();
}
// get all parameters defined by the wizard page
String ipa = getParameterChecked(PROPERTY_PRONUNCIATIONIPA).as(String.class);
// we need a default value and a try/catch-block because in older
// version we couldn't edit the pronunciationSoundLink text field
String sound = "";
try {
sound = getParameterChecked(PROPERTY_PRONUNCIATIONSOUNDLINK).as(String.class);
} catch (Exception e) {
// do nothing
}
String language = getParameterChecked(PROPERTY_LANGUAGE).as(String.class);
String sourceOfName = getParameterChecked(PROPERTY_SOURCEOFNAME).as(String.class);
String nameStatus = getParameterChecked(PROPERTY_NAMESTATUS).as(String.class);
String nativeness = getParameterChecked(PROPERTY_NATIVENESS).as(String.class);
String gender = getParameterChecked(PROPERTY_GRAMMA_GENDER).as(String.class);
String number = getParameterChecked(PROPERTY_GRAMMA_NUMBER).as(String.class);
// get the script and transliteration parameters
// should have the same order like source properties
ListMultimap<String, ParameterValue> params = getParameters();
List<ParameterValue> scripts = params.get(PROPERTY_SCRIPT);
List<ParameterValue> trans = params.get(PROPERTY_TRANSLITERATION);
if (inputs.size() != scripts.size() || inputs.size() != trans.size()) {
throw new TransformationException("Number of inputs does not match number of configured spellings, can't determine script and transliteration of spellings.");
// XXX could this be dealt with if the property "text" would be used
// again for this purpose?
}
// definition of the target property
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// instance that can be changed (add property/instance as child)
DefaultInstance targetInstance = new DefaultInstance(targetType, null);
// search for the child named "GeographicalName"
PropertyDefinition targetChildGeoName = Util.getChild("GeographicalName", targetType);
// get type definition to create the "GeographicalName" instance
TypeDefinition geoType = targetChildGeoName.getPropertyType();
// name/GeographicalName/
DefaultInstance geoInstance = new DefaultInstance(geoType, null);
targetInstance.addProperty(targetChildGeoName.getName(), geoInstance);
// name/GeographicalName/grammaticalGender/
if (gender != null && !gender.isEmpty()) {
PropertyDefinition geoChildGramGender = Util.getChild("grammaticalGender", geoType);
TypeDefinition grammarGenderType = geoChildGramGender.getPropertyType();
DefaultInstance grammarGenderInst = new DefaultInstance(grammarGenderType, null);
grammarGenderInst.setValue(gender);
geoInstance.addProperty(geoChildGramGender.getName(), grammarGenderInst);
}
// name/GeographicalName/grammaticalNumber
if (number != null && !number.isEmpty()) {
PropertyDefinition geoChildGramNumber = Util.getChild("grammaticalNumber", geoType);
TypeDefinition grammarNumberType = geoChildGramNumber.getPropertyType();
DefaultInstance grammarNumberInst = new DefaultInstance(grammarNumberType, null);
// set value of the grammaticalNumber instance
grammarNumberInst.setValue(number);
geoInstance.addProperty(geoChildGramNumber.getName(), grammarNumberInst);
}
// name/GeographicalName/language
if (language != null && !language.isEmpty()) {
PropertyDefinition geoChildLanguage = Util.getChild("language", geoType);
TypeDefinition languageType = geoChildLanguage.getPropertyType();
DefaultInstance languageInstance = new DefaultInstance(languageType, null);
// set value of the language instance
languageInstance.setValue(language);
geoInstance.addProperty(geoChildLanguage.getName(), languageInstance);
}
// name/GeographicalName/nameStatus
if (nameStatus != null && !nameStatus.isEmpty()) {
PropertyDefinition geoChildNameStatus = Util.getChild("nameStatus", geoType);
TypeDefinition nameStatusType = geoChildNameStatus.getPropertyType();
DefaultInstance nameStatusInstance = new DefaultInstance(nameStatusType, null);
// set value of the nameStatus instance
nameStatusInstance.setValue(nameStatus);
geoInstance.addProperty(geoChildNameStatus.getName(), nameStatusInstance);
}
// name/GeographicalName/nativeness
if (nativeness != null && !nativeness.isEmpty()) {
PropertyDefinition geoChildNativeness = Util.getChild("nativeness", geoType);
TypeDefinition nativenessType = geoChildNativeness.getPropertyType();
DefaultInstance nativenessInstance = new DefaultInstance(nativenessType, null);
// set value of the nativeness instance
nativenessInstance.setValue(nativeness);
geoInstance.addProperty(geoChildNativeness.getName(), nativenessInstance);
}
if ((ipa != null && !ipa.isEmpty()) || (sound != null && !sound.isEmpty())) {
// name/GeographicalName/pronunciation
PropertyDefinition geoChildPronun = Util.getChild("pronunciation", geoType);
TypeDefinition pronunType = geoChildPronun.getPropertyType();
DefaultInstance pronunInstance = new DefaultInstance(pronunType, null);
geoInstance.addProperty(geoChildPronun.getName(), pronunInstance);
// name/GeographicalName/pronunciation/PronunciationOfName
PropertyDefinition pronunChildPronOfName = Util.getChild("PronunciationOfName", pronunType);
TypeDefinition pronOfNameType = pronunChildPronOfName.getPropertyType();
DefaultInstance pronOfNameInst = new DefaultInstance(pronOfNameType, null);
pronunInstance.addProperty(pronunChildPronOfName.getName(), pronOfNameInst);
if (ipa != null && !ipa.isEmpty()) {
// name/GeographicalName/pronunciation/PronunciationOfName/pronunciationIPA
PropertyDefinition pronOfNameChildIPA = Util.getChild("pronunciationIPA", pronOfNameType);
TypeDefinition pronunIpaType = pronOfNameChildIPA.getPropertyType();
DefaultInstance pronunIpaInstance = new DefaultInstance(pronunIpaType, null);
pronunIpaInstance.setValue(ipa);
pronOfNameInst.addProperty(pronOfNameChildIPA.getName(), pronunIpaInstance);
}
if (sound != null && !sound.isEmpty()) {
// name/GeographicalName/pronunciation/PronunciationOfName/pronunciationSoundLink
PropertyDefinition pronOfNameChildSound = Util.getChild("pronunciationSoundLink", pronOfNameType);
TypeDefinition pronunSoundType = pronOfNameChildSound.getPropertyType();
DefaultInstance pronunSoundInstance = new DefaultInstance(pronunSoundType, null);
pronunSoundInstance.setValue(sound);
pronOfNameInst.addProperty(pronOfNameChildSound.getName(), pronunSoundInstance);
}
}
// name/GeographicalName/sourceOfName
if (sourceOfName != null && !sourceOfName.isEmpty()) {
PropertyDefinition geoChildSource = Util.getChild("sourceOfName", geoType);
TypeDefinition sourceType = geoChildSource.getPropertyType();
DefaultInstance sourceInstance = new DefaultInstance(sourceType, null);
// set value of the sourceOfName instance
sourceInstance.setValue(sourceOfName);
geoInstance.addProperty(geoChildSource.getName(), sourceInstance);
}
// name/GeographicalName/spelling
PropertyDefinition geoChildSpelling = Util.getChild("spelling", geoType);
TypeDefinition spellingType = geoChildSpelling.getPropertyType();
// name/GeographicalName/spelling/SpellingOfName
PropertyDefinition spellingChildSpellOfName = Util.getChild("SpellingOfName", spellingType);
TypeDefinition spellOfNameType = spellingChildSpellOfName.getPropertyType();
// create a "spelling" instance for each spelling
for (int i = 0; i < scripts.size(); i++) {
DefaultInstance spellingInstance = new DefaultInstance(spellingType, null);
DefaultInstance spellOfNameInst = new DefaultInstance(spellOfNameType, null);
// name/GeographicalName/spelling/SpellingOfName/script
PropertyDefinition spellOfNameChildScript = Util.getChild("script", spellOfNameType);
TypeDefinition scriptType = spellOfNameChildScript.getPropertyType();
DefaultInstance scriptInstance = new DefaultInstance(scriptType, null);
// name/GeographicalName/spelling/SpellingOfName/text
PropertyDefinition spellOfNameChildText = Util.getChild("text", spellOfNameType);
// name/GeographicalName/spelling/SpellingOfName/transliterationScheme
PropertyDefinition spellOfNameChildTransliteration = Util.getChild("transliterationScheme", spellOfNameType);
TypeDefinition transliterationType = spellOfNameChildTransliteration.getPropertyType();
DefaultInstance transliterationInstance = new DefaultInstance(transliterationType, null);
// build the spelling instance
scriptInstance.setValue(scripts.get(i).as(String.class));
transliterationInstance.setValue(trans.get(i).as(String.class));
spellOfNameInst.addProperty(spellOfNameChildScript.getName(), scriptInstance);
// set text value from inputs
spellOfNameInst.addProperty(spellOfNameChildText.getName(), inputs.get(i).getValue());
spellOfNameInst.addProperty(spellOfNameChildTransliteration.getName(), transliterationInstance);
spellingInstance.addProperty(spellingChildSpellOfName.getName(), spellOfNameInst);
geoInstance.addProperty(geoChildSpelling.getName(), spellingInstance);
}
return targetInstance;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class ClassificationMapping 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 {
String source = variables.values().iterator().next().getValueAs(String.class);
LookupTable lookup = ClassificationMappingUtil.getClassificationLookup(getParameters(), getExecutionContext());
if (lookup == null) {
// throw new TransformationException("No classification specified");
log.warn(log.createMessage("No classification specified", null));
} else {
Value target = lookup.lookup(Value.of(source));
if (target != null) {
// return value w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(target);
}
}
String notClassifiedAction = getOptionalParameter(PARAMETER_NOT_CLASSIFIED_ACTION, Value.of(USE_NULL_ACTION)).as(String.class);
if (USE_SOURCE_ACTION.equals(notClassifiedAction)) {
return source;
} else if (notClassifiedAction.startsWith(USE_FIXED_VALUE_ACTION_PREFIX)) {
String notClassified = notClassifiedAction.substring(notClassifiedAction.indexOf(':') + 1);
// return w/ transformation variables replaced
return getExecutionContext().getVariables().replaceVariables(notClassified);
} else {
// return null;
throw new NoResultException();
}
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class GroovyTransformationPage method validate.
@Override
protected boolean validate(String document) {
super.validate(document);
List<PropertyValue> values = new ArrayList<PropertyValue>();
for (EntityDefinition var : getVariables()) {
if (var instanceof PropertyEntityDefinition) {
PropertyEntityDefinition property = (PropertyEntityDefinition) var;
values.add(new PropertyValueImpl(testValues.get(property), property));
}
}
Property targetProperty = (Property) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
if (targetProperty == null) {
// not yet selected (NewRelationWizard)
return false;
}
InstanceBuilder builder = GroovyTransformation.createBuilder(targetProperty.getDefinition());
Cell cell = getWizard().getUnfinishedCell();
boolean useInstanceValues = CellUtil.getOptionalParameter(cell, GroovyTransformation.PARAM_INSTANCE_VARIABLES, Value.of(false)).as(Boolean.class);
AlignmentService as = PlatformUI.getWorkbench().getService(AlignmentService.class);
GroovyService gs = HaleUI.getServiceProvider().getService(GroovyService.class);
Script script = null;
try {
Collection<? extends Cell> typeCells = as.getAlignment().getTypeCells(cell);
// select one matching type cell, the script has to run for all
// matching cells
// if there is no matching cell it may produce a npe, which is okay
Cell typeCell = null;
if (!typeCells.isEmpty()) {
typeCell = typeCells.iterator().next();
}
CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
groovy.lang.Binding binding;
if (cell.getTransformationIdentifier().equals(GroovyGreedyTransformation.ID)) {
binding = GroovyGreedyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
} else {
binding = GroovyTransformation.createGroovyBinding(values, null, cell, typeCell, builder, useInstanceValues, log, context, targetProperty.getDefinition().getDefinition().getPropertyType());
}
script = gs.parseScript(document, binding);
GroovyTransformation.evaluate(script, builder, targetProperty.getDefinition().getDefinition().getPropertyType(), gs, log);
} catch (NoResultException e) {
// continue
} catch (final Exception e) {
return handleValidationResult(script, e);
}
return handleValidationResult(script, null);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class ReprojectGeometry 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 {
// Get input geometry
PropertyValue input = variables.get("source").get(0);
Object inputValue = input.getValue();
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry sourceGeometry = geoms.get(0).getGeometry();
CRSDefinition crsDef = geoms.get(0).getCRSDefinition();
if (crsDef == null) {
throw new TransformationException("Geometry does not have an associated Coordinate Reference System");
}
CoordinateReferenceSystem sourceCRS = crsDef.getCRS();
Geometry resultGeometry = sourceGeometry;
CoordinateReferenceSystem targetCRS = sourceCRS;
// Get input parameter
String srs = getParameterChecked(PARAMETER_REFERENCE_SYSTEM).as(String.class);
if (srs != null) {
try {
targetCRS = parseReferenceSystemParamter(srs);
} catch (Exception e) {
throw new TransformationException("Error determining destination Cordinate Reference System.", e);
}
// Retrieve transformation from cell context, or create a new
// instance
Map<Object, Object> cellContext = getExecutionContext().getCellContext();
MathTransform transform = getOrCreateMathTransform(sourceCRS, targetCRS, cellContext);
// Apply transformation
try {
resultGeometry = JTS.transform(sourceGeometry, transform);
} catch (MismatchedDimensionException | TransformException e) {
throw new TransformationException("Problem on execute transformation from: " + sourceCRS + " to " + targetCRS, e);
}
}
return new DefaultGeometryProperty<Geometry>(new CodeDefinition(CRS.toSRS(targetCRS), targetCRS), resultGeometry);
}
use of eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException in project hale by halestudio.
the class AggregateTransformation method aggregateGeometries.
/**
* Aggregates geometries contained in the provided objects.
*
* @param geometries the geometries or instances containing geometries
* @param cell the currently process cell or <code>null</code>
* @param log the transformation log or <code>null</code>
* @return the aggregated geometry
* @throws TransformationException if source geometries don't have a common
* CRS
* @throws NoResultException if the result extent would be <code>null</code>
*/
public static GeometryProperty<?> aggregateGeometries(Iterable<?> geometries, @Nullable TransformationLog log, @Nullable Cell cell) throws NoResultException, TransformationException {
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
CRSDefinition commonCrs = null;
Class<? extends Geometry> commonGeometryType = null;
List<Geometry> collectedGeometries = new ArrayList<>();
for (Object value : geometries) {
// find contained geometries
traverser.traverse(value, geoFind);
for (GeometryProperty<?> geom : geoFind.getGeometries()) {
// no CRS or one common CRS is OK
if (commonCrs == null) {
commonCrs = geom.getCRSDefinition();
} else {
if (geom.getCRSDefinition() != null && !geom.getCRSDefinition().equals(commonCrs)) {
// CRS doesn't match
throw new TransformationException("Source geometries don't have a common CRS.");
}
}
Geometry g = geom.getGeometry();
// determine common geometry type: point / line / polygon
if (commonGeometryType == null) {
commonGeometryType = getContainedGeometryType(g.getClass());
} else {
Class<? extends Geometry> currentType = getContainedGeometryType(g.getClass());
if (!commonGeometryType.isAssignableFrom(currentType)) {
if (currentType.isAssignableFrom(commonGeometryType)) {
commonGeometryType = currentType;
} else {
commonGeometryType = Geometry.class;
}
}
}
// collect geometry
for (int i = 0; i < g.getNumGeometries(); i++) {
collectedGeometries.add(g.getGeometryN(i));
}
}
geoFind.reset();
}
if (commonGeometryType != null && commonGeometryType.equals(Geometry.class)) {
if (log != null && cell != null) {
log.warn(new TransformationMessageImpl(cell, "Could not find common geometry type for aggregation", null));
}
}
if (commonGeometryType != null) {
Geometry combined = combineGeometries(collectedGeometries, commonGeometryType);
return new DefaultGeometryProperty<Geometry>(commonCrs, combined);
}
throw new NoResultException();
}
Aggregations