Skip to content

Commit

Permalink
Fix static Java code generation to work with custom quantity kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Nov 3, 2023
1 parent 0f18c2b commit dd7f296
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.vocabulary.RDF;

import org.eclipse.esmf.aspectmodel.java.exception.CodeGenerationException;
import org.eclipse.esmf.aspectmodel.resolver.services.DataType;
import org.eclipse.esmf.metamodel.CollectionValue;
Expand All @@ -37,7 +38,8 @@
* <li>If the value is (int) 3, it will return "3"</li>
* <li>If the value is (String) "hi", it will return "\"hi\""</li>
* <li>If the value is (LangString) "hi"@en, it will return "new LangString(\"hi\", Locale.forLanguageTag(\"en\"))"</li>
* <li>If the value is a collection, it will return the corresponding collection, e.g. "new ArrayList<>(){{ add(1); add(2); add(3); }}"</></li>
* <li>If the value is a collection, it will return the corresponding collection, e.g. "new ArrayList<>(){{ add(1); add(2); add(3); }}
* "</></li>
* <li>If the value is an Entity, it will return the corresponding constructor call, e.g. "new MyEntity(\"foo\", 2, 3)"</li>
* </ul>
*/
Expand Down Expand Up @@ -70,7 +72,8 @@ private String generateValueExpression( final ScalarValue value, final Context c
context.getCodeGenerationConfig().importTracker().importExplicit( LangString.class );
context.getCodeGenerationConfig().importTracker().importExplicit( Locale.class );
final LangString langStringValue = (LangString) value.as( ScalarValue.class ).getValue();
return String.format( "new LangString(\"%s\", Locale.forLanguageTag(\"%s\"))", AspectModelJavaUtil.escapeForLiteral( langStringValue.getValue() ),
return String.format( "new LangString(\"%s\", Locale.forLanguageTag(\"%s\"))",
AspectModelJavaUtil.escapeForLiteral( langStringValue.getValue() ),
langStringValue.getLanguageTag().toLanguageTag() );
}

Expand Down Expand Up @@ -107,7 +110,8 @@ public String visitEntityInstance( final EntityInstance instance, final Context
if ( property.isOptional() ) {
return "null";
}
throw new CodeGenerationException( "EntityInstance " + instance.getName() + " is missing value for Property " + property.getName() );
throw new CodeGenerationException(
"EntityInstance " + instance.getName() + " is missing value for Property " + property.getName() );
}
final Context newContext = new Context( context.codeGenerationConfig, property.isOptional() );
return value.accept( this, newContext );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ public class ValueInitializer {

static {
final BiFunction<Class<?>, String, String> literalExpression = ( type, valueExpression ) -> valueExpression;
final BiFunction<Class<?>, String, String> stringConstructor = ( type, valueExpression ) -> String.format( "new %s( %s )", type.getSimpleName(),
valueExpression );
final BiFunction<Class<?>, String, String> create = ( type, valueExpression ) -> String.format( "%s.create( %s )", type.getSimpleName(),
valueExpression );
final BiFunction<Class<?>, String, String> parseTypeName = ( type, valueExpression ) -> String.format( "%s.parse%s( %s )", type.getSimpleName(),
final BiFunction<Class<?>, String, String> stringConstructor = ( type, valueExpression ) -> String.format( "new %s( %s )",
type.getSimpleName(), valueExpression );
final BiFunction<Class<?>, String, String> valueOf = ( type, valueExpression ) -> String.format( "%s.valueOf( %s )", type.getSimpleName(),
valueExpression );
final BiFunction<Class<?>, String, String> gregorianCalendar = ( type, valueExpression ) -> "_datatypeFactory.newXMLGregorianCalendar( " + valueExpression
final BiFunction<Class<?>, String, String> create = ( type, valueExpression ) -> String.format( "%s.create( %s )",
type.getSimpleName(), valueExpression );
final BiFunction<Class<?>, String, String> parseTypeName = ( type, valueExpression ) -> String.format( "%s.parse%s( %s )",
type.getSimpleName(), type.getSimpleName(), valueExpression );
final BiFunction<Class<?>, String, String> valueOf = ( type, valueExpression ) -> String.format( "%s.valueOf( %s )",
type.getSimpleName(), valueExpression );
final BiFunction<Class<?>, String, String> gregorianCalendar = ( type, valueExpression ) ->
"_datatypeFactory.newXMLGregorianCalendar( " + valueExpression + " )";
final BiFunction<Class<?>, String, String> duration = ( type, valueExpression ) -> "_datatypeFactory.newDuration( " + valueExpression
+ " )";
final BiFunction<Class<?>, String, String> duration = ( type, valueExpression ) -> "_datatypeFactory.newDuration( " + valueExpression + " )";

INITIALIZERS = new HashMap<>();
INITIALIZERS.put( XSD.xstring, literalExpression );
Expand All @@ -59,11 +60,13 @@ public class ValueInitializer {
INITIALIZERS.put( XSD.date, gregorianCalendar );
INITIALIZERS.put( XSD.dateTime, gregorianCalendar );
INITIALIZERS.put( XSD.dateTimeStamp, gregorianCalendar );
INITIALIZERS.put( XSD.gYear, ( type, valueExpression ) -> "_datatypeFactory.newXMLGregorianCalendarDate( Integer.valueOf( " + valueExpression + " )"
+ ", DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED )" );
INITIALIZERS.put( XSD.gYear,
( type, valueExpression ) -> "_datatypeFactory.newXMLGregorianCalendarDate( Integer.valueOf( " + valueExpression + " )"
+ ", DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED )" );
INITIALIZERS.put( XSD.gMonth,
( type, valueExpression ) -> "_datatypeFactory.newXMLGregorianCalendarDate( DatatypeConstants.FIELD_UNDEFINED, Integer.valueOf( " + valueExpression
+ " )" + ", DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED )" );
( type, valueExpression ) ->
"_datatypeFactory.newXMLGregorianCalendarDate( DatatypeConstants.FIELD_UNDEFINED, Integer.valueOf( " + valueExpression
+ " )" + ", DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED )" );
INITIALIZERS.put( XSD.gYearMonth, gregorianCalendar );
INITIALIZERS.put( XSD.gMonthDay, gregorianCalendar );
INITIALIZERS.put( XSD.gDay, gregorianCalendar );
Expand All @@ -88,8 +91,10 @@ public class ValueInitializer {
}

public boolean needInitializationToConstructor( final List<DeconstructionSet> deconstructionSets ) {
return deconstructionSets.stream().flatMap( deconstructionSet -> deconstructionSet.getElementProperties().stream().map( property -> property.getDataType()
.map( type -> DataType.getJavaTypeForMetaModelType( ResourceFactory.createResource( type.getUrn() ), property.getMetaModelVersion() ) ) ) )
return deconstructionSets.stream()
.flatMap( deconstructionSet -> deconstructionSet.getElementProperties().stream().map( property -> property.getDataType()
.map( type -> DataType.getJavaTypeForMetaModelType( ResourceFactory.createResource( type.getUrn() ),
property.getMetaModelVersion() ) ) ) )
.anyMatch( dataType -> dataType.map( type -> type == XMLGregorianCalendar.class ).orElse( false ) );
}

Expand All @@ -114,7 +119,8 @@ public String apply( final Resource rdfType, final String valueExpression, final
* @param valueExpression an expression that, when evaluated, will return the input value <b>as a string</b>.
* @param metaModelVersion the used meta model version
*/
public String apply( final Resource rdfType, final Class<?> javaType, final String valueExpression, final KnownVersion metaModelVersion ) {
public String apply( final Resource rdfType, final Class<?> javaType, final String valueExpression,
final KnownVersion metaModelVersion ) {
final SAMM samm = new SAMM( metaModelVersion );
if ( rdfType.equals( samm.curie() ) ) {
return String.format( "new Curie( %s )", valueExpression );
Expand Down
Loading

0 comments on commit dd7f296

Please sign in to comment.