Generated on Fri Oct 6 16:26:44 2006 for Gecode/J by doxygen 1.4.7

Propagator.java

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *     Guido Tack, 2006
00009  *
00010  *  Last modified:
00011  *     $Date: 2006-09-26 16:51:04 +0200 (Tue, 26 Sep 2006) $ by $Author: zayenz $
00012  *     $Revision: 3706 $
00013  *
00014  *  This file is part of Gecode, the generic constraint
00015  *  development environment:
00016  *     http://www.gecode.org
00017  *
00018  *  See the file "LICENSE" for information on usage and
00019  *  redistribution of this file, and for a
00020  *     DISCLAIMER OF ALL WARRANTIES.
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 
00034 public abstract class Propagator 
00035   extends JavaPropagator 
00036   implements GecodeEnumConstants {
00037 
00038     public Propagator() {
00039         swigReleaseOwnership();
00040     }
00041 
00042     public void dispose(Space home) {}
00043 
00044     public final void dispose_internal(JavaSpace home) {
00045         dispose(new DummySpace(home.getCPtr(home),
00046                                home.swigCMemOwn));
00047     }
00048 
00051     public Propagator copy(Space home, boolean share) {
00052         try {
00053             Constructor ctors[] = this.getClass().getDeclaredConstructors();
00054             if (this.getClass().getEnclosingClass() == null) {
00055                 for (int i = 0; i < ctors.length; ++i) {
00056                     Class<?> args[] = ctors[i].getParameterTypes();
00057                     if (args.length != 3) continue;
00058                     if (args[0] != Space.class) continue;
00059                     if (args[1] != Boolean.class) continue;
00060                     if (args[2] != this.getClass()) continue;
00061                     ctors[i].setAccessible(true);
00062                     return (Propagator)ctors[i].newInstance(home, share, this);
00063                 }
00064             } else {
00065                 throw new FatalException("Nested classes must override " +
00066                                          "the copy(Space home, boolean share) method");
00067             }
00068             throw new NoSuchMethodException();
00069         } catch(NoSuchMethodException ex) {
00070             throw new FatalException("No suitable copy-constructor defined for " 
00071                                      + this.getClass().getName(), ex);
00072         } catch(InstantiationException ex) {
00073             throw new FatalException("No suitable copy-constructor defined for " 
00074                                      + this.getClass().getName(), ex);
00075         } catch(IllegalAccessException ex) {
00076             throw new FatalException("No suitable copy-constructor defined for " 
00077                                      + this.getClass().getName(), ex);
00078         } catch(InvocationTargetException ex) {
00079             throw new FatalException("No suitable copy-constructor defined for " 
00080                                      + this.getClass().getName(), ex);
00081         }
00082     }
00083     public final JavaPropagator copy_internal(JavaSpace home, boolean share) {
00084         JavaPropagator ret = null;
00085         try {
00086             ret = copy(new DummySpace(home.getCPtr(home),
00087                                       home.swigCMemOwn), share);
00088         } catch (Throwable e) {
00089             System.err.println("Exception while copying a propagator:");
00090             System.err.println(e.getMessage());
00091             e.printStackTrace();
00092             System.err.println("Exit.");
00093             System.exit(2);
00094         }
00095         return ret;
00096     }
00097     public abstract ExecStatus propagate(Space home);
00098     public final ExecStatus propagate_internal(JavaSpace home) {
00099         ExecStatus ret = null;
00100         try {
00101             ret = propagate(new DummySpace(home.getCPtr(home),
00102                                            home.swigCMemOwn));
00103         } catch (Throwable e) {
00104             System.err.println("Exception while propagating.");
00105             System.err.println(e.getMessage());
00106             e.printStackTrace();
00107             System.err.println("Exit.");
00108             System.exit(2);
00109         }
00110         return ret;
00111     }
00112     public abstract PropCost cost();
00113     public final PropCost cost_internal() {
00114         PropCost ret = null;
00115         try {
00116             ret = cost();
00117         } catch (Throwable e) {
00118             System.err.println("Exception in a propagator:");
00119             System.err.println(e.getMessage());
00120             e.printStackTrace();
00121             System.err.println("Exit.");
00122             System.exit(2);
00123         }
00124         return ret;
00125     }
00126     public abstract ExecStatus setup(Space home);
00127     public final ExecStatus setup_internal(JavaSpace home) {
00128         ExecStatus ret = null;
00129         try {
00130             ret = setup(new DummySpace(home.getCPtr(home),
00131                                        home.swigCMemOwn));
00132         } catch (Throwable e) {
00133             System.err.println("Exception while setting up a propagator:");
00134             System.err.println(e.getMessage());
00135             e.printStackTrace();
00136             System.err.println("Exit.");
00137             System.exit(2);
00138         }
00139         return ret;
00140     }
00141 
00144     protected ExecStatus t(ModEvent me, ExecStatus es) {
00145         return me.failed() ? ExecStatus.ES_FAILED : es;
00146     }
00147 
00152     final protected PropCost cost_lo(int n, PropCost pc) {
00153         if (n > 3) return pc;
00154         if (n < 2) return PropCost.PC_UNARY_LO;
00155         return (n > 2) ? PropCost.PC_TERNARY_LO : PropCost.PC_BINARY_LO;
00156     }
00157 
00162     final protected PropCost cost_hi(int n, PropCost pc) {
00163         if (n > 3) return pc;
00164         if (n < 2) return PropCost.PC_UNARY_HI;
00165         return (n > 2) ? PropCost.PC_TERNARY_HI : PropCost.PC_BINARY_HI;
00166     }
00167 }