Space.java
Go to the documentation of this file.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.lang.reflect.*;
00027 import java.lang.reflect.Constructor;
00028 import java.lang.reflect.InvocationTargetException;
00029
00030
00037 public abstract class Space extends JavaSpace {
00040 public Space() {
00041 super();
00042 this.name = "";
00043 }
00046 public Space(String name) {
00047 super();
00048 this.name = name;
00049 }
00050 protected Space(long cPtr, boolean cMemoryOwn, boolean releaseOwnership) {
00051 super(cPtr, cMemoryOwn);
00052 if (releaseOwnership)
00053 swigReleaseOwnership();
00054 }
00055 protected Space(Boolean share, Space s) {
00056 super(share, s);
00057 this.name = s.name;
00058 swigReleaseOwnership();
00059 }
00060
00063 public Space copy(boolean share) {
00064 try {
00065 Constructor ctors[] = this.getClass().getDeclaredConstructors();
00066 if (this.getClass().getEnclosingClass() == null) {
00067 for (int i = 0; i < ctors.length; ++i) {
00068 Class<?> args[] = ctors[i].getParameterTypes();
00069 if (args.length != 2) continue;
00070 if (args[0] != Boolean.class) continue;
00071 if (args[1] != this.getClass()) continue;
00072 ctors[i].setAccessible(true);
00073 return (Space)ctors[i].newInstance(share, this);
00074 }
00075 } else {
00076 throw new FatalException("Nested classes must override " +
00077 "the copy(boolean share) method");
00078 }
00079 throw new NoSuchMethodException();
00080 } catch(NoSuchMethodException ex) {
00081 throw new FatalException("No suitable copy-constructor defined for "
00082 + this.getClass().getName(), ex);
00083 } catch(InstantiationException ex) {
00084 throw new FatalException("No suitable copy-constructor defined for "
00085 + this.getClass().getName(), ex);
00086 } catch(IllegalAccessException ex) {
00087 throw new FatalException("No suitable copy-constructor defined for "
00088 + this.getClass().getName(), ex);
00089 } catch(InvocationTargetException ex) {
00090 throw new FatalException("No suitable copy-constructor defined for "
00091 + this.getClass().getName(), ex);
00092 }
00093 }
00094 public final JavaSpace copy_internal(boolean share) {
00095 JavaSpace ret = null;
00096 try {
00097 ret = copy(share);
00098 } catch (Throwable e) {
00099 System.err.println("Exception while copying a space:");
00100 System.err.println(e.getMessage());
00101 e.printStackTrace();
00102 System.err.println("Exit.");
00103 System.exit(2);
00104 }
00105 return ret;
00106 }
00107
00110 public void constrain(Space s) {}
00111 public final void constrain_internal(JavaSpace s) {
00112 try {
00113 constrain((Space) s);
00114 } catch (Throwable e) {
00115 System.err.println("Exception while constraining a space:");
00116 System.err.println(e.getMessage());
00117 e.printStackTrace();
00118 System.err.println("Exit.");
00119 System.exit(2);
00120 }
00121 }
00122
00123 public Space cloneSpace() {
00124 Space ret = (Space)super.cloneSpace();
00125 ret.swigTakeOwnership();
00126 return ret;
00127 }
00128
00129
00130
00131
00132 private String name;
00133 public String getName() { return name; }
00134 public void setName(String name) { this.name = name; };
00135
00136
00137
00138
00139 private String varToString(Field field, boolean isArray,
00140 Class typeClass) {
00141 try {
00142 if (typeClass.equals(VarArray.class)) {
00143 return field.getName() + "="+
00144 ((VarArray) field.get(this)).toString()+"\n";
00145 } else if (typeClass.equals(IntVar.class) ||
00146 typeClass.equals(SetVar.class) ||
00147 typeClass.equals(BoolVar.class)) {
00148
00149 String s = "";
00150 if (isArray) {
00151 s += field.getName();
00152 Object a = field.get(this);
00153 int length = Array.getLength(a);
00154 s += "[]= {";
00155 for (int i=0; i<length; i++) {
00156 s += Array.get(a, i).toString();
00157 if (i!=length-1)
00158 s+=",";
00159 }
00160 s += "}\n";
00161 } else {
00162 GecodeVar var = (GecodeVar) field.get(this);
00163 s += var.getName();
00164 if (s.equals("")) s += field.getName();
00165 s += "="+field.get(this).toString()+"\n";
00166 }
00167 return s;
00168 } else {
00169 return "";
00170 }
00171 } catch (IllegalAccessException e) {
00172 return "<IllegalAccessException>";
00173 }
00174 }
00175
00183 public String toString() {
00184 String s = "";
00185 Class c = this.getClass();
00186
00187 if (name == "") {
00188 s = s + ""+c.getName()+":\n";
00189 } else {
00190 s = s + ""+name+":\n";
00191 }
00192
00193 Field[] publicFields = c.getFields();
00194 for (int i=0; i<publicFields.length; i++) {
00195 Class typeClass = publicFields[i].getType();
00196 if (typeClass.isArray()) {
00197 Class componentClass = typeClass.getComponentType();
00198 s += varToString(publicFields[i], true, componentClass);
00199 } else {
00200 s+= varToString(publicFields[i], false, typeClass);
00201 }
00202 }
00203 return s;
00204 }
00205
00206 }
00207