EventsLogger.java

  1. /* Copyright 2002-2025 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.         /** State after reset if any, otherwise same than triggering state. */
  105.         private final SpacecraftState resetState;

  106.         /** Constructor.
  107.          * @param detector detector for event that was triggered
  108.          * @param state state at event trigger date
  109.          * @param resetState state after reset if any, otherwise same as event state
  110.          * @param increasing indicator if the event switching function was increasing
  111.          * or decreasing at event occurrence date
  112.          * @since 13.1
  113.          */
  114.         private LoggedEvent(final EventDetector detector, final SpacecraftState state,
  115.                             final SpacecraftState resetState, final boolean increasing) {
  116.             this.detector   = detector;
  117.             this.state      = state;
  118.             this.resetState = resetState;
  119.             this.increasing = increasing;
  120.         }

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

  127.         /** {@inheritDoc} */
  128.         @Override
  129.         public AbsoluteDate getDate() {
  130.             return state.getDate();
  131.         }

  132.         /** Get the triggering state.
  133.          * @return triggering state
  134.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  135.          */
  136.         public SpacecraftState getState() {
  137.             return state;
  138.         }

  139.         /** Get the reset state.
  140.          * @return reset state
  141.          * @see EventHandler#resetState(EventDetector, SpacecraftState)
  142.          * @since 13.1
  143.          */
  144.         public SpacecraftState getResetState() {
  145.             return resetState;
  146.         }

  147.         /** Get the Increasing/decreasing status of the event.
  148.          * @return increasing/decreasing status of the event
  149.          * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  150.          */
  151.         public boolean isIncreasing() {
  152.             return increasing;
  153.         }

  154.     }

  155.     /** Internal wrapper for events detectors. */
  156.     private class LoggingWrapper implements DetectorModifier {

  157.         /** Wrapped event detector. */
  158.         private final EventDetector wrappedDetector;

  159.         /** Simple constructor.
  160.          * @param detector events detector to wrap
  161.          */
  162.         LoggingWrapper(final EventDetector detector) {
  163.             this.wrappedDetector = detector;
  164.         }

  165.         /** Log an event.
  166.          * @param state state at event trigger date
  167.          * @param resetState state after reset if any, otherwise event state
  168.          * @param increasing indicator if the event switching function was increasing
  169.          */
  170.         void logEvent(final SpacecraftState state, final SpacecraftState resetState, final boolean increasing) {
  171.             log.add(new LoggedEvent(getDetector(), state, resetState, increasing));
  172.         }

  173.         /** {@inheritDoc} */
  174.         @Override
  175.         public EventDetector getDetector() {
  176.             return wrappedDetector;
  177.         }

  178.         /** {@inheritDoc} */
  179.         @Override
  180.         public EventHandler getHandler() {
  181.             final EventHandler handler = getDetector().getHandler();

  182.             return new EventHandler() {

  183.                 private SpacecraftState lastTriggeringState = null;
  184.                 private SpacecraftState lastResetState = null;

  185.                 /** {@inheritDoc} */
  186.                 @Override
  187.                 public void init(final SpacecraftState initialState, final AbsoluteDate target,
  188.                                  final EventDetector detector) {
  189.                     EventHandler.super.init(initialState, target, detector);
  190.                     lastTriggeringState = null;
  191.                     lastResetState = null;
  192.                 }

  193.                 /** {@inheritDoc} */
  194.                 @Override
  195.                 public Action eventOccurred(final SpacecraftState s, final EventDetector d, final boolean increasing) {
  196.                     final Action action = handler.eventOccurred(s, getDetector(), increasing);
  197.                     if (action == Action.RESET_STATE) {
  198.                         lastResetState = resetState(getDetector(), s);
  199.                     } else {
  200.                         lastResetState = s;
  201.                     }
  202.                     lastTriggeringState = s;
  203.                     logEvent(s, lastResetState, increasing);
  204.                     return action;
  205.                 }

  206.                 /** {@inheritDoc} */
  207.                 @Override
  208.                 public SpacecraftState resetState(final EventDetector d, final SpacecraftState oldState) {
  209.                     if (lastTriggeringState != oldState) {
  210.                         lastTriggeringState = oldState;
  211.                         lastResetState = handler.resetState(getDetector(), oldState);
  212.                     }
  213.                     return lastResetState;
  214.                 }

  215.             };
  216.         }

  217.     }

  218. }