The Classical Solution

This version of the Fibonacci Number Generator is viewed as the “classic” version because it can be implemented in basic AgentSpeak(L), while the other versions cannot.

  package examples.fibonnacci;
  
  agent Fib {
      module System system;
      module Console console;

      initial fib(1l,1l);
      initial fib(2l,1l);

      rule +!main(list args) {
          long S = system.currentTimeMillis();
          !fib(3l, 50l);
          console.println("Duration: " + (system.currentTimeMillis() - S));
          system.terminate();
      }

      rule +!fib(long X, long N) : 
              fib(X-1, long A) & fib(X-2, long B) &
              X < N {
          +fib(X, A+B);
          !fib(X+1, N);
      }
  
      rule +!fib(long X, long N) :
              fib(X-1, long A) & fib(X-2, long B) & 
              X == N {
          +fib(X, A+B);
          console.println("FINAL fib("+X+")=" + 
                  (A+B));
      }
  }

The program consists for two initial beliefs to represent the 1st and 2nd fibonacci numbers. The first rule runs the test program. This program records the start time, creates a goal to compute the first 50 fibonacci numbers, and then displays the duration of the operation. The remaining two rules deal with the case where X < N and X == N respectively.