This repository was archived by the owner on Feb 15, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
main/java/io/adminshell/aas/v3/dataformat/rdf
test/java/io/adminshell/aas/v3/dataformat/rdf Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -28,13 +28,34 @@ public class JsonLdEnumSerializer extends JsonSerializer<Enum<?>> {
2828
2929 @ Override
3030 public void serialize (Enum value , JsonGenerator gen , SerializerProvider provider ) throws IOException {
31+ //Generated Enum classes of the admin shell have @IRI annotations, which need to be used to provide proper RDF
3132 if (value .getClass ().isEnum () && value .getClass ().getName ().startsWith ("io.adminshell.aas." ))
3233 {
34+ //Try to get annotation value to get the IRI used in the ontology
3335 if (value .getClass ().getAnnotation (IRI .class ) != null && value .getClass ().getAnnotation (IRI .class ).value ().length > 0 )
3436 {
3537 gen .writeStartObject ();
3638 gen .writeStringField ("@type" , value .getClass ().getAnnotation (IRI .class ).value ()[0 ]);
37- gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
39+
40+ //Try to extract exact IRI of the enum value, if present
41+ try {
42+ var annotation = value .getClass ().getField (value .name ()).getAnnotation (IRI .class );
43+ if (annotation != null && annotation .value ().length > 0 )
44+ {
45+ gen .writeStringField ("@id" , annotation .value ()[0 ]);
46+ }
47+ else
48+ {
49+ //Didn't find an @IRI annotation - fall back to using class annotation + field name
50+ gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
51+ }
52+ }
53+ //Should be impossible
54+ catch (NoSuchFieldException e )
55+ {
56+ //Didn't find an @IRI annotation - fall back to using class annotation + field name
57+ gen .writeStringField ("@id" , translate (value .getClass (), value .name ()));
58+ }
3859 gen .writeEndObject ();
3960 }
4061 else
Original file line number Diff line number Diff line change 1515 */
1616package io .adminshell .aas .v3 .dataformat .rdf ;
1717
18- import io .adminshell .aas .v3 .dataformat .rdf .Serializer ;
1918import io .adminshell .aas .v3 .model .*;
2019import io .adminshell .aas .v3 .model .impl .*;
2120import org .apache .jena .riot .RDFLanguages ;
@@ -67,7 +66,7 @@ public void serializeEnvironment() throws IOException {
6766 .submodels (submodel )
6867 .build ();
6968 String output = new Serializer ().serialize (aasEnv , RDFLanguages .JSONLD );
70- // System.out.println(output);
69+ System .out .println (output );
7170 Assert .assertTrue (output .contains ("@context" ));
7271 Assert .assertTrue (output .contains ("rdf:" ));
7372
You can’t perform that action at this time.
0 commit comments