コード例 #1
0
ファイル: StatePatternDemo.java プロジェクト: heqingbao/Java
  public static void main(String[] args) {
    Context context = new Context();

    StartState startState = new StartState();
    startState.doAction(context);

    System.out.println(context.getState().toString());

    StopState stopState = new StopState();
    stopState.doAction(context);

    System.out.println(context.getState().toString());
  }
コード例 #2
0
  public static NullStates getNullState(Context context) {
    final VehicleState state = context.getState();
    // final Observation obs = context.getObservation();
    final BlockStateObservation blockStateObs = state.getBlockStateObservation();
    EVehiclePhase phase = state.getJourneyState().getPhase();

    if (blockStateObs == null) {
      return NullStates.NULL_STATE;
    } else {

      final boolean hasScheduledTime =
          FastMath.abs(state.getBlockStateObservation().getScheduleDeviation()) > 0d;

      if (!hasScheduledTime) {
        return NullStates.NULL_STATE;
      }

      if (!state.getBlockStateObservation().isSnapped()
          && ((EVehiclePhase.DEADHEAD_AFTER == phase
                  && state.getBlockStateObservation().getScheduleDeviation() == 0d)
              || EVehiclePhase.AT_BASE == phase
              || (EVehiclePhase.DEADHEAD_BEFORE == phase
                  && state.getBlockStateObservation().getScheduleDeviation() == 0d)
              || (EVehiclePhase.LAYOVER_BEFORE == phase)
                  && state.getBlockStateObservation().getScheduleDeviation() == 0d)) {
        return NullStates.NULL_STATE;
      }

      return NullStates.NON_NULL_STATE;
    }
  }
コード例 #3
0
ファイル: MEncoder.java プロジェクト: AndreiSochirca/jcodec
  /**
   * Encodes one bin in normal mode using supplied context model
   *
   * @param bin
   * @param cm
   * @throws IOException
   */
  public void encodeBin(int bin, Context cm) throws IOException {

    int qs = (range >> 6) & 0x3;
    int rangeLPS = MConst.rangeLPS[qs][cm.getState()];
    range -= rangeLPS;

    if (bin != cm.getMps()) {
      offset += range;
      range = rangeLPS;
      if (cm.getState() == 0) cm.setMps(1 - cm.getMps());

      cm.setState(MConst.transitLPS[cm.getState()]);
    } else {
      if (cm.getState() < 62) cm.setState(cm.getState() + 1);
    }

    renormalize();
  }
コード例 #4
0
  public RUN_TRANSITION_STATE getRunTransitionState(Context context) {
    final VehicleState state = context.getState();
    final VehicleState parentState = context.getParentState();
    final Observation obs = context.getObservation();
    final Observation prevObs = obs.getPreviousObservation();
    final BlockStateObservation blockStateObs = state.getBlockStateObservation();

    if (parentState != null
        && prevObs != null
        && MotionModelImpl.hasRunChanged(
            parentState.getBlockStateObservation(), state.getBlockStateObservation())) {
      if (!Objects.equal(obs.getRecord().getRunId(), prevObs.getRecord().getRunId())
          || !Objects.equal(obs.getRecord().getOperatorId(), prevObs.getRecord().getOperatorId())) {

        return RUN_TRANSITION_STATE.RUN_CHANGE_INFO_DIFF;
      } else {
        EVehiclePhase parentPhase = parentState.getJourneyState().getPhase();

        /*
         * TODO clean up this hack We are really in-progress, but because of the
         * out-of-service headsign, we can't report it as in-progress
         */
        if (context.getObservation().hasOutOfServiceDsc()
            && EVehiclePhase.DEADHEAD_DURING == parentPhase
            && (blockStateObs != null && blockStateObs.isOnTrip()))
          parentPhase = EVehiclePhase.IN_PROGRESS;

        if (EVehiclePhase.isLayover(parentPhase)
            || EVehiclePhase.DEADHEAD_AFTER == parentPhase
            || EVehiclePhase.DEADHEAD_BEFORE == parentPhase
            || EVehiclePhase.DEADHEAD_DURING == parentPhase) {
          if (parentState.getBlockStateObservation() != null
              && !parentState.getBlockStateObservation().isRunFormal()
              && state.getJourneyState().getPhase() == EVehiclePhase.IN_PROGRESS)
            return RUN_TRANSITION_STATE.RUN_CHANGE_FROM_OOS_NORUN_TO_IN;
          else if (state.getJourneyState().getPhase() == EVehiclePhase.IN_PROGRESS)
            return RUN_TRANSITION_STATE.RUN_CHANGE_FROM_OOS_TO_IN;
          else return RUN_TRANSITION_STATE.RUN_CHANGE_FROM_OOS_TO_OSS;
        } else {
          return RUN_TRANSITION_STATE.RUN_CHANGE_FROM_IS;
        }
      }
    } else {
      if (blockStateObs == null
          && parentState != null
          && parentState.getBlockStateObservation() == null)
        return RUN_TRANSITION_STATE.RUN_CHANGE_FROM_OOS_TO_OSS;
      else return RUN_TRANSITION_STATE.RUN_NOT_CHANGED;
    }
  }
