Author: luc
Date: Fri Apr 25 00:25:58 2008
New Revision: 651514

URL: http://svn.apache.org/viewvc?rev=651514&view=rev
Log:
added a way to handle errors in user-defined switching functions
previously, only the function evaluation could trigger errors,
not the other functions of the interface

Added:
    
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java
   (with props)
Modified:
    
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchState.java
    
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunction.java
    
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunctionsHandler.java
    commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
    
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/ode/HighamHall54IntegratorTest.java

Added: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java?rev=651514&view=auto
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java
 (added)
+++ 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java
 Fri Apr 25 00:25:58 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.ode;
+
+import org.apache.commons.math.MathException;
+
+/**
+ * This exception is made available to users to report
+ * the error conditions that are triggered by [EMAIL PROTECTED] 
SwitchingFunction}
+ * @version $Revision: 620312 $ $Date: 2008-02-10 20:28:59 +0100 (dim., 10 
févr. 2008) $
+ * @since 2.0
+ */
+public class SwitchException extends MathException {
+
+    /** Serialization UID. */
+    private static final long serialVersionUID = -3662133702316614545L;
+
+    /** Simple constructor.
+     * Build an exception by translating and formating a message
+     * @param specifier format specifier (to be translated)
+     * @param parts to insert in the format (no translation)
+     */
+    public SwitchException(String specifier, Object[] parts) {
+        super(specifier, parts);
+    }
+
+    /**
+     * Create an exception with a given root cause.
+     * @param cause  the exception or error that caused this exception to be 
thrown
+     */
+    public SwitchException(Throwable cause) {
+        super(cause);
+    }
+
+}

Propchange: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchState.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchState.java?rev=651514&r1=651513&r2=651514&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchState.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchState.java
 Fri Apr 25 00:25:58 2008
@@ -42,7 +42,7 @@
 class SwitchState implements Serializable {
 
   /** Serializable version identifier. */
-  private static final long serialVersionUID = -7307007422156119622L;
+    private static final long serialVersionUID = -7307007422156119622L;
 
   /** Switching function. */
   private SwitchingFunction function;
@@ -50,10 +50,10 @@
   /** Maximal time interval between switching function checks. */
   private double maxCheckInterval;
 
-  /** Convergence threshold for event localisation. */
+  /** Convergence threshold for event localization. */
   private double convergence;
 
-  /** Upper limit in the iteration count for event localisation. */
+  /** Upper limit in the iteration count for event localization. */
   private int maxIterationCount;
 
   /** Time at the beginning of the step. */
@@ -115,11 +115,11 @@
    * beginning of the step
    * @param y0 array containing the current value of the state vector
    * at the beginning of the step
-   * @exception FunctionEvaluationException if the switching function
+   * @exception SwitchException if the switching function
    * value cannot be evaluated at the beginning of the step
    */
   public void reinitializeBegin(double t0, double[] y0)
-    throws FunctionEvaluationException {
+    throws SwitchException {
     this.t0 = t0;
     g0 = function.g(t0, y0);
     g0Positive = (g0 >= 0);
@@ -132,12 +132,12 @@
    * rejected)
    * @exception DerivativeException if the interpolator fails to
    * compute the function somewhere within the step
-   * @exception FunctionEvaluationException if the switching function
+   * @exception SwitchException if the switching function
    * cannot be evaluated
    * @exception ConvergenceException if an event cannot be located
    */
   public boolean evaluateStep(final StepInterpolator interpolator)
-    throws DerivativeException, FunctionEvaluationException, 
ConvergenceException {
+    throws DerivativeException, SwitchException, ConvergenceException {
 
     try {
 
@@ -169,6 +169,8 @@
                       return function.g(t, 
interpolator.getInterpolatedState());
                   } catch (DerivativeException e) {
                       throw new FunctionEvaluationException(t, e);
+                  } catch (SwitchException e) {
+                      throw new FunctionEvaluationException(t, e);
                   }
               }
           });
@@ -205,10 +207,12 @@
 
     } catch (FunctionEvaluationException e) {
      Throwable cause = e.getCause();
-      if ((cause != null) && (cause instanceof DerivativeException)) {
-        throw (DerivativeException) cause;
-      }
-      throw e;
+     if ((cause != null) && (cause instanceof DerivativeException)) {
+         throw (DerivativeException) cause;
+     } else if ((cause != null) && (cause instanceof SwitchException)) {
+         throw (SwitchException) cause;
+     }
+     throw new SwitchException(e);
     }
 
   }
@@ -227,11 +231,10 @@
    * end of the step
    * @param y array containing the current value of the state vector
    * at the end of the step
-   * @exception FunctionEvaluationException if the value of the switching
+   * @exception SwitchException if the value of the switching
    * function cannot be evaluated
    */
-  public void stepAccepted(double t, double[] y)
-    throws FunctionEvaluationException {
+  public void stepAccepted(double t, double[] y) throws SwitchException {
 
     t0 = t;
     g0 = function.g(t, y);
@@ -261,8 +264,10 @@
    * @param y array were to put the desired state vector at the beginning
    * of the next step
    * @return true if the integrator should reset the derivatives too
+   * @exception SwitchException if the state cannot be reseted by the switching
+   * function
    */
-  public boolean reset(double t, double[] y) {
+  public boolean reset(double t, double[] y) throws SwitchException {
 
     if (! pendingEvent) {
       return false;

Modified: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunction.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunction.java?rev=651514&r1=651513&r2=651514&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunction.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunction.java
 Fri Apr 25 00:25:58 2008
@@ -19,8 +19,6 @@
 
 import java.io.Serializable;
 
-import org.apache.commons.math.FunctionEvaluationException;
-
 /** This interface represents a switching function.
  *
  * <p>A switching function allows to handle discrete events in
@@ -93,10 +91,9 @@
    * @param t current value of the independent <i>time</i> variable
    * @param y array containing the current value of the state vector
    * @return value of the g function
-   * @exception FunctionEvaluationException if the value of the function
-   * cannot be evaluated
+   * @exception SwitchException if the switching function cannot be evaluated
    */
-  public double g(double t, double[] y) throws FunctionEvaluationException;
+  public double g(double t, double[] y) throws SwitchException;
 
   /** Handle an event and choose what to do next.
 
@@ -131,8 +128,9 @@
    * @return indication of what the integrator should do next, this
    * value must be one of [EMAIL PROTECTED] #STOP}, [EMAIL PROTECTED] 
#RESET_STATE},
    * [EMAIL PROTECTED] #RESET_DERIVATIVES} or [EMAIL PROTECTED] #CONTINUE}
+   * @exception SwitchException if the event occurrence triggers an error
    */
-  public int eventOccurred(double t, double[] y);
+  public int eventOccurred(double t, double[] y) throws SwitchException;
   
   /** Reset the state prior to continue the integration.
 
@@ -148,7 +146,8 @@
    * @param t current value of the independent <i>time</i> variable
    * @param y array containing the current value of the state vector
    * the new state should be put in the same array
+   * @exception SwitchException if the state cannot be reseted
    */
-  public void resetState(double t, double[] y);
+  public void resetState(double t, double[] y) throws SwitchException;
 
 }

Modified: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunctionsHandler.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunctionsHandler.java?rev=651514&r1=651513&r2=651514&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunctionsHandler.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/ode/SwitchingFunctionsHandler.java
 Fri Apr 25 00:25:58 2008
@@ -17,15 +17,14 @@
 
 package org.apache.commons.math.ode;
 
-import org.apache.commons.math.ConvergenceException;
-import org.apache.commons.math.FunctionEvaluationException;
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.math.ConvergenceException;
+
 /** This class handles several [EMAIL PROTECTED] SwitchingFunction switching
  * functions} during integration.
  *
@@ -147,8 +146,8 @@
 
       return first != null;
 
-    } catch (FunctionEvaluationException fee) {
-      throw new IntegratorException(fee);
+    } catch (SwitchException se) {
+      throw new IntegratorException(se);
     } catch (ConvergenceException ce) {
       throw new IntegratorException(ce);
     }
@@ -180,8 +179,8 @@
       for (Iterator iter = functions.iterator(); iter.hasNext();) {
         ((SwitchState) iter.next()).stepAccepted(t, y);
       }
-    } catch (FunctionEvaluationException fee) {
-      throw new IntegratorException(fee);
+    } catch (SwitchException se) {
+      throw new IntegratorException(se);
     }
   }
 
@@ -204,15 +203,21 @@
    * @param y array were to put the desired state vector at the beginning
    * of the next step
    * @return true if the integrator should reset the derivatives too
+   * @exception IntegratorException if one of the switching functions
+   * that should reset the state fails to do it
    */
-  public boolean reset(double t, double[] y) {
-    boolean resetDerivatives = false;
-    for (Iterator iter = functions.iterator(); iter.hasNext();) {
-      if (((SwitchState) iter.next()).reset(t, y)) {
-        resetDerivatives = true;
+  public boolean reset(double t, double[] y) throws IntegratorException {
+      try {
+          boolean resetDerivatives = false;
+          for (Iterator iter = functions.iterator(); iter.hasNext();) {
+              if (((SwitchState) iter.next()).reset(t, y)) {
+                  resetDerivatives = true;
+              }
+          }
+          return resetDerivatives;
+      } catch (SwitchException se) {
+          throw new IntegratorException(se);
       }
-    }
-    return resetDerivatives;
   }
 
   /** Switching functions. */

Modified: commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml?rev=651514&r1=651513&r2=651514&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml Fri Apr 25 
00:25:58 2008
@@ -35,11 +35,16 @@
 
 <document>
   <properties>
-    <title>
-Commons Math Release Notes</title>
+    <title>Commons Math Release Notes</title>
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" >
+        Switching functions can now throw dedicated SwitchException from all 
their
+        method. At upper call level, the various ODE integrators handle these 
new
+        exceptions and wrap them into IntegratorException instances, hence the
+        integrators methods signature did not change.
+      </action>
       <action dev="luc" type="add" issue="MATH-202">
         Added the getSwitchingFunctions and clearSwitchingFunctions to the
         FirstOrderIntegrator interface and all its implementations

Modified: 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/ode/HighamHall54IntegratorTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/ode/HighamHall54IntegratorTest.java?rev=651514&r1=651513&r2=651514&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/ode/HighamHall54IntegratorTest.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/ode/HighamHall54IntegratorTest.java
 Fri Apr 25 00:25:58 2008
@@ -17,17 +17,11 @@
 
 package org.apache.commons.math.ode;
 
-import org.apache.commons.math.ConvergenceException;
-import org.apache.commons.math.FunctionEvaluationException;
-import org.apache.commons.math.ode.DerivativeException;
-import org.apache.commons.math.ode.FirstOrderIntegrator;
-import org.apache.commons.math.ode.HighamHall54Integrator;
-import org.apache.commons.math.ode.IntegratorException;
-import org.apache.commons.math.ode.StepHandler;
-import org.apache.commons.math.ode.StepInterpolator;
-import org.apache.commons.math.ode.SwitchingFunction;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
 
-import junit.framework.*;
+import org.apache.commons.math.ConvergenceException;
 
 public class HighamHall54IntegratorTest
   extends TestCase {
@@ -185,11 +179,12 @@
         public int eventOccurred(double t, double[] y) {
           return SwitchingFunction.CONTINUE;
         }
-        public double g(double t, double[] y) throws 
FunctionEvaluationException {
+        public double g(double t, double[] y) throws SwitchException {
           double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2;
           double offset = t - middle;
           if (offset > 0) {
-            throw new FunctionEvaluationException(t);
+            throw new SwitchException("Evaluation failed for argument = {0}",
+                                      new Object[] { new Double(t) });
           }
           return offset;
         }


Reply via email to