Search in sources :

Example 6 with Group

use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.

the class FunctionExecutor method processValue.

/**
 * Processes the given value. Does not handle {@link MultiValue}!
 *
 * @param cellLog the transformation log
 * @param function the property function
 * @param value the value to process
 * @param node the target node
 * @return the processed value
 */
private Object processValue(TransformationLog cellLog, PropertyTransformation<?> function, Object value, TargetNode node) {
    if (function.allowAutomatedResultConversion()) {
        if (!(value instanceof Group)) {
            // convert value for target
            try {
                value = convert(value, toPropertyEntityDefinition(node.getEntityDefinition()));
            } catch (Throwable e) {
                // ignore, but create error
                cellLog.error(cellLog.createMessage("Conversion according to target property failed, using value as is.", e));
            }
        } else {
        // TODO any conversion necessary/possible
        }
    } else {
        // unwrap value
        if (value instanceof Value) {
            value = ((Value) value).getValue();
        }
    }
    /*
		 * If the value is no group, but it should be one, create an instance
		 * wrapping the value
		 */
    TypeDefinition propertyType = toPropertyEntityDefinition(node.getEntityDefinition()).getDefinition().getPropertyType();
    if (!(value instanceof Group) && !propertyType.getChildren().isEmpty()) {
        MutableInstance instance = new DefaultInstance(propertyType, null);
        instance.setValue(value);
        value = instance;
    }
    return value;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MultiValue(eu.esdihumboldt.cst.MultiValue) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) Value(eu.esdihumboldt.hale.common.core.io.Value) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 7 with Group

use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.

the class InstanceValidator method validateChildren.

/**
 * Validates the given property values (their values - as instances - and/or
 * group children).
 *
 * @param properties the array of existing properties, may be null
 * @param childDef their definition
 * @param reporter the reporter to report to
 * @param type the top level type
 * @param path the current property path
 * @param onlyCheckExistingChildren whether to only validate existing
 *            children (in case of a choice) or not
 * @param reference the instance reference
 * @param context the instance validation context
 * @param entity the entity definition related to the property values or
 *            <code>null</code>
 */
