use of eu.esdihumboldt.hale.common.align.model.EntityDefinition 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.model.EntityDefinition in project hale by halestudio.
the class AbstractPropertyTransformationHandler method handleXmlElementAsGeometryType.
/**
* This method is invoked when the target property is a GML geometry type.
*
* <p>
* The target attribute is set to <code>gml:AbstractGeometry</code> and the
* concrete geometry type is specified in a
* <code><targetAttributeNode></code> tag.
* </p>
*
* @param featureType the target feature type
* @param mappingName the target feature type's mapping name (may be
* <code>null</code>)
* @param context the app-schema mapping context
*/
protected void handleXmlElementAsGeometryType(TypeDefinition featureType, String mappingName, AppSchemaMappingContext context) {
PropertyEntityDefinition geometry = targetProperty.getDefinition();
createGeometryAttributeMapping(featureType, mappingName, geometry, context);
// GeometryTypes require special handling
TypeDefinition geometryType = geometry.getDefinition().getPropertyType();
QName geomTypeName = geometryType.getName();
Namespace geomNS = context.getOrCreateNamespace(geomTypeName.getNamespaceURI(), geomTypeName.getPrefix());
attributeMapping.setTargetAttributeNode(geomNS.getPrefix() + ":" + geomTypeName.getLocalPart());
// set target attribute to parent (should be gml:AbstractGeometry)
// TODO: this is really ugly, but I don't see a better way to do it
// since HALE renames
// {http://www.opengis.net/gml/3.2}AbstractGeometry element
// to
// {http://www.opengis.net/gml/3.2/AbstractGeometry}choice
EntityDefinition parentEntityDef = AlignmentUtil.getParent(geometry);
Definition<?> parentDef = parentEntityDef.getDefinition();
String parentQName = geomNS.getPrefix() + ":" + parentDef.getDisplayName();
List<ChildContext> targetPropertyPath = parentEntityDef.getPropertyPath();
attributeMapping.setTargetAttribute(mapping.buildAttributeXPath(featureType, targetPropertyPath) + "/" + parentQName);
}
use of eu.esdihumboldt.hale.common.align.model.EntityDefinition in project hale by halestudio.
the class AbstractPropertyTransformationHandler method handleAsXmlAttribute.
/**
* This method is invoked when the target property is an XML attribute (
* {@link XmlAttributeFlag} constraint is set).
*
* <p>
* The property transformation is translated to:
*
* <pre>
* <code><ClientProperty>
* <name>[target property name]</name>
* <value>[CQL expression]</value>
* </ClientProperty></code>
* </pre>
*
* and added to the attribute mapping generated for the XML element owning
* the attribute.
* </p>
*
* @param featureType the target feature type
* @param mappingName the target feature type's mapping name (may be
* <code>null</code>)
* @param context the app-schema mapping context
*/
protected void handleAsXmlAttribute(TypeDefinition featureType, String mappingName, AppSchemaMappingContext context) {
PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
PropertyDefinition targetPropertyDef = targetPropertyEntityDef.getDefinition();
// fetch attribute mapping for parent property
EntityDefinition parentDef = AlignmentUtil.getParent(targetPropertyEntityDef);
if (parentDef != null) {
List<ChildContext> parentPropertyPath = parentDef.getPropertyPath();
PropertyDefinition parentPropertyDef = parentPropertyPath.get(parentPropertyPath.size() - 1).getChild().asProperty();
if (parentPropertyDef != null) {
attributeMapping = context.getOrCreateAttributeMapping(featureType, mappingName, parentPropertyPath);
// set targetAttribute if empty
if (attributeMapping.getTargetAttribute() == null || attributeMapping.getTargetAttribute().isEmpty()) {
attributeMapping.setTargetAttribute(mapping.buildAttributeXPath(featureType, parentPropertyPath));
}
Namespace targetPropNS = context.getOrCreateNamespace(targetPropertyDef.getName().getNamespaceURI(), targetPropertyDef.getName().getPrefix());
String unqualifiedName = targetPropertyDef.getName().getLocalPart();
boolean isQualified = targetPropNS != null && !Strings.isNullOrEmpty(targetPropNS.getPrefix());
// encode attribute as <ClientProperty>
ClientProperty clientProperty = new ClientProperty();
@SuppressWarnings("null") String clientPropName = (isQualified) ? targetPropNS.getPrefix() + ":" + unqualifiedName : unqualifiedName;
clientProperty.setName(clientPropName);
clientProperty.setValue(getSourceExpressionAsCQL());
setEncodeIfEmpty(clientProperty);
// don't add client property if it already exists
if (!hasClientProperty(clientProperty.getName())) {
attributeMapping.getClientProperty().add(clientProperty);
// when nilReason is null and viceversa)
if (isNilReason(targetPropertyDef) && isNillable(parentPropertyDef) && attributeMapping.getSourceExpression() == null) {
addOrReplaceXsiNilAttribute(clientProperty.getValue(), true, context);
}
}
}
}
}
use of eu.esdihumboldt.hale.common.align.model.EntityDefinition in project hale by halestudio.
the class EntityVisitor method accept.
/**
* Apply the visitor on a type entity definition.
*
* @param ted the type entity definition
*/
public void accept(TypeEntityDefinition ted) {
if (visit(ted)) {
for (ChildDefinition<?> child : ted.getDefinition().getChildren()) {
EntityDefinition ed = AlignmentUtil.getChild(ted, child.getName());
accept(ed);
}
}
}
use of eu.esdihumboldt.hale.common.align.model.EntityDefinition in project hale by halestudio.
the class CityGMLPropagate method generateMapping.
private void generateMapping() {
System.out.println("Indexing example cells...");
// index all cells based on the target property name
SetMultimap<String, Cell> bgisExamples = HashMultimap.create();
SetMultimap<QName, Cell> cityGMLExamples = HashMultimap.create();
for (Cell cell : examples.getCells()) {
if (cell.getTarget().size() == 1) {
// only supports cells with one target
EntityDefinition entityDef = CellUtil.getFirstEntity(cell.getTarget()).getDefinition();
// XXX check source?!
if (entityDef.getDefinition() instanceof PropertyDefinition) {
QName name = entityDef.getDefinition().getName();
if (ADE_NS.equals(name.getNamespaceURI())) {
bgisExamples.put(name.getLocalPart(), cell);
} else if (name.getNamespaceURI().startsWith(CITYGML_NAMESPACE_CORE)) {
// XXX only support level 1 properties?
cityGMLExamples.put(name, cell);
} else
System.out.println("WARNING: ignoring cell with target property neither from CityGML nor from BGIS ADE");
} else
System.out.println("WARNING: ignoring type cell");
} else
System.out.println("WARNING: ignoring cell with multiple or no targets");
}
// collect all ADE feature types
List<TypeDefinition> featureTypes = BGISAppUtil.getADEFeatureTypes(targetSchema);
// collect ADE display names
Set<String> adeTypeNames = new HashSet<String>();
for (TypeDefinition type : featureTypes) {
adeTypeNames.add(type.getDisplayName());
}
// collect possibly relevant target CityGML feature types
for (TypeDefinition type : targetSchema.getTypes()) {
if (type.getName().getNamespaceURI().startsWith(CITYGML_NAMESPACE_CORE) && BGISAppUtil.isFeatureType(type)) {
if (!adeTypeNames.contains(type.getDisplayName())) {
/*
* But ensure to only add those that do not share the
* display name with an ADE type, as in the feature map the
* type identification is only done on based on the display
* name, and ADE types take precedent.
*/
featureTypes.add(type);
}
}
}
// visit ADE properties and create cells
System.out.println("Generating mapping from example cells for");
String cellNote = MessageFormat.format("Generated through propagation of example cells on CityGML and BGIS ADE feature types.\n" + "{0,date,medium}", new Date());
CityGMLPropagateVisitor visitor = new CityGMLPropagateVisitor(cityGMLSource, bgisExamples, cityGMLExamples, config, cellNote);
for (TypeDefinition type : featureTypes) {
System.out.println(type.getDisplayName() + "...");
visitor.accept(new TypeEntityDefinition(type, SchemaSpaceID.TARGET, null));
}
if (visitor.getCells().isEmpty()) {
System.out.println("WARNING: no cells were created");
} else {
System.out.println(visitor.getCells().size() + " cells were created.");
}
// create alignment
MutableAlignment align = new DefaultAlignment();
for (MutableCell cell : visitor.getCells()) {
align.addCell(cell);
}
this.alignment = align;
}
Aggregations