from phidias.Types  import *
from phidias.Main import *
from phidias.Lib import *

import random
import math

class last_tower(SingletonBelief): pass
class tower(Belief): pass
class new_cube(Procedure): pass
class show_tower(Procedure): pass
class find_top(Procedure): pass

def_vars('X','N','Next', 'Bottom', 'NLast', 'Any')

new_cube(X) / last_tower(0) >> \
  [
      +last_tower(1),
      +tower(1, 'bottom', X),
      show_line("creazione torre 1")
  ]
new_cube(X) >> [ find_top(1, X) ]

find_top(N, X) / tower(N, Any, Next) >> [ find_top(N, X, Next) ]


find_top(N, X, Bottom) / tower(N, Bottom, Next) >> \
  [
      find_top(N, X, Next)
  ]

find_top(N, X, Bottom) / lt(Bottom, X) >> \
  [
      show_line("trovata torre ", N),
      +tower(N, Bottom, X)
  ]

find_top(N, X, Bottom) / (last_tower(NLast) & lt(N, NLast)) >> \
  [
      "N = N + 1",
       find_top(N, X)
  ]

find_top(N, X, Bottom) / last_tower(NLast) >> \
  [
      "NLast = NLast + 1",
      +last_tower(NLast),
      +tower(NLast, 'bottom', X),
      show_line("creazione torre ", NLast)
  ]

show_tower(N) / tower(N, 'bottom', X) >> \
  [
      show_line("Torre ", N, " - Cubo ", X),
      show_tower(N, X)
  ]
show_tower(N, X) / tower(N, X, Next) >> \
  [
      show_line("Torre ", N, " - Cubo ", Next),
      show_tower(N, Next)
  ]
show_tower(N, X) >> [ show_line("Fine Torre ", N) ]


PHIDIAS.assert_belief(last_tower(0))
# instantiate the engine
PHIDIAS.run()
# run the engine shell
PHIDIAS.shell(globals())

