@@ -63,84 +63,85 @@ fun FlexoModelHandler.extractModelElementToJson(elementIri: String): JsonObject
6363 out .forEach { (predicate, values) ->
6464 // extract the suffix name part
6565 var propertyKey = predicate.uri.autoSuffix
66+
67+ // reference the first value
6668 val obj = values.elementAt(0 )
69+
6770 // relations
6871 if (predicate.uri.startsWith(SYSMLV2 .RELATION )) {
6972 // multiple values means it's an array, skip and prefer JSON annotation
7073 // if we've already seen a JSON annotation with the same property key then ignore
7174 if (values.size > 1 || seenArrays.contains(propertyKey)) return @forEach
72- if (obj.isResource()) {
75+
76+ // object is a resource
77+ if (obj.isResource) {
7378 put(propertyKey, buildJsonObject {
7479 put(" @id" , obj.asResource().uri.autoSuffix)
7580 })
76- } else {
81+ }
82+ // invalid
83+ else {
7784 throw InvalidTripleError (" Relation cannot be literal" , elementIri, predicate, obj)
7885 }
7986 }
80- // properties & annotations
81- else {
82- // transform the single object
83- val obj = values.elementAt(0 )
84-
85- // properties
86- if (predicate.uri.startsWith(SYSMLV2 .PROPERTY )) {
87- // multiple values means it's an array, skip and prefer JSON annotation
88- // if we've already seen a JSON annotation with the same property key then ignore
89- if (values.size > 1 || seenArrays.contains(propertyKey)) return @forEach
90-
91- // object is a Literal
92- if (obj.isLiteral) {
93- val lit = obj.asLiteral()
94-
95- // depending on its datatype
96- when (lit.datatype.uri) {
97- XSD .xboolean.uri -> put(propertyKey, lit.boolean)
98- XSD .integer.uri -> put(propertyKey, lit.int)
99- XSD .decimal.uri, XSD .xdouble.uri -> put(propertyKey, lit.float)
100- else -> put(propertyKey, lit.string)
101- }
102- }
103- // object is a Resource
104- else {
105- throw InvalidTripleError (" Property cannot be resource" , elementIri, predicate, obj)
106- }
107- }
108- // annotations
109- else if (predicate.uri.startsWith(SYSMLV2 .ANNOTATION_JSON )) {
110- // expect exactly 1 object
111- if (values.size != 1 ) {
112- throw InvalidTripleError (" Expected exactly 1 object with this predicate" , elementIri, predicate, obj)
113- }
114-
115- // object is not a Literal
116- if (! obj.isLiteral) {
117- throw InvalidTripleError (" Expected annotation property to point to an RDF literal" , elementIri, predicate, obj)
118- }
87+ // properties
88+ else if (predicate.uri.startsWith(SYSMLV2 .PROPERTY )) {
89+ // multiple values means it's an array, skip and prefer JSON annotation
90+ // if we've already seen a JSON annotation with the same property key then ignore
91+ if (values.size > 1 || seenArrays.contains(propertyKey)) return @forEach
11992
120- // cast to literal
93+ // object is a Literal
94+ if (obj.isLiteral) {
12195 val lit = obj.asLiteral()
12296
123- // expect valid JSON
124- val jsonElement = try {
125- Json .parseToJsonElement(lit.string)
126- } catch (parse: Error ) {
127- throw InvalidTripleError (" Expected annotation property to encode a JSON element" , elementIri, predicate, obj)
97+ // depending on its datatype
98+ when (lit.datatype.uri) {
99+ XSD .xboolean.uri -> put(propertyKey, lit.boolean)
100+ XSD .integer.uri -> put(propertyKey, lit.int)
101+ XSD .decimal.uri, XSD .xdouble.uri -> put(propertyKey, lit.float)
102+ else -> put(propertyKey, lit.string)
128103 }
104+ }
105+ // object is a Resource
106+ else {
107+ throw InvalidTripleError (" Property cannot be resource" , elementIri, predicate, obj)
108+ }
109+ }
110+ // annotations
111+ else if (predicate.uri.startsWith(SYSMLV2 .ANNOTATION_JSON )) {
112+ // expect exactly 1 object
113+ if (values.size != 1 ) {
114+ throw InvalidTripleError (" Expected exactly 1 object with this predicate" , elementIri, predicate, obj)
115+ }
129116
130- // infer property key from URN
131- propertyKey = predicate.uri.urnSuffix
117+ // object is not a Literal
118+ if (! obj.isLiteral) {
119+ throw InvalidTripleError (" Expected annotation property to point to an RDF literal" , elementIri, predicate, obj)
120+ }
132121
133- // add parsed element to JSON object
134- put(propertyKey, jsonElement )
122+ // cast to literal
123+ val lit = obj.asLiteral( )
135124
136- // do not overwrite this property
137- seenArrays.add(propertyKey)
138- }
139- // something else
140- else {
141- // throw InvalidTripleError("Unrecognized triple purpose", elementIri, predicate, obj)
125+ // expect valid JSON
126+ val jsonElement = try {
127+ Json .parseToJsonElement(lit.string)
128+ } catch (parse: Error ) {
129+ throw InvalidTripleError (" Expected annotation property to encode a JSON element" , elementIri, predicate, obj)
142130 }
143- // }
131+
132+ // infer property key from URN
133+ propertyKey = predicate.uri.urnSuffix
134+
135+ // add parsed element to JSON object
136+ put(propertyKey, jsonElement)
137+
138+ // do not overwrite this property
139+ seenArrays.add(propertyKey)
140+ }
141+ // something else
142+ else {
143+ // throw InvalidTripleError("Unrecognized triple purpose", elementIri, predicate, obj)
144+ }
144145 }
145146 }
146147}
0 commit comments