Updated error messages when an error occurs within an executable array
[DTRules.git] / DTRules / src / main / java / com / dtrules / interpreter / RArray.java
blobd169252d60025aedbfd82209e47d94637e4d1b89
1 /*
2 * $Id$
3 *
4 * Copyright 2004-2007 MTBJ, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
19 package com.dtrules.interpreter;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
25 import com.dtrules.infrastructure.RulesException;
26 import com.dtrules.session.DTState;
27 import com.dtrules.session.IRSession;
28 /**
29 * Immplements Arrays for Decision Tables. Because we can't tag references,
30 * we do the same trick here that we do for RNames, i.e. we create an executable
31 * and a non-executable version of each array. However, we only create one
32 * ArrayList.
33 * <br><br>
34 * If dups is true, we allow duplicate references in the array. Sometimes it
35 * is pleasent to have an array whose values are all unique. In that case, create
36 * an array with dups set to false. <br>
37 * <br>
38 * @author ps24876
41 @SuppressWarnings("unchecked")
42 public class RArray extends ARObject implements Collection<IRObject> {
43 final ArrayList<IRObject> array;
44 final RArray pair;
45 final boolean executable;
46 final boolean dups;
47 final int id;
53 /* (non-Javadoc)
54 * @see java.util.Collection#addAll(java.util.Collection)
56 public boolean addAll(Collection<? extends IRObject> arg0) {
57 return array.addAll(arg0);
59 /* (non-Javadoc)
60 * @see java.util.Collection#clear()
62 public void clear() {
63 array.clear();
66 /* (non-Javadoc)
67 * @see java.util.Collection#contains(java.lang.Object)
69 public boolean contains(Object arg0) {
70 if(arg0 instanceof IRObject) {
71 for(IRObject o: array){
72 try{ // Really this isn't possible.
73 if (o.equals((IRObject)arg0))return true;
74 }catch(RulesException e){}
77 return false;
79 /* (non-Javadoc)
80 * @see java.util.Collection#containsAll(java.util.Collection)
82 public boolean containsAll(Collection<?> arg0) {
83 return array.containsAll(arg0);
85 /* (non-Javadoc)
86 * @see java.util.Collection#isEmpty()
88 public boolean isEmpty() {
89 return array.isEmpty();
91 /* (non-Javadoc)
92 * @see java.util.Collection#iterator()
94 public Iterator<IRObject> iterator() {
95 return array.iterator();
97 /* (non-Javadoc)
98 * @see java.util.Collection#remove(java.lang.Object)
100 public boolean remove(Object arg0) {
101 return array.remove(arg0);
103 /* (non-Javadoc)
104 * @see java.util.Collection#removeAll(java.util.Collection)
106 public boolean removeAll(Collection<?> arg0) {
107 return array.removeAll(arg0);
109 /* (non-Javadoc)
110 * @see java.util.Collection#retainAll(java.util.Collection)
112 public boolean retainAll(Collection<?> arg0) {
113 return retainAll(arg0);
115 /* (non-Javadoc)
116 * @see java.util.Collection#toArray()
118 public Object[] toArray() {
119 return array.toArray();
121 /* (non-Javadoc)
122 * @see java.util.Collection#toArray(T[])
124 public <T> T[] toArray(T[] arg0) {
125 return array.toArray( arg0);
128 * Returns the id of this array. Used by debuggers to analyze trace files
129 * @return ID of the array
131 public int getID(){ return id; }
133 * Constructor to create the core structure for an RArray.
134 * @param bogus
135 * @param exectuable
137 protected RArray(int id, boolean duplicates, ArrayList thearray, RArray otherpair, boolean executable){
138 this.id = id;
139 this.array = thearray;
140 this.executable = executable;
141 this.pair = otherpair;
142 this.dups = duplicates;
145 public RArray(int id, boolean duplicates, boolean executable){
146 this.id = id;
147 array = new ArrayList();
148 this.executable = executable;
149 dups = duplicates;
150 pair = new RArray(id,dups, array, this, !executable);
153 public RArray(int id, boolean duplicates, ArrayList thearray, boolean executable){
154 this.id = id;
155 this.array = thearray;
156 this.executable = executable;
157 dups = duplicates;
158 pair = new RArray(id,dups,thearray,this,!executable);
161 public Iterator getIterator(){ return array.iterator(); }
163 public int type() {
164 return iArray;
166 public boolean add(IRObject v){
167 if(!dups && array.contains(v))return false;
168 return array.add(v);
170 public void add(int index,IRObject v){
171 array.add(index,v);
173 public void delete(int index){
174 array.remove(index);
176 public void remove(IRObject v){
177 array.remove(v);
179 public IRObject get(int index) throws RulesException{
180 if(index<0 || index>= array.size()){
181 throw new RulesException("Undefined","RArray","Index out of bounds");
183 return (IRObject) array.get(index);
185 public ArrayList<IRObject> arrayValue() throws RulesException {
186 return array;
189 public boolean equals(IRObject o) throws RulesException {
190 if(o.type() != iArray) return false;
191 return ((RArray)o).array == array;
194 public void execute(DTState state) throws RulesException {
195 int cnt = 0; // A debugging aid.
196 for(IRObject obj : this){
197 if(obj.type()==iArray || !obj.isExecutable()){
198 state.datapush(obj);
199 }else{
200 try{
201 obj.execute(state);
202 }catch(RulesException e){
203 String ps = "";
204 for(int i=0;i<array.size();i++){
205 if (i==cnt) ps += " ERROR==> ";
206 ps += array.get(i).postFix()+" ";
207 if (i==cnt) ps += " <== ";
209 e.setPostfix(ps);
210 throw e;
213 cnt++;
218 public IRObject getExecutable() {
219 if(executable)return this;
220 return pair;
223 public IRObject getNonExecutable() {
224 if(!executable)return this;
225 return pair;
228 public boolean isExecutable() {
229 return executable;
232 public String postFix() {
233 StringBuffer result = new StringBuffer();
234 result.append(executable?"{":"[");
235 for (IRObject obj : array){
236 result.append(obj.postFix());
237 result.append(" ");
239 result.append(executable?"}":"]");
240 return result.toString();
243 public String stringValue() {
244 StringBuffer result = new StringBuffer();
245 result.append(isExecutable()?"{ ":"[ ");
246 for(IRObject obj : array){
247 result.append(obj.stringValue());
248 result.append(" ");
250 result.append(isExecutable()?"}":"]");
251 return result.toString();
253 public String toString() {
254 return stringValue();
258 * returns the clone of this object
260 public IRObject clone(IRSession session) {
261 ArrayList<IRObject> newArray = new ArrayList<IRObject>();
262 newArray.addAll(array);
263 return new RArray(session.getUniqueID(), dups, newArray, executable);
266 public RArray rArrayValue() throws RulesException {
267 return this;
271 * Returns the size of the array.
273 public int size()
275 return this.array.size();