FunctionalDetector.java

  1. /* Contributed in the public domain.
  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.function.ToDoubleFunction;

  19. import org.orekit.propagation.SpacecraftState;
  20. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  21. import org.orekit.propagation.events.handlers.EventHandler;

  22. /**
  23.  * A detector that implements the {@link #g(SpacecraftState)} function using a lambda that
  24.  * can be set using {@link #withFunction(ToDoubleFunction)}.
  25.  *
  26.  * <p>For example, to create a simple date detector use:
  27.  *
  28.  * <code><pre>
  29.  * FunctionalDetector d = new FunctionalDetector()
  30.  *     .withGFunction((s) -&gt; s.getDate().durationFrom(triggerDate))
  31.  *     .withMaxCheck(1e10);
  32.  * </pre></code>
  33.  *
  34.  * @author Evan Ward
  35.  * @since 9.2
  36.  */
  37. public class FunctionalDetector extends AbstractDetector<FunctionalDetector> {

  38.     /** Serializable UID. */
  39.     private static final long serialVersionUID = 20180525L;

  40.     /** The g function. Deprecated in favor of the standard java.util interface. */
  41.     @Deprecated
  42.     private final GFunction gFunction;

  43.     /** The g function. */
  44.     private final ToDoubleFunction<SpacecraftState> function;

  45.     /**
  46.      * Create an event detector with the default values. These are {@link
  47.      * #DEFAULT_MAXCHECK}, {@link #DEFAULT_THRESHOLD}, {@link #DEFAULT_MAX_ITER}, {@link
  48.      * ContinueOnEvent}, and a g function that is identically unity.
  49.      */
  50.     public FunctionalDetector() {
  51.         this(DEFAULT_MAXCHECK, DEFAULT_THRESHOLD, DEFAULT_MAX_ITER,
  52.                 new ContinueOnEvent<>(),
  53.                 (ToDoubleFunction<SpacecraftState>) value -> 1.0);
  54.     }

  55.     /**
  56.      * Private constructor.
  57.      *
  58.      * @param maxCheck  maximum checking interval (s)
  59.      * @param threshold convergence threshold (s)
  60.      * @param maxIter   maximum number of iterations in the event time search
  61.      * @param handler   event handler to call at event occurrences
  62.      * @param gFunction the switching function.
  63.      * @deprecated Use {@link #FunctionalDetector(double, double, int, EventHandler,
  64.      * ToDoubleFunction)} instead. Will be removed in the next major release.
  65.      */
  66.     @Deprecated
  67.     private FunctionalDetector(final double maxCheck,
  68.                                final double threshold,
  69.                                final int maxIter,
  70.                                final EventHandler<? super FunctionalDetector> handler,
  71.                                final GFunction gFunction) {
  72.         super(maxCheck, threshold, maxIter, handler);
  73.         this.gFunction = gFunction;
  74.         this.function = gFunction::apply;
  75.     }

  76.     /**
  77.      * Private constructor.
  78.      *
  79.      * @param maxCheck  maximum checking interval (s)
  80.      * @param threshold convergence threshold (s)
  81.      * @param maxIter   maximum number of iterations in the event time search
  82.      * @param handler   event handler to call at event occurrences
  83.      * @param function  the switching function.
  84.      */
  85.     private FunctionalDetector(final double maxCheck,
  86.                                final double threshold,
  87.                                final int maxIter,
  88.                                final EventHandler<? super FunctionalDetector> handler,
  89.                                final ToDoubleFunction<SpacecraftState> function) {
  90.         super(maxCheck, threshold, maxIter, handler);
  91.         this.gFunction = function::applyAsDouble;
  92.         this.function = function;
  93.     }


  94.     @Override
  95.     public double g(final SpacecraftState s) {
  96.         return gFunction.apply(s);
  97.     }

  98.     @Override
  99.     protected FunctionalDetector create(
  100.             final double newMaxCheck,
  101.             final double newThreshold,
  102.             final int newMaxIter,
  103.             final EventHandler<? super FunctionalDetector> newHandler) {

  104.         return new FunctionalDetector(newMaxCheck, newThreshold, newMaxIter, newHandler,
  105.                 gFunction);
  106.     }

  107.     /**
  108.      * Create a new event detector with a new g function, keeping all other attributes the
  109.      * same. It is recommended to use {@link #withMaxCheck(double)} and {@link
  110.      * #withThreshold(double)} to set appropriate values for this g function.
  111.      *
  112.      * @param newGFunction the new g function.
  113.      * @return a new detector with the new g function.
  114.      */
  115.     public FunctionalDetector withFunction(
  116.             final ToDoubleFunction<SpacecraftState> newGFunction) {
  117.         return new FunctionalDetector(getMaxCheckInterval(), getThreshold(),
  118.                 getMaxIterationCount(), getHandler(), newGFunction);
  119.     }

  120.     /**
  121.      * Create a new event detector with a new g function, keeping all other attributes the
  122.      * same. It is recommended to use {@link #withMaxCheck(double)} and {@link
  123.      * #withThreshold(double)} to set appropriate values for this g function.
  124.      *
  125.      * @param newGFunction the new g function.
  126.      * @return a new detector with the new g function.
  127.      * @deprecated Use {@link #withFunction(ToDoubleFunction)} instead. Will be removed in
  128.      * next major release.
  129.      */
  130.     @Deprecated
  131.     public FunctionalDetector withGFunction(final GFunction newGFunction) {
  132.         return new FunctionalDetector(getMaxCheckInterval(), getThreshold(),
  133.                 getMaxIterationCount(), getHandler(), newGFunction);
  134.     }

  135.     /**
  136.      * Get the switching function.
  137.      *
  138.      * @return the function used in {@link #g(SpacecraftState)}.
  139.      * @deprecated use {@link #getFunction()} instead. Will be removed in next major
  140.      * release.
  141.      */
  142.     @Deprecated
  143.     public GFunction getGFunction() {
  144.         return gFunction;
  145.     }

  146.     /**
  147.      * Get the switching function.
  148.      *
  149.      * @return the function used in {@link #g(SpacecraftState)}.
  150.      */
  151.     public ToDoubleFunction<SpacecraftState> getFunction() {
  152.         return function;
  153.     }

  154.     /**
  155.      * A functional interface for the {@link #g(SpacecraftState)} function.
  156.      *
  157.      * @deprecated Use {@link ToDoubleFunction}<SpaceraftState> instead. Will be removed
  158.      * in next major release.
  159.      */
  160.     @FunctionalInterface
  161.     @Deprecated
  162.     public interface GFunction {

  163.         /**
  164.          * Applies this function to the given argument.
  165.          *
  166.          * @param value the function argument
  167.          * @return the function result
  168.          * @deprecated Use {@link ToDoubleFunction#applyAsDouble(Object)} instead. Will be
  169.          * removed in next major release.
  170.          */
  171.         @Deprecated
  172.         double apply(SpacecraftState value);

  173.     }

  174. }