diff --git a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASVisitor.java b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASVisitor.java index 5138e5f4f..781ae2168 100644 --- a/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASVisitor.java +++ b/core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASVisitor.java @@ -23,6 +23,37 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn; +import org.eclipse.esmf.aspectmodel.vocabulary.SAMM; +import org.eclipse.esmf.characteristic.Code; +import org.eclipse.esmf.characteristic.Collection; +import org.eclipse.esmf.characteristic.Duration; +import org.eclipse.esmf.characteristic.Either; +import org.eclipse.esmf.characteristic.Enumeration; +import org.eclipse.esmf.characteristic.Measurement; +import org.eclipse.esmf.characteristic.Quantifiable; +import org.eclipse.esmf.characteristic.SingleEntity; +import org.eclipse.esmf.characteristic.SortedSet; +import org.eclipse.esmf.characteristic.State; +import org.eclipse.esmf.characteristic.StructuredValue; +import org.eclipse.esmf.characteristic.Trait; +import org.eclipse.esmf.metamodel.Aspect; +import org.eclipse.esmf.metamodel.Characteristic; +import org.eclipse.esmf.metamodel.CollectionValue; +import org.eclipse.esmf.metamodel.Entity; +import org.eclipse.esmf.metamodel.EntityInstance; +import org.eclipse.esmf.metamodel.ModelElement; +import org.eclipse.esmf.metamodel.Property; +import org.eclipse.esmf.metamodel.Scalar; +import org.eclipse.esmf.metamodel.ScalarValue; +import org.eclipse.esmf.metamodel.Type; +import org.eclipse.esmf.metamodel.loader.MetaModelBaseAttributes; +import org.eclipse.esmf.metamodel.visitor.AspectVisitor; +import org.eclipse.esmf.samm.KnownVersion; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.google.common.collect.ImmutableMap; import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.ResourceFactory; import org.apache.jena.vocabulary.RDF; @@ -67,40 +98,12 @@ import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultValueList; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultValueReferencePair; -import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn; -import org.eclipse.esmf.aspectmodel.vocabulary.SAMM; -import org.eclipse.esmf.characteristic.Code; -import org.eclipse.esmf.characteristic.Collection; -import org.eclipse.esmf.characteristic.Duration; -import org.eclipse.esmf.characteristic.Either; -import org.eclipse.esmf.characteristic.Enumeration; -import org.eclipse.esmf.characteristic.Measurement; -import org.eclipse.esmf.characteristic.Quantifiable; -import org.eclipse.esmf.characteristic.SingleEntity; -import org.eclipse.esmf.characteristic.SortedSet; -import org.eclipse.esmf.characteristic.State; -import org.eclipse.esmf.characteristic.StructuredValue; -import org.eclipse.esmf.characteristic.Trait; -import org.eclipse.esmf.metamodel.Aspect; -import org.eclipse.esmf.metamodel.Characteristic; -import org.eclipse.esmf.metamodel.Entity; -import org.eclipse.esmf.metamodel.ModelElement; -import org.eclipse.esmf.metamodel.Property; -import org.eclipse.esmf.metamodel.Scalar; -import org.eclipse.esmf.metamodel.Type; -import org.eclipse.esmf.metamodel.loader.MetaModelBaseAttributes; -import org.eclipse.esmf.metamodel.visitor.AspectVisitor; -import org.eclipse.esmf.samm.KnownVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.google.common.collect.ImmutableMap; - public class AspectModelAASVisitor implements AspectVisitor { - private static final Logger LOG = LoggerFactory.getLogger( AspectModelAASVisitor.class ); + private static final ValueSerializer VALUE_SERIALIZER = new ValueSerializer(); public static final String ADMIN_SHELL_NAME = "defaultAdminShell"; public static final String DEFAULT_LOCALE = "en"; @@ -642,17 +645,16 @@ public Environment visitEnumeration( final Enumeration enumeration, final Contex dataSpecificationContent.setDataType( mapIEC61360DataType( enumeration ) ); final List valueReferencePairs = enumeration.getValues().stream() - .map( - x -> - new DefaultValueReferencePair.Builder() - .value( x.toString() ) - .valueID( buildReferenceToEnumValue( enumeration, x ) ) - .build() ) + .map( enumerationValue -> { + final String value = enumerationValue.accept( VALUE_SERIALIZER, enumeration ); + return new DefaultValueReferencePair.Builder() + .value( value ) + .valueID( buildReferenceToEnumValue( enumeration, value ) ) + .build(); + } ) .collect( Collectors.toList() ); - final ValueList valueList = - new DefaultValueList.Builder().valueReferencePairs( valueReferencePairs ).build(); - + final ValueList valueList = new DefaultValueList.Builder().valueReferencePairs( valueReferencePairs ).build(); dataSpecificationContent.setValueList( valueList ); } @@ -691,4 +693,32 @@ public Environment visitTrait( final Trait trait, final Context context ) { // ignored and have to be deduced by resolving a SAMM model referenced by its semanticID return visitCharacteristic( trait.getBaseCharacteristic(), context ); } + + public static class ValueSerializer implements AspectVisitor { + @Override + public String visitBase( final ModelElement modelElement, final ModelElement context ) { + throw new UnsupportedOperationException(); + } + + @Override + public String visitScalarValue( final ScalarValue value, final ModelElement context ) { + return context.is( Characteristic.class ) + ? value.getValue().toString() + : "\"" + value.getValue().toString() + "\""; + } + + @Override + public String visitEntityInstance( final EntityInstance instance, final ModelElement context ) { + return instance.getAssertions().entrySet().stream().map( entry -> + String.format( "\"%s\":%s", entry.getKey().getName(), entry.getValue().accept( this, instance ) ) ) + .collect( Collectors.joining( ",", "{", "}" ) ); + } + + @Override + public String visitCollectionValue( final CollectionValue value, final ModelElement context ) { + return value.getValues().stream().map( collectionValue -> + collectionValue.accept( this, value ) ) + .collect( Collectors.joining( ",", "[", "]" ) ); + } + } } diff --git a/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGeneratorTest.java b/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGeneratorTest.java index 7e2bd0bc3..b69592fc5 100644 --- a/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGeneratorTest.java +++ b/core/esmf-aspect-model-aas-generator/src/test/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGeneratorTest.java @@ -321,6 +321,12 @@ void testGenerateAasxFromAspectModelWithEnumeration() throws IOException, Deseri public void testGeneration( final TestAspect testAspect ) throws IOException, DeserializationException, SAXException { final ByteArrayOutputStream baos = getByteArrayOutputStreamFromAspect( testAspect ); final byte[] xmlFile = baos.toByteArray(); + + final String aasXml = new String( xmlFile ); + assertThat( aasXml ).doesNotContain( "DefaultScalarValue[" ); + assertThat( aasXml ).doesNotContain( "DefaultEntity[" ); + assertThat( aasXml ).doesNotContain( "Optional[" ); + final Environment env = loadAASX( new ByteArrayInputStream( xmlFile ) ); assertFalse( env.getSubmodels().isEmpty(), "No Submodel in AAS present." ); try {