Skip to content

Commit 2435be0

Browse files
committed
Meta Class doesn't include exampleValue
1 parent a7cf8f2 commit 2435be0

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
2626
import org.eclipse.esmf.metamodel.HasDescription;
2727
import org.eclipse.esmf.metamodel.ModelElement;
28+
import org.eclipse.esmf.metamodel.ScalarValue;
2829
import org.eclipse.esmf.metamodel.datatype.LangString;
30+
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
31+
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
2932

3033
/**
3134
* Wrapper class for the attributes all Aspect Meta Model elements have.
@@ -38,20 +41,24 @@ public class MetaModelBaseAttributes implements HasDescription {
3841
private final boolean isAnonymous;
3942
private final AspectModelFile sourceFile;
4043

44+
private final ScalarValue exampleValue;
45+
4146
private MetaModelBaseAttributes(
4247
final AspectModelUrn urn,
4348
final Set<LangString> preferredNames,
4449
final Set<LangString> descriptions,
4550
final List<String> see,
4651
final boolean isAnonymous,
47-
final AspectModelFile sourceFile
52+
final AspectModelFile sourceFile,
53+
final ScalarValue exampleValue
4854
) {
4955
this.urn = urn;
5056
this.preferredNames = preferredNames;
5157
this.descriptions = descriptions;
5258
this.see = see;
5359
this.isAnonymous = isAnonymous;
5460
this.sourceFile = sourceFile;
61+
this.exampleValue = exampleValue;
5562
}
5663

5764
public AspectModelUrn urn() {
@@ -86,6 +93,10 @@ public AspectModelFile getSourceFile() {
8693
return sourceFile;
8794
}
8895

96+
public ScalarValue getExampleValue() {
97+
return exampleValue;
98+
}
99+
89100
public static Builder builder() {
90101
return new Builder();
91102
}
@@ -105,7 +116,7 @@ public boolean equals( final Object o ) {
105116

106117
@Override
107118
public int hashCode() {
108-
return Objects.hash( urn, preferredNames, descriptions, see, isAnonymous );
119+
return Objects.hash( urn, preferredNames, descriptions, see, isAnonymous, exampleValue );
109120
}
110121

111122
/**
@@ -116,7 +127,8 @@ public int hashCode() {
116127
*/
117128
public static MetaModelBaseAttributes fromModelElement( final ModelElement modelElement ) {
118129
return new MetaModelBaseAttributes( modelElement.urn(), modelElement.getPreferredNames(),
119-
modelElement.getDescriptions(), modelElement.getSee(), modelElement.isAnonymous(), modelElement.getSourceFile() );
130+
modelElement.getDescriptions(), modelElement.getSee(), modelElement.isAnonymous(), modelElement.getSourceFile(),
131+
new DefaultScalarValue( "", new DefaultScalar( "http://www.w3.org/2001/XMLSchema#string" ) ) );
120132
}
121133

122134
public static class Builder {
@@ -127,6 +139,8 @@ public static class Builder {
127139
private boolean isAnonymous = true;
128140
private AspectModelFile sourceFile;
129141

142+
private ScalarValue exampleValue;
143+
130144
public Builder withUrn( final String urn ) {
131145
return withUrn( AspectModelUrn.fromUrn( urn ) );
132146
}
@@ -190,8 +204,13 @@ public Builder isAnonymous( final boolean isAnonymous ) {
190204
return this;
191205
}
192206

207+
public Builder withExampleValue( final ScalarValue exampleValue ) {
208+
this.exampleValue = exampleValue;
209+
return this;
210+
}
211+
193212
public MetaModelBaseAttributes build() {
194-
return new MetaModelBaseAttributes( urn, preferredNames, descriptions, see, isAnonymous, sourceFile );
213+
return new MetaModelBaseAttributes( urn, preferredNames, descriptions, see, isAnonymous, sourceFile, exampleValue );
195214
}
196215
}
197216
}

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/metamodel/StaticMetaModelJavaArtifactGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Locale;
2222
import java.util.Map;
2323
import java.util.Properties;
24+
2425
import javax.xml.datatype.DatatypeConfigurationException;
2526
import javax.xml.datatype.DatatypeConstants;
2627
import javax.xml.datatype.DatatypeFactory;
@@ -90,6 +91,7 @@
9091
import org.eclipse.esmf.metamodel.impl.DefaultComplexType;
9192
import org.eclipse.esmf.metamodel.impl.DefaultEntity;
9293
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
94+
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
9395
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
9496
import org.eclipse.esmf.samm.KnownVersion;
9597
import org.eclipse.esmf.staticmetamodel.PropertyContainer;
@@ -170,6 +172,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
170172
.put( "DefaultRangeConstraint", DefaultRangeConstraint.class )
171173
.put( "DefaultRegularExpressionConstraint", DefaultRegularExpressionConstraint.class )
172174
.put( "DefaultScalar", DefaultScalar.class )
175+
.put( "DefaultScalarValue", DefaultScalarValue.class )
173176
.put( "DefaultSet", DefaultSet.class )
174177
.put( "DefaultSingleEntity", DefaultSingleEntity.class )
175178
.put( "DefaultSortedSet", DefaultSortedSet.class )

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/metamodel/StaticMetaModelVisitor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ private <T> String getOptionalStaticDeclarationValue( final Type type, final Opt
527527
}
528528

529529
if ( optionalValue.get() instanceof ScalarValue ) {
530-
return "Optional.of(" + ( (ScalarValue) optionalValue.get() ).accept( this, context ) + ")";
530+
return "Optional.of(" + ((ScalarValue) optionalValue.get()).accept( this, context ) + ")";
531531
}
532532

533533
context.getCodeGenerationConfig().importTracker().importExplicit( AspectModelJavaUtil.getDataTypeClass( type ) );
@@ -574,6 +574,15 @@ public String getMetaModelBaseAttributes( final ModelElement element, final Stat
574574
} );
575575
element.getSee().stream().sorted()
576576
.forEach( see -> builder.append( ".withSee(" ).append( AspectModelJavaUtil.createLiteral( see ) ).append( ")" ) );
577+
578+
if ( element instanceof final Property property ) {
579+
property.getExampleValue().ifPresent( exampleValue ->
580+
builder.append( ".withExampleValue(" )
581+
.append( this.visitScalarValue( property.getExampleValue().get(), context ) )
582+
.append( ")" )
583+
);
584+
}
585+
577586
builder.append( ".build()" );
578587
return builder.toString();
579588
}

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/StaticMetaModelJavaGeneratorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import java.time.LocalDate;
2525
import java.util.HashMap;
2626
import java.util.List;
27+
import java.util.Map;
2728
import java.util.Optional;
29+
import java.util.Set;
2830
import java.util.regex.Pattern;
31+
2932
import javax.xml.datatype.DatatypeFactory;
3033
import javax.xml.datatype.XMLGregorianCalendar;
3134

@@ -498,6 +501,26 @@ void testCharacteristicInstantiationForQuantifiableWithoutUnit() throws IOExcept
498501
ImmutableMap.<String, String> builder().put( "TEST_PROPERTY", expectedTestPropertyCharacteristicConstructorCall ).build(), 1 );
499502
}
500503

504+
@Test
505+
void testCharacteristicInstantiationForPropertyWithExampleValue() throws IOException {
506+
final TestAspect aspect = TestAspect.ASPECT_WITH_COLLECTION;
507+
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
508+
509+
result.assertNumberOfFiles( 2 );
510+
511+
Map<String, Set<String>> expectedBaseAttributes = new HashMap<>();
512+
expectedBaseAttributes.put( "TEST_PROPERTY", Set.of(
513+
"withUrn(AspectModelUrn.fromUrn(NAMESPACE + \"testProperty\"))",
514+
"withPreferredName(Locale.forLanguageTag(\"en\"), \"Test Property\")",
515+
"withDescription(Locale.forLanguageTag(\"en\"), \"This is a test property.\")",
516+
"withSee(\"http://example.com/\")",
517+
"withSee(\"http://example.com/me\")",
518+
"withExampleValue(new DefaultScalarValue(\"Example Value\", new DefaultScalar(\"http://www.w3.org/2001/XMLSchema#string\")))"
519+
) );
520+
521+
result.assertMetaModelBaseAttributesForProperties( "MetaAspectWithCollection", expectedBaseAttributes );
522+
}
523+
501524
@Test
502525
void testCharacteristicInstantiationForQuantifiableWithUnit() throws IOException {
503526
final TestAspect aspect = TestAspect.ASPECT_WITH_QUANTIFIABLE_WITH_UNIT;

0 commit comments

Comments
 (0)