Skip to content

Commit 26f6217

Browse files
committed
Rollback <Object> exampleValue to <ScalarValue>
1 parent d51fb27 commit 26f6217

File tree

20 files changed

+63
-55
lines changed

20 files changed

+63
-55
lines changed

core/esmf-aspect-meta-model-interface/src/main/java/org/eclipse/esmf/metamodel/Property.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface Property extends ModelElement {
3434
* @return an {@link Optional} which may contain an example value for the Property. The type of the value is
3535
* determined by the {@link Type} returned by {@link Property#getDataType()}.
3636
*/
37-
Optional<Object> getExampleValue();
37+
Optional<ScalarValue> getExampleValue();
3838

3939
/**
4040
* @return the name of the Property used in the runtime payload.

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/DefaultPropertyWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.eclipse.esmf.metamodel.Characteristic;
1919
import org.eclipse.esmf.metamodel.Property;
20+
import org.eclipse.esmf.metamodel.ScalarValue;
2021
import org.eclipse.esmf.metamodel.impl.DefaultProperty;
2122

2223
public class DefaultPropertyWrapper extends DefaultProperty {
@@ -32,7 +33,7 @@ public String getPayloadName() {
3233
}
3334

3435
@Override
35-
public Optional<Object> getExampleValue() {
36+
public Optional<ScalarValue> getExampleValue() {
3637
return property.getExampleValue();
3738
}
3839

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/Instantiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected Value buildValue( final RDFNode node, final Optional<Resource> charact
180180
}
181181

182182
if ( !resource.hasProperty( RDF.type, SammNs.SAMM.Value() ) ) {
183-
return new DefaultScalarValue( resource.getURI(), new DefaultScalar( type.toString() ), null );
183+
return new DefaultScalarValue( null, resource.getURI(), new DefaultScalar( type.toString() ) );
184184
}
185185

186186
if ( resource.hasProperty( RDF.type, SammNs.SAMM.Value() ) ) {
@@ -190,7 +190,7 @@ protected Value buildValue( final RDFNode node, final Optional<Resource> charact
190190
throw new AspectLoadingException( "samm:Value must contain a samm:value property" );
191191
}
192192

193-
return new DefaultScalarValue( valueOpt.get(), new DefaultScalar( type.toString() ), buildBaseAttributes( resource ) );
193+
return new DefaultScalarValue( buildBaseAttributes( resource ), valueOpt.get(), new DefaultScalar( type.toString() ) );
194194
}
195195
}
196196

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/ValueInstantiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public Optional<ScalarValue> buildScalarValue( final String lexicalRepresentatio
5151
return SammXsdType.ALL_TYPES.stream()
5252
.filter( type -> type.getURI().equals( datatypeUri ) )
5353
.map( type -> type.parse( lexicalRepresentation ) )
54-
.<ScalarValue> map( value -> new DefaultScalarValue( value, new DefaultScalar( datatypeUri ), null ) )
54+
.<ScalarValue> map( value -> new DefaultScalarValue( null, value, new DefaultScalar( datatypeUri ) ) )
5555
.findAny();
5656
}
5757

5858
public ScalarValue buildLanguageString( final String lexicalRepresentation, final String languageTag ) {
5959
final LangString langString = new LangString( lexicalRepresentation, Locale.forLanguageTag( languageTag ) );
6060
final Scalar type = new DefaultScalar( RDF.langString.getURI() );
61-
return new DefaultScalarValue( langString, type, null );
61+
return new DefaultScalarValue( null, langString, type );
6262
}
6363
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/instantiator/PropertyInstantiator.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import org.eclipse.esmf.aspectmodel.loader.ModelElementFactory;
2525
import org.eclipse.esmf.metamodel.Characteristic;
2626
import org.eclipse.esmf.metamodel.Property;
27-
import org.eclipse.esmf.metamodel.Scalar;
2827
import org.eclipse.esmf.metamodel.ScalarValue;
28+
import org.eclipse.esmf.metamodel.Type;
2929
import org.eclipse.esmf.metamodel.impl.DefaultCharacteristic;
3030
import org.eclipse.esmf.metamodel.impl.DefaultProperty;
31+
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
3132
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
3233
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
3334

34-
import org.apache.jena.datatypes.BaseDatatype;
3535
import org.apache.jena.rdf.model.Literal;
3636
import org.apache.jena.rdf.model.RDFNode;
3737
import org.apache.jena.rdf.model.Resource;
@@ -83,18 +83,8 @@ public Property apply( final Resource property ) {
8383
final Resource characteristicResource = attributeValue( property, SammNs.SAMM.characteristic() ).getResource();
8484
final Characteristic characteristic = modelElementFactory.create( Characteristic.class, characteristicResource );
8585

86-
final Optional<Object> exampleValue = optionalAttributeValue( property, SammNs.SAMM.exampleValue() )
87-
.flatMap( statement -> {
88-
RDFNode node = statement.getObject();
89-
return characteristic.getDataType()
90-
.map( type -> {
91-
if ( !type.is( Scalar.class ) ) {
92-
throw new AspectLoadingException( "Type of example value on Property " + property + " has incorrect type" );
93-
}
94-
return type.as( Scalar.class );
95-
} )
96-
.map( type -> buildValue( node, Optional.of( property ), type ) );
97-
} );
86+
final Optional<ScalarValue> exampleValue = optionalAttributeValue( property, SammNs.SAMM.exampleValue() )
87+
.map( statement -> buildScalarValue( statement.getObject(), characteristic.getDataType().orElseThrow() ) );
9888

9989
defProperty = new DefaultProperty( metaModelBaseAttributes, Optional.of( characteristic ),
10090
exampleValue, isOptional, isNotInPayload, payloadName, isAbstract, extends_ );
@@ -104,13 +94,29 @@ public Property apply( final Resource property ) {
10494
return defaultPropertyWrapper;
10595
}
10696

107-
private ScalarValue buildScalarValue( final Literal literal, final Scalar type ) {
108-
final Object literalValue = literal.getValue();
109-
if ( literalValue instanceof BaseDatatype.TypedValue ) {
110-
return new DefaultScalarValue( literal.getLexicalForm(), type, null );
111-
} else if ( literal.getDatatypeURI().equals( RDF.langString.getURI() ) ) {
112-
return valueInstantiator.buildLanguageString( literal.getLexicalForm(), literal.getLanguage() );
97+
private ScalarValue buildScalarValue( final RDFNode node, final Type expectedType ) {
98+
if ( node.isLiteral() ) {
99+
final Literal literal = node.asLiteral();
100+
return valueInstantiator.buildScalarValue( literal.getLexicalForm(), literal.getLanguage(), literal.getDatatypeURI() )
101+
.orElseThrow( () -> new AspectLoadingException( "Literal cannot be parsed: " + literal ) );
113102
}
114-
return new DefaultScalarValue( literalValue, type, null );
103+
104+
if ( node.isResource() ) {
105+
Resource resource = node.asResource();
106+
107+
if ( resource.hasProperty( RDF.type, SammNs.SAMM.Value() ) ) {
108+
Optional<String> valueOpt = optionalAttributeValue( resource, SammNs.SAMM.value() ).map( Statement::getString );
109+
110+
if ( valueOpt.isEmpty() ) {
111+
throw new AspectLoadingException( "samm:Value must contain a samm:value property" );
112+
}
113+
114+
return new DefaultScalarValue( buildBaseAttributes( resource ), valueOpt.get(), new DefaultScalar( expectedType.toString() ) );
115+
}
116+
117+
return new DefaultScalarValue( null, resource.getURI(), new DefaultScalar( expectedType.toString() ) );
118+
}
119+
120+
throw new AspectLoadingException( "Unexpected RDF node type: " + node );
115121
}
116122
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/instantiator/RangeConstraintInstantiator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public RangeConstraint apply( final Resource rangeConstraint ) {
4242

4343
final Optional<ScalarValue> minValue = optionalAttributeValue( rangeConstraint, SammNs.SAMMC.minValue() )
4444
.map( Statement::getLiteral )
45-
.map( literal -> new DefaultScalarValue( literal.getValue(), new DefaultScalar( literal.getDatatypeURI() ), null ) );
45+
.map( literal -> new DefaultScalarValue( null, literal.getValue(), new DefaultScalar( literal.getDatatypeURI() ) ) );
4646
final Optional<ScalarValue> maxValue = optionalAttributeValue( rangeConstraint, SammNs.SAMMC.maxValue() )
4747
.map( Statement::getLiteral )
48-
.map( literal -> new DefaultScalarValue( literal.getValue(), new DefaultScalar( literal.getDatatypeURI() ), null ) );
48+
.map( literal -> new DefaultScalarValue( null, literal.getValue(), new DefaultScalar( literal.getDatatypeURI() ) ) );
4949
final BoundDefinition lowerBoundDefinition = getBoundDefinitionForRangeValue( minValue,
5050
SammNs.SAMMC.lowerBoundDefinition(), rangeConstraint, BoundDefinition.AT_LEAST );
5151
final BoundDefinition upperBoundDefinition = getBoundDefinitionForRangeValue( maxValue,

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/loader/instantiator/ScalarValueInstantiator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public ScalarValueInstantiator( final ModelElementFactory modelElementFactory )
1818
@Override
1919
public ScalarValue apply( final Resource value ) {
2020
final MetaModelBaseAttributes metaModelBaseAttributes = buildBaseAttributes( value );
21-
return new DefaultScalarValue( value, new DefaultScalar( value.getURI() ), metaModelBaseAttributes );
21+
return new DefaultScalarValue( metaModelBaseAttributes, value, new DefaultScalar( value.getURI() ) );
2222
}
2323
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/builder/SammBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,25 +1371,25 @@ public static <T extends EntityInstanceBuilder<T>> EntityInstanceBuilder<T> enti
13711371
}
13721372

13731373
public static ScalarValue value( final String stringValue ) {
1374-
return new DefaultScalarValue( stringValue, xsd.string, null );
1374+
return new DefaultScalarValue( null, stringValue, xsd.string );
13751375
}
13761376

13771377
public static ScalarValue value( final boolean booleanValue ) {
1378-
return new DefaultScalarValue( booleanValue, xsd.boolean_, null );
1378+
return new DefaultScalarValue( null, booleanValue, xsd.boolean_ );
13791379
}
13801380

13811381
public static ScalarValue value( final float floatValue ) {
1382-
return new DefaultScalarValue( floatValue, xsd.float_, null );
1382+
return new DefaultScalarValue( null, floatValue, xsd.float_ );
13831383
}
13841384

13851385
public static ScalarValue value( final double doubleValue ) {
1386-
return new DefaultScalarValue( doubleValue, xsd.double_, null );
1386+
return new DefaultScalarValue( null, doubleValue, xsd.double_ );
13871387
}
13881388

13891389
/* Intentionally no value(int) method here, because an int value could imply different XSD types */
13901390

13911391
public static ScalarValue value( final Object value, final Scalar type ) {
1392-
return new DefaultScalarValue( value, dataTypeByUri( type.getUrn() ), null );
1392+
return new DefaultScalarValue( null, value, dataTypeByUri( type.getUrn() ) );
13931393
}
13941394

13951395
public static <T> java.util.List<ScalarValue> values( final Scalar type, final T... values ) {

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/impl/DefaultProperty.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import org.eclipse.esmf.aspectmodel.visitor.AspectVisitor;
2121
import org.eclipse.esmf.metamodel.Characteristic;
2222
import org.eclipse.esmf.metamodel.Property;
23+
import org.eclipse.esmf.metamodel.ScalarValue;
2324

2425
public class DefaultProperty extends ModelElementImpl implements Property {
2526
private final Optional<Characteristic> characteristic;
26-
private final Optional<Object> exampleValue;
27+
private final Optional<ScalarValue> exampleValue;
2728
private final boolean optional;
2829
private final boolean notInPayload;
2930
private final Optional<String> payloadName;
@@ -33,7 +34,7 @@ public class DefaultProperty extends ModelElementImpl implements Property {
3334

3435
public DefaultProperty( final MetaModelBaseAttributes metaModelBaseAttributes,
3536
final Optional<Characteristic> characteristic,
36-
final Optional<Object> exampleValue,
37+
final Optional<ScalarValue> exampleValue,
3738
final boolean optional,
3839
final boolean notInPayload,
3940
final Optional<String> payloadName,
@@ -65,7 +66,7 @@ public Optional<Characteristic> getCharacteristic() {
6566
* @return the exampleValue.
6667
*/
6768
@Override
68-
public Optional<Object> getExampleValue() {
69+
public Optional<ScalarValue> getExampleValue() {
6970
return exampleValue;
7071
}
7172

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/impl/DefaultScalarValue.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ public class DefaultScalarValue implements ScalarValue {
3131

3232
private final MetaModelBaseAttributes metaModelBaseAttributes;
3333

34-
public DefaultScalarValue( final Object value, final Scalar type,
35-
final MetaModelBaseAttributes metaModelBaseAttributes ) {
34+
public DefaultScalarValue( final MetaModelBaseAttributes metaModelBaseAttributes, final Object value, final Scalar type ) {
35+
this.metaModelBaseAttributes = metaModelBaseAttributes;
3636
this.value = value;
3737
this.type = type;
38-
this.metaModelBaseAttributes = metaModelBaseAttributes;
3938
}
4039

4140
@Override

0 commit comments

Comments
 (0)