""" goal.py
Copyright (C) 1998 Aloril
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Goal is to make prolog like language at higher level.
Now: goal has function that checks whether goal is fulfilled.
If it's not fullfilled, then do this and this and ... to fulfill it.
'This' can be function or subgoal.
Things to add in near future: 'real' prolog style variables and 'or' metarule.
"""
from types import *
from event import *
class goal:
def __init__(self,desc="some goal",fulfilled=None,subgoals=[],
time=None, debug=0):
self.desc=desc
#mind sets these:
#self.str
#self.key
if fulfilled: self.fulfilled=fulfilled
else: self.fulfilled=lambda :0 #false
self.subgoals=subgoals[:]
self.time=time
self.debug=debug
self.vars=[]
def info(self):
name=self.__class__.__name__
if name=="goal":
return name+"("+`self.desc`+")"
var=""
for v in self.vars:
if var: var=var+","
var=var+`getattr(self,v)`
return name+"("+var+")"
def check_goal(self, me, time):
"executes goal, see top of file"
res,deb=self.check_goal_rec(me,time,0)
if res!=None:
return res+[event("goal",what=res,what_desc=self.info()+"."+deb)]
def check_goal_rec(self, me, time, depth):
res,deb=None,""
#is it right time range?
if self.time and self.time!=time: return res,deb
if self.debug:
print "\t"*depth+"GOAL: bef fulfilled:",self.desc,self.fulfilled
if self.fulfilled(me): return res,deb
for sg in self.subgoals:
if type(sg)==FunctionType or type(sg)==MethodType:
res=sg(me)
if res!=None:
deb=sg.__name__+"()"
return res,deb
if self.debug: print "\t"*depth+"GOAL: function:",sg,res
else:
if self.debug: print "\t"*depth+"GOAL: bef sg:",sg.desc
res,deb=sg.check_goal_rec(me,time,depth+1)
if self.debug: print "\t"*depth+"GOAL: aft sg:",sg.desc,res
if res!=None:
deb=sg.info()+"."+deb
return res,deb
return res,deb
               (
geocities.com/siliconvalley/station/4279)                   (
geocities.com/siliconvalley/station)                   (
geocities.com/siliconvalley)