Skip to content

Commit e108f1e

Browse files
authored
Merge pull request #304 from bci-oss/303-load-curie-values
Successfully load models containing and generate JSON for bamm:curie
2 parents 58aeddc + 3012958 commit e108f1e

File tree

8 files changed

+159
-11
lines changed

8 files changed

+159
-11
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package io.openmanufacturing.sds.metamodel.loader;
15+
16+
import java.util.Optional;
17+
18+
import org.apache.jena.datatypes.DatatypeFormatException;
19+
import org.apache.jena.datatypes.RDFDatatype;
20+
import org.apache.jena.graph.impl.LiteralLabel;
21+
22+
import io.openmanufacturing.sds.aspectmetamodel.KnownVersion;
23+
import io.openmanufacturing.sds.aspectmodel.resolver.services.TypedRdfDatatype;
24+
import io.openmanufacturing.sds.aspectmodel.vocabulary.BAMM;
25+
import io.openmanufacturing.sds.metamodel.datatypes.Curie;
26+
27+
public class CurieRdfType implements TypedRdfDatatype<Curie> {
28+
@Override
29+
public Optional<Curie> parseTyped( final String lexicalForm ) {
30+
if ( isValid( lexicalForm ) ) {
31+
return Optional.of( new Curie( lexicalForm ) );
32+
}
33+
return Optional.empty();
34+
}
35+
36+
@Override
37+
public String unparseTyped( final Curie value ) {
38+
return value.getValue();
39+
}
40+
41+
@Override
42+
public String getURI() {
43+
return new BAMM( KnownVersion.getLatest() ).curie().getURI();
44+
}
45+
46+
@Override
47+
public String unparse( final Object value ) {
48+
if ( value instanceof Curie curie ) {
49+
return unparseTyped( curie );
50+
}
51+
throw new AspectLoadingException( "Value is no valid curie: " + value );
52+
}
53+
54+
@Override
55+
public Object parse( final String lexicalForm ) throws DatatypeFormatException {
56+
return parseTyped( lexicalForm ).orElseThrow( () -> new DatatypeFormatException() );
57+
}
58+
59+
@Override
60+
public boolean isValid( final String lexicalForm ) {
61+
return lexicalForm.matches( "[^:]*:.*" );
62+
}
63+
64+
@Override
65+
public boolean isValidValue( final Object valueForm ) {
66+
return isValid( valueForm.toString() );
67+
}
68+
69+
@Override
70+
public boolean isValidLiteral( final LiteralLabel lit ) {
71+
return isValid( lit.getLexicalForm() );
72+
}
73+
74+
@Override
75+
public boolean isEqual( final LiteralLabel value1, final LiteralLabel value2 ) {
76+
return value1.getLexicalForm().equals( value2.getLexicalForm() );
77+
}
78+
79+
@Override
80+
public int getHashCode( final LiteralLabel lit ) {
81+
return lit.getDefaultHashcode();
82+
}
83+
84+
@Override
85+
public Class<Curie> getJavaClass() {
86+
return (Class<Curie>) Curie.class;
87+
}
88+
89+
@Override
90+
public Object cannonicalise( final Object value ) {
91+
return value;
92+
}
93+
94+
@Override
95+
public Object extendedTypeDefinition() {
96+
return null;
97+
}
98+
99+
@Override
100+
public RDFDatatype normalizeSubType( final Object value, final RDFDatatype datatype ) {
101+
return datatype;
102+
}
103+
}

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/loader/Instantiator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.Map;
2323
import java.util.Optional;
2424
import java.util.function.Function;
25+
import java.util.function.Predicate;
2526
import java.util.stream.Collectors;
2627
import java.util.stream.Stream;
2728

29+
import org.apache.jena.datatypes.RDFDatatype;
2830
import org.apache.jena.rdf.model.Literal;
2931
import org.apache.jena.rdf.model.Model;
3032
import org.apache.jena.rdf.model.RDFList;
@@ -51,6 +53,7 @@
5153
import io.openmanufacturing.sds.metamodel.ScalarValue;
5254
import io.openmanufacturing.sds.metamodel.Type;
5355
import io.openmanufacturing.sds.metamodel.Value;
56+
import io.openmanufacturing.sds.metamodel.datatypes.Curie;
5457
import io.openmanufacturing.sds.metamodel.datatypes.LangString;
5558
import io.openmanufacturing.sds.metamodel.impl.DefaultCollectionValue;
5659
import io.openmanufacturing.sds.metamodel.impl.DefaultEntityInstance;
@@ -64,6 +67,7 @@ public abstract class Instantiator<T extends ModelElement> extends AttributeValu
6467
protected UNIT unit;
6568
protected Model model;
6669
protected KnownVersion metaModelVersion;
70+
protected final RDFDatatype curieDataType = new CurieRdfType();
6771

