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
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.hipparchus.ode.events.Action;
23 import org.orekit.propagation.SpacecraftState;
24 import org.orekit.propagation.events.handlers.EventHandler;
25 import org.orekit.time.AbsoluteDate;
26 import org.orekit.time.TimeStamped;
27
28 /** This class logs events detectors events during propagation.
29 *
30 * <p>As {@link EventDetector events detectors} are triggered during
31 * orbit propagation, an event specific {@link
32 * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred}
33 * method is called. This class can be used to add a global logging
34 * feature registering all events with their corresponding states in
35 * a chronological sequence (or reverse-chronological if propagation
36 * occurs backward).
37 * <p>This class works by wrapping user-provided {@link EventDetector
38 * events detectors} before they are registered to the propagator. The
39 * wrapper monitor the calls to {@link
40 * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred}
41 * and store the corresponding events as {@link LoggedEvent} instances.
42 * After propagation is complete, the user can retrieve all the events
43 * that have occurred at once by calling method {@link #getLoggedEvents()}.</p>
44 *
45 * @author Luc Maisonobe
46 */
47 public class EventsLogger {
48
49 /** List of occurred events. */
50 private final List<LoggedEvent> log;
51
52 /** Simple constructor.
53 * <p>
54 * Build an empty logger for events detectors.
55 * </p>
56 */
57 public EventsLogger() {
58 log = new ArrayList<>();
59 }
60
61 /** Monitor an event detector.
62 * <p>
63 * In order to monitor an event detector, it must be wrapped thanks to
64 * this method as follows:
65 * </p>
66 * <pre>
67 * Propagator propagator = new XyzPropagator(...);
68 * EventsLogger logger = new EventsLogger();
69 * EventDetector detector = new UvwDetector(...);
70 * propagator.addEventDetector(logger.monitorDetector(detector));
71 * </pre>
72 * <p>
73 * Note that the event detector returned by the {@link
74 * LoggedEvent#getEventDetector() getEventDetector} method in
75 * {@link LoggedEvent LoggedEvent} instances returned by {@link
76 * #getLoggedEvents()} are the {@code monitoredDetector} instances
77 * themselves, not the wrapping detector returned by this method.
78 * </p>
79 * @param monitoredDetector event detector to monitor
80 * @return the wrapping detector to add to the propagator
81 * @param <T> class type for the generic version
82 */
83 public <T extends EventDetector> EventDetector monitorDetector(final T monitoredDetector) {
84 return new LoggingWrapper(monitoredDetector);
85 }
86
87 /** Clear the logged events.
88 */
89 public void clearLoggedEvents() {
90 log.clear();
91 }
92
93 /** Get an immutable copy of the logged events.
94 * <p>
95 * The copy is independent of the logger. It is preserved
96 * event if the {@link #clearLoggedEvents() clearLoggedEvents} method
97 * is called and the logger reused in another propagation.
98 * </p>
99 * @return an immutable copy of the logged events
100 */
101 public List<LoggedEvent> getLoggedEvents() {
102 return new ArrayList<>(log);
103 }
104
105 /** Class for logged events entries. */
106 public static class LoggedEvent implements TimeStamped {
107
108 /** Event detector triggered. */
109 private final EventDetector detector;
110
111 /** Triggering state. */
112 private final SpacecraftState state;
113
114 /** Increasing/decreasing status. */
115 private final boolean increasing;
116
117 /** Simple constructor.
118 * @param detector detector for event that was triggered
119 * @param state state at event trigger date
120 * @param increasing indicator if the event switching function was increasing
121 * or decreasing at event occurrence date
122 */
123 private LoggedEvent(final EventDetector detector, final SpacecraftState state, final boolean increasing) {
124 this.detector = detector;
125 this.state = state;
126 this.increasing = increasing;
127 }
128
129 /** Get the event detector triggered.
130 * @return event detector triggered
131 */
132 public EventDetector getEventDetector() {
133 return detector;
134 }
135
136 /** {@inheritDoc} */
137 @Override
138 public AbsoluteDate getDate() {
139 return state.getDate();
140 }
141
142 /** Get the triggering state.
143 * @return triggering state
144 * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
145 */
146 public SpacecraftState getState() {
147 return state;
148 }
149
150 /** Get the Increasing/decreasing status of the event.
151 * @return increasing/decreasing status of the event
152 * @see EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
153 */
154 public boolean isIncreasing() {
155 return increasing;
156 }
157
158 }
159
160 /** Internal wrapper for events detectors. */
161 private class LoggingWrapper implements DetectorModifier {
162
163 /** Wrapped event detector. */
164 private final EventDetector detector;
165
166 /** Simple constructor.
167 * @param detector events detector to wrap
168 */
169 LoggingWrapper(final EventDetector detector) {
170 this.detector = detector;
171 }
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(getDetector(), state, increasing));
179 }
180
181 /** {@inheritDoc} */
182 @Override
183 public EventDetector getDetector() {
184 return detector;
185 }
186
187 /** {@inheritDoc} */
188 @Override
189 public EventHandler getHandler() {
190 final EventHandler handler = getDetector().getHandler();
191
192 return new EventHandler() {
193
194 /** {@inheritDoc} */
195 public Action eventOccurred(final SpacecraftState s, final EventDetector d, final boolean increasing) {
196 logEvent(s, increasing);
197 return handler.eventOccurred(s, getDetector(), increasing);
198 }
199
200 /** {@inheritDoc} */
201 @Override
202 public SpacecraftState resetState(final EventDetector d, final SpacecraftState oldState) {
203 return handler.resetState(getDetector(), oldState);
204 }
205
206 };
207 }
208
209 }
210
211 }