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 "Epsilon/EMF"

(Created page with "== What is the difference between containment and non-containment references in EMF? == Briefly, a model element can belong to as most one containment reference at a time. Co...")
 
 
Line 3: Line 3:
 
Briefly, a model element can belong to as most one containment reference at a time. Containment references also demonstrate a cascade-delete behaviour. For example, consider the following Ecore metamodel (captured in Emfatic).
 
Briefly, a model element can belong to as most one containment reference at a time. Containment references also demonstrate a cascade-delete behaviour. For example, consider the following Ecore metamodel (captured in Emfatic).
  
  package cars;
+
package cars;
 
    
 
    
  class Person {
+
class Person {
    ref Person[*] friends; //non-containment reference
+
  ref Person[*] friends; //non-containment reference
    val Car[*] cars; // containment reference
+
  val Car[*] cars; // containment reference
  }
+
}
 
    
 
    
  class Car { }
+
class Car { }
  
 
Now consider the following EOL code which demonstrates the similarities/differences of containment and non-containment references.
 
Now consider the following EOL code which demonstrates the similarities/differences of containment and non-containment references.
  
  // Set up a few model elements to play with
+
// Set up a few model elements to play with
  var c1 = new Car;
+
var c1 = new Car;
  var c2 = new Car;
+
var c2 = new Car;
  var p1 = new Person;
+
var p1 = new Person;
  var p2 = new Person;
+
var p2 = new Person;
  var p3 = new Person;
+
var p3 = new Person;
 
    
 
    
  // p1's car is c1 and p2's car is c2
+
// p1's car is c1 and p2's car is c2
  p1.cars.add(c1);
+
p1.cars.add(c1);
  p2.cars.add(c2);
+
p2.cars.add(c2);
 
    
 
    
  // p3 is a friend of both p1 and p2
+
// p3 is a friend of both p1 and p2
  p1.friends.add(p3);
+
p1.friends.add(p3);
  p2.friends.add(p3);
+
p2.friends.add(p3);
 
    
 
    
  p1.friends.println(); // prints {p3}
+
p1.friends.println(); // prints {p3}
  p2.friends.println(); // prints {p3}
+
p2.friends.println(); // prints {p3}
 
    
 
    
  //add c2 to p1's cars
+
//add c2 to p1's cars
  p1.cars.add(c2);
+
p1.cars.add(c2);
  p1.cars.println(); // prints {c1, c2}
+
p1.cars.println(); // prints {c1, c2}
 
    
 
    
  // The following statement prints an empty set!  
+
// The following statement prints an empty set!  
  // As discussed above, model elements can belong to at  
+
// As discussed above, model elements can belong to at  
  // most 1 containment reference. As such, by adding c2 to
+
// most 1 containment reference. As such, by adding c2 to
  // the cars of p1, EMF removes it from the cars of p2
+
// the cars of p1, EMF removes it from the cars of p2
  p2.cars.println();
+
p2.cars.println();
 
    
 
    
  // Delete p1 from the model
+
// Delete p1 from the model
  delete p1;
+
delete p1;
 
    
 
    
  Person.all.println(); // prints {p2, p3}
+
Person.all.println(); // prints {p2, p3}
 
    
 
    
  // The following statement prints an empty set!
+
// The following statement prints an empty set!
  // As discussed above, containment references demonstrate
+
// As discussed above, containment references demonstrate
  // a cascade-delete behaviour. As such, when we deleted p1,
+
// a cascade-delete behaviour. As such, when we deleted p1,
  // all the model elements contained in its cars containment reference
+
// all the model elements contained in its cars containment reference
  // were also deleted from the model. Note how the friends of p1 (p2 and p3)
+
// were also deleted from the model. Note how the friends of p1 (p2 and p3)
  // were not deleted from the model, since they were referenced through a  
+
// were not deleted from the model, since they were referenced through a  
  // non-containment reference (friends)
+
// non-containment reference (friends)
  Car.all.println();
+
Car.all.println();

Latest revision as of 16:16, 14 December 2014

What is the difference between containment and non-containment references in EMF?

Briefly, a model element can belong to as most one containment reference at a time. Containment references also demonstrate a cascade-delete behaviour. For example, consider the following Ecore metamodel (captured in Emfatic).

package cars;
 
class Person {
  ref Person[*] friends; //non-containment reference
  val Car[*] cars; // containment reference
}
 
class Car { }

Now consider the following EOL code which demonstrates the similarities/differences of containment and non-containment references.

// Set up a few model elements to play with
var c1 = new Car;
var c2 = new Car;
var p1 = new Person;
var p2 = new Person;
var p3 = new Person;
 
// p1's car is c1 and p2's car is c2
p1.cars.add(c1);
p2.cars.add(c2);
 
// p3 is a friend of both p1 and p2
p1.friends.add(p3);
p2.friends.add(p3);
 
p1.friends.println(); // prints {p3}
p2.friends.println(); // prints {p3}
 
//add c2 to p1's cars
p1.cars.add(c2);
p1.cars.println(); // prints {c1, c2}
 
// The following statement prints an empty set! 
// As discussed above, model elements can belong to at 
// most 1 containment reference. As such, by adding c2 to
// the cars of p1, EMF removes it from the cars of p2
p2.cars.println();
 
// Delete p1 from the model
delete p1;
 
Person.all.println(); // prints {p2, p3}
 
// The following statement prints an empty set!
// As discussed above, containment references demonstrate
// a cascade-delete behaviour. As such, when we deleted p1,
// all the model elements contained in its cars containment reference
// were also deleted from the model. Note how the friends of p1 (p2 and p3)
// were not deleted from the model, since they were referenced through a 
// non-containment reference (friends)
Car.all.println();

Copyright © Eclipse Foundation, Inc. All Rights Reserved.