|
| 1 | +package io.openmanufacturing.sds.aspectmodel.aas; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import org.apache.jena.rdf.model.Resource; |
| 6 | +import org.apache.jena.rdf.model.ResourceFactory; |
| 7 | +import org.apache.jena.vocabulary.XSD; |
| 8 | +import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd; |
| 9 | +import org.eclipse.digitaltwin.aas4j.v3.model.Key; |
| 10 | +import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes; |
| 11 | +import org.eclipse.digitaltwin.aas4j.v3.model.Reference; |
| 12 | +import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; |
| 13 | +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey; |
| 14 | +import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultReference; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableMap; |
| 17 | + |
| 18 | +import io.openmanufacturing.sds.aspectmodel.urn.AspectModelUrn; |
| 19 | +import io.openmanufacturing.sds.metamodel.NamedElement; |
| 20 | +import io.openmanufacturing.sds.metamodel.Property; |
| 21 | +import io.openmanufacturing.sds.metamodel.Type; |
| 22 | + |
| 23 | +/** |
| 24 | + * Base interface for any class that can map a property to a {@link SubmodelElement}. |
| 25 | + * |
| 26 | + * @param <T> the concrete type of {@link SubmodelElement} the implementing mapper produces |
| 27 | + */ |
| 28 | +public interface PropertyMapper<T extends SubmodelElement> { |
| 29 | + static final String UNKNOWN_TYPE = "Unknown"; |
| 30 | + |
| 31 | + static final String UNKNOWN_EXAMPLE = UNKNOWN_TYPE; |
| 32 | + |
| 33 | + static final LangStringMapper LANG_STRING_MAPPER = new LangStringMapper(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Maps Aspect types to DataTypeDefXsd Schema types, with no explicit mapping defaulting to |
| 37 | + * string |
| 38 | + */ |
| 39 | + static final Map<Resource, DataTypeDefXsd> AAS_XSD_TYPE_MAP = |
| 40 | + ImmutableMap.<Resource, DataTypeDefXsd> builder() |
| 41 | + .put( XSD.anyURI, DataTypeDefXsd.ANY_URI ) |
| 42 | + .put( XSD.yearMonthDuration, DataTypeDefXsd.YEAR_MONTH_DURATION ) |
| 43 | + .put( XSD.xboolean, DataTypeDefXsd.BOOLEAN ) |
| 44 | + .put( XSD.xbyte, DataTypeDefXsd.BYTE ) |
| 45 | + .put( XSD.date, DataTypeDefXsd.DATE ) |
| 46 | + .put( XSD.dateTime, DataTypeDefXsd.DATE_TIME ) |
| 47 | + .put( XSD.dateTimeStamp, DataTypeDefXsd.DATE_TIME_STAMP ) |
| 48 | + .put( XSD.dayTimeDuration, DataTypeDefXsd.DAY_TIME_DURATION ) |
| 49 | + .put( XSD.decimal, DataTypeDefXsd.DECIMAL ) |
| 50 | + .put( XSD.xdouble, DataTypeDefXsd.DOUBLE ) |
| 51 | + .put( XSD.duration, DataTypeDefXsd.DURATION ) |
| 52 | + .put( XSD.xfloat, DataTypeDefXsd.FLOAT ) |
| 53 | + .put( XSD.gMonth, DataTypeDefXsd.GMONTH ) |
| 54 | + .put( XSD.gMonthDay, DataTypeDefXsd.GMONTH_DAY ) |
| 55 | + .put( XSD.gYear, DataTypeDefXsd.GYEAR ) |
| 56 | + .put( XSD.gYearMonth, DataTypeDefXsd.GYEAR_MONTH ) |
| 57 | + .put( XSD.hexBinary, DataTypeDefXsd.HEX_BINARY ) |
| 58 | + .put( XSD.xint, DataTypeDefXsd.INT ) |
| 59 | + .put( XSD.integer, DataTypeDefXsd.INTEGER ) |
| 60 | + .put( XSD.xlong, DataTypeDefXsd.LONG ) |
| 61 | + .put( XSD.negativeInteger, DataTypeDefXsd.NEGATIVE_INTEGER ) |
| 62 | + .put( XSD.nonNegativeInteger, DataTypeDefXsd.NON_NEGATIVE_INTEGER ) |
| 63 | + .put( XSD.positiveInteger, DataTypeDefXsd.POSITIVE_INTEGER ) |
| 64 | + .put( XSD.xshort, DataTypeDefXsd.SHORT ) |
| 65 | + .put( XSD.normalizedString, DataTypeDefXsd.STRING ) |
| 66 | + .put( XSD.time, DataTypeDefXsd.TIME ) |
| 67 | + .put( XSD.unsignedByte, DataTypeDefXsd.UNSIGNED_BYTE ) |
| 68 | + .put( XSD.unsignedInt, DataTypeDefXsd.UNSIGNED_INT ) |
| 69 | + .put( XSD.unsignedLong, DataTypeDefXsd.UNSIGNED_LONG ) |
| 70 | + .put( XSD.unsignedShort, DataTypeDefXsd.UNSIGNED_SHORT ) |
| 71 | + .build(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Performs the mapping of the given property to a AAS {@link SubmodelElement}. |
| 75 | + * |
| 76 | + * @param type the type of the given property |
| 77 | + * @param property the property to map |
| 78 | + * @param context the current visitor context |
| 79 | + * @return the newly created {@link SubmodelElement} |
| 80 | + */ |
| 81 | + T mapToAasProperty( Type type, Property property, Context context ); |
| 82 | + |
| 83 | + /** |
| 84 | + * Whether this {@code PropertyMapper} can handle the given property. |
| 85 | + * |
| 86 | + * Defaults to {@code true}, implementors should override. |
| 87 | + * |
| 88 | + * @param property the property to test |
| 89 | + * @return {@code true} if this property mapper can handle the given property, {@code false} else |
| 90 | + */ |
| 91 | + default boolean canHandle( Property property ) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Maps the given URN to a {@link DataTypeDefXsd} schema type. |
| 97 | + * |
| 98 | + * @param urn the URN to map |
| 99 | + * @return the {@code DataTypeDefXsd} for the given URN |
| 100 | + */ |
| 101 | + default DataTypeDefXsd mapAASXSDataType( String urn ) { |
| 102 | + final Resource resource = ResourceFactory.createResource( urn ); |
| 103 | + return AAS_XSD_TYPE_MAP.getOrDefault( resource, DataTypeDefXsd.STRING ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Builds a concept description reference for the given property. |
| 108 | + * |
| 109 | + * @param property the property to build the reference for |
| 110 | + * @return the newly created reference |
| 111 | + */ |
| 112 | + default Reference buildReferenceToConceptDescription( Property property ) { |
| 113 | + final Key key = |
| 114 | + new DefaultKey.Builder() |
| 115 | + .type( KeyTypes.CONCEPT_DESCRIPTION ) |
| 116 | + .value( determineIdentifierFor( property ) ) |
| 117 | + .build(); |
| 118 | + return new DefaultReference.Builder().keys( key ).build(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Determines the identifier for the given {@link NamedElement}. |
| 123 | + * |
| 124 | + * @param element the element to get the identifier for |
| 125 | + * @return the identifier |
| 126 | + */ |
| 127 | + default String determineIdentifierFor( NamedElement element ) { |
| 128 | + return element.getAspectModelUrn() |
| 129 | + .map( AspectModelUrn::toString ) |
| 130 | + .orElseGet( element::getName ); |
| 131 | + } |
| 132 | +} |
0 commit comments