EventsLogger.java

  1. /* Copyright 2002-2024 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. import org.orekit.time.TimeStamped;

  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.  * EventHandler#eventOccurred(SpacecraftState, EventDetector, 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).
  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.  * EventHandler#eventOccurred(SpacecraftState, EventDetector, 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 {

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

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

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

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

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

  96.     /** Class for logged events entries. */
  97.     public static class LoggedEvent implements TimeStamped {

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

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

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

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

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

  121.         /** {@inheritDoc} */
  122.         @Override
  123.         public AbsoluteDate getDate() {
  124.             return state.getDate();
  125.         }

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

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

  140.     }

  141.     /** Internal wrapper for events detectors. */
  142.     private class LoggingWrapper extends AbstractDetector<LoggingWrapper> {

  143.         /** Wrapped events detector. */
  144.         private final EventDetector detector;

  145.         /** Simple constructor.
  146.          * @param detector events detector to wrap
  147.          */
  148.         LoggingWrapper(final EventDetector detector) {
  149.             this(detector.getDetectionSettings(), null, detector);
  150.         }

  151.         /** Private constructor with full parameters.
  152.          * <p>
  153.          * This constructor is private as users are expected to use the builder
  154.          * API with the various {@code withXxx()} methods to set up the instance
  155.          * in a readable manner without using a huge amount of parameters.
  156.          * </p>
  157.          * @param detectionSettings detection settings
  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 EventDetectionSettings detectionSettings, final EventHandler handler,
  163.                                final EventDetector detector) {
  164.             super(detectionSettings, handler);
  165.             this.detector = detector;
  166.         }

  167.         /** {@inheritDoc} */
  168.         @Override
  169.         protected LoggingWrapper create(final AdaptableInterval newMaxCheck, final double newThreshold,
  170.                                         final int newMaxIter, final EventHandler newHandler) {
  171.             return new LoggingWrapper(new EventDetectionSettings(newMaxCheck, newThreshold, newMaxIter), newHandler, detector);
  172.         }

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

  180.         /** {@inheritDoc} */
  181.         @Override
  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.         /** {@inheritDoc} */
  192.         @Override
  193.         public void finish(final SpacecraftState state) {
  194.             detector.finish(state);
  195.         }

  196.         /** {@inheritDoc} */
  197.         @Override
  198.         public EventHandler getHandler() {

  199.             final EventHandler handler = detector.getHandler();

  200.             return new EventHandler() {

  201.                 /** {@inheritDoc} */
  202.                 public Action eventOccurred(final SpacecraftState s, final EventDetector d, final boolean increasing) {
  203.                     logEvent(s, increasing);
  204.                     return handler.eventOccurred(s, detector, increasing);
  205.                 }

  206.                 /** {@inheritDoc} */
  207.                 @Override
  208.                 public SpacecraftState resetState(final EventDetector d, final SpacecraftState oldState) {
  209.                     return handler.resetState(detector, oldState);
  210.                 }

  211.             };
  212.         }

  213.     }

  214. }