Skip to content

Commit 2529576

Browse files
committed
Merge branch 'main' into 2.0.x
2 parents f44105e + 92871d0 commit 2529576

File tree

26 files changed

+988
-25
lines changed

26 files changed

+988
-25
lines changed

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/ComplexType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ default boolean isAbstractEntity() {
2020
*/
2121
default List<Property> getAllProperties() {
2222
if ( getExtends().isPresent() ) {
23-
return Stream.of( getProperties(), getExtends().get().getProperties() ).flatMap( Collection::stream ).collect( Collectors.toList() );
23+
return Stream.of( getProperties(), getExtends().get().getAllProperties() ).flatMap( Collection::stream ).collect( Collectors.toList() );
2424
}
2525
return List.copyOf( getProperties() );
2626
}

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/EntityInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import java.util.Map;
1717

18-
public interface EntityInstance extends Base, IsDescribed, Value, Comparable<EntityInstance> {
18+
public interface EntityInstance extends Base, IsDescribed, Value {
1919
Map<Property, Value> getAssertions();
2020

2121
default Entity getEntityType() {

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/impl/BaseImpl.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.openmanufacturing.sds.metamodel.impl;
1414

15+
import java.util.Comparator;
1516
import java.util.List;
1617
import java.util.Objects;
1718
import java.util.Optional;
@@ -27,7 +28,7 @@
2728
/**
2829
* The base implemenation of all model elements.
2930
*/
30-
public abstract class BaseImpl implements Base, IsDescribed {
31+
public abstract class BaseImpl implements Base, IsDescribed, Comparable<BaseImpl> {
3132
private final KnownVersion metaModelVersion;
3233
private final Optional<AspectModelUrn> urn;
3334
private final String name;
@@ -121,4 +122,15 @@ public boolean equals( final Object o ) {
121122
public int hashCode() {
122123
return Objects.hash( urn, name );
123124
}
125+
126+
@Override
127+
public int compareTo( BaseImpl o ) {
128+
if ( this.urn.isPresent() && o.urn.isPresent() )
129+
return this.urn.get().compareTo( o.urn.get() );
130+
return Comparator
131+
.comparing( BaseImpl::getMetaModelVersion )
132+
.thenComparing( BaseImpl::getName )
133+
.thenComparing( BaseImpl::hasSyntheticName )
134+
.compare( this, o );
135+
}
124136
}

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/impl/DefaultAbstractEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.openmanufacturing.sds.metamodel.impl;
1515

1616
import java.util.List;
17+
import java.util.Objects;
1718
import java.util.Optional;
1819
import java.util.stream.Collectors;
1920

@@ -52,7 +53,7 @@ private DefaultAbstractEntity( final MetaModelBaseAttributes metaModelBaseAttrib
5253
*/
5354
@Override
5455
public List<ComplexType> getExtendingElements() {
55-
return extendingElements.stream().map( instances::get ).collect( Collectors.toList() );
56+
return extendingElements.stream().map( instances::get ).filter( Objects::nonNull ).collect( Collectors.toList() );
5657
}
5758

5859
/**

core/sds-aspect-meta-model-java/src/main/java/io/openmanufacturing/sds/metamodel/impl/DefaultEntityInstance.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public Map<Property, Value> getAssertions() {
4444
return assertions;
4545
}
4646

47-
@Override
48-
public int compareTo( final EntityInstance other ) {
49-
return getName().compareTo( other.getName() );
50-
}
51-
5247
@Override
5348
public <T, C> T accept( final AspectVisitor<T, C> visitor, final C context ) {
5449
return visitor.visitEntityInstance( this, context );

core/sds-aspect-model-java-generator/src/main/java/io/openmanufacturing/sds/aspectmodel/java/AspectModelJavaUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,15 @@ public static String objectEqualsExpression( final StructureElement element ) {
535535
.collect( Collectors.joining( " && " ) );
536536
}
537537

538-
public static String objectsHashCodeExpression( final StructureElement element ) {
539-
return element.getProperties().stream()
538+
public static String objectsHashCodeExpression( final StructureElement element, final boolean needsLeadingComma ) {
539+
final String elementString = element.getProperties().stream()
540540
.filter( property -> !property.isAbstract() )
541541
.map( Property::getPayloadName )
542542
.collect( Collectors.joining( ", " ) );
543+
if ( StringUtils.isNotBlank( elementString ) && needsLeadingComma ) {
544+
return ", " + elementString;
545+
}
546+
return elementString;
543547
}
544548

545549
/**
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#macro( javaPojoHashCodeMethod )
22
@Override
33
public int hashCode() {
4-
return Objects.hash(
4+
return Objects.hash(
55
#if ( $element.is( $ComplexType ) )
66
#set( $complexElement = $ComplexType.cast( $element ) )
77
#if ( $complexElement.getExtends().isPresent() )
8-
super.hashCode(),
8+
super.hashCode()
9+
${util.objectsHashCodeExpression( $element, true )}
10+
#else
11+
${util.objectsHashCodeExpression( $element, false )}
912
#end
13+
#else
14+
${util.objectsHashCodeExpression( $element, false )}
1015
#end
11-
${util.objectsHashCodeExpression( $element )}
12-
);
16+
);
1317
}
1418
#end

core/sds-aspect-model-java-generator/src/test/java/io/openmanufacturing/sds/aspectmodel/java/StaticMetaModelGeneratorTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import io.openmanufacturing.sds.test.MetaModelVersions;
2525
import io.openmanufacturing.sds.test.TestAspect;
2626
import io.openmanufacturing.sds.test.TestResources;
27+
import io.openmanufacturing.sds.test.TestSharedAspect;
28+
import io.openmanufacturing.sds.test.TestSharedModel;
2729

2830
abstract class StaticMetaModelGeneratorTest extends MetaModelVersions {
2931

@@ -41,4 +43,10 @@ Collection<JavaGenerator> getGenerators( final TestAspect aspect, final KnownVer
4143
final JavaGenerator staticGenerator = new StaticMetaModelJavaGenerator( model, false, null );
4244
return List.of( pojoGenerator, staticGenerator );
4345
}
46+
Collection<JavaGenerator> getGenerators( final TestSharedAspect aspect, final KnownVersion version ) {
47+
final VersionedModel model = TestResources.getModel( aspect, version ).get();
48+
final JavaGenerator pojoGenerator = new AspectModelJavaGenerator( model, false, false, null );
49+
final JavaGenerator staticGenerator = new StaticMetaModelJavaGenerator( model, false, null );
50+
return List.of( pojoGenerator, staticGenerator );
51+
}
4452
}

core/sds-aspect-model-java-generator/src/test/java/io/openmanufacturing/sds/aspectmodel/java/StaticMetaModelJavaGeneratorTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import io.openmanufacturing.sds.staticmetamodel.constraint.StaticConstraintContainerProperty;
5353
import io.openmanufacturing.sds.staticmetamodel.constraint.StaticConstraintProperty;
5454
import io.openmanufacturing.sds.test.TestAspect;
55+
import io.openmanufacturing.sds.test.TestSharedAspect;
5556

5657
public class StaticMetaModelJavaGeneratorTest extends StaticMetaModelGeneratorTest {
5758

@@ -65,6 +66,17 @@ public void testCodeGeneration( final TestAspect testAspect ) {
6566
assertThatCode( () -> TestContext.generateStaticAspectCode().apply( getGenerators( testAspect, KnownVersion.getLatest() ) ) ).doesNotThrowAnyException();
6667
}
6768

69+
/**
70+
* Tests that code generation succeeds for all test models, that have properties shared over two files, for the latest meta model version
71+
* @param testAspect the injected shared Aspect models
72+
*/
73+
@ParameterizedTest
74+
@EnumSource( value = TestSharedAspect.class )
75+
public void testCodeGenerationSharedAspect( final TestSharedAspect testAspect ) {
76+
assertThatCode( () -> TestContext.generateStaticAspectCode().apply( getGenerators( testAspect, KnownVersion.getLatest() ) ) ).doesNotThrowAnyException();
77+
}
78+
79+
6880
@ParameterizedTest
6981
@MethodSource( value = "allVersions" )
7082
public void testGenerateStaticMetaModelWithOptionalProperties( final KnownVersion metaModelVersion ) throws IOException {
@@ -142,6 +154,18 @@ ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "M
142154
} ).build(), new HashMap<>() );
143155
}
144156

157+
@ParameterizedTest
158+
@MethodSource( value = "versionsStartingWith2_0_0" )
159+
public void testGenerateStaticMetaModelWithMeasurementTwo( final KnownVersion metaModelVersion ) throws IOException {
160+
final TestAspect aspect = TestAspect.ASPECT_WITH_EXTENDED_ENTITY;
161+
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect, metaModelVersion ) );
162+
result.assertNumberOfFiles( 8 );
163+
result.assertFields( "MetaAspectWithExtendedEntity",
164+
ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "MODEL_ELEMENT_URN", String.class )
165+
.put( "CHARACTERISTIC_NAMESPACE", String.class ).put( "INSTANCE", "MetaAspectWithExtendedEntity" )
166+
.put( "TEST_PROPERTY", "StaticContainerProperty<TestEntity,java.util.LinkedHashSet<TestEntity>>").build(), new HashMap<>() );
167+
}
168+
145169
@ParameterizedTest
146170
@MethodSource( value = "allVersions" )
147171
public void testGenerateStaticMetaModelWithRecursiveAspectWithOptional( final KnownVersion metaModelVersion ) throws IOException {
@@ -211,7 +235,6 @@ ImmutableMap.<String, Object> builder().put( "NAMESPACE", String.class ).put( "M
211235
}
212236

213237
@ParameterizedTest
214-
// @MethodSource( value = "allVersions" )
215238
@MethodSource( value = "latestVersion" )
216239
public void testGenerateStaticMetaModelWithConstraints( final KnownVersion metaModelVersion ) throws IOException {
217240
final TestAspect aspect = TestAspect.ASPECT_WITH_CONSTRAINTS;

core/sds-aspect-model-resolver/src/main/java/io/openmanufacturing/sds/aspectmodel/resolver/ClasspathStrategy.java

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
33
*
44
* See the AUTHORS file(s) distributed with this work for additional
5-
* information regarding authorship.
5+
* information regarding authorship.
66
*
77
* This Source Code Form is subject to the terms of the Mozilla Public
88
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -13,15 +13,24 @@
1313

1414
package io.openmanufacturing.sds.aspectmodel.resolver;
1515

16+
import java.io.File;
1617
import java.io.FileNotFoundException;
1718
import java.io.IOException;
1819
import java.io.InputStream;
1920
import java.net.URL;
2021
import java.nio.charset.StandardCharsets;
22+
import java.util.ArrayList;
2123
import java.util.Arrays;
24+
import java.util.Comparator;
25+
import java.util.Enumeration;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import java.util.jar.JarEntry;
29+
import java.util.jar.JarFile;
2230
import java.util.stream.Stream;
2331

2432
import org.apache.commons.io.IOUtils;
33+
import org.apache.commons.lang3.StringUtils;
2534
import org.apache.jena.rdf.model.Model;
2635
import org.slf4j.Logger;
2736
import org.slf4j.LoggerFactory;
@@ -88,18 +97,58 @@ protected URL resourceUrl( final String directory, final String filename ) {
8897

8998
protected Stream<String> filesInDirectory( final String directory ) {
9099
try {
91-
final InputStream directoryUrl = getClass().getResourceAsStream( directory );
92-
if ( directoryUrl == null ) {
93-
LOG.warn( "No such classpath directory {}", directory );
94-
return Stream.empty();
100+
final Optional<File> file = getDirectoryFile( directory );
101+
if ( file.isPresent() && file.get().isFile() ) {
102+
return getFilesFromJar( directory, file.get() );
103+
} else {
104+
return getFilesFromResources( directory );
95105
}
96-
return Arrays.stream( IOUtils.toString( directoryUrl, StandardCharsets.UTF_8 ).split( "\\n" ) );
97106
} catch ( final IOException exception ) {
98107
LOG.warn( "Could not list files in classpath directory {}", directory, exception );
99108
return Stream.empty();
100109
}
101110
}
102111

112+
private Optional<File> getDirectoryFile( String directory ) {
113+
// The incoming URL will look like this: jar:file:/pathToJar/o.jar/packageName/className
114+
// In case we run the code from a jar. Because of that we need to deconstruct the path to get the path to the jar only and remove the unwanted part of the URL.
115+
final URL url = getClass().getClassLoader().getResource( directory );
116+
if ( url == null ) {
117+
return Optional.empty();
118+
}
119+
final String urlString = url.toString();
120+
final int jarIndex = urlString.indexOf( ".jar" );
121+
final String path = jarIndex > 0 ? urlString.substring( 0, jarIndex + 4 ).replace( "jar:file:", "" ) : urlString;
122+
return Optional.of( new File( path ) );
123+
}
124+
125+
private Stream<String> getFilesFromResources( String directory ) throws IOException {
126+
final InputStream directoryUrl = getClass().getClassLoader().getResourceAsStream( directory );
127+
if ( directoryUrl == null ) {
128+
LOG.warn( "No such classpath directory {}", directory );
129+
return Stream.empty();
130+
}
131+
return Arrays.stream( IOUtils.toString( directoryUrl, StandardCharsets.UTF_8 ).split( "\\n" ) );
132+
}
133+
134+
private Stream<String> getFilesFromJar( String directory, File jarFile ) throws IOException {
135+
final List<String> fileList = new ArrayList<>();
136+
final JarFile jar = new JarFile( jarFile );
137+
final Enumeration<JarEntry> entries = jar.entries();
138+
final String dir = directory.endsWith( "/" ) ? directory : directory + "/";
139+
while ( entries.hasMoreElements() ) {
140+
final String name = entries.nextElement().getName();
141+
if ( name.startsWith( dir ) ) {
142+
final String fileName = name.replace( dir, "" );
143+
if ( StringUtils.isNotEmpty( fileName ) ) {
144+
fileList.add( fileName );
145+
}
146+
}
147+
}
148+
jar.close();
149+
return fileList.stream();
150+
}
151+
103152
@Override
104153
public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
105154
final String modelsRootTrailingSlash = modelsRoot.isEmpty() ? "" : "/";
@@ -117,13 +166,13 @@ public Try<Model> apply( final AspectModelUrn aspectModelUrn ) {
117166
return filesInDirectory( directory )
118167
.filter( name -> name.endsWith( ".ttl" ) )
119168
.map( name -> resourceUrl( directory, name ) )
120-
.sorted()
169+
.sorted( Comparator.comparing( URL::getPath ) )
121170
.map( this::loadFromUrl )
122171
.filter( tryModel -> tryModel
123172
.map( model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
124173
.getOrElse( false ) )
125174
.findFirst()
126175
.orElse( Try.failure( new FileNotFoundException(
127-
"The AspectModel: " + aspectModelUrn.toString() + " could not be found in directory: " + directory ) ) );
176+
"The AspectModel: " + aspectModelUrn + " could not be found in directory: " + directory ) ) );
128177
}
129178
}

0 commit comments

Comments
 (0)