FieldEventsLogger.java

  1. /* Copyright 2002-2019 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.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.RealFieldElement;
  21. import org.orekit.propagation.FieldSpacecraftState;
  22. import org.orekit.propagation.events.handlers.FieldEventHandler;
  23. import org.orekit.time.FieldAbsoluteDate;

  24. /** This class logs events detectors events during propagation.
  25.  *
  26.  * <p>As {@link FieldEventDetector events detectors} are triggered during
  27.  * orbit propagation, an event specific {@link
  28.  * FieldEventDetector#eventOccurred(FieldSpacecraftState, 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).<p>
  33.  * <p>This class works by wrapping user-provided {@link FieldEventDetector
  34.  * events detectors} before they are registered to the propagator. The
  35.  * wrapper monitor the calls to {@link
  36.  * FieldEventDetector#eventOccurred(FieldSpacecraftState, boolean) eventOccurred}
  37.  * and store the corresponding events as {@link FieldLoggedEvent} 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 FieldEventsLogger<T extends RealFieldElement<T>> {



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

  46.     /** Simple constructor.
  47.      * <p>
  48.      * Build an empty logger for events detectors.
  49.      * </p>
  50.      */
  51.     public FieldEventsLogger() {
  52.         log = new ArrayList<FieldEventsLogger.FieldLoggedEvent<T>>();
  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.      * FieldEventDetector<T> detector = new UvwDetector(...);
  63.      * propagator.addEventDetector(logger.monitorDetector(detector));
  64.      * </pre>
  65.      * <p>
  66.      * Note that the event detector returned by the {@link
  67.      * FieldLoggedEvent#getEventDetector() getEventDetector} method in
  68.      * {@link FieldLoggedEvent FieldLoggedEvent} 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 <D> class type for the generic version
  75.      */
  76.     public <D extends FieldEventDetector<T>> FieldEventDetector<T> monitorDetector(final D monitoredDetector) {
  77.         return new FieldLoggingWrapper<>(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<FieldLoggedEvent<T>> getLoggedEvents() {
  93.         return new ArrayList<FieldEventsLogger.FieldLoggedEvent<T>>(log);
  94.     }

  95.     /** Class for logged events entries. */
  96.     public static class FieldLoggedEvent <T extends RealFieldElement<T>> {

  97.         /** Event detector triggered. */
  98.         private final FieldEventDetector<T> detector;

  99.         /** Triggering state. */
  100.         private final FieldSpacecraftState<T> state;

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

  103.         /** Simple constructor.
  104.          * @param detectorN detector for event that was triggered
  105.          * @param stateN state at event trigger date
  106.          * @param increasingN indicator if the event switching function was increasing
  107.          * or decreasing at event occurrence date
  108.          */
  109.         private FieldLoggedEvent(final FieldEventDetector<T> detectorN, final FieldSpacecraftState<T> stateN, final boolean increasingN) {
  110.             detector   = detectorN;
  111.             state      = stateN;
  112.             increasing = increasingN;
  113.         }

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

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

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

  134.     }

  135.     /** Internal wrapper for events detectors.
  136.      * @param <D> class type for the generic version
  137.      */
  138.     private class FieldLoggingWrapper<D extends FieldEventDetector<T>> extends FieldAbstractDetector<FieldLoggingWrapper<D>, T> {

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

  141.         /** Simple constructor.
  142.          * @param detector events detector to wrap
  143.          */
  144.         FieldLoggingWrapper(final D detector) {
  145.             this(detector.getMaxCheckInterval(), detector.getThreshold(),
  146.                  detector.getMaxIterationCount(), new FieldLocalHandler<>(),
  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 FieldLoggingWrapper(final T maxCheck, final T threshold,
  163.                                final int maxIter, final FieldEventHandler<? super FieldLoggingWrapper<D>, T> handler,
  164.                                final D detector) {
  165.             super(maxCheck, threshold, maxIter, handler);
  166.             this.detector = detector;
  167.         }

  168.         /** {@inheritDoc} */
  169.         @Override
  170.         protected FieldLoggingWrapper<D> create(final T newMaxCheck, final T newThreshold,
  171.                                            final int newMaxIter, final FieldEventHandler<? super FieldLoggingWrapper<D>, T> newHandler) {
  172.             return new FieldLoggingWrapper<>(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 FieldSpacecraftState<T> state, final boolean increasing) {
  179.             log.add(new FieldLoggedEvent<>(detector, state, increasing));
  180.         }

  181.         /** {@inheritDoc} */
  182.         public void init(final FieldSpacecraftState<T> s0,
  183.                          final FieldAbsoluteDate<T> t) {
  184.             super.init(s0, t);
  185.             detector.init(s0, t);
  186.         }

  187.         /** {@inheritDoc} */
  188.         public T g(final FieldSpacecraftState<T> s) {
  189.             return detector.g(s);
  190.         }

  191.     }

  192.     /** Local class for handling events.
  193.      * @param <D> class type for the generic version
  194.      */
  195.     private class FieldLocalHandler<D extends FieldEventDetector<T>> implements FieldEventHandler<FieldLoggingWrapper<D>, T> {

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

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

  206.     }

  207. }