Skip to content

Commit

Permalink
Meta Class doesn't include exampleValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Yauhenikapl committed Nov 17, 2024
1 parent a7cf8f2 commit 2435be0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
import org.eclipse.esmf.metamodel.HasDescription;
import org.eclipse.esmf.metamodel.ModelElement;
import org.eclipse.esmf.metamodel.ScalarValue;
import org.eclipse.esmf.metamodel.datatype.LangString;
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;

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

private final ScalarValue exampleValue;

private MetaModelBaseAttributes(
final AspectModelUrn urn,
final Set<LangString> preferredNames,
final Set<LangString> descriptions,
final List<String> see,
final boolean isAnonymous,
final AspectModelFile sourceFile
final AspectModelFile sourceFile,
final ScalarValue exampleValue
) {
this.urn = urn;
this.preferredNames = preferredNames;
this.descriptions = descriptions;
this.see = see;
this.isAnonymous = isAnonymous;
this.sourceFile = sourceFile;
this.exampleValue = exampleValue;
}

public AspectModelUrn urn() {
Expand Down Expand Up @@ -86,6 +93,10 @@ public AspectModelFile getSourceFile() {
return sourceFile;
}

public ScalarValue getExampleValue() {
return exampleValue;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -105,7 +116,7 @@ public boolean equals( final Object o ) {

@Override
public int hashCode() {
return Objects.hash( urn, preferredNames, descriptions, see, isAnonymous );
return Objects.hash( urn, preferredNames, descriptions, see, isAnonymous, exampleValue );
}

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

public static class Builder {
Expand All @@ -127,6 +139,8 @@ public static class Builder {
private boolean isAnonymous = true;
private AspectModelFile sourceFile;

private ScalarValue exampleValue;

public Builder withUrn( final String urn ) {
return withUrn( AspectModelUrn.fromUrn( urn ) );
}
Expand Down Expand Up @@ -190,8 +204,13 @@ public Builder isAnonymous( final boolean isAnonymous ) {
return this;
}

public Builder withExampleValue( final ScalarValue exampleValue ) {
this.exampleValue = exampleValue;
return this;
}

public MetaModelBaseAttributes build() {
return new MetaModelBaseAttributes( urn, preferredNames, descriptions, see, isAnonymous, sourceFile );
return new MetaModelBaseAttributes( urn, preferredNames, descriptions, see, isAnonymous, sourceFile, exampleValue );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
Expand Down Expand Up @@ -90,6 +91,7 @@
import org.eclipse.esmf.metamodel.impl.DefaultComplexType;
import org.eclipse.esmf.metamodel.impl.DefaultEntity;
import org.eclipse.esmf.metamodel.impl.DefaultScalar;
import org.eclipse.esmf.metamodel.impl.DefaultScalarValue;
import org.eclipse.esmf.metamodel.vocabulary.SammNs;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.staticmetamodel.PropertyContainer;
Expand Down Expand Up @@ -170,6 +172,7 @@ public JavaArtifact apply( final E element, final JavaCodeGenerationConfig confi
.put( "DefaultRangeConstraint", DefaultRangeConstraint.class )
.put( "DefaultRegularExpressionConstraint", DefaultRegularExpressionConstraint.class )
.put( "DefaultScalar", DefaultScalar.class )
.put( "DefaultScalarValue", DefaultScalarValue.class )
.put( "DefaultSet", DefaultSet.class )
.put( "DefaultSingleEntity", DefaultSingleEntity.class )
.put( "DefaultSortedSet", DefaultSortedSet.class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private <T> String getOptionalStaticDeclarationValue( final Type type, final Opt
}

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

context.getCodeGenerationConfig().importTracker().importExplicit( AspectModelJavaUtil.getDataTypeClass( type ) );
Expand Down Expand Up @@ -574,6 +574,15 @@ public String getMetaModelBaseAttributes( final ModelElement element, final Stat
} );
element.getSee().stream().sorted()
.forEach( see -> builder.append( ".withSee(" ).append( AspectModelJavaUtil.createLiteral( see ) ).append( ")" ) );

if ( element instanceof final Property property ) {
property.getExampleValue().ifPresent( exampleValue ->
builder.append( ".withExampleValue(" )
.append( this.visitScalarValue( property.getExampleValue().get(), context ) )
.append( ")" )
);
}

builder.append( ".build()" );
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

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

@Test
void testCharacteristicInstantiationForPropertyWithExampleValue() throws IOException {
final TestAspect aspect = TestAspect.ASPECT_WITH_COLLECTION;
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );

result.assertNumberOfFiles( 2 );

Map<String, Set<String>> expectedBaseAttributes = new HashMap<>();
expectedBaseAttributes.put( "TEST_PROPERTY", Set.of(
"withUrn(AspectModelUrn.fromUrn(NAMESPACE + \"testProperty\"))",
"withPreferredName(Locale.forLanguageTag(\"en\"), \"Test Property\")",
"withDescription(Locale.forLanguageTag(\"en\"), \"This is a test property.\")",
"withSee(\"http://example.com/\")",
"withSee(\"http://example.com/me\")",
"withExampleValue(new DefaultScalarValue(\"Example Value\", new DefaultScalar(\"http://www.w3.org/2001/XMLSchema#string\")))"
) );

result.assertMetaModelBaseAttributesForProperties( "MetaAspectWithCollection", expectedBaseAttributes );
}

@Test
void testCharacteristicInstantiationForQuantifiableWithUnit() throws IOException {
final TestAspect aspect = TestAspect.ASPECT_WITH_QUANTIFIABLE_WITH_UNIT;
Expand Down

0 comments on commit 2435be0

Please sign in to comment.