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.         /** 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 implements DetectorModifier {

  143.         /** Wrapped event 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 = detector;
  150.         }

  151.         /** Log an event.
  152.          * @param state state at event trigger date
  153.          * @param increasing indicator if the event switching function was increasing
  154.          */
  155.         public void logEvent(final SpacecraftState state, final boolean increasing) {
  156.             log.add(new LoggedEvent(getDetector(), state, increasing));
  157.         }

  158.         /** {@inheritDoc} */
  159.         @Override
  160.         public EventDetector getDetector() {
  161.             return detector;
  162.         }

  163.         /** {@inheritDoc} */
  164.         @Override
  165.         public EventHandler getHandler() {
  166.             final EventHandler handler = getDetector().getHandler();

  167.             return new EventHandler() {

  168.                 /** {@inheritDoc} */
  169.                 public Action eventOccurred(final SpacecraftState s, final EventDetector d, final boolean increasing) {
  170.                     logEvent(s, increasing);
  171.                     return handler.eventOccurred(s, getDetector(), increasing);
  172.                 }

  173.                 /** {@inheritDoc} */
  174.                 @Override
  175.                 public SpacecraftState resetState(final EventDetector d, final SpacecraftState oldState) {
  176.                     return handler.resetState(getDetector(), oldState);
  177.                 }

  178.             };
  179.         }

  180.     }

  181. }