コード例 #5
0
  @Override
  public SensorModelResult likelihood(SensorModelSupportLibrary library, Context context) {

    VehicleState parentState = context.getParentState();
    VehicleState state = context.getState();
    Observation obs = context.getObservation();

    JourneyState js = state.getJourneyState();
    EVehiclePhase phase = js.getPhase();
    BlockState blockState = state.getBlockState();

    /** These probabilities only apply if are IN_PROGRESS and have a block state */
    if (phase != EVehiclePhase.IN_PROGRESS || blockState == null)
      return new SensorModelResult("pInProgress (n/a)");

    SensorModelResult result = new SensorModelResult("pInProgress");

    /** Rule: IN_PROGRESS => block location is close to gps location */
    double pBlockLocation = library.computeBlockLocationProbability(parentState, blockState, obs);
    result.addResultAsAnd("pBlockLocation", pBlockLocation);

    return result;
  }
コード例 #6
0
  public DSC_STATE getDscState(final Context context) {
    final VehicleState state = context.getState();
    final Observation obs = context.getObservation();

    final JourneyState js = state.getJourneyState();
    EVehiclePhase phase = js.getPhase();

    final String observedDsc = obs.getLastValidDestinationSignCode();

    if (observedDsc == null || !obs.hasValidDsc() || obs.hasOutOfServiceDsc()) {
      /** If we haven't yet seen a valid DSC, or it's out of service */
      if (!obs.hasValidDsc()) {
        return DSC_STATE.DSC_NOT_VALID;
      } else if (EVehiclePhase.IN_PROGRESS == phase && obs.hasOutOfServiceDsc()) {
        return DSC_STATE.DSC_OOS_IP;
      } else {
        return DSC_STATE.DSC_OOS_NOT_IP;
      }
    } else {
      final BlockState bs = state.getBlockState();
      if (bs == null) {
        return DSC_STATE.DSC_IS_NO_BLOCK;
      } else {
        final Set<String> dscs = Sets.newHashSet();
        dscs.add(bs.getDestinationSignCode());
        final Set<AgencyAndId> routes = Sets.newHashSet();
        routes.add(bs.getBlockLocation().getActiveTrip().getTrip().getRouteCollection().getId());

        /*
         * dsc changes occur between parts of a block, so account for that
         */
        if (EVehiclePhase.LAYOVER_DURING == phase || EVehiclePhase.DEADHEAD_DURING == phase) {
          final BlockTripEntry nextTrip = bs.getBlockLocation().getActiveTrip().getNextTrip();

          if (nextTrip != null) {
            final String dsc =
                _destinationSignCodeService.getDestinationSignCodeForTripId(
                    nextTrip.getTrip().getId());
            if (dsc != null) dscs.add(dsc);
            routes.add(nextTrip.getTrip().getRouteCollection().getId());
          }
        }

        /*
         * Check if it's active, since deadhead-after/before's
         * can/should have o.o.s. dsc's, although they often don't.
         * TODO perhaps check the last non-o.o.s. dsc to give
         * higher weight to deadhead-after's that match (when
         * we have good run-info, perhaps).
         */
        if (!EVehiclePhase.isActiveAfterBlock(phase)
            && !EVehiclePhase.isActiveBeforeBlock(phase)
            && dscs.contains(observedDsc)) {
          if (EVehiclePhase.IN_PROGRESS == phase) return DSC_STATE.DSC_MATCH;
          else return DSC_STATE.DSC_DEADHEAD_MATCH;
        } else {
          /*
           * a dsc implies a route. even though the reported dsc may not match,
           * we expect the general route to be the same...
           */
          boolean routeMatch = false;
          for (final AgencyAndId dscRoute : obs.getDscImpliedRouteCollections()) {
            if (routes.contains(dscRoute)) {
              routeMatch = true;
              break;
            }
          }

          if (routeMatch) return DSC_STATE.DSC_ROUTE_MATCH;
          else return DSC_STATE.DSC_NO_ROUTE_MATCH;
        }
      }
    }
  }
