BooleanDetector.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.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.     /** Serializable UID. */
  58.     private static final long serialVersionUID = 20170410L;

  59.     /** Original detectors: the operands. */
  60.     private final List<EventDetector> detectors;

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

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

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

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

  126.         return new BooleanDetector(new ArrayList<>(detectors), // copy for immutability
  127.                 Operator.AND,
  128.                 detectors.stream().map(EventDetector::getMaxCheckInterval).min(Double::compareTo).get(),
  129.                 detectors.stream().map(EventDetector::getThreshold).min(Double::compareTo).get(),
  130.                 detectors.stream().map(EventDetector::getMaxIterationCount).min(Integer::compareTo).get(),
  131.                 new ContinueOnEvent<>());
  132.     }

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

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

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

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

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

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

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

  234.     /** Local, serializable class for operator. */
  235.     private enum Operator {

  236.         /** And operator. */
  237.         AND() {

  238.             @Override
  239.             /** {@inheritDoc} */
  240.             public double combine(final double g1, final double g2) {
  241.                 return FastMath.min(g1, g2);
  242.             }

  243.         },

  244.         /** Or operator. */
  245.         OR() {

  246.             @Override
  247.             /** {@inheritDoc} */
  248.             public double combine(final double g1, final double g2) {
  249.                 return FastMath.max(g1, g2);
  250.             }

  251.         };

  252.         /** Combine two g functions evaluations.
  253.          * @param g1 first evaluation
  254.          * @param g2 second evaluation
  255.          * @return combined evaluation
  256.          */
  257.         public abstract double combine(double g1, double g2);

  258.     };

  259. }