1 package eu.simuline.relana.model;
2
3 import eu.simuline.relana.expressions.Type;
4 import eu.simuline.relana.expressions.Formula;
5
6 import java.util.Set;
7
8 /**
9 * Represents the instance of an {@link SClass}.
10 *
11 *
12 * Created: Thu Apr 14 22:51:12 2005
13 *
14 * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
15 * @version 1.0
16 */
17 public final class SInstance {
18
19 /* -------------------------------------------------------------------- *
20 * attributes. *
21 * -------------------------------------------------------------------- */
22
23 /**
24 * The <code>SClass</code> of this instance.
25 */
26 private final Type type;
27
28 private final ProbDistr distr;
29
30 private Formula form;
31
32 private final String name; // for debugging
33
34 String getName() {
35 return this.name;
36 }
37
38 /* -------------------------------------------------------------------- *
39 * constructors. *
40 * -------------------------------------------------------------------- */
41
42
43 public SInstance(Type type, ProbDistr distr, String name) {
44 this.type = type;
45 this.distr = distr;
46 this.name = name;
47 assert !type.equals(Type.getEmpty());
48
49 }
50
51 public void setFormula(Formula form) {
52 this.form = form;
53 }
54
55 public SInstance(SInstance other) {
56 this.type = other.type;
57 this.distr = other.distr;
58 this.form = other.form;
59 this.name = other.name;
60 } // SInstance constructor
61
62 /* -------------------------------------------------------------------- *
63 * methods. *
64 * -------------------------------------------------------------------- */
65
66 public Type getType() {
67 return this.type;
68 }
69
70 public ProbDistr getDistr() {
71 return this.distr;
72 }
73
74 public Formula getFormula() {
75 return this.form;
76 }
77
78 public Set<Deficiency> getMin() {
79 return getType().getMin();
80 }
81
82 /**
83 * Returns the <code>SInstance</code> arising from this one
84 * by removing <code>def</code>
85 * in a way that means that <code>def</code> does not occur.
86 * This means that all deficiencies above <code>def</code>
87 * are removed as well.
88 * CAUTION: This may return <code>null</code>.
89 *
90 * @param def
91 * a <code>Deficiency</code>
92 * minimal within the type of this effect.
93 * **** is this unique or not? ****
94 * @return
95 * the <code>SInstance</code> arising from this one
96 * by assuming that <code>def</code> does not occur for this effect.
97 * Note that if <code>def</code> is minimal
98 * within the type of this effect,
99 * the resulting type is empty.
100 * Since this is not allowed for <code>SInstance</code>s,
101 * this method must return <code>null</code>.
102 */
103 SInstance remove(Deficiency def) {
104 if ((this.distr == null) && (this.form != null)) {
105 throw new IllegalArgumentException("****");
106 }
107 Type newType = getType().removeAndAbove(def);
108 if (newType.equals(Type.getEmpty())) {
109 return null;
110 }
111
112 return new SInstance(newType,
113 this.distr.add(def),
114 this.name);
115 }
116
117 /**
118 * Returns the <code>SInstance</code> arising from this one
119 * by removing <code>def</code>
120 * in a way that means that <code>def</code> occurs.
121 *
122 * @param def
123 * a <code>Deficiency</code>
124 * minimal within the type of this effect.
125 * **** is this unique or not? ****
126 * @return
127 * the <code>SInstance</code> arising from this one
128 * by assuming that <code>def</code> does occurs for this effect.
129 */
130 SInstance add(Deficiency def) {
131 if ((this.distr == null) && (this.form != null)) {
132 throw new IllegalArgumentException("****");
133 }
134 return new SInstance(getType() .remove(def),
135 this.distr == null ? null : this.distr.remove(def),
136 this.name);
137
138 }
139
140 SInstance substitute(SInstance serv, Formula form) {
141 SInstance res = new SInstance(this);
142 if (this.form != null) {
143 Formula newForm = res.form.substitute(serv, form);
144 res.setFormula(newForm);
145 }
146 return res;
147 }
148
149 // prevent overwriting: enable for (Weak)HashSet/Map
150 public boolean equals(Object obj) { //final
151 return super.equals(obj);
152 }
153
154 // enable for (Weak)HashSet/Map
155 public int hashCode() {
156 return super.hashCode();
157 }
158
159 public String toString() {
160 StringBuffer res = new StringBuffer();
161 res.append("\n<SInstance name=\"");
162 res.append(this.name);
163 res.append("\">\nformula: ");
164 res.append(this.form == null ? "null" : this.form);
165 res.append("\n</SInstance>");
166
167 return res.toString();
168 }
169
170 } // SInstance