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.errors.OrekitException;
  25. import org.orekit.propagation.SpacecraftState;
  26. import org.orekit.propagation.events.handlers.ContinueOnEvent;
  27. import org.orekit.propagation.events.handlers.EventHandler;
  28. import org.orekit.time.AbsoluteDate;

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

  58.     /** Serializable UID. */
  59.     private static final long serialVersionUID = 20170410L;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  244.         },

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

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

  252.         };

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

  259.     };

  260. }