Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "ATL/Design Patterns"

< ATL
(Created page with "== ATL Design Patterns == This page describes various [http://en.wikipedia.org/wiki/Software_design_pattern design patterns] that are specific to the use of ATL. === Mapped-...")
 
Line 1: Line 1:
== ATL Design Patterns ==
 
 
 
This page describes various [http://en.wikipedia.org/wiki/Software_design_pattern design patterns] that are specific to the use of ATL.
 
This page describes various [http://en.wikipedia.org/wiki/Software_design_pattern design patterns] that are specific to the use of ATL.
  
=== Mapped-By ===
+
== Mapped-By ==
  
 
If you find yourself executing the following code many times, the Mapped-By pattern may be useful to you:
 
If you find yourself executing the following code many times, the Mapped-By pattern may be useful to you:

Revision as of 05:17, 28 February 2015

This page describes various design patterns that are specific to the use of ATL.

Mapped-By

If you find yourself executing the following code many times, the Mapped-By pattern may be useful to you:

 METAMODEL!MetaClass.allInstances()->any(e | e.property = variable.property)

This code is expensive if executed more than once, because it has to iterate over all instances of METAMODEL!MetaClass every time (O(n)). Instead, you can create a Map that indexes all instanced by their requested property:

 helper def : metaClassByProperty : Map(OclAny, METAMODEL!MetaClass) =
   METAMODEL!MetaClass.allInstances()->iterate(e; acc : Map(OclAny, METAMODEL!MetaClass) = Map{} |
     acc->including(e.property, e)
   );

Building the Map still runs in O(n), but after that, you can write the initial code as follows:

 thisModule.metaClassByProperty.get(variable.property)

Because this is a hash map lookup, it runs in O(1).

Copyright © Eclipse Foundation, Inc. All Rights Reserved.