private void validateChildren(Object[] properties, ChildDefinition<?> childDef, InstanceValidationReporter reporter, QName type, List<QName> path, boolean onlyCheckExistingChildren, InstanceReference reference, InstanceValidationContext context, @Nullable EntityDefinition entity) {
    if (properties != null && properties.length > 0) {
        for (Object property : properties) {
            if (property instanceof Instance) {
                validateInstance((Instance) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else if (property instanceof Group) {
                validateGroupChildren((Group) property, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
            } else {
                if (childDef.asGroup() != null)
                    reporter.warn(new DefaultInstanceValidationMessage(reference, type, new ArrayList<QName>(path), "Wrong group", "A property is no group"));
                else if (childDef.asProperty() != null) {
                    if (!skipValidation(childDef.asProperty().getPropertyType(), property)) {
                        // don't skip property
                        // wrap value in dummy instance for type validation
                        MutableInstance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                        instance.setValue(property);
                        validateInstance(instance, reporter, type, path, onlyCheckExistingChildren, reference, context, childDef, entity);
                    }
                }
            }
        }
    } else {
        /*
			 * Special case: No property value, but a combination of minimum
			 * cardinality greater than zero and NillableFlag is set. Then there
			 * can be sub-properties that are required.
			 * 
			 * Applicable for XML (simple) types with mandatory attributes.
			 */
        if (childDef.asProperty() != null && childDef.asProperty().getConstraint(Cardinality.class).getMinOccurs() > 0 && childDef.asProperty().getConstraint(NillableFlag.class).isEnabled() && childDef.asProperty().getPropertyType().getConstraint(HasValueFlag.class).isEnabled() && !childDef.asProperty().getPropertyType().getChildren().isEmpty()) {
            // collect XML attribute children
            List<ChildDefinition<?>> attributes = new ArrayList<ChildDefinition<?>>();
            for (ChildDefinition<?> child : childDef.asProperty().getPropertyType().getChildren()) {
                if (child.asProperty() != null && child.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
                    attributes.add(child);
                }
            }
            if (!attributes.isEmpty()) {
                // create an empty dummy instance
                Instance instance = new DefaultInstance(childDef.asProperty().getPropertyType(), null);
                validateGroupChildren(instance, attributes, reporter, type, path, onlyCheckExistingChildren, reference, context, entity);
            }
        }
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstanceValidationMessage(eu.esdihumboldt.hale.common.instance.extension.validation.report.impl.DefaultInstanceValidationMessage) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ArrayList(java.util.ArrayList) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition)

Example 8 with Group

use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.

the class PropertyResolver method getValues.

/**
 * Method for retrieving values from instances using a certain path query
 * for searching through the instance definitions. Calls methods for
 * traversing the definition tree.
 *
 * @param instance the instance
 * @param propertyPath the property path
 * @param forceValue if this is <code>true</code>, when the object at the
 *            end of a path is an instance, its value will be returned
 * @return the values or instances contained in the instance matching the
 *         given path, may be <code>null</code>
 */
public static Collection<Object> getValues(Instance instance, String propertyPath, boolean forceValue) {
    if (instance.getDefinition() == null) {
        // instance w/o a definition -> search only in instance structure
        // XXX group hiding and incomplete names not supported!
        Collection<Object> result = new ArrayList<Object>();
        Collection<Object> parents = new ArrayList<Object>();
        parents.add(instance);
        Queue<QName> names = new LinkedList<QName>(getQNamesFromPath(propertyPath));
        while (!names.isEmpty() && !parents.isEmpty()) {
            QName property = names.poll();
            Collection<Object> values = new ArrayList<Object>();
            for (Object parent : parents) {
                if (parent instanceof Group) {
                    Object[] propertyValues = ((Group) parent).getProperty(property);
                    if (propertyValues != null) {
                        for (Object propertyValue : propertyValues) {
                            if (propertyValue instanceof Instance && ((Instance) propertyValue).getDefinition() != null) {
                                Instance pi = (Instance) propertyValue;
                                // call getValues again an perform the
                                // search based on definitions
                                // with a reduced path
                                String path = pathString(names);
                                Collection<Object> deepValues = getValues(pi, path, forceValue);
                                if (deepValues != null) {
                                    result.addAll(deepValues);
                                }
                            } else {
                                values.add(propertyValue);
                            }
                        }
                    }
                }
            }
            parents = values;
        }
        if (names.isEmpty()) {
            // return all values found
            for (Object value : parents) {
                if (!forceValue) {
                    result.add(value);
                } else {
                    if (value instanceof Instance) {
                        result.add(((Instance) value).getValue());
                    } else if (!(value instanceof Group)) {
                        result.add(value);
                    }
                }
            }
        }
        return result;
    }
    // definition based check and retrieval
    if (hasProperty(instance, propertyPath)) {
        LinkedList<String> paths = getQueryPath(instance, propertyPath);
        Collection<Object> result = new ArrayList<Object>();
        for (String path : paths) {
            List<QName> qnames = getQNamesFromPath(path);
            Object[] props = instance.getProperty(qnames.get(0));
            if (props == null) {
                continue;
            }
            Queue<Object> currentQueue = new LinkedList<Object>();
            Queue<Object> nextQueue = new LinkedList<Object>();
            for (Object prop : props) {
                currentQueue.add(prop);
            }
            for (int i = 1; i < qnames.size(); i++) {
                while (!currentQueue.isEmpty()) {
                    Object prop = currentQueue.poll();
                    if (prop instanceof Group) {
                        Object[] nextPropertys = ((Group) prop).getProperty(qnames.get(i));
                        if (nextPropertys == null) {
                            continue;
                        }
                        for (Object np : nextPropertys) {
                            nextQueue.add(np);
                        }
                    } else {
                    // TODO ERROR wrong path given from the cache
                    }
                }
                while (!nextQueue.isEmpty()) {
                    currentQueue.add(nextQueue.poll());
                }
            }
            while (!currentQueue.isEmpty()) {
                Object finalProp = currentQueue.poll();
                if (finalProp instanceof Instance) {
                    if (forceValue) {
                        result.add(((Instance) finalProp).getValue());
                    } else {
                        result.add(finalProp);
                    }
                } else if (finalProp instanceof Group && forceValue) {
                // TODO error
                } else
                    result.add(finalProp);
            }
        }
        if (!result.isEmpty()) {
            return result;
        } else {
            return null;
        }
    } else
        return null;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 9 with Group

use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.

the class InstanceTestValues method extractValue.

/**
 * Extract the value represented by the given property from the given
 * instance.
 *
 * @param instance the instance
 * @param property the property
 * @return the first property value in the instance, or <code>null</code>
 */
protected Object extractValue(Instance instance, PropertyEntityDefinition property) {
    List<ChildContext> path = property.getPropertyPath();
    Object current = instance;
    // step down for each path element
    for (ChildContext element : path) {
        if (current instanceof Group) {
            Group group = (Group) current;
            Object[] vals = group.getProperty(element.getChild().getName());
            if (vals != null && vals.length > 0) {
                // TODO match filter?
                // child
                current = vals[0];
            } else {
                // property not present
                return null;
            }
        } else {
            // property not present
            return null;
        }
    }
    return current;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) ChildContext(eu.esdihumboldt.hale.common.align.model.ChildContext)

Example 10 with Group

use of eu.esdihumboldt.hale.common.instance.model.Group in project hale by halestudio.

the class GmlInstanceCollectionTest method testWVAInstances.

private void testWVAInstances(InstanceCollection instances) {
    String ns = "http://www.esdi-humboldt.org/waterVA";
    String gmlNs = "http://www.opengis.net/gml";
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // check type and element
        TypeDefinition type = instance.getDefinition();
        assertEquals(new QName(ns, "Watercourses_VA_Type"), type.getName());
        XmlElements elements = type.getConstraint(XmlElements.class);
        Collection<? extends XmlElement> elementCollection = elements.getElements();
        assertEquals(1, elementCollection.size());
        XmlElement element = elementCollection.iterator().next();
        assertEquals(new QName(ns, "Watercourses_VA"), element.getName());
        // check instance
        // check a simple property first (FGW_ID)
        Object[] fgwID = instance.getProperty(new QName(ns, "FGW_ID"));
        assertNotNull(fgwID);
        assertEquals(1, fgwID.length);
        assertEquals("81011403", fgwID[0]);
        // the_geom
        Object[] the_geom = instance.getProperty(new QName(ns, "the_geom"));
        assertNotNull(the_geom);
        assertEquals(1, the_geom.length);
        assertTrue(the_geom[0] instanceof Instance);
        // MultiLineString
        Object[] multiLineString = ((Instance) the_geom[0]).getProperty(new QName(gmlNs, "MultiLineString"));
        assertNotNull(multiLineString);
        assertEquals(1, multiLineString.length);
        assertTrue(multiLineString[0] instanceof Instance);
        // TODO the MultiLineString should have a GeometryProperty value
        // with a MultiLineString as geometry and a CRS definition
        // ...getValue()
        // srsName
        Object[] srsName = ((Instance) multiLineString[0]).getProperty(new QName("srsName"));
        assertNotNull(srsName);
        assertEquals(1, srsName.length);
        assertEquals("EPSG:31251", srsName[0].toString());
        // lineStringMember
        Object[] lineStringMember = ((Instance) multiLineString[0]).getProperty(new QName(gmlNs, "lineStringMember"));
        assertNotNull(lineStringMember);
        assertEquals(1, lineStringMember.length);
        assertTrue(lineStringMember[0] instanceof Instance);
        // LineString
        Object[] lineString = ((Instance) lineStringMember[0]).getProperty(new QName(gmlNs, "LineString"));
        assertNotNull(lineString);
        assertEquals(1, lineString.length);
        assertTrue(lineString[0] instanceof Instance);
        // TODO the LineString should have a GeometryProperty value with a
        // LineString as geometry and a CRS definition
        // ...getValue()
        // choice
        Object[] choice_1 = ((Instance) lineString[0]).getProperty(new QName(gmlNs + "/LineStringType", "choice_1"));
        assertNotNull(choice_1);
        assertEquals(1, choice_1.length);
        assertTrue(choice_1[0] instanceof Group);
        // coordinates
        Object[] coordinates = ((Group) choice_1[0]).getProperty(new QName(gmlNs, "coordinates"));
        assertNotNull(coordinates);
        assertEquals(1, coordinates.length);
        assertTrue(coordinates[0] instanceof Instance);
        assertTrue(((Instance) coordinates[0]).getValue().toString().contains("-39799.68820381"));
        // only one instance should be present
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

Group (eu.esdihumboldt.hale.common.instance.model.Group)26 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)15 QName (javax.xml.namespace.QName)10 ArrayList (java.util.ArrayList)6 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)5 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)5 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)4 Pair (eu.esdihumboldt.util.Pair)4 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)3 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 CellNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)2 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)2 TransformationContext (eu.esdihumboldt.hale.common.align.model.transformation.tree.context.TransformationContext)2 InstanceValidationReport (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReport)2 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)2 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)2 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)2 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)2 LinkedList (java.util.LinkedList)2