Search in sources :

Example 1 with DefaultAssociationRole

use of org.apache.sis.feature.DefaultAssociationRole in project sis by apache.

the class AssociationRoleBuilderTest method testMetadata.

/**
 * Tests the name, designation, definition, description and cardinality associated to the role.
 */
@Test
public void testMetadata() {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("Highway");
    final NamedIdentifier target = new NamedIdentifier(null, "Bridge");
    final AssociationRoleBuilder builder = new AssociationRoleBuilder(ftb, null, target).setDescription("Bridges on the highway").setDefinition("A definition").setDesignation("A designation").setMaximumOccurs(2).setMinimumOccurs(1);
    final DefaultAssociationRole role = builder.build();
    assertEquals("minimumOccurs", 1, role.getMinimumOccurs());
    assertEquals("maximumOccurs", 2, role.getMaximumOccurs());
    assertEquals("designation", new SimpleInternationalString("A designation"), role.getDesignation());
    assertEquals("definition", new SimpleInternationalString("A definition"), role.getDefinition());
    assertEquals("description", new SimpleInternationalString("Bridges on the highway"), role.getDescription());
}
Also used : DefaultAssociationRole(org.apache.sis.feature.DefaultAssociationRole) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) NamedIdentifier(org.apache.sis.referencing.NamedIdentifier) Test(org.junit.Test)

Example 2 with DefaultAssociationRole

use of org.apache.sis.feature.DefaultAssociationRole in project sis by apache.

the class FeatureTypeBuilder method initialize.

/**
 * Initializes this builder to the value of the given type.
 * The caller is responsible to invoke {@link #clear()} (if needed) before this method.
 */
private void initialize(final DefaultFeatureType template) {
    super.initialize(template);
    feature = template;
    isAbstract = template.isAbstract();
    superTypes.addAll(template.getSuperTypes());
    /*
         * For each attribute and association, wrap those properties in a builder.
         * For each operation, wrap them in pseudo-builder only if the operation
         * is not one of the operations automatically generated by this builder.
         */
    final Map<String, Set<AttributeRole>> propertyRoles = new HashMap<>();
    for (final AbstractIdentifiedType property : template.getProperties(false)) {
        PropertyTypeBuilder builder;
        if (property instanceof DefaultAttributeType<?>) {
            builder = new AttributeTypeBuilder<>(this, (DefaultAttributeType<?>) property);
        } else if (property instanceof DefaultAssociationRole) {
            builder = new AssociationRoleBuilder(this, (DefaultAssociationRole) property);
        } else {
            // Do not create OperationWrapper now - see below.
            builder = null;
        }
        /*
             * If the property name is one of our (Apache SIS specific) conventional names, try to reconstitute
             * the attribute roles that caused FeatureTypeBuilder to produce such property. Those roles usually
             * need to be applied on the source properties used for calculating the current property. There is
             * usually at most one role for each source property, but we nevertheless allow an arbitrary amount.
             */
        final AttributeRole role;
        final GenericName name = property.getName();
        if (AttributeConvention.IDENTIFIER_PROPERTY.equals(name)) {
            role = AttributeRole.IDENTIFIER_COMPONENT;
        } else if (AttributeConvention.GEOMETRY_PROPERTY.equals(name)) {
            role = AttributeRole.DEFAULT_GEOMETRY;
        } else if (AttributeConvention.ENVELOPE_PROPERTY.equals(name)) {
            // If "sis:envelope" is an operation, skip it completely.
            // It will be recreated if a default geometry exists.
            role = null;
        } else {
            if (builder == null) {
                // For all unknown operation, wrap as-is.
                builder = new OperationWrapper(this, property);
            }
            role = null;
        }
        if (role != null) {
            final Set<AttributeRole> rc = Collections.singleton(role);
            if (property instanceof AbstractOperation) {
                for (final String dependency : ((AbstractOperation) property).getDependencies()) {
                    propertyRoles.merge(dependency, rc, AttributeRole::merge);
                }
            } else {
                propertyRoles.merge(name.toString(), rc, AttributeRole::merge);
            }
        }
        if (builder != null) {
            properties.add(builder);
        }
    }
    /*
         * At this point we finished to collect information about the attribute roles.
         * Now assign those roles to the attribute builders. Note that some roles may
         * be ignored if we didn't found a suitable builder. The roles inference done
         * in this constructor is only a "best effort".
         */
    if (!propertyRoles.isEmpty()) {
        for (final Map.Entry<String, Set<AttributeRole>> entry : propertyRoles.entrySet()) {
            final PropertyTypeBuilder property = forName(properties, entry.getKey());
            if (property instanceof AttributeTypeBuilder<?>) {
                ((AttributeTypeBuilder<?>) property).roles().addAll(entry.getValue());
            }
        }
    }
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) AbstractIdentifiedType(org.apache.sis.feature.AbstractIdentifiedType) GenericName(org.opengis.util.GenericName) DefaultAssociationRole(org.apache.sis.feature.DefaultAssociationRole) AbstractOperation(org.apache.sis.feature.AbstractOperation) DefaultAttributeType(org.apache.sis.feature.DefaultAttributeType) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DefaultAssociationRole (org.apache.sis.feature.DefaultAssociationRole)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 AbstractIdentifiedType (org.apache.sis.feature.AbstractIdentifiedType)1 AbstractOperation (org.apache.sis.feature.AbstractOperation)1 DefaultAttributeType (org.apache.sis.feature.DefaultAttributeType)1 NamedIdentifier (org.apache.sis.referencing.NamedIdentifier)1 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)1 Test (org.junit.Test)1 GenericName (org.opengis.util.GenericName)1