Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Clone Dict
Clone Dict [message #1735985] Fri, 24 June 2016 08:23 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 344
Registered: August 2013
Senior Member
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 08:31 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
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?
Previous Topic:Announcing OCL 6.1.0 (and 6.2.0) for Neon
Next Topic:Evaluate OCL constraints in an UML model programmatically
Goto Forum:
  


Current Time: Sun May 05 08:54:06 GMT 2024

Powered by FUDForum. Page generated in 0.02338 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top