4 * Copyright 2004-2007 MTBJ, Inc.
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
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.
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
;
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
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>
41 @SuppressWarnings("unchecked")
42 public class RArray
extends ARObject
implements Collection
<IRObject
> {
43 final ArrayList
<IRObject
> array
;
45 final boolean executable
;
54 * @see java.util.Collection#addAll(java.util.Collection)
56 public boolean addAll(Collection
<?
extends IRObject
> arg0
) {
57 return array
.addAll(arg0
);
60 * @see java.util.Collection#clear()
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
){}
80 * @see java.util.Collection#containsAll(java.util.Collection)
82 public boolean containsAll(Collection
<?
> arg0
) {
83 return array
.containsAll(arg0
);
86 * @see java.util.Collection#isEmpty()
88 public boolean isEmpty() {
89 return array
.isEmpty();
92 * @see java.util.Collection#iterator()
94 public Iterator
<IRObject
> iterator() {
95 return array
.iterator();
98 * @see java.util.Collection#remove(java.lang.Object)
100 public boolean remove(Object arg0
) {
101 return array
.remove(arg0
);
104 * @see java.util.Collection#removeAll(java.util.Collection)
106 public boolean removeAll(Collection
<?
> arg0
) {
107 return array
.removeAll(arg0
);
110 * @see java.util.Collection#retainAll(java.util.Collection)
112 public boolean retainAll(Collection
<?
> arg0
) {
113 return retainAll(arg0
);
116 * @see java.util.Collection#toArray()
118 public Object
[] toArray() {
119 return array
.toArray();
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.
137 protected RArray(int id
, boolean duplicates
, ArrayList thearray
, RArray otherpair
, boolean executable
){
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
){
147 array
= new ArrayList();
148 this.executable
= executable
;
150 pair
= new RArray(id
,dups
, array
, this, !executable
);
153 public RArray(int id
, boolean duplicates
, ArrayList thearray
, boolean executable
){
155 this.array
= thearray
;
156 this.executable
= executable
;
158 pair
= new RArray(id
,dups
,thearray
,this,!executable
);
161 public Iterator
getIterator(){ return array
.iterator(); }
166 public boolean add(IRObject v
){
167 if(!dups
&& array
.contains(v
))return false;
170 public void add(int index
,IRObject v
){
173 public void delete(int index
){
176 public void remove(IRObject 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
{
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()){
202 }catch(RulesException e
){
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
+= " <== ";
218 public IRObject
getExecutable() {
219 if(executable
)return this;
223 public IRObject
getNonExecutable() {
224 if(!executable
)return this;
228 public boolean isExecutable() {
232 public String
postFix() {
233 StringBuffer result
= new StringBuffer();
234 result
.append(executable?
"{":"[");
235 for (IRObject obj
: array
){
236 result
.append(obj
.postFix());
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());
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
{
271 * Returns the size of the array.
275 return this.array
.size();