|
13 | 13 |
|
14 | 14 | package org.eclipse.esmf.metamodel.loader.instantiator;
|
15 | 15 |
|
16 |
| -import java.util.*; |
17 |
| - |
18 |
| -import org.apache.jena.rdf.model.Resource; |
19 |
| -import org.apache.jena.rdf.model.Statement; |
20 |
| -import org.apache.jena.vocabulary.RDF; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Optional; |
21 | 18 |
|
22 | 19 | import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
|
23 |
| -import org.eclipse.esmf.characteristic.Collection; |
24 | 20 | import org.eclipse.esmf.metamodel.AbstractEntity;
|
25 | 21 | import org.eclipse.esmf.metamodel.ComplexType;
|
26 |
| -import org.eclipse.esmf.metamodel.Entity; |
27 |
| -import org.eclipse.esmf.metamodel.Aspect; |
28 | 22 | import org.eclipse.esmf.metamodel.Property;
|
29 | 23 | import org.eclipse.esmf.metamodel.impl.DefaultAbstractEntity;
|
30 |
| -import org.eclipse.esmf.metamodel.impl.DefaultComplexType; |
31 |
| -import org.eclipse.esmf.metamodel.loader.Instantiator; |
32 | 24 | import org.eclipse.esmf.metamodel.loader.MetaModelBaseAttributes;
|
33 | 25 | import org.eclipse.esmf.metamodel.loader.ModelElementFactory;
|
34 | 26 |
|
35 |
| -public class AbstractEntityInstantiator extends Instantiator<AbstractEntity> { |
36 |
| - |
37 |
| - private final Set<Resource> processedExtendingElements = new HashSet<>(); |
38 |
| - private final Map<Resource, AbstractEntity> creatingElements = new HashMap<>(); |
| 27 | +public class AbstractEntityInstantiator extends ComplexTypeInstantiator<AbstractEntity> { |
39 | 28 |
|
40 | 29 | public AbstractEntityInstantiator( final ModelElementFactory modelElementFactory ) {
|
41 | 30 | super( modelElementFactory, AbstractEntity.class );
|
42 | 31 | }
|
43 | 32 |
|
44 |
| - /** |
45 |
| - * Initializes a {@link DefaultAbstractEntity}. |
46 |
| - * Since {@link ComplexType}s have a reference to the element which is extended by a particular {@link ComplexType}, |
47 |
| - * and the {@link AbstractEntity} has a list of references to all elements which extend the {@link AbstractEntity}, |
48 |
| - * a circular dependency exists between the {@link ComplexType} and the {@link AbstractEntity}. |
49 |
| - * |
50 |
| - * The reference between the {@link ComplexType}s represents the `extends` relationship between an {@link Entity} or |
51 |
| - * an {@link AbstractEntity} and another {@link Entity} or {@link AbstractEntity}. |
52 |
| - * The list of references between an {@link AbstractEntity} and all {@link ComplexType}s extending the |
53 |
| - * {@link AbstractEntity} has been implemented to ensure that all extending {@link ComplexType}s are loaded into the |
54 |
| - * {@link Aspect} whether they are directly linked to that Aspect or not. This |
55 |
| - * scenario may occur for example when the Aspect Model contains a {@link Collection} |
56 |
| - * with an {@link AbstractEntity} as its data type with multiple Entities extending the {@link AbstractEntity}. |
57 |
| - * |
58 |
| - * In order to prevent processing elements in this circular dependency more than once, causing an infinite loop, |
59 |
| - * the elements which are processed are tracked in the {@link AbstractEntityInstantiator#processedExtendingElements} |
60 |
| - * {@link Set}. |
61 |
| - * Using the {@link DefaultComplexType#instances} Map to check whether an |
62 |
| - * element has been processed does not work since the instance is only created once the `create` method of the |
63 |
| - * corresponding {@link ComplexType} has been called. Creating the child elements however happens before this call. |
64 |
| - */ |
65 | 33 | @Override
|
66 |
| - public AbstractEntity apply( final Resource abstractEntity ) { |
67 |
| - if ( creatingElements.containsKey( abstractEntity ) ) |
68 |
| - return creatingElements.get( abstractEntity ); |
69 |
| - final MetaModelBaseAttributes metaModelBaseAttributes = buildBaseAttributes( abstractEntity ); |
70 |
| - final List<Property> properties = getPropertiesModels( abstractEntity, samm.properties() ); |
71 |
| - |
72 |
| - final Optional<ComplexType> extendedEntity = optionalAttributeValue( abstractEntity, samm._extends() ).map( Statement::getResource ) |
73 |
| - .map( extendedEntityResource -> attributeValue( extendedEntityResource, RDF.type ) ).map( entityStatement -> { |
74 |
| - if ( samm.AbstractEntity().equals( entityStatement.getObject().asResource() ) ) { |
75 |
| - return modelElementFactory.create( AbstractEntity.class, entityStatement.getSubject() ); |
76 |
| - } |
77 |
| - return modelElementFactory.create( Entity.class, entityStatement.getSubject() ); |
78 |
| - } ); |
79 |
| - |
80 |
| - final List<AspectModelUrn> extendingComplexTypes = new ArrayList<>(); |
81 |
| - DefaultAbstractEntity entity = DefaultAbstractEntity.createDefaultAbstractEntity( metaModelBaseAttributes, properties, extendedEntity, |
| 34 | + protected AbstractEntity createDefaultEntity( |
| 35 | + MetaModelBaseAttributes metaModelBaseAttributes, |
| 36 | + List<Property> properties, |
| 37 | + Optional<ComplexType> extendedEntity, |
| 38 | + List<AspectModelUrn> extendingComplexTypes ) { |
| 39 | + return new DefaultAbstractEntity( |
| 40 | + metaModelBaseAttributes, |
| 41 | + properties, |
| 42 | + extendedEntity, |
82 | 43 | extendingComplexTypes );
|
83 |
| - creatingElements.put( abstractEntity, entity ); |
84 |
| - extendingComplexTypes.addAll( model.listSubjectsWithProperty( samm._extends(), abstractEntity ) |
85 |
| - .mapWith( extendingComplexType -> attributeValue( extendingComplexType, RDF.type ) ).mapWith( statement -> { |
86 |
| - if ( processedExtendingElements.contains( statement.getSubject() ) ) { |
87 |
| - return AspectModelUrn.fromUrn( statement.getSubject().getURI() ); |
88 |
| - } |
89 |
| - processedExtendingElements.add( statement.getSubject() ); |
90 |
| - if ( samm.AbstractEntity().equals( statement.getObject().asResource() ) ) { |
91 |
| - return modelElementFactory.create( AbstractEntity.class, statement.getSubject() ).getAspectModelUrn().get(); |
92 |
| - } |
93 |
| - return modelElementFactory.create( Entity.class, statement.getSubject() ).getAspectModelUrn().get(); |
94 |
| - } ).toList() ); |
95 |
| - creatingElements.remove( abstractEntity ); |
96 |
| - return entity; |
97 | 44 | }
|
98 | 45 | }
|
0 commit comments