EventsLogger.java

  1. /* Copyright 2002-2013 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.propagation.events;

  18. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.propagation.SpacecraftState;
  23. import org.orekit.propagation.events.handlers.EventHandler;
  24. import org.orekit.time.AbsoluteDate;

  25. /** This class logs events detectors events during propagation.
  26.  *
  27.  * <p>As {@link EventDetector events detectors} are triggered during
  28.  * orbit propagation, an event specific {@link
  29.  * EventDetector.Action#eventOccurred(SpacecraftState, boolean) eventOccurred}
  30.  * method is called. This class can be used to add a global logging
  31.  * feature registering all events with their corresponding states in
  32.  * a chronological sequence (or reverse-chronological if propagation
  33.  * occurs backward).<p>
  34.  * <p>This class works by wrapping user-provided {@link EventDetector
  35.  * events detectors} before they are registered to the propagator. The
  36.  * wrapper monitor the calls to {@link
  37.  * EventDetector.Action#eventOccurred(SpacecraftState, boolean) eventOccurred}
  38.  * and store the corresponding events as {@link LoggedEvent} instances.
  39.  * After propagation is complete, the user can retrieve all the events
  40.  * that have occurred at once by calling method {@link #getLoggedEvents()}.</p>
  41.  *
  42.  * @author Luc Maisonobe
  43.  */
  44. public class EventsLogger implements Serializable {

  45.     /** Serializable UID. */
  46.     private static final long serialVersionUID = -8643810076248572648L;

  47.     /** List of occurred events. */
  48.     private final List<LoggedEvent> log;

  49.     /** Simple constructor.
  50.      * <p>
  51.      * Build an empty logger for events detectors.
  52.      * </p>
  53.      */
  54.     public EventsLogger() {
  55.         log = new ArrayList<EventsLogger.LoggedEvent>();
  56.     }

  57.     /** Monitor an event detector.
  58.      * <p>
  59.      * In order to monitor an event detector, it must be wrapped thanks to
  60.      * this method as follows:
  61.      * </p>
  62.      * <pre>
  63.      * Propagator propagator = new XyzPropagator(...);
  64.      * EventsLogger logger = new EventsLogger();
  65.      * EventDetector detector = new UvwDetector(...);
  66.      * propagator.addEventDetector(logger.monitorDetector(detector));
  67.      * </pre>
  68.      * <p>
  69.      * Note that the event detector returned by the {@link
  70.      * LoggedEvent#getEventDetector() getEventDetector} method in
  71.      * {@link LoggedEvent LoggedEvent} instances returned by {@link
  72.      * #getLoggedEvents()} are the {@code monitoredDetector} instances
  73.      * themselves, not the wrapping detector returned by this method.
  74.      * </p>
  75.      * @param monitoredDetector event detector to monitor
  76.      * @return the wrapping detector to add to the propagator
  77.      * @param <T> class type for the generic version
  78.      */
  79.     public <T extends EventDetector> EventDetector monitorDetector(final T monitoredDetector) {
  80.         return new LoggingWrapper<T>(monitoredDetector);
  81.     }

  82.     /** Clear the logged events.
  83.      */
  84.     public void clearLoggedEvents() {
  85.         log.clear();
  86.     }

  87.     /** Get an immutable copy of the logged events.
  88.      * <p>
  89.      * The copy is independent of the logger. It is preserved
  90.      * event if the {@link #clearLoggedEvents() clearLoggedEvents} method
  91.      * is called and the logger reused in another propagation.
  92.      * </p>
  93.      * @return an immutable copy of the logged events
  94.      */
  95.     public List<LoggedEvent> getLoggedEvents() {
  96.         return new ArrayList<EventsLogger.LoggedEvent>(log);
  97.     }

  98.     /** Class for logged events entries. */
  99.     public static class LoggedEvent implements Serializable {

  100.         /** Serializable UID. */
  101.         private static final long serialVersionUID = 20131202L;

  102.         /** Event detector triggered. */
  103.         private final EventDetector detector;

  104.         /** Triggering state. */
  105.         private final SpacecraftState state;

  106.         /** Increasing/decreasing status. */
  107.         private final boolean increasing;

  108.         /** Simple constructor.
  109.          * @param detector detector for event that was triggered
  110.          * @param state state at event trigger date
  111.          * @param increasing indicator if the event switching function was increasing
  112.          * or decreasing at event occurrence date
  113.          */
  114.         private LoggedEvent(final EventDetector detector, final SpacecraftState state, final boolean increasing) {
  115.             this.detector   = detector;
  116.             this.state      = state;
  117.             this.increasing = increasing;
  118.         }

  119.         /** Get the event detector triggered.
  120.          * @return event detector triggered
  121.          */
  122.         public EventDetector getEventDetector() {
  123.             return detector;
  124.         }

  125.         /** Get the triggering state.
  126.          * @return triggering state
  127.          * @see EventDetector.Action#eventOccurred(SpacecraftState, boolean)
  128.          */
  129.         public SpacecraftState getState() {
  130.             return state;
  131.         }

  132.         /** Get the Increasing/decreasing status of the event.
  133.          * @return increasing/decreasing status of the event
  134.          * @see EventDetector.Action#eventOccurred(SpacecraftState, boolean)
  135.          */
  136.         public boolean isIncreasing() {
  137.             return increasing;
  138.         }

  139.     }

  140.     /** Internal wrapper for events detectors.
  141.      * @param <T> class type for the generic version
  142.      */
  143.     private class LoggingWrapper<T extends EventDetector> extends AbstractReconfigurableDetector<LoggingWrapper<T>> {

  144.         /** Serializable UID. */
  145.         private static final long serialVersionUID = 20131118L;

  146.         /** Wrapped events detector. */
  147.         private final T detector;

  148.         /** Simple constructor.
  149.          * @param detector events detector to wrap
  150.          */
  151.         public LoggingWrapper(final T detector) {
  152.             this(detector.getMaxCheckInterval(), detector.getThreshold(),
  153.                  detector.getMaxIterationCount(), new LocalHandler<T>(),
  154.                  detector);
  155.         }

  156.         /** Private constructor with full parameters.
  157.          * <p>
  158.          * This constructor is private as users are expected to use the builder
  159.          * API with the various {@code withXxx()} methods to set up the instance
  160.          * in a readable manner without using a huge amount of parameters.
  161.          * </p>
  162.          * @param maxCheck maximum checking interval (s)
  163.          * @param threshold convergence threshold (s)
  164.          * @param maxIter maximum number of iterations in the event time search
  165.          * @param handler event handler to call at event occurrences
  166.          * @param detector events detector to wrap
  167.          * @since 6.1
  168.          */
  169.         private LoggingWrapper(final double maxCheck, final double threshold,
  170.                                final int maxIter, final EventHandler<LoggingWrapper<T>> handler,
  171.                                final T detector) {
  172.             super(maxCheck, threshold, maxIter, handler);
  173.             this.detector = detector;
  174.         }

  175.         /** {@inheritDoc} */
  176.         @Override
  177.         protected LoggingWrapper<T> create(final double newMaxCheck, final double newThreshold,
  178.                                            final int newMaxIter, final EventHandler<LoggingWrapper<T>> newHandler) {
  179.             return new LoggingWrapper<T>(newMaxCheck, newThreshold, newMaxIter, newHandler, detector);
  180.         }

  181.         /** Log an event.
  182.          * @param state state at event trigger date
  183.          * @param increasing indicator if the event switching function was increasing
  184.          */
  185.         public void logEvent(final SpacecraftState state, final boolean increasing) {
  186.             log.add(new LoggedEvent(detector, state, increasing));
  187.         }

  188.         /** {@inheritDoc} */
  189.         public void init(final SpacecraftState s0, final AbsoluteDate t) {
  190.             detector.init(s0, t);
  191.         }

  192.         /** {@inheritDoc} */
  193.         public double g(final SpacecraftState s) throws OrekitException {
  194.             return detector.g(s);
  195.         }

  196.     }

  197.     /** Local class for handling events.
  198.      * @param <T> class type for the generic version
  199.      */
  200.     private static class LocalHandler<T extends EventDetector> implements EventHandler<LoggingWrapper<T>> {

  201.         /** {@inheritDoc} */
  202.         public Action eventOccurred(final SpacecraftState s, final LoggingWrapper<T> wrapper, final boolean increasing)
  203.             throws OrekitException {
  204.             wrapper.logEvent(s, increasing);
  205.             if (wrapper.detector instanceof AbstractReconfigurableDetector) {
  206.                 @SuppressWarnings("unchecked")
  207.                 final EventHandler<T> handler = ((AbstractReconfigurableDetector<T>) wrapper.detector).getHandler();
  208.                 return handler.eventOccurred(s, wrapper.detector, increasing);
  209.             } else {
  210.                 @SuppressWarnings("deprecation")
  211.                 final EventDetector.Action a = wrapper.detector.eventOccurred(s, increasing);
  212.                 return AbstractReconfigurableDetector.convert(a);
  213.             }
  214.         }

  215.         /** {@inheritDoc} */
  216.         @Override
  217.         public SpacecraftState resetState(final LoggingWrapper<T> wrapper, final SpacecraftState oldState)
  218.             throws OrekitException {
  219.             if (wrapper.detector instanceof AbstractReconfigurableDetector) {
  220.                 @SuppressWarnings("unchecked")
  221.                 final EventHandler<T> handler = ((AbstractReconfigurableDetector<T>) wrapper.detector).getHandler();
  222.                 return handler.resetState(wrapper.detector, oldState);
  223.             } else {
  224.                 @SuppressWarnings("deprecation")
  225.                 final SpacecraftState newState = wrapper.detector.resetState(oldState);
  226.                 return newState;
  227.             }
  228.         }

  229.     }

  230. }