Skip to content

Commit 245b39e

Browse files
authored
Merge pull request #317 from bci-oss/bugfix/4943-multiplication-abstract-entities
the bugfix multiple instantiation abstract entities
2 parents 700e45d + 920453c commit 245b39e

File tree

7 files changed

+180
-22
lines changed

7 files changed

+180
-22
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/loader/instantiator/AbstractEntityInstantiator.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
package org.eclipse.esmf.metamodel.loader.instantiator;
1515

16-
import java.util.HashSet;
17-
import java.util.List;
18-
import java.util.Optional;
19-
import java.util.Set;
16+
import java.util.*;
2017

2118
import org.apache.jena.rdf.model.Resource;
2219
import org.apache.jena.rdf.model.Statement;
@@ -37,11 +34,11 @@
3734

3835
public class AbstractEntityInstantiator extends Instantiator<AbstractEntity> {
3936

40-
private final Set<Resource> processedExtendingElements;
37+
private final Set<Resource> processedExtendingElements = new HashSet<>();
38+
private final Map<Resource, AbstractEntity> creatingElements = new HashMap<>();
4139

4240
public AbstractEntityInstantiator( final ModelElementFactory modelElementFactory ) {
4341
super( modelElementFactory, AbstractEntity.class );
44-
processedExtendingElements = new HashSet<>();
4542
}
4643

4744
/**
@@ -67,36 +64,35 @@ public AbstractEntityInstantiator( final ModelElementFactory modelElementFactory
6764
*/
6865
@Override
6966
public AbstractEntity apply( final Resource abstractEntity ) {
67+
if ( creatingElements.containsKey( abstractEntity ) )
68+
return creatingElements.get( abstractEntity );
7069
final MetaModelBaseAttributes metaModelBaseAttributes = buildBaseAttributes( abstractEntity );
7170
final List<Property> properties = getPropertiesModels( abstractEntity, samm.properties() );
7271

73-
final Optional<ComplexType> extendedEntity = optionalAttributeValue( abstractEntity, samm._extends() )
74-
.map( Statement::getResource )
75-
.map( extendedEntityResource -> attributeValue( extendedEntityResource, RDF.type ) )
76-
.map( entityStatement -> {
72+
final Optional<ComplexType> extendedEntity = optionalAttributeValue( abstractEntity, samm._extends() ).map( Statement::getResource )
73+
.map( extendedEntityResource -> attributeValue( extendedEntityResource, RDF.type ) ).map( entityStatement -> {
7774
if ( samm.AbstractEntity().equals( entityStatement.getObject().asResource() ) ) {
7875
return modelElementFactory.create( AbstractEntity.class, entityStatement.getSubject() );
7976
}
8077
return modelElementFactory.create( Entity.class, entityStatement.getSubject() );
8178
} );
8279

83-
final List<AspectModelUrn> extendingComplexTypes = model
84-
.listSubjectsWithProperty( samm._extends(), abstractEntity )
85-
.mapWith( extendingComplexType -> attributeValue( extendingComplexType, RDF.type ) )
86-
.mapWith( statement -> {
80+
final List<AspectModelUrn> extendingComplexTypes = new ArrayList<>();
81+
DefaultAbstractEntity entity = DefaultAbstractEntity.createDefaultAbstractEntity( metaModelBaseAttributes, properties, extendedEntity,
82+
extendingComplexTypes );
83+
creatingElements.put( abstractEntity, entity );
84+
extendingComplexTypes.addAll( model.listSubjectsWithProperty( samm._extends(), abstractEntity )
85+
.mapWith( extendingComplexType -> attributeValue( extendingComplexType, RDF.type ) ).mapWith( statement -> {
8786
if ( processedExtendingElements.contains( statement.getSubject() ) ) {
8887
return AspectModelUrn.fromUrn( statement.getSubject().getURI() );
8988
}
9089
processedExtendingElements.add( statement.getSubject() );
9190
if ( samm.AbstractEntity().equals( statement.getObject().asResource() ) ) {
92-
return modelElementFactory.create( AbstractEntity.class,
93-
statement.getSubject() ).getAspectModelUrn().get();
91+
return modelElementFactory.create( AbstractEntity.class, statement.getSubject() ).getAspectModelUrn().get();
9492
}
9593
return modelElementFactory.create( Entity.class, statement.getSubject() ).getAspectModelUrn().get();
96-
} )
97-
.toList();
98-
99-
return DefaultAbstractEntity.createDefaultAbstractEntity( metaModelBaseAttributes, properties, extendedEntity,
100-
extendingComplexTypes );
94+
} ).toList() );
95+
creatingElements.remove( abstractEntity );
96+
return entity;
10197
}
10298
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package org.eclipse.esmf.metamodel.loader;
15+
16+
import io.vavr.Tuple;
17+
import io.vavr.collection.List;
18+
19+
import org.eclipse.esmf.metamodel.AbstractEntity;
20+
import org.eclipse.esmf.metamodel.ComplexType;
21+
import org.eclipse.esmf.samm.KnownVersion;
22+
import org.eclipse.esmf.test.MetaModelVersions;
23+
import org.eclipse.esmf.test.TestAspect;
24+
import org.eclipse.esmf.test.TestResources;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
28+
import java.util.Optional;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
class AspectModelLoaderTest extends MetaModelVersions {
33+
@ParameterizedTest
34+
@MethodSource( value = "allVersions" )
35+
public void testOfAbstractEntityCyclomaticCreation( final KnownVersion version ) {
36+
var entities = TestResources.getModel( TestAspect.ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND, version ).flatMap( AspectModelLoader::getElements )
37+
.map( List::ofAll ).get().filter( e -> e instanceof ComplexType ).map( e -> (ComplexType) e ).toMap( e -> Tuple.of( e.getName(), e ) );
38+
39+
assertThat( entities.getOrElse( "AbstractTestEntity", null ) ).isNotNull().isInstanceOf( AbstractEntity.class );
40+
assertThat( entities.getOrElse( "AbstractSecondTestEntity", null ) ).isNotNull().isInstanceOf( AbstractEntity.class );
41+
42+
var abstractEntity = (AbstractEntity) entities.get( "AbstractTestEntity" ).get();
43+
var abstractSecondEntity = (AbstractEntity) entities.get( "AbstractSecondTestEntity" ).get();
44+
45+
assertThat( entities.getOrElse( "testEntityOne", null ) ).extracting( ComplexType::getExtends ).extracting( Optional::get ).isSameAs( abstractEntity );
46+
assertThat( entities.getOrElse( "testEntityTwo", null ) ).extracting( ComplexType::getExtends ).extracting( Optional::get ).isSameAs( abstractEntity );
47+
assertThat( entities.getOrElse( "testEntityThree", null ) ).extracting( ComplexType::getExtends ).extracting( Optional::get )
48+
.isSameAs( abstractSecondEntity );
49+
}
50+
}

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/diagram/Enumeration2BoxModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void testSeeAttributeIsPresentExpectSuccess( final KnownVersion metaModel
6161
@ParameterizedTest
6262
@MethodSource( value = "allVersions" )
6363
public void testSeeAttributesArePresentExpectSuccess( final KnownVersion metaModelVersion ) {
64-
boolean newerThanSamm1 = metaModelVersion.isNewerThan( KnownVersion.SAMM_1_0_0 );
64+
final boolean newerThanSamm1 = metaModelVersion.isNewerThan( KnownVersion.SAMM_1_0_0 );
6565
final String characteristicIdentifier = newerThanSamm1 ? "Enumeration46dba23" : "TestEnumeration";
6666
final String boxSelectorStatement = getBoxSelectorStatement( characteristicIdentifier, newerThanSamm1 );
6767
final String entriesSelectorStatement = getEntriesSelectorStatement( characteristicIdentifier, newerThanSamm1 );

core/esmf-aspect-model-serializer/src/test/java/org/eclipse/esmf/aspectmodel/serializer/RdfModelCreatorVisitorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class RdfModelCreatorVisitorTest extends MetaModelVersions {
5959
"ASPECT_WITH_COLLECTION_WITH_ABSTRACT_ENTITY",
6060
"ASPECT_WITH_ABSTRACT_SINGLE_ENTITY",
6161
"ASPECT_WITH_ABSTRACT_PROPERTY",
62+
"ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND",
6263
"MODEL_WITH_CYCLES",
6364
"MODEL_WITH_BROKEN_CYCLES",
6465
"MODEL_WITH_BLANK_AND_ADDITIONAL_NODES"

core/esmf-test-aspect-models/src/main/java/org/eclipse/esmf/test/TestAspect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public enum TestAspect implements TestModel {
117117
ASPECT_WITH_MULTIPLE_ENTITIES,
118118
ASPECT_WITH_MULTIPLE_ENTITIES_AND_EITHER,
119119
ASPECT_WITH_MULTIPLE_ENTITIES_ON_MULTIPLE_LEVELS,
120+
ASPECT_WITH_MULTIPLE_ENTITIES_SAME_EXTEND,
120121
ASPECT_WITH_MULTIPLE_ENTITY_COLLECTIONS,
121122
ASPECT_WITH_MULTIPLE_ENUMERATIONS_ON_MULTIPLE_LEVELS,
122123
ASPECT_WITH_MULTIPLE_SEE_ATTRIBUTES,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
3+
#
4+
# See the AUTHORS file(s) distributed with this work for additional
5+
# information regarding authorship.
6+
#
7+
# This Source Code Form is subject to the terms of the Mozilla Public
8+
# License, v. 2.0. If a copy of the MPL was not distributed with this
9+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
#
11+
# SPDX-License-Identifier: MPL-2.0
12+
#
13+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
14+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:1.0.0#> .
15+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:1.0.0#> .
16+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
17+
18+
:AspectWithMultipleEntitiesSameExtend a samm:Aspect ;
19+
samm:name "TestAspect" ;
20+
samm:properties ( :testPropertyOne :testPropertyTwo ) ;
21+
samm:operations ( ) .
22+
23+
:testPropertyOne a samm:Property ;
24+
samm:name "testPropertyOne" ;
25+
samm:characteristic :testCharacteristicOne .
26+
27+
:testPropertyTwo a samm:Property ;
28+
samm:name "testPropertyTwo" ;
29+
samm:characteristic :testCharacteristicTwo .
30+
31+
:testCharacteristicOne a samm:Characteristic ;
32+
samm:name "testCharacteristicOne" ;
33+
samm:dataType :testEntityOne .
34+
35+
:testCharacteristicTwo a samm:Characteristic ;
36+
samm:name "testCharacteristicTwo" ;
37+
samm:dataType :testEntityTwo .
38+
39+
:testEntityOne a samm:Entity ;
40+
samm:name "TestEntityOne" ;
41+
samm:extends :AbstractTestEntity ;
42+
samm:properties ( ) .
43+
44+
:testEntityTwo a samm:Entity ;
45+
samm:name "TestEntityTwo" ;
46+
samm:extends :AbstractTestEntity ;
47+
samm:properties ( ) .
48+
49+
:testEntityThree a samm:Entity ;
50+
samm:name "TestEntityThree" ;
51+
samm:extends :AbstractSecondTestEntity ;
52+
samm:properties ( ) .
53+
54+
:AbstractTestEntity a samm:AbstractEntity ;
55+
samm:name "AbstractTestEntity" ;
56+
samm:properties ( ).
57+
58+
:AbstractSecondTestEntity a samm:AbstractEntity ;
59+
samm:name "AbstractSecondTestEntity" ;
60+
samm:properties ( ).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
3+
#
4+
# See the AUTHORS file(s) distributed with this work for additional
5+
# information regarding authorship.
6+
#
7+
# This Source Code Form is subject to the terms of the Mozilla Public
8+
# License, v. 2.0. If a copy of the MPL was not distributed with this
9+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
#
11+
# SPDX-License-Identifier: MPL-2.0
12+
13+
@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
14+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
15+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
16+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
17+
18+
:AspectWithMultipleEntitiesSameExtend a samm:Aspect ;
19+
samm:properties ( :testPropertyOne :testPropertyTwo ) ;
20+
samm:operations ( ) .
21+
22+
:testPropertyOne a samm:Property ;
23+
samm:characteristic :testCharacteristicOne .
24+
25+
:testPropertyTwo a samm:Property ;
26+
samm:characteristic :testCharacteristicTwo .
27+
28+
:testCharacteristicOne a samm:Characteristic ;
29+
samm:dataType :testEntityOne .
30+
31+
:testCharacteristicTwo a samm:Characteristic ;
32+
samm:dataType :testEntityTwo .
33+
34+
:testEntityOne a samm:Entity ;
35+
samm:extends :AbstractTestEntity ;
36+
samm:properties ( ) .
37+
38+
:testEntityTwo a samm:Entity ;
39+
samm:extends :AbstractTestEntity ;
40+
samm:properties ( ) .
41+
42+
:testEntityThree a samm:Entity ;
43+
samm:extends :AbstractSecondTestEntity ;
44+
samm:properties ( ) .
45+
46+
:AbstractTestEntity a samm:AbstractEntity ;
47+
samm:properties ( ).
48+
49+
:AbstractSecondTestEntity a samm:AbstractEntity ;
50+
samm:properties ( ).

0 commit comments

Comments
 (0)