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