@@ -10,59 +10,102 @@ public static void main(String[] args) {
1010 try (World world = new World ()) {
1111 world .component (Position .class );
1212
13- Entity root = world .obtainEntity (world .entity ("Root" ));
14- root .set (new Position (0 , 0 ));
13+ childOfHierarchy (world );
14+ parentHierarchy (world );
15+ customRelationships (world );
16+ isaInheritance (world );
17+ }
18+ }
1519
16- Entity child1 = world .obtainEntity (world .entity ("Child1" ));
17- child1 .set (new Position (10 , 10 ));
18- child1 .childOf (root );
20+ private static void childOfHierarchy (World world ) {
21+ System .out .println ("=== ChildOf Hierarchy ===" );
1922
20- Entity child2 = world .obtainEntity (world .entity ("Child2" ));
21- child2 .set (new Position (20 , 20 ));
22- child2 .childOf (root );
23+ Entity scene = world .obtainEntity (world .entity ("Scene" ));
24+ scene .set (new Position (0 , 0 ));
2325
24- Entity grandchild = world .obtainEntity (world .entity ("Grandchild " ));
25- grandchild .set (new Position (15 , 15 ));
26- grandchild .childOf (child1 );
26+ Entity child1 = world .obtainEntity (world .entity ("Child1 " ));
27+ child1 .set (new Position (10 , 10 ));
28+ child1 .childOf (scene );
2729
28- System .out .println ("Child1 parent: " + world .obtainEntity (child1 .parent ()).getName ());
29- System .out .println ("Grandchild parent: " + world .obtainEntity (grandchild .parent ()).getName ());
30+ Entity child2 = world .obtainEntity (world .entity ("Child2" ));
31+ child2 .set (new Position (20 , 20 ));
32+ child2 .childOf (scene );
3033
31- System .out .println ("\n Root children:" );
32- root .children (child -> System .out .println (" - " + world .obtainEntity (child ).getName ()));
34+ Entity grandchild = world .obtainEntity (world .entity ("Grandchild" ));
35+ grandchild .set (new Position (15 , 15 ));
36+ grandchild .childOf (child1 );
3337
34- System .out .println ("\n Child1 children:" );
35- child1 . children ( child -> System .out .println (" - " + world .obtainEntity (child ) .getName () ));
38+ System .out .println ("Child1 parent: " + world . obtainEntity ( child1 . parent ()). getName () );
39+ System .out .println ("Grandchild parent: " + world .obtainEntity (grandchild . parent ()) .getName ());
3640
37- long likesRelation = world . entity ( "Likes " );
38- long eatsRelation = world .entity ( "Eats" );
41+ System . out . println ( "Scene children: " );
42+ scene . children ( c -> System . out . println ( " - " + world .obtainEntity ( c ). getName ()) );
3943
40- Entity alice = world . obtainEntity ( world . entity ( "Alice" ) );
41- Entity bob = world .obtainEntity (world . entity ( "Bob" ));
42- Entity apple = world . obtainEntity ( world . entity ( "Apple" ));
44+ System . out . println ( "Child1 children:" );
45+ child1 . children ( c -> System . out . println ( " - " + world .obtainEntity (c ). getName () ));
46+ }
4347
44- alice .addRelation (likesRelation , bob .id ());
45- alice .addRelation (eatsRelation , apple .id ());
46- bob .addRelation (likesRelation , alice .id ());
48+ private static void parentHierarchy (World world ) {
49+ System .out .println ("\n === Parent Hierarchy ===" );
4750
48- System .out .println ("\n Alice likes Bob: " + alice .hasRelation (likesRelation , bob .id ()));
49- System .out .println ("Alice eats Apple: " + alice .hasRelation (eatsRelation , apple .id ()));
50- System .out .println ("Bob likes Alice: " + bob .hasRelation (likesRelation , alice .id ()));
51+ Entity spaceship = world .obtainEntity (world .entity ("Spaceship" ));
52+ spaceship .set (new Position (100 , 200 ));
5153
52- long vehicleId = world .entity ("Vehicle" );
53- long carId = world . entity ( "Car" );
54+ Entity cockpit = world .obtainEntity ( world . entity (spaceship . id (), "Cockpit" ) );
55+ cockpit . set ( new Position ( 105 , 210 ) );
5456
55- Entity myCar = world .obtainEntity (world .entity ("MyCar " ));
56- myCar . isA ( carId );
57+ Entity engine = world .obtainEntity (world .entity (spaceship . id (), "Engine " ));
58+ engine . set ( new Position ( 95 , 190 ) );
5759
58- Entity car = world .obtainEntity (carId );
59- car . isA ( vehicleId );
60+ Entity fuelTank = world .obtainEntity (world . entity ( engine . id (), "FuelTank" ) );
61+ fuelTank . set ( new Position ( 93 , 188 ) );
6062
61- System .out .println ("\n MyCar is a Car: " + myCar .hasRelation (world .lookup ("IsA" ), carId ));
63+ System .out .println ("Cockpit parent: " + world .obtainEntity (cockpit .parent ()).getName ());
64+ System .out .println ("FuelTank parent: " + world .obtainEntity (fuelTank .parent ()).getName ());
6265
63- alice .removeRelation (likesRelation , bob .id ());
64- System .out .println ("\n Alice likes Bob after remove: " + alice .hasRelation (likesRelation , bob .id ()));
65- }
66+ System .out .println ("Spaceship children:" );
67+ spaceship .children (c -> System .out .println (" - " + world .obtainEntity (c ).getName ()));
68+
69+ System .out .println ("Engine children:" );
70+ engine .children (c -> System .out .println (" - " + world .obtainEntity (c ).getName ()));
6671 }
67- }
6872
73+ private static void customRelationships (World world ) {
74+ System .out .println ("\n === Custom Relationships ===" );
75+
76+ long likes = world .entity ("Likes" );
77+ long eats = world .entity ("Eats" );
78+
79+ Entity alice = world .obtainEntity (world .entity ("Alice" ));
80+ Entity bob = world .obtainEntity (world .entity ("Bob" ));
81+ Entity apple = world .obtainEntity (world .entity ("Apple" ));
82+
83+ alice .addRelation (likes , bob .id ());
84+ alice .addRelation (eats , apple .id ());
85+ bob .addRelation (likes , alice .id ());
86+
87+ System .out .println ("Alice likes Bob: " + alice .hasRelation (likes , bob .id ()));
88+ System .out .println ("Alice eats Apple: " + alice .hasRelation (eats , apple .id ()));
89+ System .out .println ("Bob likes Alice: " + bob .hasRelation (likes , alice .id ()));
90+
91+ alice .removeRelation (likes , bob .id ());
92+ System .out .println ("Alice likes Bob after remove: " + alice .hasRelation (likes , bob .id ()));
93+ }
94+
95+ private static void isaInheritance (World world ) {
96+ System .out .println ("\n === IsA Inheritance ===" );
97+
98+ long vehicleId = world .entity ("Vehicle" );
99+ long carId = world .entity ("Car" );
100+
101+ Entity car = world .obtainEntity (carId );
102+ car .isA (vehicleId );
103+
104+ Entity myCar = world .obtainEntity (world .entity ("MyCar" ));
105+ myCar .isA (carId );
106+
107+ long isA = world .lookup ("IsA" );
108+ System .out .println ("MyCar isA Car: " + myCar .hasRelation (isA , carId ));
109+ System .out .println ("Car isA Vehicle: " + car .hasRelation (isA , vehicleId ));
110+ }
111+ }
0 commit comments