View Javadoc
1   package eu.simuline.relana.model;
2   
3   //import eu.simuline.relana.expressions.Formula;
4   //import eu.simuline.relana.expressions.Operation;
5   //import eu.simuline.relana.expressions.Operation;
6   //import eu.simuline.relana.expressions.Type;
7   
8   //import java.math.BigDecimal;
9   
10  import java.util.Map;
11  //import java.util.HashMap;
12  import java.util.TreeMap;
13  //import java.util.Iterator;
14  import java.util.List;
15  import java.util.ArrayList;
16  //import java.util.Comparator;
17  
18  
19  /**
20   * Instance of Component. 
21   *
22   *
23   * Created: Thu Apr 14 23:02:05 2005
24   *
25   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
26   * @version 1.0
27   */
28  public final class CInstance {
29  
30      /* -------------------------------------------------------------------- *
31       * attributes.                                                          *
32       * -------------------------------------------------------------------- */
33  
34      /**
35       * Maps the names of the effects to their instances. 
36       */
37      private final Map<String, SInstance> effects;
38  
39      /**
40       * Maps the names of the subcomponents to their instances. 
41       */
42      private final Map<String, CInstance> components;
43  
44  
45      /* -------------------------------------------------------------------- *
46       * constructors.                                                        *
47       * -------------------------------------------------------------------- */
48  
49      /**
50       * Creates a new <code>CInstance</code> instance 
51       * without effects and subcomponents 
52       * (only for class {@link CClass#COMPONENT}). 
53       */
54      public CInstance() {
55  	this.effects   = new TreeMap<String, SInstance>();
56  	this.components = new TreeMap<String, CInstance>();
57      } // CInstance constructor
58  
59      /* -------------------------------------------------------------------- *
60       * methods.                                                             *
61       * -------------------------------------------------------------------- */
62  
63      /**
64       * Adds the given effect under the given name. 
65       *
66       * @param name 
67       *    a <code>String</code> representing the name of <code>sInst</code>. 
68       * @param sInst 
69       *    an <code>SInstance</code> saved under the given name. 
70       */
71      void addEffect(String name, SInstance sInst) {
72  	this.effects.put(name, sInst);
73      }
74  
75      /**
76       * Adds the given component under the given name. 
77       *
78       * @param name 
79       *    a <code>String</code> representing the name of <code>cInst</code>. 
80       * @param cInst 
81       *    an <code>SInstance</code> saved under the given name. 
82       */
83      void addComponent(String name, CInstance cInst) {
84  	this.components.put(name, cInst);
85      }
86  
87      Map<String, SInstance> getEffects() {
88  	return this.effects;
89      }
90  
91      public SInstance getEffect(List<String> path) {
92  	CInstance comp = this;
93  	for (int i = 0; i < path.size() - 1; i++) {
94  	    comp = comp.components.get(path.get(i));
95  	}
96  	return comp.effects.get(path.get(path.size() - 1));
97      }
98  
99      public FlatCInstance flatten() {
100 	Map<List<String>, SInstance> longName2effect = 
101 	    new TreeMap<List<String>, SInstance>(FlatCInstance.PATH_CMP);
102 
103 	// flatten subcomponents 
104 	String prefix;
105 	List<String> longName;
106 	Map<List<String>, SInstance> effects;
107 	for (Map.Entry<String, CInstance> cEntry 
108 		 : this.components.entrySet()) {
109 	    effects = cEntry.getValue().flatten().getEffects();
110 	    prefix = cEntry.getKey();
111 	    for (Map.Entry<List<String>, SInstance> sEntry 
112 		     : effects.entrySet()) {
113 		    longName = new ArrayList<String>(sEntry.getKey());
114 		    longName.add(0, prefix);
115 		    longName2effect.put(longName, sEntry.getValue());
116 	    }
117 	}
118 
119 	// add top-level effects 
120 	for (Map.Entry<String, SInstance> sEntry : this.effects.entrySet()) {
121 	    longName = new ArrayList<String>();
122 	    longName.add(sEntry.getKey());
123 	    longName2effect.put(longName, sEntry.getValue());
124 	}
125 	
126 	return new FlatCInstance(longName2effect);
127     }
128 
129     public String toString() {
130 	StringBuffer res = new StringBuffer();
131 	res.append("\n<CInstance><Effects>");
132 	res.append(this.effects);
133 	res.append("</Effects>\n</CInstance>\n");
134 
135 	return res.toString();
136     }
137 
138 } // CInstance