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.     private BooleanDetector(final List<EventDetector> detectors,
  73.                             final Operator operator,
  74.                             final double newMaxCheck,
  75.                             final double newThreshold,
  76.                             final int newMaxIter,
  77.                             final EventHandler<? super BooleanDetector> 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.                 detectors.stream().map(EventDetector::getMaxCheckInterval).min(Double::compareTo).get(),
  127.                 detectors.stream().map(EventDetector::getThreshold).min(Double::compareTo).get(),
  128.                 detectors.stream().map(EventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  129.                 new ContinueOnEvent<>());
  130.     }

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

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

  173.         return new BooleanDetector(new ArrayList<>(detectors), // copy for immutability
  174.                 Operator.OR,
  175.                 detectors.stream().map(EventDetector::getMaxCheckInterval).min(Double::compareTo).get(),
  176.                 detectors.stream().map(EventDetector::getThreshold).min(Double::compareTo).get(),
  177.                 detectors.stream().map(EventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  178.                 new ContinueOnEvent<>());
  179.     }

  180.     /**
  181.      * Create a new event detector that negates the g function of another detector.
  182.      *
  183.      * <p> This detector will be initialized with the same {@link
  184.      * EventDetector#getMaxCheckInterval()}, {@link EventDetector#getThreshold()}, and
  185.      * {@link EventDetector#getMaxIterationCount()} as {@code detector}. The event handler
  186.      * of the underlying detector is not used, instead the default handler is {@link
  187.      * ContinueOnEvent}.
  188.      *
  189.      * @param detector to negate.
  190.      * @return an new event detector whose g function is the same magnitude but opposite
  191.      * sign of {@code detector}.
  192.      * @see #andCombine(Collection)
  193.      * @see #orCombine(Collection)
  194.      * @see BooleanDetector
  195.      */
  196.     public static NegateDetector notCombine(final EventDetector detector) {
  197.         return new NegateDetector(detector);
  198.     }

  199.     @Override
  200.     public double g(final SpacecraftState s) {
  201.         // can't use stream/lambda here because g(s) throws a checked exception
  202.         // so write out and combine the map and reduce loops
  203.         double ret = Double.NaN; // return value
  204.         boolean first = true;
  205.         for (final EventDetector detector : detectors) {
  206.             if (first) {
  207.                 ret = detector.g(s);
  208.                 first = false;
  209.             } else {
  210.                 ret = operator.combine(ret, detector.g(s));
  211.             }
  212.         }
  213.         // return the result of applying the operator to all operands
  214.         return ret;
  215.     }

  216.     @Override
  217.     protected BooleanDetector create(final double newMaxCheck,
  218.                                      final double newThreshold,
  219.                                      final int newMaxIter,
  220.                                      final EventHandler<? super BooleanDetector> newHandler) {
  221.         return new BooleanDetector(detectors, operator, newMaxCheck, newThreshold,
  222.                 newMaxIter, newHandler);
  223.     }

  224.     @Override
  225.     public void init(final SpacecraftState s0,
  226.                      final AbsoluteDate t) {
  227.         super.init(s0, t);
  228.         for (final EventDetector detector : detectors) {
  229.             detector.init(s0, t);
  230.         }
  231.     }

  232.     /**
  233.      * Get the list of original detectors.
  234.      * @return the list of original detectors
  235.      * @since 10.2
  236.      */
  237.     public List<EventDetector> getDetectors() {
  238.         return new ArrayList<EventDetector>(detectors);
  239.     }

  240.     /** Local class for operator. */
  241.     private enum Operator {

  242.         /** And operator. */
  243.         AND() {

  244.             @Override
  245.             /** {@inheritDoc} */
  246.             public double combine(final double g1, final double g2) {
  247.                 return FastMath.min(g1, g2);
  248.             }

  249.         },

  250.         /** Or operator. */
  251.         OR() {

  252.             @Override
  253.             /** {@inheritDoc} */
  254.             public double combine(final double g1, final double g2) {
  255.                 return FastMath.max(g1, g2);
  256.             }

  257.         };

  258.         /** Combine two g functions evaluations.
  259.          * @param g1 first evaluation
  260.          * @param g2 second evaluation
  261.          * @return combined evaluation
  262.          */
  263.         public abstract double combine(double g1, double g2);

  264.     };

  265. }