@@ -120,7 +120,8 @@ private Handlebars setupHandlebars() {
120120 if (searchedPropertyName .equals ("type" )) {
121121 TRelationshipTemplate tRelationshipTemplate = TRelationshipTemplate
122122 .safeInstantiate ((Association ) context );
123- propertyStringValue = getQualifiedName (tRelationshipTemplate .getRelationshipType ().getElement ());
123+ propertyStringValue = getQualifiedName (
124+ tRelationshipTemplate .getRelationshipType ().getElement ());
124125 }
125126 } else if (stereotype .getName ().equals ("TNodeType" )) {
126127 TNodeType tNodeType = TNodeType .safeInstantiate ((Class ) context );
@@ -175,34 +176,26 @@ private Handlebars setupHandlebars() {
175176 }
176177 throw new RuntimeException ("Stereotype property " + searchedPropertyName + " not found in " + context );
177178 });
178- // handlebars.registerHelper("noStereotypeApplications",
179- // (ModelTree context, Options options) -> Stream
180- // .concat(context.getOwnedElement().stream(),
181- // context instanceof Class ? ((Class) context).getOwnedAttribute().stream()
182- // : Stream.empty())
183- // .noneMatch(element -> element.getExtension().stream()
184- // .anyMatch(stereotype -> stereotype.getName().equals(options.params[0]))));
179+
185180 handlebars .registerHelper ("noStereotypeApplications" , (context , options ) -> {
186- // Ensure the context is a ModelTree
187- if (!(context instanceof ModelTree )) {
188- // Log an error or throw an exception if the context is not the expected type
189-
190- return true ; // Default to true if context is not valid
191- }
192-
193- // Get the stereotype name from options.params[0]
194- // Need to handle potential ClassCastException if options.params[0] is not a String
195- String stereotypeName = null ;
196- if (options .params .length > 0 && options .params [0 ] instanceof String ) {
197- stereotypeName = (String ) options .params [0 ];
198- } else {
199-
200- return true ; // Default to true if parameter is missing/invalid
201- }
202-
203- // Call our core logic
204- return noneHasStereotypeInSubtree ((ModelTree ) context , stereotypeName );
205- });
181+ // Ensure the context is a ModelTree
182+ if (!(context instanceof ModelTree )) {
183+ return true ; // Default to true if context is not valid
184+ }
185+
186+ // Get the stereotype name from options.params[0]
187+ // Need to handle potential ClassCastException if options.params[0] is not a
188+ // String
189+ String stereotypeName = null ;
190+ if (options .params .length > 0 && options .params [0 ] instanceof String ) {
191+ stereotypeName = (String ) options .params [0 ];
192+ } else {
193+ return true ; // Default to true if parameter is missing/invalid
194+ }
195+
196+ // Call our core logic
197+ return noneHasStereotypeInSubtree ((ModelTree ) context , stereotypeName );
198+ });
206199 handlebars .registerHelper ("imports" , (ModelElement context , Options options ) -> {
207200
208201 Set <Import > imports = new HashSet <>();
@@ -216,9 +209,10 @@ private Handlebars setupHandlebars() {
216209
217210 if (tNodeType .getDerivedFrom () != null ) {
218211
219- String derivedFromValue = tNodeType .getDerivedFrom ().getName ();
212+ String derivedFromValue = tNodeType .getDerivedFrom ().getElement (). getName ();
220213 String targetNamespace = tNodeType .getTargetNamespace ();
221- if (derivedFromValue != null && !derivedFromValue .startsWith ("tosca" )) {
214+ if (derivedFromValue != null
215+ && !getQualifiedName (tNodeType .getDerivedFrom ().getElement ()).startsWith ("tosca" )) {
222216 imports .add (new Import (derivedFromValue + ".tosca" , targetNamespace , "MYRTUS-" ));
223217 }
224218 }
@@ -246,7 +240,11 @@ private Handlebars setupHandlebars() {
246240 for (TNodeTemplate nodeTemplate : nodeTemplates ) {
247241
248242 String targetNamespace = ((Class ) context ).getOwner ().getName ();
249- imports .add (new Import (nodeTemplate .getElement ().getName () + ".tosca" , targetNamespace , "MYRTUS-" ));
243+ if (!getQualifiedName (nodeTemplate .getNodeType ().getElement ()).startsWith ("tosca" )) {
244+ imports .add (new Import (nodeTemplate .getNodeType ().getElement ().getName () + ".tosca" ,
245+ targetNamespace , "MYRTUS-" ));
246+
247+ }
250248
251249 }
252250
@@ -260,36 +258,42 @@ private Handlebars setupHandlebars() {
260258 handlebars .registerHelpers (ConditionalHelpers .class );
261259 return handlebars ;
262260 }
263-
264- private static Stream <ModelElement > flattenSubtree (ModelTree element ) {
265- return (element == null ) ? Stream .empty () : Stream .concat (
266- Stream .of (element ),
267- Stream .concat (
268- element .getOwnedElement ().stream ().filter (Objects ::nonNull ),
269- (element instanceof Class ) ? Stream .concat (((Class ) element ).getOwnedAttribute ().stream ().filter (Objects ::nonNull ), ((Class ) element ).getTargetingEnd ().stream ().filter (Objects ::nonNull ))
270- : Stream .empty ()
271- ).flatMap (child -> (child instanceof ModelTree ) ? flattenSubtree ((ModelTree ) child ) : Stream .of (child ))
272- );
273- }
274-
275- /**
276- * Checks if NONE of the elements in the entire subtree rooted at 'context'
277- * (including 'context' itself) have a stereotype with the specified name.
278- *
279- * @param context The root ModelTree element to start the search from.
280- * @param stereotypeName The name of the stereotype to check for.
281- * @return true if no element in the subtree has the stereotype, false otherwise.
282- */
283- public static boolean noneHasStereotypeInSubtree (ModelTree context , String stereotypeName ) {
284- // Handle null or invalid inputs gracefully
285- if (context == null || stereotypeName == null || stereotypeName .isEmpty ()) {
286- return true ;
287- }
288-
289- return flattenSubtree (context ) // Get all elements in the subtree
290- .noneMatch (element -> element .getExtension ().stream () // For each element
291- .anyMatch (stereotype -> stereotype != null && stereotype .getName ().equals (stereotypeName ))); // Check if it has the stereotype
292- }
261+
262+ private static Stream <ModelElement > flattenSubtree (ModelTree element ) {
263+ return (element == null ) ? Stream .empty ()
264+ : Stream .concat (Stream .of (element ), Stream
265+ .concat (element .getOwnedElement ().stream ().filter (Objects ::nonNull ), (element instanceof Class )
266+ ? Stream .concat (((Class ) element ).getOwnedAttribute ().stream ().filter (Objects ::nonNull ),
267+ ((Class ) element ).getTargetingEnd ().stream ().filter (Objects ::nonNull ))
268+ : Stream .empty ())
269+ .flatMap (child -> (child instanceof ModelTree ) ? flattenSubtree ((ModelTree ) child )
270+ : Stream .of (child )));
271+ }
272+
273+ /**
274+ * Checks if NONE of the elements in the entire subtree rooted at 'context'
275+ * (including 'context' itself) have a stereotype with the specified name.
276+ *
277+ * @param context The root ModelTree element to start the search from.
278+ * @param stereotypeName The name of the stereotype to check for.
279+ * @return true if no element in the subtree has the stereotype, false
280+ * otherwise.
281+ */
282+ public static boolean noneHasStereotypeInSubtree (ModelTree context , String stereotypeName ) {
283+ // Handle null or invalid inputs gracefully
284+ if (context == null || stereotypeName == null || stereotypeName .isEmpty ()) {
285+ return true ;
286+ }
287+
288+ return flattenSubtree (context ) // Get all elements in the subtree
289+ .noneMatch (element -> element .getExtension ().stream () // For each element
290+ .anyMatch (stereotype -> stereotype != null && stereotype .getName ().equals (stereotypeName ))); // Check
291+ // if
292+ // it
293+ // has
294+ // the
295+ // stereotype
296+ }
293297
294298 @ objid ("4e0fc0cf-420e-4626-b34d-fd6df12a1e01" )
295299 private String generateImportString (Set <Import > imports ) {
@@ -302,6 +306,10 @@ private String generateImportString(Set<Import> imports) {
302306 .append (anImport .getNamespacePrefix ()).append ("\n " );
303307 }
304308
309+ // Prune the last newline character if the string is not empty
310+ if (importString .length () > 0 && importString .charAt (importString .length () - 1 ) == '\n' ) {
311+ importString .setLength (importString .length () - 1 );
312+ }
305313 return importString .toString ();
306314 }
307315
0 commit comments