Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fluent builder API and custom AssertJ assertions #703

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package org.eclipse.esmf.metamodel;

import java.util.List;
import java.util.Optional;

import org.eclipse.esmf.metamodel.characteristic.Collection;

/**
* An Aspect encapsulates a number of properties and operations that define one functional facet of a Digital Twin.
Expand All @@ -31,4 +34,11 @@ public interface Aspect extends StructureElement {
* @since SAMM 2.0.0
*/
List<Event> getEvents();

default boolean isCollectionAspect() {
return getProperties().stream()
.map( Property::getCharacteristic )
.flatMap( Optional::stream )
.filter( Collection.class::isInstance ).count() == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* Defines the data type of a {@link Characteristic} as being a complex value.
*/
public interface ComplexType extends Type, StructureElement {

/**
* @return a {@link java.util.List} of {@link ComplexType}s which extend this Entity
*/
Expand All @@ -41,12 +40,18 @@ default boolean isAbstractEntity() {
*/
default List<Property> getAllProperties() {
if ( getExtends().isPresent() ) {
return Stream.of( getProperties(), getExtends().get().getAllProperties() ).flatMap( Collection::stream )
.collect( Collectors.toList() );
return Stream.of( getProperties(), getExtends().get().getAllProperties() ).flatMap( Collection::stream ).toList();
}
return List.copyOf( getProperties() );
}

default List<ComplexType> getAllSuperTypes() {
if ( getExtends().isPresent() ) {
return Stream.of( getExtends().stream(), getExtends().get().getAllSuperTypes().stream() ).flatMap( Function.identity() ).toList();
}
return List.of();
}

@Override
default String getUrn() {
return urn().toString();
Expand All @@ -63,4 +68,15 @@ default Optional<ComplexType> getExtends() {
default boolean isComplexType() {
return true;
}

@Override
default boolean isTypeOrSubtypeOf( final Type other ) {
if ( equals( other ) ) {
return true;
}
if ( !other.isComplexType() ) {
return false;
}
return ( (ComplexType) other ).getAllSuperTypes().contains( this );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

package org.eclipse.esmf.metamodel;

import java.util.Map;

import org.eclipse.esmf.metamodel.vocabulary.SammNs;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.XSD;

/**
* Defines the data type of a {@link Characteristic} as being a scalar value.
*/
Expand All @@ -21,4 +30,65 @@ public interface Scalar extends Type {
default boolean isScalar() {
return true;
}

private boolean transitivelyCastable( final String from, final String to ) {
if ( from.equals( to ) ) {
return true;
}
final Map<String, String> castable = ImmutableMap.<String, String> builder()
.put( XSD.xbyte.getURI(), XSD.xshort.getURI() )
.put( XSD.xshort.getURI(), XSD.xint.getURI() )
.put( XSD.xint.getURI(), XSD.xlong.getURI() )
.put( XSD.xlong.getURI(), XSD.integer.getURI() )
.put( XSD.integer.getURI(), XSD.decimal.getURI() )
.put( XSD.unsignedByte.getURI(), XSD.unsignedShort.getURI() )
.put( XSD.unsignedShort.getURI(), XSD.unsignedInt.getURI() )
.put( XSD.unsignedInt.getURI(), XSD.unsignedLong.getURI() )
.put( XSD.unsignedLong.getURI(), XSD.nonNegativeInteger.getURI() )
.put( XSD.positiveInteger.getURI(), XSD.nonNegativeInteger.getURI() )
.put( XSD.nonNegativeInteger.getURI(), XSD.integer.getURI() )
.put( XSD.negativeInteger.getURI(), XSD.nonPositiveInteger.getURI() )
.put( XSD.nonPositiveInteger.getURI(), XSD.integer.getURI() )
.put( XSD.dateTimeStamp.getURI(), XSD.dateTime.getURI() )
.put( XSD.yearMonthDuration.getURI(), XSD.duration.getURI() )
.put( XSD.dayTimeDuration.getURI(), XSD.duration.getURI() )
.build();

final String entry = castable.get( from );
if ( entry == null ) {
return false;
} else if ( entry.equals( to ) ) {
return true;
}
return transitivelyCastable( entry, to );
}

@Override
default boolean isTypeOrSubtypeOf( final Type other ) {
return transitivelyCastable( getUrn(), other.getUrn() );
}

default boolean hasStringLikeValueSpace() {
return ImmutableList.<String> builder()
.add( XSD.xstring.getURI() )
.add( XSD.date.getURI() )
.add( XSD.time.getURI() )
.add( XSD.dateTime.getURI() )
.add( XSD.dateTimeStamp.getURI() )
.add( XSD.gYear.getURI() )
.add( XSD.gMonth.getURI() )
.add( XSD.gDay.getURI() )
.add( XSD.gYearMonth.getURI() )
.add( XSD.gMonthDay.getURI() )
.add( XSD.duration.getURI() )
.add( XSD.yearMonthDuration.getURI() )
.add( XSD.dayTimeDuration.getURI() )
.add( XSD.hexBinary.getURI() )
.add( XSD.base64Binary.getURI() )
.add( XSD.anyURI.getURI() )
.add( SammNs.SAMM.curie().getURI() )
.add( RDF.langString.getURI() )
.build()
.contains( getUrn() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ default boolean isScalar() {
default boolean isComplexType() {
return false;
}

boolean isTypeOrSubtypeOf( Type other );
}


Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
* @since SAMM 1.0.0
*/
public interface Collection extends Characteristic {

/**
* @return a {@link boolean} which determines whether the elements in the collection are ordered.
*/
boolean isOrdered();

/**
* @deprecated Use {@link #allowsDuplicates()} instead
*/
@Deprecated( forRemoval = true )
default boolean isAllowDuplicates() {
return allowsDuplicates();
}

/**
* @return a {@link boolean} which determines whether the collection may contain duplicate values.
*/
boolean isAllowDuplicates();
boolean allowsDuplicates();

/**
* @return {@link Optional} containing the {@link Characteristic} describing the elements of the Collection
*
* @since SAMM 1.0.0
*/
default Optional<Characteristic> getElementCharacteristic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;

import org.eclipse.esmf.metamodel.Characteristic;
import org.eclipse.esmf.metamodel.Property;

/**
* @since SAMM 1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,27 @@
public interface RangeConstraint extends Constraint {

/**
* @return the lower bound of the range. The type of the values is determined by the {@link Type} returned by
* {@link RangeConstraint#getDataType()}.
* @return the lower bound of the range. The type of the values is determined by the {@link Type} of the Trait that the Constraint
* is used with.
*/
Optional<ScalarValue> getMinValue();

/**
* @return the upper bound of the range. The type of the values is determined by the {@link Type} returned by
* {@link RangeConstraint#getDataType()}.
* @return the upper bound of the range. The type of the values is determined by the {@link Type} of the Trait that the Constraint
* is used with.
*/
Optional<ScalarValue> getMaxValue();

/**
* @return the definition of how the lower bound of the range is to be interpreted. Possible values are
* 'OPEN', 'AT_LEAST' and 'GREATER_THAN'
*
* 'OPEN', 'AT_LEAST' and 'GREATER_THAN'
* @since SAMM 1.0.0
*/
BoundDefinition getLowerBoundDefinition();

/**
* @return the definition of how the upper bound of the range is to be interpreted. Possible values are
* 'OPEN', 'AT_MOST' and 'LESS_THAN'
*
* 'OPEN', 'AT_MOST' and 'LESS_THAN'
* @since SAMM 1.0.0
*/
BoundDefinition getUpperBoundDefinition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ private SammXsdType( final Resource dataTypeResource, final XSSimpleType xstype,
value -> URI.create( (String) XSDDatatype.XSDanyURI.parse( value ) ),
URI::toString, XSDDatatype.XSDanyURI::isValid );

@SuppressWarnings( "checkstyle:LambdaParameterName" )
public static final SammXsdType<String> STRING = new SammXsdType<>(
org.apache.jena.vocabulary.XSD.xstring, String.class, Function.identity(), Function.identity(), __ -> true );

public static final List<RDFDatatype> ALL_TYPES = List
.of( XSDDatatype.XSDstring, BOOLEAN, DECIMAL, INTEGER, DOUBLE, FLOAT, DATE, TIME, DATE_TIME, DATE_TIME_STAMP,
G_YEAR, G_MONTH, G_YEAR_MONTH, G_DAY, G_MONTH_DAY, DURATION, YEAR_MONTH_DURATION, DAY_TIME_DURATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ default Resource resource( final String name ) {
static Map<String, String> createPrefixMap( final KnownVersion metaModelVersion ) {
final Map<String, String> result = new LinkedHashMap<>();
final SAMM samm = new SAMM( metaModelVersion );
result.put( "samm", samm.getNamespace() );
result.put( "samm-c", new SAMMC( metaModelVersion ).getNamespace() );
result.put( samm.getShortForm(), samm.getNamespace() );
final SAMMC sammc = new SAMMC( metaModelVersion );
result.put( sammc.getShortForm(), sammc.getNamespace() );
result.put( "samm-e", new SAMME( metaModelVersion, samm ).getNamespace() );
result.put( "unit", new UNIT( metaModelVersion, samm ).getNamespace() );
final UNIT unit = new UNIT( metaModelVersion, samm );
result.put( unit.getShortForm(), unit.getNamespace() );
result.put( "rdf", RDF.getURI() );
result.put( "rdfs", RDFS.getURI() );
result.put( "xsd", XSD.getURI() );
Expand All @@ -65,10 +67,10 @@ static Map<String, String> createPrefixMap( final KnownVersion metaModelVersion

static Map<String, String> createPrefixMap() {
final Map<String, String> result = new LinkedHashMap<>();
result.put( "samm", SammNs.SAMM.getNamespace() );
result.put( "samm-c", SammNs.SAMMC.getNamespace() );
result.put( "samm-e", SammNs.SAMME.getNamespace() );
result.put( "unit", SammNs.UNIT.getNamespace() );
result.put( SammNs.SAMM.getShortForm(), SammNs.SAMM.getNamespace() );
result.put( SammNs.SAMMC.getShortForm(), SammNs.SAMMC.getNamespace() );
result.put( SammNs.SAMME.getShortForm(), SammNs.SAMME.getNamespace() );
result.put( SammNs.UNIT.getShortForm(), SammNs.UNIT.getNamespace() );
result.put( "rdf", RDF.getURI() );
result.put( "rdfs", RDFS.getURI() );
result.put( "xsd", XSD.getURI() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
*/
package org.eclipse.esmf.metamodel.vocabulary;

import java.util.List;

import org.eclipse.esmf.samm.KnownVersion;

import com.google.common.collect.ImmutableList;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,21 @@ public Resource TimeSeriesEntity() {
}

@SuppressWarnings( "checkstyle:MethodName" )
public Resource ThreeDimensionalPosition() {
return resource( "ThreeDimensionalPosition" );
public Resource Point3d() {
return resource( "Point3d" );
}

@SuppressWarnings( "checkstyle:MethodName" )
public Resource FileResource() {
return resource( "FileResource" );
}

public Property resource() {
return property( "resource" );
}

public Property mimeType() {
return property( "mimeType" );
}

public Property timestamp() {
Expand All @@ -76,6 +89,6 @@ public Property z() {
}

public Stream<Resource> allEntities() {
return Stream.of( TimeSeriesEntity(), ThreeDimensionalPosition() );
return Stream.of( TimeSeriesEntity(), Point3d() );
}
}
15 changes: 11 additions & 4 deletions core/esmf-aspect-meta-model-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
<configuration>
<compileSourceRoots>${build-time-sources}</compileSourceRoots>
<generatedSourcesDirectory>${generated-sources}</generatedSourcesDirectory>
<excludes>
<exclude>**/buildtime/template/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
Expand All @@ -180,15 +183,19 @@
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-units-and-quantitykinds</id>
<id>generate-sources</id>
<phase>process-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.eclipse.esmf.buildtime.GenerateUnitsTtl</mainClass>
<!-- args[0]: Path to src-gen directory -->
<commandlineArgs>${generated-sources}</commandlineArgs>
<mainClass>org.eclipse.esmf.buildtime.GenerateBuildtimeCode</mainClass>
<arguments>
<!-- args[0]: Path to src-gen directory -->
<argument>${generated-sources}</argument>
<!-- args[1]: Path to src-buildtime directory -->
<argument>${build-time-sources}</argument>
</arguments>
</configuration>
</execution>
</executions>
Expand Down
Loading
Loading