| 
| [QVTO] Avoiding multiple object creation [message #91769] | Wed, 08 October 2008 11:37  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: mahdider.students.uni-mainz.de 
 Hello,
 
 I have the following code for creating a mutator for each attribute:
 
 /*
 Generates a mutator or setter method for a given attribute.
 */
 mapping SIMPLE_UML::UmlAttribute::attribute2Setter() :
 SIMPLE_UML::UmlMethod {
 name := 'set' + self.name.firstToUpper();
 visibility := UmlVisibility::PUBLIC;
 
 -- TODO add parameters.
 
 -- Create a new object, since void is unknown in the incoming model.
 classifier := object UmlClassifier {
 name := 'void';
 };
 }
 
 The issue is of course, that this method is being called multiple times
 and this will result in multiple "void" classifier instances in the
 target model. I played around with var, basically trying to keep a
 reference to the created object but it did not really work (syntax wise)
 as I expected.
 
 What is a good way to create such instances only once? Or should I
 basically check somehow if there is already such an instance in self.
 with some resolve() call) and then create the instance only?
 
 Thanks for your help!
 
 Best Regards,
 Mahdi D-Manesh
 |  |  |  | 
| 
| Re: [QVTO] Avoiding multiple object creation [message #91783 is a reply to message #91769] | Wed, 08 October 2008 12:16   |  | 
| Eclipse User  |  |  |  |  | Hi Mahdi, 
 The easiest way to define a singleton is using mapping with no input  =
 
 parameters.
 If such a mapping is executed, a trace for the mapping call with empty s=
 et  =
 
 of source
 objects is recorded.
 Any subsequent call represents a call on the same source objects (empty =
 =
 
 set) and
 the result is fetched from traces instead of executing the mapping again=
 ..
 
 See the example bellow:
 
 main() {
 var x :=3D map singleVoid();
 var y :=3D map singleVoid();
 assert fatal (x =3D y);
 }
 
 mapping singleVoid() : UML::Classifier {
 name :=3D 'void';
 }
 
 
 Another technique could be to check for existing instances in the init {=
 }  =
 
 section and
 eventually assign one of them to the 'result' variable explicitly, thus =
 =
 
 limiting a repeated creation.
 This might be useful, if you have specific constraints on the number of =
 =
 
 instances
 to be created. The former way is the simplest for your usecase.
 
 Regards,
 /Radek
 
 
 
 On Wed, 08 Oct 2008 17:37:26 +0200, Mahdi D-Manesh  =
 
 <mahdider@students.uni-mainz.de> wrote:
 
 > Hello,
 >
 > I have the following code for creating a mutator for each attribute:
 >
 > /*
 > 	Generates a mutator or setter method for a given attribute.
 > */
 > mapping SIMPLE_UML::UmlAttribute::attribute2Setter() :  =
 
 > SIMPLE_UML::UmlMethod {
 > 	name :=3D 'set' + self.name.firstToUpper();
 > 	visibility :=3D UmlVisibility::PUBLIC;
 > 	=
 
 > 	-- TODO add parameters.
 >
 > 	-- Create a new object, since void is unknown in the incoming model.
 > 	classifier :=3D object UmlClassifier {
 > 		name :=3D 'void';
 > 	};
 > }
 >
 > The issue is of course, that this method is being called multiple time=
 s  =
 
 > and this will result in multiple "void" classifier instances in the  =
 
 > target model. I played around with var, basically trying to keep a  =
 
 > reference to the created object but it did not really work (syntax wis=
 e)  =
 
 > as I expected.
 >
 > What is a good way to create such instances only once? Or should I  =
 
 > basically check somehow if there is already such an instance in self. =
 =
 
 > with some resolve() call) and then create the instance only?
 >
 > Thanks for your help!
 >
 > Best Regards,
 > Mahdi D-Manesh
 
 
 
 -- =
 
 Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 |  |  |  | 
| 
| Re: [QVTO] Avoiding multiple object creation [message #91867 is a reply to message #91783] | Thu, 09 October 2008 05:25  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: mahdider.students.uni-mainz.de 
 Radek,
 
 I tried your approach and it works like expected. So I am using the
 "singleton" way now. :)
 
 Best Regards,
 Mahdi
 
 
 Radek Dvorak schrieb:
 > Hi Mahdi,
 >
 > The easiest way to define a singleton is using mapping with no input
 > parameters.
 > If such a mapping is executed, a trace for the mapping call with empty
 > set of source
 > objects is recorded.
 > Any subsequent call represents a call on the same source objects (empty
 > set) and
 > the result is fetched from traces instead of executing the mapping again.
 >
 > See the example bellow:
 >
 > main() {
 >     var x := map singleVoid();
 >     var y := map singleVoid();
 >     assert fatal (x = y);
 > }
 >
 > mapping singleVoid() : UML::Classifier {
 >     name := 'void';
 > }
 >
 >
 > Another technique could be to check for existing instances in the init
 > {} section and
 > eventually assign one of them to the 'result' variable explicitly, thus
 > limiting a repeated creation.
 > This might be useful, if you have specific constraints on the number of
 > instances
 > to be created. The former way is the simplest for your usecase.
 >
 > Regards,
 > /Radek
 >
 >
 >
 > On Wed, 08 Oct 2008 17:37:26 +0200, Mahdi D-Manesh
 > <mahdider@students.uni-mainz.de> wrote:
 >
 >> Hello,
 >>
 >> I have the following code for creating a mutator for each attribute:
 >>
 >> /*
 >>     Generates a mutator or setter method for a given attribute.
 >> */
 >> mapping SIMPLE_UML::UmlAttribute::attribute2Setter() :
 >> SIMPLE_UML::UmlMethod {
 >>     name := 'set' + self.name.firstToUpper();
 >>     visibility := UmlVisibility::PUBLIC;
 >>
 >>     -- TODO add parameters.
 >>
 >>     -- Create a new object, since void is unknown in the incoming model.
 >>     classifier := object UmlClassifier {
 >>         name := 'void';
 >>     };
 >> }
 >>
 >> The issue is of course, that this method is being called multiple
 >> times and this will result in multiple "void" classifier instances in
 >> the target model. I played around with var, basically trying to keep a
 >> reference to the created object but it did not really work (syntax
 >> wise) as I expected.
 >>
 >> What is a good way to create such instances only once? Or should I
 >> basically check somehow if there is already such an instance in self.
 >> with some resolve() call) and then create the instance only?
 >>
 >> Thanks for your help!
 >>
 >> Best Regards,
 >> Mahdi D-Manesh
 >
 >
 >
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.05393 seconds