examples/ggp-wam-rover.lisp @ 55019395ba93

Normalize facts and queries because people are lazy
author Steve Losh <steve@stevelosh.com>
date Wed, 06 Jul 2016 20:41:56 +0000
parents 802872f9505a
children (none)
(in-package #:bones.wam)

(declaim (optimize (speed 3) (debug 0) (safety 0)))

(defparameter *d* (make-database))

(with-database *d*
  (rules ((member :thing (list* :thing :rest)))
         ((member :thing (list* :other :rest))
          (member :thing :rest)))

  (rule (true :state :thing)
    (member :thing :state))

  (rule (does :performed :role :move)
    (member (does :role :move) :performed))

  (rules ((not :x) (call :x) ! fail)
         ((not :x)))

  (fact (role solver))

  (facts (init (thing empty_rover0store))
         (init (thing available_rover0))
         (init (thing at_rover0_waypoint3))
         (init (thing channel_free_general))
         (init (thing at_rock_sample_waypoint3))
         (init (thing at_soil_sample_waypoint3))
         (init (thing at_rock_sample_waypoint2))
         (init (thing at_soil_sample_waypoint2))
         (init (thing at_rock_sample_waypoint1))
         (init (thing at_soil_sample_waypoint0))
         (init (step num1)))

  (rule (solved :state)
    (true :state (thing communicated_image_data_objective1_high_res))
    (true :state (thing communicated_rock_data_waypoint3))
    (true :state (thing communicated_soil_data_waypoint2)))

  (rules ((terminal :state)
          (true :state (thing communicated_image_data_objective1_high_res))
          (true :state (thing communicated_rock_data_waypoint3))
          (true :state (thing communicated_soil_data_waypoint2)))
         ((terminal :state)
          (true :state (step num30))))

  (rules
    ((goal :state solver num100)
     (true :state (thing communicated_image_data_objective1_high_res))
     (true :state (thing communicated_rock_data_waypoint3))
     (true :state (thing communicated_soil_data_waypoint2)))
    ((goal :state solver num0)
     (not (solved :state))))

  (rules ((legal :state solver navigate_rover0_waypoint0_waypoint3)
          (true :state (thing at_rover0_waypoint0))
          (true :state (thing available_rover0)))
         ((legal :state solver navigate_rover0_waypoint1_waypoint3)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing available_rover0)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_high_res_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_high_res))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_colour_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_colour))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_high_res_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_high_res))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_colour_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_colour))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_high_res_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_high_res))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_colour_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_colour))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_high_res_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_high_res))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_colour_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_colour))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_high_res_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_high_res))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_image_data_rover0_general_objective1_colour_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective1_colour))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_high_res_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_high_res))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_image_data_rover0_general_objective0_colour_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_image_rover0_objective0_colour))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint3_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint2_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint1_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint1))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint3_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint2_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint1_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint1))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint3_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint2_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_rock_data_rover0_general_waypoint1_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_rock_analysis_rover0_waypoint1))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint3_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint2_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint0_waypoint1_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint0))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint3_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint2_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint0_waypoint2_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint0))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint3_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint3))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint2_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint2))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver communicate_soil_data_rover0_general_waypoint0_waypoint3_waypoint0)
          (true :state (thing channel_free_general))
          (true :state (thing available_rover0))
          (true :state (thing have_soil_analysis_rover0_waypoint0))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver take_image_rover0_waypoint0_objective0_camera0_colour)
          (true :state (thing at_rover0_waypoint0))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint0_objective0_camera0_high_res)
          (true :state (thing at_rover0_waypoint0))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint1_objective0_camera0_colour)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint1_objective0_camera0_high_res)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint2_objective0_camera0_colour)
          (true :state (thing at_rover0_waypoint2))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint2_objective0_camera0_high_res)
          (true :state (thing at_rover0_waypoint2))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint3_objective0_camera0_colour)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint3_objective0_camera0_high_res)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint0_objective1_camera0_colour)
          (true :state (thing at_rover0_waypoint0))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint0_objective1_camera0_high_res)
          (true :state (thing at_rover0_waypoint0))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint1_objective1_camera0_colour)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint1_objective1_camera0_high_res)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint2_objective1_camera0_colour)
          (true :state (thing at_rover0_waypoint2))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint2_objective1_camera0_high_res)
          (true :state (thing at_rover0_waypoint2))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint3_objective1_camera0_colour)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver take_image_rover0_waypoint3_objective1_camera0_high_res)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing calibrated_camera0_rover0)))
         ((legal :state solver calibrate_rover0_camera0_objective1_waypoint0)
          (true :state (thing at_rover0_waypoint0)))
         ((legal :state solver calibrate_rover0_camera0_objective1_waypoint1)
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver calibrate_rover0_camera0_objective1_waypoint2)
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver calibrate_rover0_camera0_objective1_waypoint3)
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver drop_rover0_rover0store)
          (true :state (thing full_rover0store)))
         ((legal :state solver sample_rock_rover0_rover0store_waypoint3)
          (true :state (thing empty_rover0store))
          (true :state (thing at_rock_sample_waypoint3))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver sample_rock_rover0_rover0store_waypoint2)
          (true :state (thing empty_rover0store))
          (true :state (thing at_rock_sample_waypoint2))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver sample_rock_rover0_rover0store_waypoint1)
          (true :state (thing empty_rover0store))
          (true :state (thing at_rock_sample_waypoint1))
          (true :state (thing at_rover0_waypoint1)))
         ((legal :state solver sample_soil_rover0_rover0store_waypoint3)
          (true :state (thing empty_rover0store))
          (true :state (thing at_soil_sample_waypoint3))
          (true :state (thing at_rover0_waypoint3)))
         ((legal :state solver sample_soil_rover0_rover0store_waypoint2)
          (true :state (thing empty_rover0store))
          (true :state (thing at_soil_sample_waypoint2))
          (true :state (thing at_rover0_waypoint2)))
         ((legal :state solver sample_soil_rover0_rover0store_waypoint0)
          (true :state (thing empty_rover0store))
          (true :state (thing at_soil_sample_waypoint0))
          (true :state (thing at_rover0_waypoint0)))
         ((legal :state solver navigate_rover0_waypoint2_waypoint1)
          (true :state (thing at_rover0_waypoint2))
          (true :state (thing available_rover0)))
         ((legal :state solver navigate_rover0_waypoint1_waypoint2)
          (true :state (thing at_rover0_waypoint1))
          (true :state (thing available_rover0)))
         ((legal :state solver navigate_rover0_waypoint3_waypoint0)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing available_rover0)))
         ((legal :state solver navigate_rover0_waypoint3_waypoint1)
          (true :state (thing at_rover0_waypoint3))
          (true :state (thing available_rover0)))
         ((legal :state solver noop)))


  (facts (addeffect calibrate_rover0_camera0_objective1_waypoint0 (thing calibrated_camera0_rover0))
         (addeffect calibrate_rover0_camera0_objective1_waypoint1 (thing calibrated_camera0_rover0))
         (addeffect calibrate_rover0_camera0_objective1_waypoint2 (thing calibrated_camera0_rover0))
         (addeffect calibrate_rover0_camera0_objective1_waypoint3 (thing calibrated_camera0_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint1_waypoint0 (thing communicated_image_data_objective0_colour))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint2_waypoint0 (thing communicated_image_data_objective0_colour))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_colour_waypoint3_waypoint0 (thing communicated_image_data_objective0_colour))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint1_waypoint0 (thing communicated_image_data_objective0_high_res))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint2_waypoint0 (thing communicated_image_data_objective0_high_res))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective0_high_res_waypoint3_waypoint0 (thing communicated_image_data_objective0_high_res))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint1_waypoint0 (thing communicated_image_data_objective1_colour))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint2_waypoint0 (thing communicated_image_data_objective1_colour))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_colour_waypoint3_waypoint0 (thing communicated_image_data_objective1_colour))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint1_waypoint0 (thing communicated_image_data_objective1_high_res))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint2_waypoint0 (thing communicated_image_data_objective1_high_res))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_image_data_rover0_general_objective1_high_res_waypoint3_waypoint0 (thing communicated_image_data_objective1_high_res))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint1_waypoint0 (thing communicated_rock_data_waypoint1))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint2_waypoint0 (thing communicated_rock_data_waypoint1))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint1_waypoint3_waypoint0 (thing communicated_rock_data_waypoint1))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing communicated_rock_data_waypoint2))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing communicated_rock_data_waypoint2))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing communicated_rock_data_waypoint2))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing communicated_rock_data_waypoint3))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing communicated_rock_data_waypoint3))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_rock_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing communicated_rock_data_waypoint3))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint1_waypoint0 (thing communicated_soil_data_waypoint0))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint2_waypoint0 (thing communicated_soil_data_waypoint0))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint0_waypoint3_waypoint0 (thing communicated_soil_data_waypoint0))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint1_waypoint0 (thing communicated_soil_data_waypoint2))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint2_waypoint0 (thing communicated_soil_data_waypoint2))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint2_waypoint3_waypoint0 (thing communicated_soil_data_waypoint2))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint1_waypoint0 (thing communicated_soil_data_waypoint3))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint2_waypoint0 (thing communicated_soil_data_waypoint3))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing available_rover0))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing channel_free_general))
         (addeffect communicate_soil_data_rover0_general_waypoint3_waypoint3_waypoint0 (thing communicated_soil_data_waypoint3))
         (addeffect drop_rover0_rover0store (thing empty_rover0store))
         (addeffect navigate_rover0_waypoint0_waypoint3 (thing at_rover0_waypoint3))
         (addeffect navigate_rover0_waypoint1_waypoint2 (thing at_rover0_waypoint2))
         (addeffect navigate_rover0_waypoint1_waypoint3 (thing at_rover0_waypoint3))
         (addeffect navigate_rover0_waypoint2_waypoint1 (thing at_rover0_waypoint1))
         (addeffect navigate_rover0_waypoint3_waypoint0 (thing at_rover0_waypoint0))
         (addeffect navigate_rover0_waypoint3_waypoint1 (thing at_rover0_waypoint1))
         (addeffect sample_rock_rover0_rover0store_waypoint1 (thing full_rover0store))
         (addeffect sample_rock_rover0_rover0store_waypoint1 (thing have_rock_analysis_rover0_waypoint1))
         (addeffect sample_rock_rover0_rover0store_waypoint2 (thing full_rover0store))
         (addeffect sample_rock_rover0_rover0store_waypoint2 (thing have_rock_analysis_rover0_waypoint2))
         (addeffect sample_rock_rover0_rover0store_waypoint3 (thing full_rover0store))
         (addeffect sample_rock_rover0_rover0store_waypoint3 (thing have_rock_analysis_rover0_waypoint3))
         (addeffect sample_soil_rover0_rover0store_waypoint0 (thing full_rover0store))
         (addeffect sample_soil_rover0_rover0store_waypoint0 (thing have_soil_analysis_rover0_waypoint0))
         (addeffect sample_soil_rover0_rover0store_waypoint2 (thing full_rover0store))
         (addeffect sample_soil_rover0_rover0store_waypoint2 (thing have_soil_analysis_rover0_waypoint2))
         (addeffect sample_soil_rover0_rover0store_waypoint3 (thing full_rover0store))
         (addeffect sample_soil_rover0_rover0store_waypoint3 (thing have_soil_analysis_rover0_waypoint3))
         (addeffect take_image_rover0_waypoint0_objective0_camera0_colour (thing have_image_rover0_objective0_colour))
         (addeffect take_image_rover0_waypoint0_objective0_camera0_high_res (thing have_image_rover0_objective0_high_res))
         (addeffect take_image_rover0_waypoint0_objective1_camera0_colour (thing have_image_rover0_objective1_colour))
         (addeffect take_image_rover0_waypoint0_objective1_camera0_high_res (thing have_image_rover0_objective1_high_res))
         (addeffect take_image_rover0_waypoint1_objective0_camera0_colour (thing have_image_rover0_objective0_colour))
         (addeffect take_image_rover0_waypoint1_objective0_camera0_high_res (thing have_image_rover0_objective0_high_res))
         (addeffect take_image_rover0_waypoint1_objective1_camera0_colour (thing have_image_rover0_objective1_colour))
         (addeffect take_image_rover0_waypoint1_objective1_camera0_high_res (thing have_image_rover0_objective1_high_res))
         (addeffect take_image_rover0_waypoint2_objective0_camera0_colour (thing have_image_rover0_objective0_colour))
         (addeffect take_image_rover0_waypoint2_objective0_camera0_high_res (thing have_image_rover0_objective0_high_res))
         (addeffect take_image_rover0_waypoint2_objective1_camera0_colour (thing have_image_rover0_objective1_colour))
         (addeffect take_image_rover0_waypoint2_objective1_camera0_high_res (thing have_image_rover0_objective1_high_res))
         (addeffect take_image_rover0_waypoint3_objective0_camera0_colour (thing have_image_rover0_objective0_colour))
         (addeffect take_image_rover0_waypoint3_objective0_camera0_high_res (thing have_image_rover0_objective0_high_res))
         (addeffect take_image_rover0_waypoint3_objective1_camera0_colour (thing have_image_rover0_objective1_colour))
         (addeffect take_image_rover0_waypoint3_objective1_camera0_high_res (thing have_image_rover0_objective1_high_res)))

  (facts (deleteeffect drop_rover0_rover0store (thing full_rover0store))
         (deleteeffect navigate_rover0_waypoint0_waypoint3 (thing at_rover0_waypoint0))
         (deleteeffect navigate_rover0_waypoint1_waypoint2 (thing at_rover0_waypoint1))
         (deleteeffect navigate_rover0_waypoint1_waypoint3 (thing at_rover0_waypoint1))
         (deleteeffect navigate_rover0_waypoint2_waypoint1 (thing at_rover0_waypoint2))
         (deleteeffect navigate_rover0_waypoint3_waypoint0 (thing at_rover0_waypoint3))
         (deleteeffect navigate_rover0_waypoint3_waypoint1 (thing at_rover0_waypoint3))
         (deleteeffect sample_rock_rover0_rover0store_waypoint1 (thing at_rock_sample_waypoint1))
         (deleteeffect sample_rock_rover0_rover0store_waypoint1 (thing empty_rover0store))
         (deleteeffect sample_rock_rover0_rover0store_waypoint2 (thing at_rock_sample_waypoint2))
         (deleteeffect sample_rock_rover0_rover0store_waypoint2 (thing empty_rover0store))
         (deleteeffect sample_rock_rover0_rover0store_waypoint3 (thing at_rock_sample_waypoint3))
         (deleteeffect sample_rock_rover0_rover0store_waypoint3 (thing empty_rover0store))
         (deleteeffect sample_soil_rover0_rover0store_waypoint0 (thing at_soil_sample_waypoint0))
         (deleteeffect sample_soil_rover0_rover0store_waypoint0 (thing empty_rover0store))
         (deleteeffect sample_soil_rover0_rover0store_waypoint2 (thing at_soil_sample_waypoint2))
         (deleteeffect sample_soil_rover0_rover0store_waypoint2 (thing empty_rover0store))
         (deleteeffect sample_soil_rover0_rover0store_waypoint3 (thing at_soil_sample_waypoint3))
         (deleteeffect sample_soil_rover0_rover0store_waypoint3 (thing empty_rover0store))
         (deleteeffect take_image_rover0_waypoint0_objective0_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint0_objective0_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint0_objective1_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint0_objective1_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint1_objective0_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint1_objective0_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint1_objective1_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint1_objective1_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint2_objective0_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint2_objective0_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint2_objective1_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint2_objective1_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint3_objective0_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint3_objective0_camera0_high_res (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint3_objective1_camera0_colour (thing calibrated_camera0_rover0))
         (deleteeffect take_image_rover0_waypoint3_objective1_camera0_high_res (thing calibrated_camera0_rover0)))

  (rules
    ((next :state :performed (thing :pred))
     (does :performed solver :action)
     (true :state (thing :pred))
     (not (deleteeffect :action (thing :pred))))

    ((next :state :performed (thing :pred))
     (does :performed solver :action)
     (addeffect :action (thing :pred)))

    ((next :state :performed (step :y))
     (true :state (step :x))
     (succ :x :y)))

  (facts (succ num1 num2)
         (succ num2 num3)
         (succ num3 num4)
         (succ num4 num5)
         (succ num5 num6)
         (succ num6 num7)
         (succ num7 num8)
         (succ num8 num9)
         (succ num9 num10)
         (succ num10 num11)
         (succ num11 num12)
         (succ num12 num13)
         (succ num13 num14)
         (succ num14 num15)
         (succ num15 num16)
         (succ num16 num17)
         (succ num17 num18)
         (succ num18 num19)
         (succ num19 num20)
         (succ num20 num21)
         (succ num21 num22)
         (succ num22 num23)
         (succ num23 num24)
         (succ num24 num25)
         (succ num25 num26)
         (succ num26 num27)
         (succ num27 num28)
         (succ num28 num29)
         (succ num29 num30)
         (succ num30 num31))
  )


