예제 #1
0
 public StudentDecision decide(
     StudentView controlledStudent, StudentEnvironmentView environmentView) {
   // jesli juz dotarlismy do celu
   if (controlledStudent.getLocation().equals(target)) {
     logger.level1("[StudentRouteAlgorithm] Osiagnieto cel:" + target);
     // dotarlismy
     target = null;
   }
   // jesli nie mamy celu
   if (target == null) {
     // pytamy algorytm wewnetrzny o decyzje
     StudentDecision internalAlgorithmDecision =
         internalAlgorithm.decide(controlledStudent, environmentView);
     logger.level1("[StudentRouteAlgorithm] Wykonywanie rozkazu: " + internalAlgorithmDecision);
     if (internalAlgorithmDecision.getDecisionType() == StudentDecisionType.GO_TO) {
       // zapis nowego celu
       target = (Location) internalAlgorithmDecision.getArg0();
       logger.level1("[StudentRouteAlgorithm] Obrano nowy cel: " + target);
     } else {
       // podejmowanie innej decyzji
       return internalAlgorithmDecision;
     }
   }
   // dazenie do celu
   // jesli stoimy na celu
   if (controlledStudent.getLocation().equals(target)) {
     logger.level1("[StudentRouteAlgorithm] Osiagneto cel: " + target);
     // to nie robimy nic bo nie ma to sensu
     return new StudentDecision(StudentDecisionType.WAIT, null, null);
   }
   // tworzenie grafu
   Graph graph =
       new Graph(
           environmentView.getWidth(),
           environmentView.getHeight(),
           controlledStudent.getLocation(),
           environmentView.getStudentAreaMarkerViews());
   List<Location> path = graph.getPathToLocation(target);
   logger.level1("[StudentRouteAlgorithm] Wyznaczono sciezke: " + path);
   // interesuje nsa nastepny krok wiec
   Turn turn = Turn.getTurn(path.get(0), path.get(1), 1);
   // zwracanie rozkazu
   return new StudentDecision(StudentDecisionType.MOVE, turn, null);
 }
예제 #2
0
  public Decision decide(RobotView controlledRobot, RobotEnvironmentView environmentView) {

    // jesli aktualnie nic nie robimy
    if (currentOperation == null) {
      // pytamy algorytm wewnetrzny o decyzje
      Decision internalAlgorithmDecision =
          internalAlgorithm.decide(controlledRobot, environmentView);
      logger.level1("[DeliverAlgorithm] Wykonywanie rozkazu: " + internalAlgorithmDecision);
      switch (internalAlgorithmDecision.getDecisionType()) {
          // wyluskiwanie decyzji
        case DELIVER_TO_DESK:
        case DELIVER_TO_BOOKSHELF:
        case TAKE_FROM_DESK:
        case TAKE_FROM_BOOKSHELF:
          currentOperation = internalAlgorithmDecision;
          logger.level1("[DeliverAlgorithm] Wyznaczono nowa operacje: " + currentOperation);
          break;

          // jesli to zaden z obsluiwanych rozkazow, to przekazuje dalej
        default:
          return internalAlgorithmDecision;
      }
    }

    // wykonywanie operacji
    switch (currentOperation.getDecisionType()) {
        // przywozenie ksiazki
      case DELIVER_TO_DESK:
        {
          // wyluskiwanie argumentow
          int deskNumber = -1;
          if (currentOperation.getArg0() instanceof DeskView) {
            deskNumber = ((DeskView) currentOperation.getArg0()).getNumber();
          } else {
            deskNumber = (Integer) currentOperation.getArg0();
          }
          DeskView desk = environmentView.getDeskViewByNumber(deskNumber);
          int isbn = -1;
          if (currentOperation.getArg0() instanceof Book) {
            isbn = ((Book) currentOperation.getArg1()).getIsbn();
          } else {
            isbn = (Integer) currentOperation.getArg1();
          }

          // jesli jestesmy na wlasciwym miejscu
          if (desk.getRobotPadLocation().equals(controlledRobot.getLocation())) {
            logger.level1("[DeliverAlgorithm] Konczenie wykonywania operacji: " + currentOperation);
            currentOperation = null;
            // oddawanie ksiazki
            return new Decision(
                DecisionType.PUT,
                Turn.getTurn(controlledRobot.getLocation(), desk.getLocation(), 1),
                isbn);
          } else {
            return new Decision(DecisionType.GO_TO_DESK, desk);
          }
        }
        // odkladanie ksiazki
      case DELIVER_TO_BOOKSHELF:
        {
          // wyluskiwanie argumentow
          int isbn = -1;
          if (currentOperation.getArg0() instanceof Book) {
            isbn = ((Book) currentOperation.getArg0()).getIsbn();
          } else {
            isbn = (Integer) currentOperation.getArg0();
          }

          BookshelfView bookshelf = environmentView.getBookshelfViewByIsbn(isbn);
          // jesli jestesmy na wlasciwym miejscu
          if (bookshelf.getRobotPadLocation().equals(controlledRobot.getLocation())) {
            logger.level1("[DeliverAlgorithm] Konczenie wykonywania operacji: " + currentOperation);
            currentOperation = null;
            // oddawanie ksiazki
            return new Decision(
                DecisionType.PUT,
                Turn.getTurn(controlledRobot.getLocation(), bookshelf.getLocation(), 1),
                isbn);
          } else {
            return new Decision(DecisionType.GO_TO_BOOKSHELF, bookshelf);
          }
        }
        // pobieranie ksiazki z polki
      case TAKE_FROM_DESK:
        {
          // wyluskiwanie argumentow
          int deskNumber = -1;
          if (currentOperation.getArg0() instanceof DeskView) {
            deskNumber = ((DeskView) currentOperation.getArg0()).getNumber();
          } else {
            deskNumber = (Integer) currentOperation.getArg0();
          }
          DeskView desk = environmentView.getDeskViewByNumber(deskNumber);
          int isbn = -1;
          if (currentOperation.getArg0() instanceof Book) {
            isbn = ((Book) currentOperation.getArg1()).getIsbn();
          } else {
            isbn = (Integer) currentOperation.getArg1();
          }

          // jesli jestesmy na wlasciwym miejscu
          if (desk.getRobotPadLocation().equals(controlledRobot.getLocation())) {
            logger.level1("[DeliverAlgorithm] Konczenie wykonywania operacji: " + currentOperation);
            currentOperation = null;
            // oddawanie ksiazki
            return new Decision(
                DecisionType.TAKE,
                Turn.getTurn(controlledRobot.getLocation(), desk.getLocation(), 1),
                isbn);
          } else {
            return new Decision(DecisionType.GO_TO_DESK, desk);
          }
        }
        // pobieranie ksiazki z polki
      case TAKE_FROM_BOOKSHELF:
        {
          // wyluskiwanie argumentow
          int isbn = (Integer) currentOperation.getArg0();

          BookshelfView bookshelf = environmentView.getBookshelfViewByIsbn(isbn);
          // jesli jestesmy na wlasciwym miejscu
          if (bookshelf.getRobotPadLocation().equals(controlledRobot.getLocation())) {
            logger.level1("[DeliverAlgorithm] Konczenie wykonywania operacji: " + currentOperation);
            currentOperation = null;
            // oddawanie ksiazki
            return new Decision(
                DecisionType.TAKE,
                Turn.getTurn(controlledRobot.getLocation(), bookshelf.getLocation(), 1),
                isbn);
          } else {
            return new Decision(DecisionType.GO_TO_BOOKSHELF, bookshelf);
          }
        }
      default:
        throw new AlgorithmException("Ten blad nie powinien wystapic");
    }
  }