Search in sources :

Example 31 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class TypeStructureTray method createSourceSample.

/**
 * Create sample code for a single tree path specifying a source property.
 *
 * @param path the tree path
 * @param types the types serving as input
 * @return the sample code or <code>null</code>
 */
private String createSourceSample(TreePath path, Collection<? extends TypeDefinition> types) {
    DefinitionGroup parent;
    int startIndex = 0;
    boolean hasPropDef = false;
    StringBuilder access = new StringBuilder();
    // determine parent type
    if (path.getFirstSegment() instanceof TypeDefinition) {
        // types are the top level elements
        parent = (DefinitionGroup) path.getFirstSegment();
        startIndex = 1;
        access.append(GroovyConstants.BINDING_SOURCE);
    } else {
        // types are not in the tree, single type must be root
        TypeDefinition type = types.iterator().next();
        parent = type;
        if (VARIABLES_TYPE_NAME.equals(type.getName())) {
            // Groovy property transformation
            // first segment is variable name
            Definition<?> def = (Definition<?>) path.getFirstSegment();
            startIndex++;
            access.append(def.getName().getLocalPart());
            // XXX add namespace if necessary
            // XXX currently groovy transformation does not support multiple
            // variables with the same name
            parent = DefinitionUtil.getDefinitionGroup(def);
        } else {
            // assuming Retype/Merge
            access.append(GroovyConstants.BINDING_SOURCE);
        }
    }
    // is a property or list of inputs referenced -> use accessor
    boolean propertyOrList = path.getSegmentCount() > startIndex || (path.getLastSegment() instanceof ChildDefinition<?> && canOccureMultipleTimes((ChildDefinition<?>) path.getLastSegment()));
    if (!propertyOrList) {
        if (parent instanceof TypeDefinition && canHaveValue((TypeDefinition) parent)) {
            if (!DefinitionUtil.hasChildren((Definition<?>) path.getLastSegment())) {
                // variable w/o children referenced
                return "// access variable\ndef value = " + access;
            } else {
                // variable w/ children referenced
                return "// access instance variable value\ndef value = " + access + ".value";
            }
        } else {
        // return null;
        }
    }
    if (path.getFirstSegment() instanceof TypeDefinition) {
        access.append(".links." + ((TypeDefinition) path.getFirstSegment()).getDisplayName());
    } else {
        access.append(".p");
        // check for encountering property definition, so that there should
        // not be 2 'p's appended.
        hasPropDef = true;
    }
    for (int i = startIndex; i < path.getSegmentCount(); i++) {
        Definition<?> def = (Definition<?>) path.getSegment(i);
        if (def instanceof PropertyDefinition) {
            for (int j = i - 1; j >= 0; j--) {
                Definition<?> def1 = (Definition<?>) path.getSegment(j);
                if (def1 instanceof TypeDefinition) {
                // do nothing
                } else {
                    hasPropDef = true;
                }
            }
            if (!hasPropDef) {
                access.append(".p");
            }
            // property name
            access.append('.');
            access.append(def.getName().getLocalPart());
            // test if uniquely accessible from parent
            boolean useNamespace = true;
            if (parent instanceof Definition<?>) {
                useNamespace = namespaceNeeded((Definition<?>) parent, def);
            }
            // add namespace if necessary
            if (useNamespace) {
                access.append("('");
                access.append(def.getName().getNamespaceURI());
                access.append("')");
            }
        } else if (def instanceof TypeDefinition) {
            access.append("." + ((TypeDefinition) def).getDisplayName());
        }
        // set the new parent
        parent = DefinitionUtil.getDefinitionGroup(def);
    }
    if (parent instanceof TypeDefinition) {
        // only properties at the end of the path are supported
        TypeDefinition propertyType = (TypeDefinition) parent;
        StringBuilder example = new StringBuilder();
        boolean canOccurMultipleTimes = false;
        if (path.getFirstSegment() instanceof TypeDefinition) {
            canOccurMultipleTimes = true;
        }
        /*
			 * Instances/values may occur multiple times if any element in the
			 * path may occur multiple times.
			 */
        for (int i = path.getSegmentCount() - 1; i >= 0 && !canOccurMultipleTimes; i--) {
            if (path.getSegment(i) instanceof ChildDefinition<?>) {
                canOccurMultipleTimes = canOccureMultipleTimes((ChildDefinition<?>) path.getSegment(i));
            }
        }
        if (canHaveValue(propertyType)) {
            // single value
            if (canOccurMultipleTimes) {
                example.append("// access first value\n");
            } else {
                example.append("// access value\n");
            }
            example.append("def value = ");
            example.append(access);
            example.append(".value()\n\n");
            // multiple values
            if (canOccurMultipleTimes) {
                example.append("// access all values as list\n");
                example.append("def valueList = ");
                example.append(access);
                example.append(".values()\n\n");
            }
        }
        if (DefinitionUtil.hasChildren(propertyType)) {
            // single instance
            if (canOccurMultipleTimes) {
                example.append("// access first instance\n");
            } else {
                example.append("// access instance\n");
            }
            example.append("def instance = ");
            example.append(access);
            example.append(".first()\n\n");
            if (canOccurMultipleTimes) {
                // multiple values
                example.append("// access all instances as list\n");
                example.append("def instanceList = ");
                example.append(access);
                example.append(".list()\n\n");
                // iterate over instances
                example.append("// iterate over instances\n");
                example.append(access);
                example.append(".each {\n");
                example.append("\tinstance ->\n");
                example.append("}\n\n");
            }
        } else if (canOccurMultipleTimes && propertyType.getConstraint(HasValueFlag.class).isEnabled()) {
            // iterate over values
            example.append("// iterate over values\n");
            example.append(access);
            example.append(".each {\n");
            example.append("\tvalue ->\n");
            example.append("}\n\n");
        }
        return example.toString();
    }
    return null;
}
Also used : HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Definition(eu.esdihumboldt.hale.common.schema.model.Definition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) ChildDefinition(eu.esdihumboldt.hale.common.schema.model.ChildDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 32 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class SequentialIDParameterPage method validateValue.

/**
 * Validates if the given value is valid for the target property.
 *
 * @param value the value to validate
 * @return if the value is valid
 */
protected boolean validateValue(String value) {
    Cell cell = getWizard().getUnfinishedCell();
    List<? extends Entity> targets = cell.getTarget().get(null);
    if (!targets.isEmpty()) {
        Entity entity = targets.get(0);
        Definition<?> def = entity.getDefinition().getDefinition();
        if (def instanceof PropertyDefinition) {
            TypeDefinition propertyType = ((PropertyDefinition) def).getPropertyType();
            Validator validator = propertyType.getConstraint(ValidationConstraint.class).getValidator();
            // TODO conversion to binding needed?
            String error = validator.validate(value);
            // update the example decoration
            if (error == null) {
                exampleDecoration.hide();
            } else {
                exampleDecoration.setDescriptionText(error);
                exampleDecoration.show();
            }
            return error == null;
        }
    }
    // no validation possible
    return true;
}
Also used : Entity(eu.esdihumboldt.hale.common.align.model.Entity) ValidationConstraint(eu.esdihumboldt.hale.common.schema.model.constraint.type.ValidationConstraint) Cell(eu.esdihumboldt.hale.common.align.model.Cell) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Validator(eu.esdihumboldt.util.validator.Validator) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 33 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class AbstractGeometrySchemaService method determineDefaultGeometry.

/**
 * Determine the path to a geometry property to be used as default geometry
 * for the given type. By default the first geometry property found with a
 * breadth first search is used.
 *
 * @param type the type definition
 * @return the path to the default geometry property or <code>null</code> if
 *         unknown
 */
protected List<QName> determineDefaultGeometry(TypeDefinition type) {
    // breadth first search for geometry properties
    Queue<Pair<List<QName>, DefinitionGroup>> groups = new LinkedList<Pair<List<QName>, DefinitionGroup>>();
    groups.add(new Pair<List<QName>, DefinitionGroup>(new ArrayList<QName>(), type));
    List<QName> firstGeometryPath = null;
    while (firstGeometryPath == null && !groups.isEmpty()) {
        // for each parent group...
        Pair<List<QName>, DefinitionGroup> group = groups.poll();
        DefinitionGroup parent = group.getSecond();
        List<QName> parentPath = group.getFirst();
        // max depth for default geometries
        if (parentPath.size() > 5)
            continue;
        // queue
        for (ChildDefinition<?> child : DefinitionUtil.getAllChildren(parent)) {
            // path for child
            List<QName> path = new ArrayList<QName>(parentPath);
            path.add(child.getName());
            if (child.asProperty() != null) {
                PropertyDefinition property = child.asProperty();
                TypeDefinition propertyType = property.getPropertyType();
                // check if we found a geometry property
                if (propertyType.getConstraint(GeometryType.class).isGeometry()) {
                    // match
                    firstGeometryPath = path;
                } else {
                    // test children later on
                    groups.add(new Pair<List<QName>, DefinitionGroup>(path, propertyType));
                }
            } else if (child.asGroup() != null) {
                // test group later on
                GroupPropertyDefinition childGroup = child.asGroup();
                groups.add(new Pair<List<QName>, DefinitionGroup>(path, childGroup));
            } else {
                throw new IllegalStateException("Invalid child definition encountered");
            }
        }
    }
    if (firstGeometryPath != null) {
        // a geometry property was found
        return generalizeGeometryProperty(type, firstGeometryPath);
    } else {
        return null;
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) LinkedList(java.util.LinkedList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) GeometryType(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(eu.esdihumboldt.util.Pair)

Example 34 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition 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)

Example 35 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class PatternTest method testDirect.

/**
 * Test a direct match
 */
@Ignore
@Test
public void testDirect() {
    // $NON-NLS-1$
    Pattern pattern = Pattern.parse("Curve");
    TypeDefinition start = createCurveType();
    DefinitionPath path = pattern.match(start, new DefinitionPath(start, CURVE_ELEMENT, false), GML_NS);
    // $NON-NLS-1$
    assertNotNull("A match should have been found", path);
    // $NON-NLS-1$
    assertTrue("Path should be empty", path.isEmpty());
    assertEquals(start, path.getLastType());
}
Also used : DefinitionPath(eu.esdihumboldt.hale.io.gml.writer.internal.geometry.DefinitionPath) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)192 QName (javax.xml.namespace.QName)58 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)38 ArrayList (java.util.ArrayList)32 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)26 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)26 Test (org.junit.Test)24 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)22 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)21 HashSet (java.util.HashSet)21 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)20 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)20 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)16 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)15 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)14 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)12 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)12 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)12