コード例 #7
0
  @Override
  public SensorModelResult likelihood(Context context)
      throws BadProbabilityParticleFilterException {

    final VehicleState state = context.getState();
    final VehicleState parentState = context.getParentState();
    final Observation obs = context.getObservation();
    final EVehiclePhase phase = state.getJourneyState().getPhase();
    final BlockStateObservation blockStateObs = state.getBlockStateObservation();

    //    /*-
    //     * TODO clean up this hack
    //     * We are really in-progress, but because of the
    //     * out-of-service headsign, we can't report it as in-progress
    //     */
    //    if (obs.hasOutOfServiceDsc() && EVehiclePhase.DEADHEAD_DURING == phase
    //        && (blockStateObs != null && blockStateObs.isOnTrip()))
    //      phase = EVehiclePhase.IN_PROGRESS;

    final SensorModelResult result = new SensorModelResult("pEdge", 1d);

    if (obs.getPreviousObservation() == null || parentState == null) {
      if (EVehiclePhase.isActiveDuringBlock(phase)) {
        result.addResultAsAnd("no prev. obs./vehicle-state(in-progress)", 1d);
      } else {
        result.addResultAsAnd("no prev. obs./vehicle-state", 1d);
      }
      return result;
    }

    if (obs.isAtBase()) {
      result.addResultAsAnd("pNotInProgress(base)", 1d);
      return result;
    }

    if (blockStateObs == null) {

      result.addResultAsAnd(computeNoEdgeMovementLogProb(state, obs));
      return result;
    }

    /*
     * Edge Movement
     */
    final boolean previouslyInactive = parentState.getBlockState() == null;
    final boolean newRun =
        MotionModelImpl.hasRunChanged(
            parentState.getBlockStateObservation(), state.getBlockStateObservation());

    final boolean hasMoved = !state.getMotionState().hasVehicleNotMoved();
    if (!previouslyInactive && !newRun) {

      /*
       * This could be a transition from being on a trip geom to off, and vice
       * versa.
       */
      if (EVehiclePhase.IN_PROGRESS != phase) {
        /*
         * We're currently not in-progress
         */
        if (EVehiclePhase.isActiveDuringBlock(phase)) {
          if (state
              .getBlockState()
              .getBlockInstance()
              .equals(parentState.getBlockState().getBlockInstance())) {
            result.addResultAsAnd(
                computeEdgeMovementLogProb(obs, state, parentState, hasMoved, true));
          } else {
            result.addResultAsAnd(computeNoEdgeMovementLogProb(state, parentState, obs));
          }
        } else {
          result.addResultAsAnd(computeNoEdgeMovementLogProb(state, obs));
        }
      } else if (EVehiclePhase.IN_PROGRESS != parentState.getJourneyState().getPhase()) {
        /*
         * We were previously not in-progress, and now we are.
         */
        if (EVehiclePhase.isActiveDuringBlock(parentState.getJourneyState().getPhase())) {
          result.addResultAsAnd(
              computeEdgeMovementLogProb(obs, state, parentState, hasMoved, true));
        } else {
          result.addResultAsAnd(computeNoEdgeMovementLogProb(state, parentState, obs));
        }
      } else {
        /*
         * We're in-progress and were before.
         */
        result.addResultAsAnd(computeEdgeMovementLogProb(obs, state, parentState, hasMoved, false));
      }

    } else {
      /*
       * We have a new run, or just got one after having nothing.
       */

      if (EVehiclePhase.IN_PROGRESS == phase) {
        if (EVehiclePhase.isActiveDuringBlock(parentState.getJourneyState().getPhase())) {
          result.addResultAsAnd(
              computeEdgeMovementLogProb(obs, state, parentState, hasMoved, false));
        } else {
          result.addResultAsAnd(computeNoEdgeMovementLogProb(state, parentState, obs));
        }

      } else {
        result.addResultAsAnd(computeNoEdgeMovementLogProb(state, obs));
      }
    }

    return result;
  }