6872
public Instantiator( final ModelElementFactory modelElementFactory, final Class<T> targetClass ) {
6973
super( modelElementFactory.getBamm() );
@@ -226,7 +230,7 @@ private ScalarValue buildScalarValue( final Literal literal ) {
226230
return new DefaultScalarValue( langString, type );
227231
}
228232

229-
return ExtendedXsdDataType.supportedXsdTypes.stream()
233+
return Stream.concat( ExtendedXsdDataType.supportedXsdTypes.stream(), Stream.of( curieDataType ) )
230234
.filter( type -> type.getURI().equals( literal.getDatatypeURI() ) )
231235
.map( type -> type.parse( literal.getLexicalForm() ) )
232236
.map( value -> new DefaultScalarValue( value, new DefaultScalar( literal.getDatatypeURI(), metaModelVersion ) ) )

core/sds-aspect-meta-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/services/ExtendedXsdDataType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class ExtendedXsdDataType<T> extends XSDDatatype implements TypedRdfDatat
4545
private static boolean checking = true;
4646
private static final ExtendedSchemaDVFactoryImpl extendedSchemaDVFactory = new ExtendedSchemaDVFactoryImpl();
4747

48-
private ExtendedXsdDataType( final Resource dataTypeResource, final Class<T> correspondingJavaClass,
48+
public ExtendedXsdDataType( final Resource dataTypeResource, final Class<T> correspondingJavaClass,
4949
final Function<String, T> parser,
5050
final Function<T, String> unparser,
5151
final Predicate<String> lexicalValidator ) {

core/sds-aspect-meta-model-types/src/main/java/io/openmanufacturing/sds/metamodel/datatypes/Curie.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public boolean equals( final Object o ) {
4545
public int hashCode() {
4646
return value != null ? value.hashCode() : 0;
4747
}
48+
49+
@Override
50+
public String toString() {
51+
return value;
52+
}
4853
}

core/sds-aspect-model-document-generators/src/main/java/io/openmanufacturing/sds/aspectmodel/generator/json/ValueToPayloadStructure.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121

2222
import com.google.common.collect.ImmutableMap;
2323

24-
import io.openmanufacturing.sds.metamodel.ModelElement;
2524
import io.openmanufacturing.sds.metamodel.CollectionValue;
2625
import io.openmanufacturing.sds.metamodel.EntityInstance;
26+
import io.openmanufacturing.sds.metamodel.ModelElement;
2727
import io.openmanufacturing.sds.metamodel.Property;
2828
import io.openmanufacturing.sds.metamodel.ScalarValue;
2929
import io.openmanufacturing.sds.metamodel.Value;
30+
import io.openmanufacturing.sds.metamodel.datatypes.Curie;
3031
import io.openmanufacturing.sds.metamodel.datatypes.LangString;
3132
import io.openmanufacturing.sds.metamodel.visitor.AspectVisitor;
3233

@@ -47,6 +48,9 @@ public Object visitScalarValue( final ScalarValue scalarValue, final Void contex
4748
final LangString langString = (LangString) scalarValue.getValue();
4849
return ImmutableMap.of( langString.getLanguageTag().toLanguageTag(), langString.getValue() );
4950
}
51+
if ( scalarValue.getValue() instanceof Curie curie ) {
52+
return curie.getValue();
53+
}
5054
return scalarValue.getValue();
5155
}
5256

core/sds-aspect-model-document-generators/src/test/java/io/openmanufacturing/sds/aspectmodel/generator/jsonschema/AspectModelJsonSchemaGeneratorTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
package io.openmanufacturing.sds.aspectmodel.generator.jsonschema;
1515

16-
import static org.assertj.core.api.Assertions.*;
17-
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1818
import static org.junit.jupiter.api.Assertions.fail;
1919

2020
import java.io.ByteArrayOutputStream;
@@ -111,17 +111,19 @@ private JsonSchema parseSchema( final JsonNode jsonSchema ) {
111111
}
112112

113113
private void assertPayloadIsValid( final JsonNode schema, final JsonNode payload ) {
114-
System.out.println( "Payload:" );
115-
showJson( payload );
116-
System.out.println( "Schema:" );
117-
showJson( schema );
118-
assertThatCode( () -> {
114+
try {
119115
final ProcessingReport report = parseSchema( schema ).validate( payload );
120116
if ( !report.isSuccess() ) {
121117
System.out.println( report );
122118
}
123119
assertThat( report.isSuccess() ).isTrue();
124-
} ).doesNotThrowAnyException();
120+
} catch ( final Throwable throwable ) {
121+
System.out.println( "Payload:" );
122+
showJson( payload );
123+
System.out.println( "Schema:" );
124+
showJson( schema );
125+
fail();
126+
}
125127
}
126128

127129
private void assertPayloadIsValid( final JsonNode schema, final Aspect aspect ) {

core/sds-test-aspect-models/src/main/java/io/openmanufacturing/sds/test/TestAspect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum TestAspect implements TestModel {
5454
ASPECT_WITH_CONSTRAINT_WITH_MULTIPLE_SEE_ATTRIBUTES,
5555
ASPECT_WITH_CONSTRAINT_WITH_SEE_ATTRIBUTE,
5656
ASPECT_WITH_CURIE,
57+
ASPECT_WITH_CURIE_ENUMERATION,
5758
ASPECT_WITH_CUSTOM_NAMESPACE,
5859
ASPECT_WITH_CUSTOM_UNIT,
5960
ASPECT_WITH_DATE_TIME_TYPE_FOR_RANGE_CONSTRAINTS,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
@prefix : <urn:bamm:io.openmanufacturing.test:1.0.0#> .
13+
@prefix bamm: <urn:bamm:io.openmanufacturing:meta-model:2.0.0#> .
14+
@prefix bamm-c: <urn:bamm:io.openmanufacturing:characteristic:2.0.0#> .
15+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
16+
17+
:AspectWithCurieEnumeration a bamm:Aspect ;
18+
bamm:properties ( :testProperty ) ;
19+
bamm:operations ( ) .
20+
21+
:testProperty a bamm:Property ;
22+
bamm:exampleValue "unit:hectopascal"^^bamm:curie ;
23+
bamm:characteristic :TestEnumeration .
24+
25+
:TestEnumeration a bamm-c:Enumeration ;
26+
bamm:preferredName "Test Enumeration"@en ;
27+
bamm:description "This is a test for enumeration."@en ;
28+
bamm:dataType bamm:curie ;
29+
bamm-c:values ( "unit:hectopascal"^^bamm:curie "unit:gram"^^bamm:curie ) .

0 commit comments

Comments
 (0)