コード例 #1
0
ファイル: MyArbitrator.java プロジェクト: wtcichon/SDP-8
  /**
   * This method starts the arbitration of behaviours and runs an endless loop. <br>
   * Note: Arbitrator does not run in a separate thread. The start() method will never return unless
   * <br>
   * 1. no action() method is running and <br>
   * 2. no behaviour takeControl() returns <B> true </B> and <br>
   * 3. the <i>returnWhenInacative </i> flag is true,
   */
  public void start() {
    //	  int i =0;
    monitor.start();

    while (_highestPriority == NONE) {
      Thread.yield(); // wait for some behaviour to take contro
    }
    while (true) {
      synchronized (monitor) {
        if (_highestPriority != NONE) {
          _active = _highestPriority;

        } else if (_returnWhenInactive) { // no behaviour wants to run
          monitor.more = false; // 9 shut down monitor thread
          return;
        }
      }

      // monotor released before action is called
      if (_active != NONE) // _highestPrioirty could be NONE
      {
        visCall = 0;
        _behaviour[_active].action();
        _active = NONE; // no active behaviour at the moment
      }
      Thread.yield();
      // i++;
      // System.out.println("i " + i);
      // if (i==10) return;
    }
  }
コード例 #2
0
ファイル: MyArbitrator.java プロジェクト: wtcichon/SDP-8
    public void run() {

      while (more) {

        // System.out.println("call V");
        MyArbitrator.objects = Launcher.findObjects();
        //	System.out.println(" Arbit ball "+
        // MyArbitrator.objects[2].getCentre().x+","+MyArbitrator.objects[2].getCentre().y);

        // System.out.println("ball " + MyArbitrator.objects[2].getCentre().x);
        visCall++;
        //	System.out.println("Vision Called" + visCall);

        //	System.out.println(" mon while" + monitorw);
        //  ObjectItem[] objects = Launcher.findObjects();
        //  System.out.println("call V");
        // FIND HIGHEST PRIORITY behaviour THAT WANTS CONTROL
        synchronized (this) {
          _highestPriority = NONE;
          for (int i = maxPriority; i >= 0; i--) {
            if (MyArbitrator._behaviour[i].takeControl(MyArbitrator.objects)) {

              _highestPriority = i;
              break;
            }
          }

          int active = _active; // local copy: avoid out of bounds error in 134
          if (active != NONE && _highestPriority > active) {
            MyArbitrator._behaviour[active].suppress();
          }
        } // end synchronize block - main thread can run now
        Thread.yield();
      }
    }