-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammars.lisp
116 lines (88 loc) · 3.33 KB
/
grammars.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; grammars.lisp --- more procedural generation hacks
;; Copyright (C) 2009 David O'Toole
;; Author: David O'Toole <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; http://en.wikipedia.org/wiki/Context-free_grammar
;;; Code:
(in-package :xe2)
(defparameter *test-grammar*
'((mission >> (at location please goal+ in exchange for reward))
(location >> mars zeta-base nebula-m corva-3)
(goal+ >> goal (goal and goal+))
(goal >> (defeat foe) (defend friend) (activate button) (retrieve documents)
(collect mineral+))
(mineral+ >> mineral (mineral and mineral+))
(mineral >> endurium technetium molybdenum francium a-biosilicates)
(foe >> scanner biclops unique)
(friend >> transport skiff soldier scientist)
(unique >> zx-90 xioblade)
(reward >> money part)
(money >> 10000 20000 30000 40000 50000)
(part >> muon-pistol lepton-cannon ion-shield-belt)))
(defvar *grammar* *test-grammar*
"The current context-free grammar used for sentence generation.
This is an association list of the form:
((VARIABLE >> EXPANSIONS)
(VARIABLE >> EXPANSIONS)
...)
Where EXPANSIONS is a list of alternatives, each of which may be
either (1) single symbols or (2) a list of symbols, representing
concatenation.")
(defun one-of (set)
(list (nth (random (length set)) set)))
(defun left-hand-side (rule)
(first rule))
(defun right-hand-side (rule)
(rest (rest rule)))
(defun expansions (variable)
(right-hand-side (assoc variable *grammar*)))
(defun generate (phrase)
"Generate a random phrase using the grammar in `*grammar*'."
(cond ((listp phrase)
(apply #'append (mapcar #'generate phrase)))
((expansions phrase)
(generate (one-of (expansions phrase))))
(t (list phrase))))
;; (generate 'mission)
;; examples:
;; (AT MARS PLEASE COLLECT A-BIOSILICATES IN EXCHANGE FOR MUON-PISTOL)
;; (AT NEBULA-M PLEASE DEFEAT XIOBLADE IN EXCHANGE FOR MUON-PISTOL)
;; (AT CORVA-3 PLEASE RETRIEVE DOCUMENTS AND ACTIVATE BUTTON IN EXCHANGE FOR
;; ION-SHIELD-BELT)
(defstruct goal
name
description
condition ;; either a symbol or a function
state ; one of nil, :achieved, :failed
prerequisites)
(defun achieved-p (goal)
(or (eq :achieved (goal-state goal))
(check-condition goal)))
(defun check-condition (goal)
(let ((condition (goal-condition goal))
(prerequisites (goal-prerequisites goal)))
(when (and (etypecase condition
(symbol (symbol-value condition))
(function (funcall condition)))
(or (null prerequisites)
(every #'achieved-p prerequisites)))
(setf (goal-state goal) :achieved))))
(define-prototype mission ()
name
grammar
description
world
goals)
;; TODO (defmacro defmission ()
;;; grammars.lisp ends here