1 | package desmoj.core.simulator; |
2 | |
3 | import desmoj.core.report.ErrorMessage; |
4 | |
5 | /** |
6 | * Derive from this class to create a conditional which an entity (including |
7 | * processes as special entities) may fulfill or not, e.g. for the purpose |
8 | * of queue filtering. Override method check(E e) such that it returns |
9 | * <code>true</code> whenever an entity complies to the condition. |
10 | * |
11 | * For conditions that do not apply to a specific Entity, but to the |
12 | * model as whole (e.g. when to stop an experiment), use |
13 | * <code>ModelCondition</code>. |
14 | * |
15 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
16 | * @author Tim Lechler |
17 | * |
18 | * Licensed under the Apache License, Version 2.0 (the "License"); |
19 | * you may not use this file except in compliance with the License. You |
20 | * may obtain a copy of the License at |
21 | * http://www.apache.org/licenses/LICENSE-2.0 |
22 | * |
23 | * Unless required by applicable law or agreed to in writing, software |
24 | * distributed under the License is distributed on an "AS IS" |
25 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
26 | * or implied. See the License for the specific language governing |
27 | * permissions and limitations under the License. |
28 | * |
29 | */ |
30 | public abstract class Condition<E extends Entity> extends ModelComponent { |
31 | |
32 | /** |
33 | * Stores the arguments of a Constructor-call to make a recreation |
34 | * of the Condition possible. |
35 | */ |
36 | private Object[] _arguments = null; |
37 | |
38 | /** |
39 | * Stores if all arguments of a Constructor-call are of primitive |
40 | * types or Strings. |
41 | */ |
42 | private boolean _argumentsPrimitive; |
43 | |
44 | /** |
45 | * Constructs a condition with the given name and parameters for trace |
46 | * files. |
47 | * |
48 | * @param owner |
49 | * Model : The main model this condition is associated to |
50 | * @param name |
51 | * java.lang.String : The name of this condition |
52 | * @param showInTrace |
53 | * boolean : Flag for showing this condition in trace-files. Set |
54 | * it to <code>true</code> if model should show up in trace, |
55 | * <code>false</code> if model should not be shown in trace. |
56 | * @param args |
57 | * Object... : Arguments to pass to the condition (can be omitted) |
58 | * |
59 | */ |
60 | public Condition(Model owner, String name, boolean showInTrace, Object... args) { |
61 | |
62 | super(owner, name, showInTrace); // create a ModelComponent |
63 | |
64 | Class<?>[] types = new Class[3 + args.length]; |
65 | types[0] = Model.class; |
66 | types[1] = String.class; |
67 | types[2] = Boolean.TYPE; |
68 | |
69 | _arguments = new Object[3 + args.length]; |
70 | _arguments[0] = owner; |
71 | _arguments[1] = name; |
72 | _arguments[2] = showInTrace; |
73 | |
74 | _argumentsPrimitive = true; |
75 | |
76 | for (int i = 0; i < args.length; i++) { |
77 | |
78 | Class<?> type = getType(args[i]); |
79 | _argumentsPrimitive &= (type != null); |
80 | types[i + 3] = type; |
81 | _arguments[i + 3] = args[i]; |
82 | } |
83 | |
84 | } |
85 | |
86 | //TODO Javadoc |
87 | public boolean hasPrimitiveArguments() |
88 | { |
89 | return _argumentsPrimitive; |
90 | } |
91 | |
92 | //TODO Javadoc |
93 | public Object[] getConstructArguments() |
94 | { |
95 | return _arguments; |
96 | } |
97 | |
98 | /** |
99 | * Returns a boolean showing whether the given Entity complies to the |
100 | * condition tested by this method or not. Inherit from this class and |
101 | * implement this abstract method to return true if the passed Entity |
102 | * conforms to your special condition. |
103 | * |
104 | * @return boolean : Is <code>true</code>, if the entity given conforms |
105 | * to the condition, <code>false</code> otherwise. |
106 | * @param e |
107 | * E : The Entity to test the condition on |
108 | */ |
109 | public abstract boolean check(E e); // changed to generic parameter |
110 | |
111 | /** |
112 | * @deprecated Replaced by <code>ModelCondition.check() |
113 | * |
114 | * Returns a boolean showing whether the model complies to the |
115 | * condition tested by this method or not. Inherit from this class and |
116 | * override this method to return true if the model |
117 | * conforms to your special condition; the entity type this condition |
118 | * was parameterised with is ignored. |
119 | * |
120 | * @return boolean : Is <code>true</code>, if the model conforms |
121 | * to the condition, <code>false</code> otherwise. |
122 | */ |
123 | @Deprecated |
124 | public boolean check() { |
125 | throw (new desmoj.core.exception.DESMOJException( |
126 | new ErrorMessage( |
127 | this.getModel(), |
128 | "Call to deprecated Condition.check() without appropriately overriding this method", |
129 | this.getName() + ":check()", |
130 | "Subclass did not provide a routine to check", |
131 | "Override Condition.check(); preferredly, use ModelCondition for conditions applying to the model as whole", |
132 | this.presentTime()))); |
133 | } |
134 | |
135 | private Class<?> getType(Object obj) |
136 | { |
137 | Class<?> cls = obj.getClass(); |
138 | |
139 | return cls.equals(Boolean.class) ? Boolean.TYPE : |
140 | cls.equals(Integer.class) ? Integer.TYPE : |
141 | cls.equals(Character.class) ? Character.TYPE : |
142 | cls.equals(Byte.class) ? Byte.TYPE : |
143 | cls.equals(Short.class) ? Short.TYPE : |
144 | cls.equals(Double.class) ? Double.TYPE : |
145 | cls.equals(Long.class) ? Long.TYPE : |
146 | cls.equals(Float.class) ? Float.TYPE : |
147 | cls.equals(String.class) ? String.class : |
148 | null; |
149 | } |
150 | } |