3636import org .slf4j .Logger ;
3737import org .slf4j .LoggerFactory ;
3838
39+ /**
40+ * Immutable class supporting creation of AML file by encalsulating different
41+ * common tasks and functionality, e.g. id generation and caching. To create new
42+ * instances with modified properties use provided with...(...) functions.
43+ */
3944public class AmlGenerator {
4045
4146 private static final Logger log = LoggerFactory .getLogger (AmlGenerator .class );
4247 public static final String REFERABLE_REFERENCE_INTERFACE_CLASS = "ReferableReference" ;
4348 private static final String DEFAULT_REF_SEMANTIC_PREFIX = "AAS" ;
49+
50+ public static Builder builder () {
51+ return new Builder ();
52+ }
4453 private final String refSemanticPrefix ;
4554 private final CAEXObject .Builder current ;
4655 private final AmlDocumentInfo documentInfo ;
@@ -66,6 +75,12 @@ private AmlGenerator(
6675 this .reference = reference ;
6776 }
6877
78+ /**
79+ * Creates a new instance with new current CAEXObject.Builder
80+ *
81+ * @param current new current CAEXObject.Builder
82+ * @return new immutable instance with CAEXObject.Builder
83+ */
6984 public AmlGenerator with (CAEXObject .Builder current ) {
7085 return new AmlGenerator (documentInfo ,
7186 idGenerator ,
@@ -76,6 +91,12 @@ public AmlGenerator with(CAEXObject.Builder current) {
7691 reference );
7792 }
7893
94+ /**
95+ * Creates a new instance with reference to new parent object in AAS
96+ *
97+ * @param parent reference to new parent element
98+ * @return new immutable instance with new parent
99+ */
79100 public AmlGenerator with (Referable parent ) {
80101 return new AmlGenerator (
81102 documentInfo ,
@@ -87,6 +108,12 @@ public AmlGenerator with(Referable parent) {
87108 AasUtils .toReference (reference , parent ));
88109 }
89110
111+ /**
112+ * Creates a new instance with given prefix for refSemantic tags
113+ *
114+ * @param value new prefix for refSemantic tags
115+ * @return new immutable instance given refSemantic prefix
116+ */
90117 public AmlGenerator withRefSemanticPrefix (String value ) {
91118 return new AmlGenerator (
92119 documentInfo ,
@@ -98,62 +125,36 @@ public AmlGenerator withRefSemanticPrefix(String value) {
98125 reference );
99126 }
100127
128+ /**
129+ * Adds additional information to the AML file
130+ *
131+ * @param additionalInformation additional information to add
132+ */
101133 public void addAdditionalInformation (List <Object > additionalInformation ) {
102134 fileBuilder .addAdditionalInformation (additionalInformation );
103135 }
104136
105- public static Builder builder () {
106- return new Builder ();
107- }
108-
109- public static class Builder {
110-
111- private String refSemanticPrefix = DEFAULT_REF_SEMANTIC_PREFIX ;
112- private CAEXObject .Builder current ;
113- private CAEXFile .Builder fileBuilder = CAEXFile .builder ();
114- private AmlDocumentInfo documentInfo = new AmlDocumentInfo ();
115- private IdGenerator idGenerator = new UuidGenerator ();
116-
117- public AmlGenerator build () {
118- return new AmlGenerator (documentInfo ,
119- idGenerator ,
120- new HashMap <>(),
121- refSemanticPrefix ,
122- fileBuilder ,
123- current ,
124- new DefaultReference .Builder ().build ());
125- }
126-
127- public Builder refSemanticPrefix (String value ) {
128- this .refSemanticPrefix = value ;
129- return this ;
130- }
131-
132- public Builder current (CAEXObject .Builder value ) {
133- this .current = value ;
134- return this ;
135- }
136-
137- public Builder idGenerator (IdGenerator value ) {
138- this .idGenerator = value ;
139- return this ;
140- }
141-
142- public Builder file (CAEXFile .Builder value ) {
143- this .fileBuilder = value ;
144- return this ;
145- }
146-
147- public Builder documentInfo (AmlDocumentInfo value ) {
148- this .documentInfo = value ;
149- return this ;
150- }
151- }
152-
137+ /**
138+ * Generates a new ID without caching it.
139+ *
140+ * @return a new ID
141+ */
153142 public String newId () {
154143 return idGenerator .next ();
155144 }
156145
146+ /**
147+ * Gets the ID for given object. If obj is an instance of Referable it is
148+ * first converted to reference pointing to that element. In that case, or
149+ * if the obj is already an instance of Reference, an already existing ID
150+ * will be returned if available, otherwise a new one is created and cached.
151+ * If obj is neither an instance of Reference nor Referable, a new ID is
152+ * generated and not cached.
153+ *
154+ * @param obj object to generate an ID for
155+ * @return an ID that might or might not be cached (depending of type of
156+ * obj)
157+ */
157158 public String getId (Object obj ) {
158159 if (obj == null ) {
159160 return idGenerator .next ();
@@ -177,6 +178,11 @@ public String getId(Object obj) {
177178 return result ;
178179 }
179180
181+ /**
182+ * Adds an object to the current element of the AML document
183+ *
184+ * @param caexObject the object to add
185+ */
180186 public void add (CAEXObject caexObject ) {
181187 if (caexObject == null ) {
182188 return ;
@@ -190,6 +196,11 @@ public void add(CAEXObject caexObject) {
190196 }
191197 }
192198
199+ /**
200+ * Adds an attribute to the current element of the AML document
201+ *
202+ * @param attribute the attribute to add
203+ */
193204 public void addAttribute (AttributeType attribute ) {
194205 if (attribute == null ) {
195206 return ;
@@ -221,14 +232,26 @@ private void addExternalInterface(RoleClassType.ExternalInterface externalInterf
221232 }
222233 }
223234
224- public void addExternalInterfaceForReference (MappingContext context ) {
235+ /**
236+ * Adds an external interface with name and roleCall ReferableReference to
237+ * the current element of the AML document
238+ */
239+ public void addExternalInterfaceForReference () {
225240 addExternalInterface (RoleClassType .ExternalInterface .builder ()
226241 .withID (newId ())
227242 .withName (REFERABLE_REFERENCE_INTERFACE_CLASS )
228243 .withRefBaseClassPath (documentInfo .getAssetAdministrationShellInterfaceClassLib () + "/" + REFERABLE_REFERENCE_INTERFACE_CLASS )
229244 .build (), false );
230245 }
231246
247+ /**
248+ * Adds an internal link with given namen pointing from source to target to
249+ * the current element of the AML document
250+ *
251+ * @param name name if the link
252+ * @param source source element of the link
253+ * @param target reference pointing to the target element
254+ */
232255 public void addInternalLink (String name , Referable source , Reference target ) {
233256 if (InternalElementType .Builder .class .isAssignableFrom (current .getClass ())) {
234257 InternalElementType .Builder builder = (InternalElementType .Builder ) current ;
@@ -243,7 +266,15 @@ public void addInternalLink(String name, Referable source, Reference target) {
243266 }
244267 }
245268
246- public void addExternalInterfaceForUnresolvableReference (String name , Reference reference , MappingContext context ) {
269+ /**
270+ * Adds an external interface of type ReferableReference with given name
271+ * with unresolvabled reference as value to the current element of the AML
272+ * document
273+ *
274+ * @param name name of the interface
275+ * @param reference unresolvable target reference
276+ */
277+ public void addExternalInterfaceForUnresolvableReference (String name , Reference reference ) {
247278 addExternalInterface (RoleClassType .ExternalInterface .builder ()
248279 .withID (idGenerator .next ())
249280 .withName (name )
@@ -258,6 +289,12 @@ public void addExternalInterfaceForUnresolvableReference(String name, Reference
258289 false );
259290 }
260291
292+ /**
293+ * Clears the ID cache. This should be used with caution as it may cause
294+ * reference resolution mechanism to fail. Typically it will only be called
295+ * after processing the InstanceHierarchy elements and before creating
296+ * custom SystemUnitClasses
297+ */
261298 public void clearIdCache () {
262299 idCache .clear ();
263300 }
@@ -344,23 +381,66 @@ public AttributeType.RefSemantic refSemantic(Class<?> type, String propertyName)
344381 .build ();
345382 }
346383
384+ /**
385+ * Creates a refSemantic object for a given property
386+ *
387+ * @param property the property
388+ * @return the generated refSemantic object
389+ */
347390 public AttributeType .RefSemantic refSemantic (PropertyDescriptor property ) {
348391 return refSemantic (property .getReadMethod ().getDeclaringClass (), property .getName ());
349392 }
350393
351- public String refBaseSystemUnitPath (Object value , MappingContext context ) {
352- return documentInfo .getAssetAdministrationShellSystemUnitClassLib ()
353- + "/" + context .getInternalElementNamingStrategy ().getName (
354- value .getClass (),
355- value ,
356- null );
357- }
358-
394+ /**
395+ * Gets to AML document info
396+ *
397+ * @return the AML document info
398+ */
359399 public AmlDocumentInfo getDocumentInfo () {
360400 return documentInfo ;
361401 }
362402
363- public Reference getReference () {
364- return reference ;
403+ public static class Builder {
404+
405+ private String refSemanticPrefix = DEFAULT_REF_SEMANTIC_PREFIX ;
406+ private CAEXObject .Builder current ;
407+ private CAEXFile .Builder fileBuilder = CAEXFile .builder ();
408+ private AmlDocumentInfo documentInfo = new AmlDocumentInfo ();
409+ private IdGenerator idGenerator = new UuidGenerator ();
410+
411+ public AmlGenerator build () {
412+ return new AmlGenerator (documentInfo ,
413+ idGenerator ,
414+ new HashMap <>(),
415+ refSemanticPrefix ,
416+ fileBuilder ,
417+ current ,
418+ new DefaultReference .Builder ().build ());
419+ }
420+
421+ public Builder refSemanticPrefix (String value ) {
422+ this .refSemanticPrefix = value ;
423+ return this ;
424+ }
425+
426+ public Builder current (CAEXObject .Builder value ) {
427+ this .current = value ;
428+ return this ;
429+ }
430+
431+ public Builder idGenerator (IdGenerator value ) {
432+ this .idGenerator = value ;
433+ return this ;
434+ }
435+
436+ public Builder file (CAEXFile .Builder value ) {
437+ this .fileBuilder = value ;
438+ return this ;
439+ }
440+
441+ public Builder documentInfo (AmlDocumentInfo value ) {
442+ this .documentInfo = value ;
443+ return this ;
444+ }
365445 }
366446}
0 commit comments