Search in sources :

Example 81 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance 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 82 with Instance

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

the class CSVInstanceReaderTest method testReadSimple.

/**
 * Test - read a sample csv schema and data.
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadSimple() throws Exception {
    String typeName = "location";
    String[] properties = { "Name", "Xcoord", "Ycoord", "id" };
    String[] dataFirstColumn = { "test", "12", "16", "1" };
    int numberOfInstances = 2;
    // read Schema ###
    Schema schema = readCSVSchema("/data/test1.csv", typeName, "java.lang.String,java.lang.String,java.lang.String,java.lang.String", "Name,Xcoord,Ycoord,id", null, null, null);
    // Test properties
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    // Check every property for their existence
    for (String propertyName : properties) {
        assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
    }
    // read Instances ###
    InstanceCollection instances = readCSVInstances("/data/test1.csv", typeName, true, schema, null, null, null);
    assertEquals(numberOfInstances, collectionSize(instances));
    // get Type to check property definition (schema and instance
    // combination)
    TypeDefinition type = instances.iterator().next().getDefinition();
    ChildDefinition<?> child = null;
    assertEquals(typeName, type.getDisplayName());
    for (int i = 0; i < properties.length; i++) {
        child = type.getChild(QName.valueOf(properties[i]));
        assertEquals(properties[i], child.getDisplayName());
    }
    // Check the values of the first (type) instance
    Instance instance = instances.iterator().next();
    Object[] value;
    for (int i = 0; i < dataFirstColumn.length; i++) {
        value = instance.getProperty(QName.valueOf(properties[i]));
        assertEquals(dataFirstColumn[i], value[0]);
        assertTrue(value[0] instanceof String);
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 83 with Instance

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

the class CSVInstanceReaderTest method testReadWithPointDecimal.

/**
 * Test - read a sample csv schema and data with point as a decimal divisor
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadWithPointDecimal() throws Exception {
    String typeName = "Random";
    String[] properties = { "A", "B", "C", "D", "E" };
    Object[] dataFirstColumn = { new Integer(1), "A", new Float(32647968.61), new Float(5649088.376), "Linderbacher Straße" };
    int numberOfInstances = 5;
    // read Schema ###
    Schema schema = readCSVSchema("/data/test3-pointdecimal.csv", typeName, "java.lang.Integer,java.lang.String,java.lang.Float,java.lang.Float,java.lang.String", "A,B,C,D,E", ";", null, null, ".");
    // Test properties
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    // Check every property for their existence
    for (String propertyName : properties) {
        assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
    }
    // read Instances ###
    InstanceCollection instances = readCSVInstances("/data/test3-pointdecimal.csv", typeName, true, schema, ";", null, null, ".");
    assertEquals(numberOfInstances, collectionSize(instances));
    // get Type to check property definition (schema and instance
    // combination)
    TypeDefinition type = instances.iterator().next().getDefinition();
    ChildDefinition<?> child = null;
    assertEquals(typeName, type.getDisplayName());
    for (int i = 0; i < properties.length; i++) {
        child = type.getChild(QName.valueOf(properties[i]));
        assertEquals(properties[i], child.getDisplayName());
    }
    // Check the values of the first (type) instance
    Instance instance = instances.iterator().next();
    Object[] value;
    for (int i = 0; i < dataFirstColumn.length; i++) {
        value = instance.getProperty(QName.valueOf(properties[i]));
        assertEquals(dataFirstColumn[i], value[0]);
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 84 with Instance

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

the class EnvelopeHandlerTest method testEnvelopeGml3_Grid.

/**
 * Test envelope geometries read from a GML 3 file
 *
 * @throws Exception if an error occurs
 */
@Test
public void testEnvelopeGml3_Grid() throws Exception {
    ReaderConfiguration config = InterpolationConfigurations.ALL_TO_GRID_DEFAULT;
    InstanceCollection instances = AbstractHandlerTest.loadXMLInstances(getClass().getResource("/data/gml/geom-gml3.xsd").toURI(), getClass().getResource("/data/envelope/sample-envelope-gml3.xml").toURI(), config);
    // one instances expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // 1. EnvelopeProperty defined through pos
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, combine(referenceChecker(reference, InterpolationHelper.DEFAULT_MAX_POSITION_ERROR), config.geometryChecker()));
    } finally {
        it.close();
    }
}
Also used : ReaderConfiguration(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.ReaderConfiguration) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Example 85 with Instance

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

the class EnvelopeHandlerTest method testEnvelopeGml3.

/**
 * Test envelope geometries read from a GML 3 file
 *
 * @throws Exception if an error occurs
 */
@Test
public void testEnvelopeGml3() throws Exception {
    InstanceCollection instances = AbstractHandlerTest.loadXMLInstances(getClass().getResource("/data/gml/geom-gml3.xsd").toURI(), getClass().getResource("/data/envelope/sample-envelope-gml3.xml").toURI());
    // one instances expected
    ResourceIterator<Instance> it = instances.iterator();
    try {
        // 1. EnvelopeProperty defined through pos
        assertTrue("First sample feature missing", it.hasNext());
        Instance instance = it.next();
        checkSingleGeometry(instance, referenceChecker(reference));
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) Test(org.junit.Test) AbstractHandlerTest(eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)203 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)131 Test (org.junit.Test)122 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 QName (javax.xml.namespace.QName)29 ArrayList (java.util.ArrayList)26 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)25 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)23 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)22 Group (eu.esdihumboldt.hale.common.instance.model.Group)15 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 Geometry (com.vividsolutions.jts.geom.Geometry)12 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 HashSet (java.util.HashSet)8 Point (com.vividsolutions.jts.geom.Point)7