ContentsIndexHome
PreviousUpNext
Java Pitfalls example

The pitfall example in Java.

 1: package com.aimms.aimmssdk.examples.pitfalls;
 2: 
 3: import com.aimms.aimmssdk.ISession;
 4: import com.aimms.aimmssdk.ISetData;
 5: import com.aimms.aimmssdk.ISetIterator;
 6: 
 7: /**
 8:  * This example demonstrates some easily made mistakes using the SDK.
 9:  */
10: class Pitfalls {
11: 
12:     ISession m_Session;
13: 
14:     public Pitfalls(ISession session) {
15:         m_Session = session;
16:     }
17: 
18:     /**
19:      * Using set ordering. The SDK uses only the 'natural' Aimms ordering, which
20:      * is the creation order of the labels. See also the section on "Set element
21:      * ordering" in the Execution Efficiency Cookbook chapter in the Aimms
22:      * Language Reference.
23:      */
24:     public void useOrdinals() {
25:         ISetData setCustomers = m_Session.openSet("Customers");
26:         setCustomers.add("Oslo");
27:         setCustomers.add("Madrid");
28: 
29:         // If the root set was completely empty until now, the order will be as expected.
30:         ISetIterator itCustomers = setCustomers.createIterator();
31:         itCustomers.next();
32:         assert ("Oslo" == itCustomers.element().getLabel());
33:         itCustomers.next();
34:         assert ("Madrid" == itCustomers.element().getLabel());
35: 
36: 
37:         ISetData setDepots = m_Session.openSet("Depots");
38:         setDepots.add("London");
39:         setDepots.add("Oslo");
40: 
41:         // Since Depots and Customers share a rootset (Locations), 
42:         // "Oslo" was added to the rootset before "London".
43:         // It will be the first Depot!
44:         ISetIterator itDepots = setDepots.createIterator();
45:         itDepots.next();
46:         assert ("Oslo" == itDepots.element().getLabel());
47:         itDepots.next();
48:         assert ("London" == itDepots.element().getLabel());
49:     }
50: 
51:     /**
52:      * Writing sets. Set write actions are recursive: - adding a label to a subset
53:      * will also add it to the superset - deleting a label from a set will also
54:      * delete it from its subsets, - emptying a set will empty all its subsets.
55:      * - setLabels() has an implicit empty, therefore setLabels empties all the
56:      * subsets
57:      */
58:     public void initializeSets() {
59:         // the data:
60:         String[] locations = {"London", "Oslo", "Madrid"};
61:         String[] customers = {"Oslo", "Madrid"};
62:         String[] depots = {"London", "Oslo"};
63: 
64:         // adding the data incorrectly:
65:         {
66:             ISetData setCustomers = m_Session.openSet("Customers");
67:             setCustomers.setLabels(customers); // This will also add Oslo and Madrid to the superset Locations.
68:             ISetData setDepots = m_Session.openSet("Depots");
69:             setDepots.setLabels(depots); // This will also add London to the superset Locations, the others were already present.
70:             ISetData setLocations = m_Session.openSet("Locations");
71:             setLocations.setLabels(locations); // This will empty the set and its subsets, and then adds London and Oslo to only itself.
72: 
73:             assert (setDepots.getCardinality() == 0); // Probably not intended.
74:         }
75:     }
76: }