Commit 4b5c7307 authored by Evan Ward's avatar Evan Ward

Call EventHandler.init()

Previously EventHandler.init() was not called. Now it is called by
AbstractDetector.init() and similarly for Fields. Added a test case.

Fixes #471
parent 1d611e0a
......@@ -144,7 +144,8 @@ public class AttitudesSequence implements AttitudeProvider {
/** {@inheritDoc} */
@Override
public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
public void init(final FieldSpacecraftState<T> s0,
final FieldAbsoluteDate<T> t) throws OrekitException {
sw.init(s0.toSpacecraftState(), t.toAbsoluteDate());
}
......@@ -400,7 +401,8 @@ public class AttitudesSequence implements AttitudeProvider {
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
// reset the transition parameters (this will be done once for each switch,
// despite doing it only once would have sufficient; its not really a problem)
......
......@@ -69,9 +69,17 @@ public abstract class AbstractDetector<T extends EventDetector> implements Event
this.forward = true;
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
/**
* {@inheritDoc}
*
* <p> This implementation sets the direction of propagation and initializes the event
* handler. If a subclass overrides this method it should call {@code
* super.init(s0, t)}.
*/
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
forward = t.durationFrom(s0.getDate()) >= 0.0;
getHandler().init(s0, t);
}
/** {@inheritDoc} */
......
......@@ -243,7 +243,8 @@ public class BooleanDetector extends AbstractDetector<BooleanDetector> {
}
@Override
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
for (final EventDetector detector : detectors) {
detector.init(s0, t);
......
......@@ -59,8 +59,10 @@ public interface EventDetector extends Serializable {
* </p>
* @param s0 initial state
* @param t target time for the integration
*
* @throws OrekitException if some specific error occurs
*/
default void init(SpacecraftState s0, AbsoluteDate t) {
default void init(SpacecraftState s0, AbsoluteDate t) throws OrekitException {
// nothing by default
}
......
......@@ -130,7 +130,9 @@ public class EventEnablingPredicateFilter<T extends EventDetector>
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
// delegate to raw detector
rawDetector.init(s0, t);
......
......@@ -130,7 +130,9 @@ public class EventShifter<T extends EventDetector> extends AbstractDetector<Even
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
detector.init(s0, t);
}
......
......@@ -129,7 +129,9 @@ public class EventSlopeFilter<T extends EventDetector> extends AbstractDetector<
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
// delegate to raw detector
rawDetector.init(s0, t);
......
......@@ -139,8 +139,11 @@ public class EventState<T extends EventDetector> implements Serializable {
* </p>
* @param s0 initial state
* @param t target time for the integration
*
* @throws OrekitException if some specific error occurs
*/
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
detector.init(s0, t);
lastT = AbsoluteDate.PAST_INFINITY;
lastG = Double.NaN;
......
......@@ -213,7 +213,9 @@ public class EventsLogger implements Serializable {
}
/** {@inheritDoc} */
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
detector.init(s0, t);
}
......
......@@ -69,8 +69,10 @@ public abstract class FieldAbstractDetector<D extends FieldEventDetector<T>,
}
/** {@inheritDoc} */
public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
public void init(final FieldSpacecraftState<T> s0,
final FieldAbsoluteDate<T> t) throws OrekitException {
forward = t.durationFrom(s0.getDate()).getReal() >= 0.0;
getHandler().init(s0, t);
}
/** {@inheritDoc} */
......
......@@ -58,8 +58,11 @@ public interface FieldEventDetector <T extends RealFieldElement<T>> {
* </p>
* @param s0 initial state
* @param t target time for the integration
*
* @throws OrekitException if some specific error occurs
*/
default void init(FieldSpacecraftState<T> s0, FieldAbsoluteDate<T> t) {
default void init(FieldSpacecraftState<T> s0,
FieldAbsoluteDate<T> t) throws OrekitException {
// nothing by default
}
......
......@@ -140,8 +140,11 @@ public class FieldEventState<D extends FieldEventDetector<T>, T extends RealFiel
* </p>
* @param s0 initial state
* @param t target time for the integration
*
* @throws OrekitException if some specific error occurs
*/
public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
public void init(final FieldSpacecraftState<T> s0,
final FieldAbsoluteDate<T> t) throws OrekitException {
detector.init(s0, t);
final Field<T> field = detector.getMaxCheckInterval().getField();
lastT = FieldAbsoluteDate.getPastInfinity(field);
......
......@@ -207,7 +207,9 @@ public class FieldEventsLogger<T extends RealFieldElement<T>> {
}
/** {@inheritDoc} */
public void init(final FieldSpacecraftState<T> s0, final FieldAbsoluteDate<T> t) {
public void init(final FieldSpacecraftState<T> s0,
final FieldAbsoluteDate<T> t) throws OrekitException {
super.init(s0, t);
detector.init(s0, t);
}
......
......@@ -73,7 +73,8 @@ public class NegateDetector extends AbstractDetector<NegateDetector> {
}
@Override
public void init(final SpacecraftState s0, final AbsoluteDate t) {
public void init(final SpacecraftState s0,
final AbsoluteDate t) throws OrekitException {
super.init(s0, t);
this.original.init(s0, t);
}
......
......@@ -85,8 +85,11 @@ public interface FieldEventHandler<KK extends FieldEventDetector<T>, T extends R
* </p>
* @param initialState initial state
* @param target target date for the propagation
*
* @throws OrekitException if some specific error occurs
*/
default void init(FieldSpacecraftState<T> initialState, FieldAbsoluteDate<T> target) {
default void init(FieldSpacecraftState<T> initialState,
FieldAbsoluteDate<T> target) throws OrekitException {
// nothing by default
}
......
......@@ -21,6 +21,10 @@
</properties>
<body>
<release version="9.2" date="TBD" description="TBD">
<action dev="evan" type="fix">
Fixed issue where EventHandler.init() was never called.
Fixes issue #471.
</action>
<action dev="luc" type="fix">
Fixed error in relative humidity units in Marini-Marray tropospheric model.
Fixes issue #352.
......
......@@ -9,6 +9,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.handlers.EventHandler;
import org.orekit.propagation.events.handlers.EventHandler.Action;
import org.orekit.time.AbsoluteDate;
......@@ -101,13 +102,19 @@ public class AndDetectorTest {
Assert.assertTrue("positive", and.g(s) > 0);
}
/** Check wrapped detectors are initialized. */
/**
* Check wrapped detectors are initialized.
*
* @throws OrekitException on error.
*/
@Test
public void testInit() {
public void testInit() throws OrekitException {
// setup
EventDetector a = Mockito.mock(EventDetector.class);
EventDetector b = Mockito.mock(EventDetector.class);
BooleanDetector and = BooleanDetector.andCombine(a, b);
@SuppressWarnings("unchecked")
EventHandler<EventDetector> c = Mockito.mock(EventHandler.class);
BooleanDetector and = BooleanDetector.andCombine(a, b).withHandler(c);
AbsoluteDate t = AbsoluteDate.CCSDS_EPOCH;
s = Mockito.mock(SpacecraftState.class);
Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
......@@ -118,6 +125,7 @@ public class AndDetectorTest {
// verify
Mockito.verify(a).init(s, t);
Mockito.verify(b).init(s, t);
Mockito.verify(c).init(s, t);
}
/** check when no operands are passed to the constructor. */
......
......@@ -52,6 +52,44 @@ public class EventDetectorTest {
private double mu;
@Test
public void testEventHandlerInit() throws OrekitException {
final TimeScale utc = TimeScalesFactory.getUTC();
final Vector3D position = new Vector3D(-6142438.668, 3492467.56, -25767.257);
final Vector3D velocity = new Vector3D(505.848, 942.781, 7435.922);
final AbsoluteDate date = new AbsoluteDate(2003, 9, 16, utc);
final Orbit orbit = new CircularOrbit(new PVCoordinates(position, velocity),
FramesFactory.getEME2000(), date, mu);
// mutable boolean
final boolean[] eventOccurred = new boolean[1];
EventHandler<DateDetector> handler = new EventHandler<DateDetector>() {
private boolean initCalled;
@Override
public Action eventOccurred(SpacecraftState s,
DateDetector detector,
boolean increasing) {
if (!initCalled) {
throw new RuntimeException("init() not called before eventOccurred()");
}
eventOccurred[0] = true;
return Action.STOP;
}
@Override
public void init(SpacecraftState initialState,
AbsoluteDate target) {
initCalled = true;
}
};
Propagator propagator = new KeplerianPropagator(orbit);
double stepSize = 60.0;
propagator.addEventDetector(new DateDetector(date.shiftedBy(5.25 * stepSize)).withHandler(handler));
propagator.propagate(date.shiftedBy(10 * stepSize));
Assert.assertTrue(eventOccurred[0]);
}
@Test
public void testBasicScheduling() throws OrekitException {
......
......@@ -59,6 +59,56 @@ public class FieldEventDetectorTest {
private double mu;
@Test
public void testEventHandlerInit() throws OrekitException {
doTestEventHandlerInit(Decimal64Field.getInstance());
}
private <T extends RealFieldElement<T>> void doTestEventHandlerInit(Field<T> field)
throws OrekitException {
final T zero = field.getZero();
final TimeScale utc = TimeScalesFactory.getUTC();
final FieldVector3D<T> position = new FieldVector3D<>(zero.add(-6142438.668),
zero.add(3492467.56),
zero.add(-25767.257));
final FieldVector3D<T> velocity = new FieldVector3D<>(zero.add(505.848),
zero.add(942.781),
zero.add(7435.922));
final FieldAbsoluteDate<T> date = new FieldAbsoluteDate<>(field, 2003, 9, 16, utc);
final FieldOrbit<T> orbit = new FieldCircularOrbit<>(new FieldPVCoordinates<>(position, velocity),
FramesFactory.getEME2000(), date, mu);
// mutable boolean
final boolean[] eventOccurred = new boolean[1];
FieldEventHandler<FieldDateDetector<T>, T> handler =
new FieldEventHandler<FieldDateDetector<T>, T>() {
private boolean initCalled;
@Override
public Action eventOccurred(FieldSpacecraftState<T> s,
FieldDateDetector<T> detector,
boolean increasing) {
if (!initCalled) {
throw new RuntimeException("init() not called before eventOccurred()");
}
eventOccurred[0] = true;
return Action.STOP;
}
@Override
public void init(FieldSpacecraftState<T> initialState,
FieldAbsoluteDate<T> target) {
initCalled = true;
}
};
FieldPropagator<T> propagator = new FieldKeplerianPropagator<>(orbit);
T stepSize = zero.add(60.0);
propagator.addEventDetector(new FieldDateDetector<>(date.shiftedBy(stepSize.multiply(5.25))).withHandler(handler));
propagator.propagate(date.shiftedBy(stepSize.multiply(10)));
Assert.assertTrue(eventOccurred[0]);
}
@Test
public void testBasicScheduling() throws OrekitException {
doTestBasicScheduling(Decimal64Field.getInstance());
......
......@@ -6,6 +6,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.handlers.EventHandler;
import org.orekit.time.AbsoluteDate;
/**
......@@ -15,12 +16,18 @@ import org.orekit.time.AbsoluteDate;
*/
public class NegateDetectorTest {
/** check {@link NegateDetector#init(SpacecraftState, AbsoluteDate)}. */
/**
* check {@link NegateDetector#init(SpacecraftState, AbsoluteDate)}.
*
* @throws OrekitException on error.
*/
@Test
public void testInit() {
public void testInit() throws OrekitException {
//setup
EventDetector a = Mockito.mock(EventDetector.class);
NegateDetector detector = new NegateDetector(a);
@SuppressWarnings("unchecked")
EventHandler<EventDetector> c = Mockito.mock(EventHandler.class);
NegateDetector detector = new NegateDetector(a).withHandler(c);
AbsoluteDate t = AbsoluteDate.GPS_EPOCH;
SpacecraftState s = Mockito.mock(SpacecraftState.class);
Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
......@@ -30,6 +37,7 @@ public class NegateDetectorTest {
//verify
Mockito.verify(a).init(s, t);
Mockito.verify(c).init(s, t);
}
/**
......
......@@ -9,6 +9,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.orekit.errors.OrekitException;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.handlers.EventHandler;
import org.orekit.propagation.events.handlers.EventHandler.Action;
import org.orekit.time.AbsoluteDate;
......@@ -105,13 +106,19 @@ public class OrDetectorTest {
Assert.assertTrue("positive", or.g(s) > 0);
}
/** Check wrapped detectors are initialized. */
/**
* Check wrapped detectors are initialized.
*
* @throws OrekitException on error.
*/
@Test
public void testInit() {
public void testInit() throws OrekitException {
// setup
EventDetector a = Mockito.mock(EventDetector.class);
EventDetector b = Mockito.mock(EventDetector.class);
BooleanDetector or = BooleanDetector.orCombine(a, b);
@SuppressWarnings("unchecked")
EventHandler<EventDetector> c = Mockito.mock(EventHandler.class);
BooleanDetector or = BooleanDetector.orCombine(a, b).withHandler(c);
AbsoluteDate t = AbsoluteDate.CCSDS_EPOCH;
s = Mockito.mock(SpacecraftState.class);
Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
......@@ -122,6 +129,7 @@ public class OrDetectorTest {
// verify
Mockito.verify(a).init(s, t);
Mockito.verify(b).init(s, t);
Mockito.verify(c).init(s, t);
}
/** check when no operands are passed to the constructor. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment