1818import io .adminshell .aas .v3 .dataformat .aml .AmlGenerator ;
1919import io .adminshell .aas .v3 .dataformat .aml .id .IdGenerator ;
2020import io .adminshell .aas .v3 .dataformat .aml .naming .NamingStrategy ;
21+ import io .adminshell .aas .v3 .dataformat .aml .util .AASUtils ;
22+ import io .adminshell .aas .v3 .dataformat .aml .util .ReferencedByViewCollector ;
2123import io .adminshell .aas .v3 .dataformat .aml .util .ReferencedReferableCollector ;
22- import io .adminshell .aas .v3 .dataformat .mapping .MappingContext ;
2324import io .adminshell .aas .v3 .dataformat .mapping .MappingException ;
2425import io .adminshell .aas .v3 .dataformat .mapping .MappingProvider ;
26+ import io .adminshell .aas .v3 .dataformat .mapping .SourceBasedMappingContext ;
2527import io .adminshell .aas .v3 .model .AssetAdministrationShellEnvironment ;
2628import io .adminshell .aas .v3 .model .Referable ;
2729import java .beans .PropertyDescriptor ;
3133import java .util .stream .Collectors ;
3234import org .slf4j .Logger ;
3335import org .slf4j .LoggerFactory ;
36+ import io .adminshell .aas .v3 .model .Reference ;
37+ import java .util .Optional ;
38+ import java .util .Set ;
3439
35- public class Aas2AmlMappingContext extends MappingContext {
40+ public class Aas2AmlMappingContext extends SourceBasedMappingContext {
3641
3742 private static final String EMPTY_STRING = "" ;
3843 private static final Logger log = LoggerFactory .getLogger (Aas2AmlMappingContext .class );
@@ -43,7 +48,7 @@ public class Aas2AmlMappingContext extends MappingContext {
4348 private final Map <Object , String > idCache ;
4449
4550 private final PropertyDescriptor property ;
46- private final Map < Referable , String > referecedReferableIDs ;
51+ private final Set < Reference > referencedReferables ;
4752
4853 public Aas2AmlMappingContext (MappingProvider mappingProvider ,
4954 IdGenerator idGenerator ,
@@ -55,7 +60,10 @@ public Aas2AmlMappingContext(MappingProvider mappingProvider,
5560 internalElementNamingStrategy ,
5661 attributeNamingStrategy ,
5762 environment ,
58- null , null , null );
63+ null ,
64+ null ,
65+ null );
66+
5967 }
6068
6169 public NamingStrategy getInternalElementNamingStrategy () {
@@ -71,7 +79,7 @@ private Aas2AmlMappingContext(MappingProvider mappingProvider,
7179 NamingStrategy internalElementNamingStrategy ,
7280 NamingStrategy attributeNamingStrategy ,
7381 AssetAdministrationShellEnvironment environment ,
74- Map < Referable , String > referecedReferableIDs ,
82+ Set < Reference > referencedReferables ,
7583 Map <Object , String > idCache ,
7684 PropertyDescriptor property ) {
7785 super (mappingProvider );
@@ -80,41 +88,60 @@ private Aas2AmlMappingContext(MappingProvider mappingProvider,
8088 this .attributeNamingStrategy = attributeNamingStrategy ;
8189 this .environment = environment ;
8290 this .property = property ;
83- if (referecedReferableIDs == null ) {
84- this .referecedReferableIDs = new ReferencedReferableCollector (environment ).collect ().stream ()
85- .collect (Collectors .toMap (x -> x , x -> EMPTY_STRING ));
91+ if (referencedReferables == null ) {
92+ this .referencedReferables = new ReferencedReferableCollector (environment ).collect ();
8693 } else {
87- this .referecedReferableIDs = referecedReferableIDs ;
94+ this .referencedReferables = referencedReferables ;
8895 }
8996 if (idCache == null ) {
9097 this .idCache = new HashMap <>();
9198 } else {
9299 this .idCache = idCache ;
93100 }
94101 }
102+ //
103+ // public String generateId() {
104+ // return idGenerator.generateId();
105+ // }
95106
96- public String getInternalLinkTargetId (Referable target ) {
97- if (!isTargetOfInternalLink (target )) {
98- referecedReferableIDs .put (target , getCachedId (target ));
99- }
100- return referecedReferableIDs .get (target );
101- }
102-
103- public String generateId () {
107+ public String newId () {
104108 return idGenerator .generateId ();
105109 }
106110
107- public String getCachedId (Object obj ) {
108- if (idCache .containsKey (obj )) {
109- return idCache .get (obj );
111+ public String getId (Reference reference ) {
112+ if (reference == null ) {
113+ return idGenerator .generateId ();
114+ }
115+ Optional <Reference > cacheKey = findMatchingKey (reference );
116+ if (cacheKey .isPresent ()) {
117+ return idCache .get (cacheKey .get ());
110118 }
111119 String result = idGenerator .generateId ();
112- idCache .put (obj , result );
120+ idCache .put (reference , result );
113121 return result ;
114122 }
115123
116- public boolean isTargetOfInternalLink (Referable target ) {
117- return referecedReferableIDs .containsKey (target ) && !referecedReferableIDs .get (target ).equals (EMPTY_STRING );
124+ private Optional <Reference > findMatchingKey (Reference reference ) {
125+ return idCache .keySet ().stream ()
126+ .filter (x -> Reference .class .isAssignableFrom (x .getClass ()))
127+ .map (x -> (Reference ) x )
128+ .filter (x -> AASUtils .equals (x , reference ))
129+ .findFirst ();
130+ }
131+
132+ // public String getCachedId(Object obj) {
133+ // if (elementsReferencedByViews.containsKey(obj)) {
134+ // return elementsReferencedByViews.get(obj);
135+ // }
136+ // if (idCache.containsKey(obj)) {
137+ // return idCache.get(obj);
138+ // }
139+ // String result = idGenerator.generateId();
140+ // idCache.put(obj, result);
141+ // return result;
142+ // }
143+ public boolean isTargetOfInternalLink (Reference targetRef ) {
144+ return referencedReferables .stream ().anyMatch (x -> AASUtils .equals (x , targetRef ));
118145 }
119146
120147 public <T > void map (T value , AmlGenerator generator ) throws MappingException {
@@ -127,36 +154,36 @@ public <T> void map(Type type, T value, AmlGenerator generator) throws MappingEx
127154
128155 public Aas2AmlMappingContext with (PropertyDescriptor property ) {
129156 return new Aas2AmlMappingContext (
130- mappingProvider ,
157+ getMappingProvider () ,
131158 idGenerator ,
132159 internalElementNamingStrategy ,
133160 attributeNamingStrategy ,
134161 environment ,
135- referecedReferableIDs ,
162+ referencedReferables ,
136163 idCache ,
137164 property );
138165 }
139166
140167 public Aas2AmlMappingContext withoutProperty () {
141168 return new Aas2AmlMappingContext (
142- mappingProvider ,
169+ getMappingProvider () ,
143170 idGenerator ,
144171 internalElementNamingStrategy ,
145172 attributeNamingStrategy ,
146173 environment ,
147- referecedReferableIDs ,
174+ referencedReferables ,
148175 idCache ,
149176 null );
150177 }
151178
152179 public Aas2AmlMappingContext withoutIdCache () {
153180 return new Aas2AmlMappingContext (
154- mappingProvider ,
181+ getMappingProvider () ,
155182 idGenerator ,
156183 internalElementNamingStrategy ,
157184 attributeNamingStrategy ,
158185 environment ,
159- referecedReferableIDs ,
186+ referencedReferables ,
160187 null ,
161188 property );
162189 }
@@ -168,13 +195,4 @@ public PropertyDescriptor getProperty() {
168195 public AssetAdministrationShellEnvironment getEnvironment () {
169196 return environment ;
170197 }
171-
172- public MappingProvider getMappingProvider () {
173- return mappingProvider ;
174- }
175-
176- public Map <Referable , String > getReferecedReferableIDs () {
177- return referecedReferableIDs ;
178- }
179-
180198}
0 commit comments