| 
| Clone Dict [message #1735985] | Fri, 24 June 2016 04:23  |  | 
| Eclipse User  |  |  |  |  | Hi 
 There is a hierarchy of mappings in my project. Each mapping receives a dictionary which holds some context. Each mapping may add something to the context (dictionary) and pass extended context to nested mappings. The problem is that nested mappings modifies context of the nesting mappings. For example map1 modifies variables dictionary in main:
 
 
 
modeltype ECORE uses 'http://www.eclipse.org/emf/2002/Ecore';
transformation TestDict();
main() {
    var variables : Dict(String, EClass);
    variables->put('a', object EClass { name := 'classA' });
    variables->print();
    object EClass { name := 'test' }->map1(variables);
    variables->print();
}
mapping EClass::map1(variables : Dict(String, EClass)) : EClass
{
    var variables2 := variables;
    variables2->put('b', object EClass { name := 'classB' });
}
mapping EClass::map2(variables : Dict(String, EClass)) : EClass
{
    var variables2 : Dict(String, EClass);
    variables->keys()->forEach (k) {
        variables2->put(k, variables->get(k));
    };
    variables2->put('b', object EClass { name := 'classB' });
}
query Dict(String, EClass)::print()
{
    log('Variables >>>');
    self->keys()->forEach (k) {
        log(k + ' => ' + self->get(k).name);
    };
    log('<<<\n');
}
 So I have to create a new dictionary each time (see map2). Is there a better approach to clone dictionaries?
 |  |  |  | 
| 
| Re: Clone Dict [message #1735989 is a reply to message #1735985] | Fri, 24 June 2016 04:31  |  | 
| Eclipse User  |  |  |  |  | HI 
 OCL is side effect free so it has no mutable Dicts. Presumably you meant
 to post on the QVTo newsgroup.
 
 Dicts are a convenient tool. I prefer them since I can understand and
 debug them.
 
 resolve() is a better approach. QVT 1.3 provides a much clearer
 specification of how resolve() works. Eclipse QVTo is not fully
 compliant with the clarifications, but it is close. The description may
 enable you to predict what should happen.
 
 Regards
 
 Ed Willink
 
 On 24/06/2016 09:23, Denis Nikiforov wrote:
 > Hi
 >
 > There is a hierarchy of mappings in my project. Each mapping receives
 > a dictionary which holds some context. Each mapping may add something
 > to the context (dictionary) and pass extended context to nested
 > mappings. The problem is that nested mappings modifies context of the
 > nesting mappings. For example map1 modifies variables dictionary in main:
 >
 >
 > modeltype ECORE uses 'http://www.eclipse.org/emf/2002/Ecore';
 >
 > transformation TestDict();
 >
 > main() {
 >    var variables : Dict(String, EClass);
 >    variables->put('a', object EClass { name := 'classA' });
 >    variables->print();
 >    object EClass { name := 'test' }->map1(variables);
 >    variables->print();
 > }
 >
 > mapping EClass::map1(variables : Dict(String, EClass)) : EClass
 > {
 >    var variables2 := variables;
 >    variables2->put('b', object EClass { name := 'classB' });
 > }
 >
 > mapping EClass::map2(variables : Dict(String, EClass)) : EClass
 > {
 >    var variables2 : Dict(String, EClass);
 >    variables->keys()->forEach (k) {
 >        variables2->put(k, variables->get(k));
 >    };
 >    variables2->put('b', object EClass { name := 'classB' });
 > }
 >
 > query Dict(String, EClass)::print()
 > {
 >    log('Variables >>>');
 >    self->keys()->forEach (k) {
 >        log(k + ' => ' + self->get(k).name);
 >    };
 >    log('<<<\n');
 > }
 >
 >
 > So I have to create a new dictionary each time (see map2). Is there a
 > better approach to clone dictionaries?
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.05748 seconds