00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package org.gecode;
00025
00026 import java.util.ArrayList;
00027 import java.lang.reflect.Constructor;
00028 import java.lang.reflect.InvocationTargetException;
00029
00054 public class ViewArray<View extends GecodeView> extends ArrayList<View> {
00057 public ViewArray() {
00058 super();
00059 }
00060
00063 public ViewArray(int size) {
00064 super(size);
00065 }
00066
00069 @SuppressWarnings("unchecked")
00070 public <C extends Iterable<View>> ViewArray(JavaSpace home,
00071 boolean share, C va) {
00072 super();
00073 for (View v: va) {
00074 add((View)v.copy(home, share));
00075 }
00076 }
00077
00080 public <C extends Iterable<? extends View>> ViewArray(C va) {
00081 super();
00082 for (View v: va) add(v);
00083 }
00084
00087 public <V2 extends View> ViewArray(V2... args) {
00088 super(args.length);
00089 for (V2 v: args) add(v);
00090 }
00091
00095 public ViewArray(JavaSpace home, int size, Class<? extends View> type) {
00096 super(size);
00097 try {
00098 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class);
00099
00100 for (int i = 0; i < size; ++i) {
00101 add(ctor.newInstance(home));
00102 }
00103 } catch(NoSuchMethodException ex) {
00104 throw new FatalException(ex);
00105 } catch(InstantiationException ex) {
00106 throw new FatalException(ex);
00107 } catch(IllegalAccessException ex) {
00108 throw new FatalException(ex);
00109 } catch(InvocationTargetException ex) {
00110 throw new FatalException(ex);
00111 }
00112 }
00113
00117 public ViewArray(JavaSpace home, int size, Class<? extends View> type, Object arg) {
00118 super(size);
00119 try {
00120 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class, arg.getClass());
00121
00122 for (int i = 0; i < size; ++i) {
00123 add(ctor.newInstance(home, arg));
00124 }
00125 } catch(NoSuchMethodException ex) {
00126 throw new FatalException(ex);
00127 } catch(InstantiationException ex) {
00128 throw new FatalException(ex);
00129 } catch(IllegalAccessException ex) {
00130 throw new FatalException(ex);
00131 } catch(InvocationTargetException ex) {
00132 throw new FatalException(ex);
00133 }
00134 }
00135
00139 public ViewArray(JavaSpace home, int size, Class<? extends View> type,
00140 Object arg1, Object arg2) {
00141 super(size);
00142 try {
00143 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class,
00144 arg1.getClass(),
00145 arg2.getClass());
00146
00147 for (int i = 0; i < size; ++i) {
00148 add(ctor.newInstance(home, arg1, arg2));
00149 }
00150 } catch(NoSuchMethodException ex) {
00151 throw new FatalException(ex);
00152 } catch(InstantiationException ex) {
00153 throw new FatalException(ex);
00154 } catch(IllegalAccessException ex) {
00155 throw new FatalException(ex);
00156 } catch(InvocationTargetException ex) {
00157 throw new FatalException(ex);
00158 }
00159 }
00160
00164 public ViewArray(JavaSpace home, int size, Class<? extends View> type,
00165 Object arg1, Object arg2, Object arg3) {
00166 super(size);
00167 try {
00168 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class,
00169 arg1.getClass(),
00170 arg2.getClass(),
00171 arg3.getClass());
00172
00173 for (int i = 0; i < size; ++i) {
00174 add(ctor.newInstance(home, arg1, arg2, arg3));
00175 }
00176 } catch(NoSuchMethodException ex) {
00177 throw new FatalException(ex);
00178 } catch(InstantiationException ex) {
00179 throw new FatalException(ex);
00180 } catch(IllegalAccessException ex) {
00181 throw new FatalException(ex);
00182 } catch(InvocationTargetException ex) {
00183 throw new FatalException(ex);
00184 }
00185 }
00186
00190 public <Var extends GecodeVar> ViewArray(JavaSpace home, Class<? extends View> type,
00191 VarArray<Var> va) {
00192 super(va.size());
00193 if (va.size() == 0) return;
00194 try {
00195 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class,
00196 va.get(0).getClass());
00197 for (Var v : va) {
00198 add(ctor.newInstance(home, v));
00199 }
00200 } catch(NoSuchMethodException ex) {
00201 throw new FatalException(ex);
00202 } catch(InstantiationException ex) {
00203 throw new FatalException(ex);
00204 } catch(IllegalAccessException ex) {
00205 throw new FatalException(ex);
00206 } catch(InvocationTargetException ex) {
00207 throw new FatalException(ex);
00208 }
00209 }
00210
00214 public <A> ViewArray(JavaSpace home, Class<? extends View> type, A[] as) {
00215 super(as.length);
00216 if (as.length == 0) return;
00217 try {
00218 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class, as[0].getClass());
00219 for (A a: as) {
00220 add(ctor.newInstance(home, a));
00221 }
00222 } catch(NoSuchMethodException ex) {
00223 throw new FatalException(ex);
00224 } catch(InstantiationException ex) {
00225 throw new FatalException(ex);
00226 } catch(IllegalAccessException ex) {
00227 throw new FatalException(ex);
00228 } catch(InvocationTargetException ex) {
00229 throw new FatalException(ex);
00230 }
00231 }
00232
00236 public <A,B> ViewArray(JavaSpace home, Class<? extends View> type, A[] as, B[] bs) {
00237 super(as.length);
00238 if (as.length != bs.length)
00239 throw new IllegalArgumentException("Arraylengths do not match: " +
00240 as.length + " != "+ bs.length);
00241 if (as.length == 0) return;
00242 try {
00243 Constructor<? extends View> ctor = type.getConstructor(JavaSpace.class,
00244 as[0].getClass(),
00245 bs[0].getClass());
00246 for (int i = 0; i < as.length; ++i) {
00247 add(ctor.newInstance(home, as[i], bs[i]));
00248 }
00249 } catch(NoSuchMethodException ex) {
00250 throw new FatalException(ex);
00251 } catch(InstantiationException ex) {
00252 throw new FatalException(ex);
00253 } catch(IllegalAccessException ex) {
00254 throw new FatalException(ex);
00255 } catch(InvocationTargetException ex) {
00256 throw new FatalException(ex);
00257 }
00258 }
00259
00263 public void cancel(Space home, JavaPropagator p, PropCond pc) {
00264 for (View v : this)
00265 v.cancel(home, p, pc);
00266 }
00267
00271 public void subscribe(JavaSpace home, JavaPropagator p, PropCond pc) {
00272 for (View v : this)
00273 v.subscribe(home, p, pc);
00274 }
00275
00278 public boolean assigned() {
00279 for (View v : this)
00280 if (!v.assigned())
00281 return false;
00282 return true;
00283 }
00284
00287 public void swap(int a, int b) {
00288 View tmp = get(a);
00289 set(a, get(b));
00290 set(b, tmp);
00291 }
00292 }