BooleanDetector.java

  1. /* Contributed in the public domain.
  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.Arrays;
  20. import java.util.Collection;
  21. import java.util.List;
  22. import java.util.NoSuchElementException;

  23. import org.hipparchus.util.FastMath;
  24. import org.orekit.propagation.SpacecraftState;
  25. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  26. import org.orekit.propagation.events.handlers.EventHandler;
  27. import org.orekit.time.AbsoluteDate;

  28. /**
  29.  * This class provides AND and OR operations for event detectors. This class treats
  30.  * positive values of the g function as true and negative values as false.
  31.  *
  32.  * <p> One example for an imaging satellite might be to only detect events when a
  33.  * satellite is overhead (elevation &gt; 0) AND when the ground point is sunlit (Sun
  34.  * elevation &gt; 0). Another slightly contrived example using the OR operator would be to
  35.  * detect access to a set of ground stations and only report events when the satellite
  36.  * enters or leaves the field of view of the set, but not hand-offs between the ground
  37.  * stations.
  38.  *
  39.  * <p> For the BooleanDetector is important that the sign of the g function of the
  40.  * underlying event detector is not arbitrary, but has a semantic meaning, e.g. in or out,
  41.  * true or false. This class works well with event detectors that detect entry to or exit
  42.  * from a region, e.g. {@link EclipseDetector}, {@link ElevationDetector}, {@link
  43.  * LatitudeCrossingDetector}. Using this detector with detectors that are not based on
  44.  * entry to or exit from a region, e.g. {@link DateDetector}, {@link
  45.  * LongitudeCrossingDetector}, will likely lead to unexpected results. To apply conditions
  46.  * to this latter type of event detectors a {@link EventEnablingPredicateFilter} is
  47.  * usually more appropriate.
  48.  *
  49.  * @author Evan Ward
  50.  * @see #andCombine(Collection)
  51.  * @see #orCombine(Collection)
  52.  * @see #notCombine(EventDetector)
  53.  * @see EventEnablingPredicateFilter
  54.  * @see EventSlopeFilter
  55.  */
  56. public class BooleanDetector extends AbstractDetector<BooleanDetector> {

  57.     /** Original detectors: the operands. */
  58.     private final List<EventDetector> detectors;

  59.     /** The composition function. Should be associative for predictable behavior. */
  60.     private final Operator operator;

  61.     /**
  62.      * Private constructor with all the parameters.
  63.      *
  64.      * @param detectors    the operands.
  65.      * @param operator     reduction operator to apply to value of the g function of the
  66.      *                     operands.
  67.      * @param newMaxCheck  max check interval in seconds.
  68.      * @param newThreshold convergence threshold in seconds.
  69.      * @param newMaxIter   max iterations.
  70.      * @param newHandler   event handler.
  71.      */
  72.     protected BooleanDetector(final List<EventDetector> detectors,
  73.                               final Operator operator,
  74.                               final AdaptableInterval newMaxCheck,
  75.                               final double newThreshold,
  76.                               final int newMaxIter,
  77.                               final EventHandler newHandler) {
  78.         super(newMaxCheck, newThreshold, newMaxIter, newHandler);
  79.         this.detectors = detectors;
  80.         this.operator = operator;
  81.     }

  82.     /**
  83.      * Create a new event detector that is the logical AND of the given event detectors.
  84.      *
  85.      * <p> The created event detector's g function is positive if and only if the g
  86.      * functions of all detectors in {@code detectors} are positive.
  87.      *
  88.      * <p> The starting interval, threshold, and iteration count are set to the most
  89.      * stringent (minimum) of all the {@code detectors}. The event handlers of the
  90.      * underlying {@code detectors} are not used, instead the default handler is {@link
  91.      * ContinueOnEvent}.
  92.      *
  93.      * @param detectors the operands. Must contain at least one detector.
  94.      * @return a new event detector that is the logical AND of the operands.
  95.      * @throws NoSuchElementException if {@code detectors} is empty.
  96.      * @see BooleanDetector
  97.      * @see #andCombine(Collection)
  98.      * @see #orCombine(EventDetector...)
  99.      * @see #notCombine(EventDetector)
  100.      */
  101.     public static BooleanDetector andCombine(final EventDetector... detectors) {
  102.         return andCombine(Arrays.asList(detectors));
  103.     }

  104.     /**
  105.      * Create a new event detector that is the logical AND of the given event detectors.
  106.      *
  107.      * <p> The created event detector's g function is positive if and only if the g
  108.      * functions of all detectors in {@code detectors} are positive.
  109.      *
  110.      * <p> The starting interval, threshold, and iteration count are set to the most
  111.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  112.      * underlying {@code detectors} are not used, instead the default handler is {@link
  113.      * ContinueOnEvent}.
  114.      *
  115.      * @param detectors the operands. Must contain at least one detector.
  116.      * @return a new event detector that is the logical AND of the operands.
  117.      * @throws NoSuchElementException if {@code detectors} is empty.
  118.      * @see BooleanDetector
  119.      * @see #andCombine(EventDetector...)
  120.      * @see #orCombine(Collection)
  121.      * @see #notCombine(EventDetector)
  122.      */
  123.     public static BooleanDetector andCombine(final Collection<? extends EventDetector> detectors) {

  124.         return new BooleanDetector(new ArrayList<>(detectors), // copy for immutability
  125.                 Operator.AND,
  126.                 s -> {
  127.                     double minInterval = Double.POSITIVE_INFINITY;
  128.                     for (final EventDetector detector : detectors) {
  129.                         minInterval = FastMath.min(minInterval, detector.getMaxCheckInterval().currentInterval(s));
  130.                     }
  131.                     return minInterval;
  132.                 },
  133.                 detectors.stream().map(EventDetector::getThreshold).min(Double::compareTo).get(),
  134.                 detectors.stream().map(EventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  135.                 new ContinueOnEvent());
  136.     }

  137.     /**
  138.      * Create a new event detector that is the logical OR of the given event detectors.
  139.      *
  140.      * <p> The created event detector's g function is positive if and only if at least
  141.      * one of g functions of the event detectors in {@code detectors} is positive.
  142.      *
  143.      * <p> The starting interval, threshold, and iteration count are set to the most
  144.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  145.      * underlying EventDetectors are not used, instead the default handler is {@link
  146.      * ContinueOnEvent}.
  147.      *
  148.      * @param detectors the operands. Must contain at least one detector.
  149.      * @return a new event detector that is the logical OR of the operands.
  150.      * @throws NoSuchElementException if {@code detectors} is empty.
  151.      * @see BooleanDetector
  152.      * @see #orCombine(Collection)
  153.      * @see #andCombine(EventDetector...)
  154.      * @see #notCombine(EventDetector)
  155.      */
  156.     public static BooleanDetector orCombine(final EventDetector... detectors) {
  157.         return orCombine(Arrays.asList(detectors));
  158.     }

  159.     /**
  160.      * Create a new event detector that is the logical OR of the given event detectors.
  161.      *
  162.      * <p> The created event detector's g function is positive if and only if at least
  163.      * one of g functions of the event detectors in {@code detectors} is positive.
  164.      *
  165.      * <p> The starting interval, threshold, and iteration count are set to the most
  166.      * stringent (minimum) of the {@code detectors}. The event handlers of the
  167.      * underlying EventDetectors are not used, instead the default handler is {@link
  168.      * ContinueOnEvent}.
  169.      *
  170.      * @param detectors the operands. Must contain at least one detector.
  171.      * @return a new event detector that is the logical OR of the operands.
  172.      * @throws NoSuchElementException if {@code detectors} is empty.
  173.      * @see BooleanDetector
  174.      * @see #orCombine(EventDetector...)
  175.      * @see #andCombine(Collection)
  176.      * @see #notCombine(EventDetector)
  177.      */
  178.     public static BooleanDetector orCombine(final Collection<? extends EventDetector> detectors) {

  179.         return new BooleanDetector(new ArrayList<>(detectors), // copy for immutability
  180.                 Operator.OR,
  181.                 s -> {
  182.                     double minInterval = Double.POSITIVE_INFINITY;
  183.                     for (final EventDetector detector : detectors) {
  184.                         minInterval = FastMath.min(minInterval, detector.getMaxCheckInterval().currentInterval(s));
  185.                     }
  186.                     return minInterval;
  187.                 },
  188.                 detectors.stream().map(EventDetector::getThreshold).min(Double::compareTo).get(),
  189.                 detectors.stream().map(EventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  190.                 new ContinueOnEvent());
  191.     }

  192.     /**
  193.      * Create a new event detector that negates the g function of another detector.
  194.      *
  195.      * <p> This detector will be initialized with the same {@link
  196.      * EventDetector#getMaxCheckInterval()}, {@link EventDetector#getThreshold()}, and
  197.      * {@link EventDetector#getMaxIterationCount()} as {@code detector}. The event handler
  198.      * of the underlying detector is not used, instead the default handler is {@link
  199.      * ContinueOnEvent}.
  200.      *
  201.      * @param detector to negate.
  202.      * @return an new event detector whose g function is the same magnitude but opposite
  203.      * sign of {@code detector}.
  204.      * @see #andCombine(Collection)
  205.      * @see #orCombine(Collection)
  206.      * @see BooleanDetector
  207.      */
  208.     public static NegateDetector notCombine(final EventDetector detector) {
  209.         return new NegateDetector(detector);
  210.     }

  211.     @Override
  212.     public double g(final SpacecraftState s) {
  213.         // can't use stream/lambda here because g(s) throws a checked exception
  214.         // so write out and combine the map and reduce loops
  215.         double ret = Double.NaN; // return value
  216.         boolean first = true;
  217.         for (final EventDetector detector : detectors) {
  218.             if (first) {
  219.                 ret = detector.g(s);
  220.                 first = false;
  221.             } else {
  222.                 ret = operator.combine(ret, detector.g(s));
  223.             }
  224.         }
  225.         // return the result of applying the operator to all operands
  226.         return ret;
  227.     }

  228.     @Override
  229.     protected BooleanDetector create(final AdaptableInterval newMaxCheck,
  230.                                      final double newThreshold,
  231.                                      final int newMaxIter,
  232.                                      final EventHandler newHandler) {
  233.         return new BooleanDetector(detectors, operator, newMaxCheck, newThreshold,
  234.                 newMaxIter, newHandler);
  235.     }

  236.     @Override
  237.     public void init(final SpacecraftState s0,
  238.                      final AbsoluteDate t) {
  239.         super.init(s0, t);
  240.         for (final EventDetector detector : detectors) {
  241.             detector.init(s0, t);
  242.         }
  243.     }

  244.     /**
  245.      * Get the list of original detectors.
  246.      * @return the list of original detectors
  247.      * @since 10.2
  248.      */
  249.     public List<EventDetector> getDetectors() {
  250.         return new ArrayList<EventDetector>(detectors);
  251.     }

  252.     /** Local class for operator. */
  253.     private enum Operator {

  254.         /** And operator. */
  255.         AND() {

  256.             @Override
  257.             /** {@inheritDoc} */
  258.             public double combine(final double g1, final double g2) {
  259.                 return FastMath.min(g1, g2);
  260.             }

  261.         },

  262.         /** Or operator. */
  263.         OR() {

  264.             @Override
  265.             /** {@inheritDoc} */
  266.             public double combine(final double g1, final double g2) {
  267.                 return FastMath.max(g1, g2);
  268.             }

  269.         };

  270.         /** Combine two g functions evaluations.
  271.          * @param g1 first evaluation
  272.          * @param g2 second evaluation
  273.          * @return combined evaluation
  274.          */
  275.         public abstract double combine(double g1, double g2);

  276.     };

  277. }