(defun extract (key results)
  (mapcar (lambda (result) (getf result key)) results))

(defun to-prolog-list (l)
  (if (null l)
    nil
    (list* 'list l)))

(defun initial-state ()
  (to-prolog-list
    (with-database *d*
      (extract :what (return-all (init :what))))))

(defun terminalp (state)
  (with-database *d*
    (perform-prove `((terminal ,state)))))

(defun legal-moves (state)
  (with-database *d*
    (perform-return `((legal ,state :role :move)) :all)))

(defun roles ()
  (with-database *d*
    (extract :role (return-all (role :role)))))

(defun goal-value (state role)
  (with-database *d*
    (getf (perform-return `((goal ,state ,role :goal)) :one) :goal)))

(defun goal-values (state)
  (with-database *d*
    (perform-return `((goal ,state :role :goal)) :all)))

(defun next-state (current-state move)
  (let ((does `(list (does
                       ,(getf move :role)
                       ,(getf move :move)))))
    (with-database *d*
      (to-prolog-list
        (extract :what
                 (perform-return `((next ,current-state ,does :what)) :all))))))



(defvar *count* 0)

(defstruct search-path state (path nil) (previous nil))

(defun tree-search (states goal-p children combine)
  (labels
      ((recur (states)
         (if (null states)
           nil
           (destructuring-bind (state . remaining) states
             (incf *count*)
             (when (zerop (rem *count* 1000))
               (format t "~D...~%" *count*))
             ; (format t "Searching: ~S (~D remaining)~%"
             ;         state
             ;         (length remaining))
             (if (funcall goal-p state)
               state
               (recur (funcall combine
                               (funcall children state)
                               remaining)))))))
    (let ((result (recur states)))
      (when result
        (reverse (search-path-path result))))))


(defun game-goal-p (search-path)
  (let ((state (search-path-state search-path)))
    (and (terminalp state)
         (eql (goal-value state 'player) 'num100))))

(defun game-children (search-path)
  (let ((state (search-path-state search-path))
        (path (search-path-path search-path)))
    (when (not (terminalp state))
      (loop :for move :in (legal-moves state)
            :collect (make-search-path :state (next-state state move)
                                       :path (cons move path)
                                       :previous search-path)))))

(defun never (&rest args)
  (declare (ignore args))
  nil)

(defun dfs ()
  (let ((*count* 0))
    (tree-search (list (make-search-path :state (initial-state)))
               #'game-goal-p
               #'game-children
               #'append)))

(defun dfs-exhaust ()
  (let ((*count* 0))
    (prog1
        (tree-search (list (make-search-path :state (initial-state)))
                     #'never
                     #'game-children
                     #'append)
      (format t "Searched ~D nodes.~%" *count*))))

(defun bfs ()
  (tree-search (list (make-search-path :state (initial-state)))
               #'game-goal-p
               #'game-children
               (lambda (x y)
                 (append y x))))