EventsLogger.java

  1. /* Copyright 2002-2020 CS GROUP
  2.  * Licensed to CS GROUP (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.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.ode.events.Action;
  21. import org.orekit.propagation.SpacecraftState;
  22. import org.orekit.propagation.events.handlers.EventHandler;
  23. import org.orekit.time.AbsoluteDate;

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

  44.     /** List of occurred events. */
  45.     private final List<LoggedEvent> log;

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

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

  79.     /** Clear the logged events.
  80.      */
  81.     public void clearLoggedEvents() {
  82.         log.clear();
  83.     }

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

  95.     /** Class for logged events entries. */
  96.     public static class LoggedEvent {

  97.         /** Event detector triggered. */
  98.         private final EventDetector detector;

  99.         /** Triggering state. */
  100.         private final SpacecraftState state;

  101.         /** Increasing/decreasing status. */
  102.         private final boolean increasing;

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

  114.         /** Get the event detector triggered.
  115.          * @return event detector triggered
  116.          */
  117.         public EventDetector getEventDetector() {
  118.             return detector;
  119.         }

  120.         /** Get the triggering state.
  121.          * @return triggering state
  122.          * @see EventDetector#eventOccurred(SpacecraftState, boolean)
  123.          */
  124.         public SpacecraftState getState() {
  125.             return state;
  126.         }

  127.         /** Get the Increasing/decreasing status of the event.
  128.          * @return increasing/decreasing status of the event
  129.          * @see EventDetector#eventOccurred(SpacecraftState, boolean)
  130.          */
  131.         public boolean isIncreasing() {
  132.             return increasing;
  133.         }

  134.     }

  135.     /** Internal wrapper for events detectors.
  136.      * @param <T> class type for the generic version
  137.      */
  138.     private class LoggingWrapper<T extends EventDetector> extends AbstractDetector<LoggingWrapper<T>> {

  139.         /** Wrapped events detector. */
  140.         private final T detector;

  141.         /** Simple constructor.
  142.          * @param detector events detector to wrap
  143.          */
  144.         LoggingWrapper(final T detector) {
  145.             this(detector.getMaxCheckInterval(), detector.getThreshold(),
  146.                  detector.getMaxIterationCount(), new LocalHandler<T>(),
  147.                  detector);
  148.         }

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

  168.         /** {@inheritDoc} */
  169.         @Override
  170.         protected LoggingWrapper<T> create(final double newMaxCheck, final double newThreshold,
  171.                                            final int newMaxIter, final EventHandler<? super LoggingWrapper<T>> newHandler) {
  172.             return new LoggingWrapper<T>(newMaxCheck, newThreshold, newMaxIter, newHandler, detector);
  173.         }

  174.         /** Log an event.
  175.          * @param state state at event trigger date
  176.          * @param increasing indicator if the event switching function was increasing
  177.          */
  178.         public void logEvent(final SpacecraftState state, final boolean increasing) {
  179.             log.add(new LoggedEvent(detector, state, increasing));
  180.         }

  181.         /** {@inheritDoc} */
  182.         public void init(final SpacecraftState s0,
  183.                          final AbsoluteDate t) {
  184.             super.init(s0, t);
  185.             detector.init(s0, t);
  186.         }

  187.         /** {@inheritDoc} */
  188.         public double g(final SpacecraftState s) {
  189.             return detector.g(s);
  190.         }

  191.     }

  192.     /** Local class for handling events.
  193.      * @param <T> class type for the generic version
  194.      */
  195.     private static class LocalHandler<T extends EventDetector> implements EventHandler<LoggingWrapper<T>> {

  196.         /** {@inheritDoc} */
  197.         public Action eventOccurred(final SpacecraftState s, final LoggingWrapper<T> wrapper, final boolean increasing) {
  198.             wrapper.logEvent(s, increasing);
  199.             return wrapper.detector.eventOccurred(s, increasing);
  200.         }

  201.         /** {@inheritDoc} */
  202.         @Override
  203.         public SpacecraftState resetState(final LoggingWrapper<T> wrapper, final SpacecraftState oldState) {
  204.             return wrapper.detector.resetState(oldState);
  205.         }

  206.     }

  207. }