use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class InteriorPoint 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(null).get(0);
Object inputValue = input.getValue();
GeometryProperty<?> result = calculateInteriorPoint(inputValue);
// try to yield a result compatible to the target
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// TODO check element type?
Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getClass())) {
return result.getGeometry();
}
return result;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class GroovyGreedyTransformation method createGroovyBinding.
/**
* Create a Groovy binding from the list of variables.
*
* @param vars the variable values
* @param varDefs definition of the assigned variables, in case some
* variable values are not set, may be <code>null</code>
* @param cell the cell the binding is created for
* @param typeCell the type cell the binding is created for, may be
* <code>null</code>
* @param builder the instance builder for creating target instances, or
* <code>null</code> if not applicable
* @param useInstanceVariables if instances should be used as variables for
* the binding instead of extracting the instance values
* @param log the transformation log
* @param context the execution context
* @param targetInstanceType the type of the target instance
* @return the binding for use with {@link GroovyShell}
*/
public static Binding createGroovyBinding(List<PropertyValue> vars, List<? extends Entity> varDefs, Cell cell, Cell typeCell, InstanceBuilder builder, boolean useInstanceVariables, TransformationLog log, ExecutionContext context, TypeDefinition targetInstanceType) {
Binding binding = GroovyUtil.createBinding(builder, cell, typeCell, log, context, targetInstanceType);
// collect definitions to check if all were provided
Set<EntityDefinition> notDefined = new HashSet<>();
if (varDefs != null) {
for (Entity entity : varDefs) {
notDefined.add(entity.getDefinition());
}
}
// keep only defs where no value is provided
if (!notDefined.isEmpty()) {
for (PropertyValue var : vars) {
notDefined.remove(var.getProperty());
}
}
// add empty lists to environment if necessary
if (!notDefined.isEmpty()) {
for (EntityDefinition entity : notDefined) {
GroovyTransformation.addToBinding(binding, (PropertyEntityDefinition) entity, Collections.emptyList());
}
}
Map<PropertyEntityDefinition, InstanceAccessorArrayList<Object>> bindingMap = new HashMap<>();
// collect the values
for (PropertyValue var : vars) {
PropertyEntityDefinition property = var.getProperty();
InstanceAccessorArrayList<Object> valueList = bindingMap.get(property);
if (valueList == null) {
valueList = new InstanceAccessorArrayList<>();
bindingMap.put(property, valueList);
}
valueList.add(GroovyTransformation.getUseValue(var.getValue(), useInstanceVariables));
}
// add collected values to environment
for (Entry<PropertyEntityDefinition, InstanceAccessorArrayList<Object>> entry : bindingMap.entrySet()) {
GroovyTransformation.addToBinding(binding, entry.getKey(), entry.getValue());
}
return binding;
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue 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.PropertyValue in project hale by halestudio.
the class MathematicalExpression method evaluateExpression.
/**
* Evaluate a mathematical expression.
*
* @param expression the mathematical expression. It may contain references
* to variables
* @param vars the list of available property values that may be bound to
* variables
* @return the evaluated expression, which can be Double, Integer or String
* @throws XExpression if the expression could not be evaluated
*/
public static Object evaluateExpression(String expression, List<PropertyValue> vars) throws XExpression {
Environment env = new Environment();
for (PropertyValue var : vars) {
// add the variable to the environment
// determine the variable value
Object value = var.getValue();
Number number;
if (value instanceof Number) {
number = (Number) value;
} else {
// try conversion to Double as default
number = var.getValueAs(Double.class);
}
// Floats
if (!(number instanceof Integer) && !(number instanceof Double)) {
number = number.doubleValue();
}
// determine the variable name
String name = var.getProperty().getDefinition().getName().getLocalPart();
Constant varValue = new Constant(number);
// name is overridden
if (env.getVariable(name) == null || var.getProperty().getPropertyPath().size() == 1) {
env.addVariable(name, varValue);
}
// add with long name if applicable
if (var.getProperty().getPropertyPath().size() > 1) {
List<String> names = new ArrayList<String>();
for (ChildContext context : var.getProperty().getPropertyPath()) {
names.add(context.getChild().getName().getLocalPart());
}
String longName = Joiner.on('.').join(names);
env.addVariable(longName, varValue);
}
}
Expression ex = new Expression(expression, env);
return ex.evaluate();
}
use of eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue in project hale by halestudio.
the class MathematicalExpression 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 {
// get the mathematical expression
String expression = getParameterChecked(PARAMETER_EXPRESSION).as(String.class);
// replace transformation variables in expression
expression = getExecutionContext().getVariables().replaceVariables(expression);
List<PropertyValue> vars = variables.get(ENTITY_VARIABLE);
try {
return evaluateExpression(expression, vars);
} catch (XExpression e) {
throw new TransformationException("Error evaluating the cell expression", e);
}
}
Aggregations