[spade2] r2241 committed - Solved some issues with the new KBs module. Import exceptions are now ...

15 views
Skip to first unread message

spa...@googlecode.com

unread,
Jul 11, 2012, 7:19:12 AM7/11/12
to spade-...@googlegroups.com
Revision: 2241
Author: jpalanca
Date: Wed Jul 11 04:18:54 2012
Log: Solved some issues with the new KBs module. Import exceptions are
now catched. Unit tests fail if executable is not available. Also for the
SPARQLWrapper module. Removed custom paths.
http://code.google.com/p/spade2/source/detail?r=2241

Modified:
/trunk/spade/bdi.py
/trunk/spade/pyxf.py
/trunk/test/kbTestCase.py

=======================================
--- /trunk/spade/bdi.py Tue Jul 10 16:56:02 2012
+++ /trunk/spade/bdi.py Wed Jul 11 04:18:54 2012
@@ -5,16 +5,14 @@
import Agent
import Behaviour

-import SPARQLKB
-import XSBKB
-import Flora2KB
-import SWIKB
-import ECLiPSeKB
-
-
class PreConditionFailed (Exception): pass
class PostConditionFailed(Exception): pass
class ServiceFailed(Exception): pass
+class KBConfigurationFailed(Exception):
+ def __init__(self, msg):
+ self.msg = msg
+ def __str__(self):
+ return repr(self.msg)

class Goal:

@@ -155,9 +153,19 @@
self._needDeliberate = True

def configureKB(self, typ, sentence=None, path=None):
- kbs = ["ECLiPSe", "Flora2", "SPARQL", "SWI", "XSB"]
+ """
+ Supported Knowledge Bases are:
["ECLiPSe", "Flora2", "SPARQL", "SWI", "XSB"]
+ """
try:
- if not (typ in kbs): raise Exception
+ if typ not in ["ECLiPSe", "Flora2", "SPARQL", "SWI", "XSB"]:
+ raise KBConfigurationFailed(typ + " is not a valid KB.")
+ if typ=="SPARQL": import SPARQLKB
+ elif typ=="XSB": import XSBKB
+ elif typ=="Flora2": import Flora2KB
+ elif typ=="SWI": import SWIKB
+ elif typ=="ECLiPSe":import ECLiPSeKB
+ else: raise KBConfigurationFailed("Could not
import "+str(typ)+" KB.")
+
typ+="KB"
#module = eval("__import__("+typ+")")

@@ -165,28 +173,36 @@
self.kb =
eval(typ+"."+typ+"("+str(sentence)+", '"+path+"')")
else:
self.kb = eval(typ+"."+typ+"("+str(sentence)+")")
- except Exception, e:
- print str(e)
- self.DEBUG("Could not use " + typ + "KB. Using Fol
KB.", 'warn')
+ except KBConfigurationFailed as e:
+ self.DEBUG(str(e)+" Using Fol KB.", 'warn')
self.kb = FolKB()


def addBelieve(self, sentence, type="insert"):
- if isinstance(sentence,types.StringType) and issubclass(FolKB,
self.kb.__class__):
- self.kb.tell(expr(sentence))
- elif issubclass(Flora2KB.Flora2KB,self.kb.__class__):
- self.kb.tell(sentence,type)
-
- else:
+ if isinstance(sentence,types.StringType):
+ try:
+ if issubclass(Flora2KB.Flora2KB,self.kb.__class__):
+ self.kb.tell(sentence,type)
+ except:
+ if issubclass(FolKB, self.kb.__class__):
+ self.kb.tell(expr(sentence))
+ else:
+ self.kb.tell(sentence)
+ else:
self.kb.tell(sentence)
self._needDeliberate = True
self.newBelieveCB(sentence)

def removeBelieve(self, sentence, type="delete"):
- if isinstance(sentence,types.StringType) and issubclass(FolKB,
self.kb.__class__):
- self.kb.retract(expr(sentence))
- elif issubclass(Flora2KB.Flora2KB,self.kb.__class__):
- self.kb.retract(sentence,type)
+ if isinstance(sentence,types.StringType):
+ try:
+ if issubclass(Flora2KB.Flora2KB,self.kb.__class__):
+ self.kb.retract(sentence,type)
+ except:
+ if issubclass(FolKB, self.kb.__class__):
+ self.kb.retract(expr(sentence))
+ else:
+ self.kb.retract(sentence)
else:
self.kb.retract(sentence)
self._needDeliberate = True
=======================================
--- /trunk/spade/pyxf.py Tue Jul 10 16:56:02 2012
+++ /trunk/spade/pyxf.py Wed Jul 11 04:18:54 2012
@@ -378,7 +378,7 @@
self.engine.expect( flora2prompt )
self.engine.expect( flora2prompt )
except px.ExceptionPexpect:
- raise SWIExecutableNotFound, 'Flora-2 executable not found on the
specified path. Try using flora2( "/path/to/flora2/runflora" )'
+ raise Flora2ExecutableNotFound, 'Flora-2 executable not found on the
specified path. Try using flora2( "/path/to/flora2/runflora" )'

def load( self, module ):
'''Loads module into self.engine
=======================================
--- /trunk/test/kbTestCase.py Tue Jul 10 16:56:52 2012
+++ /trunk/test/kbTestCase.py Wed Jul 11 04:18:54 2012
@@ -5,7 +5,7 @@

import spade
from spade.bdi import *
-from spade import SPARQLKB, ECLiPSeKB, Flora2KB, SWIKB, XSBKB
+from spade.pyxf import ECLiPSeExecutableNotFound,
Flora2ExecutableNotFound, SWIExecutableNotFound, XSBExecutableNotFound
host = "127.0.0.1"

sparql1='''
@@ -42,42 +42,61 @@
pass

def testSPARQLopenlink(self):
- self.a.configureKB("SPARQL", sentence=None,
path='http://lod.openlinksw.com/sparql')
- for result in self.a.askBelieve(sparql1):
- assert result["p"] in ["0.5", "0.55", "0.75", "0.85"]
- assert result["c"] == "GBP"
+
+ try:
+ from spade import SPARQLKB
+ except:
+ self.fail("Could not import SPARQLKB. Try installing
SPARQLWrapper (sudo easy_install SPARQLWrapper)")
+
+ self.a.configureKB("SPARQL", sentence=None,
path='http://lod.openlinksw.com/sparql')
+ for result in self.a.askBelieve(sparql1):
+ assert result["p"] in ["0.5", "0.55", "0.75", "0.85"]
+ assert result["c"] == "GBP"

def testSPARQLdbpedia(self):
- self.a.configureKB("SPARQL", sentence=None,
path='http://dbpedia.org/sparql')
- for result in self.a.askBelieve(sparql2):
- assert result["name"] in ["Croatia","Republic of Croatia","Republika
Hrvatska"]
+ try:
+ from spade import SPARQLKB
+ except:
+ self.fail("Could not import SPARQLKB. Try installing
SPARQLWrapper (sudo easy_install SPARQLWrapper)")
+
+ self.a.configureKB("SPARQL", sentence=None,
path='http://dbpedia.org/sparql')
+ for result in self.a.askBelieve(sparql2):
+ assert result["name"] in ["Croatia","Republic of
Croatia","Republika Hrvatska"]

def testEclipse(self):
-
- path="/Users/jpalanca/Downloads/eclipse/bin/x86_64_macosx/eclipse"
- if not os.path.exists(path): path="eclipse"
-
- self.a.configureKB("ECLiPSe", None, path)
-
- if not issubclass(self.a.kb.__class__,ECLiPSeKB.ECLiPSeKB):
- self.fail("Could not find ECLiPSe.")
-
- self.a.addBelieve( 'a(b,c)' )
- self.a.addBelieve( 'a(c,d)' )
- self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Y) )' )
- self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Z), p(_Z,_Y) )' )
-
- for result in self.a.askBelieve( 'p(X,Y)' ):
+ try:
+ from spade import ECLiPSeKB
+ except:
+ self.fail("Could not import ECLiPSeKB")
+
+ try:
+ self.a.configureKB("ECLiPSe", None, "eclipse")
+ except ECLiPSeExecutableNotFound:
+ self.fail('ECLiPSe Prolog executable not found on the specified
path.')
+
+ if not issubclass(self.a.kb.__class__,ECLiPSeKB.ECLiPSeKB):
+ self.fail("ECLiPSe was not properly configured.")
+
+ self.a.addBelieve( 'a(b,c)' )
+ self.a.addBelieve( 'a(c,d)' )
+ self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Y) )' )
+ self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Z), p(_Z,_Y) )' )
+
+ for result in self.a.askBelieve( 'p(X,Y)' ):
assert result in
[{'Y': 'c', 'X': 'b'},{'Y': 'd', 'X': 'c'},{'Y': 'd', 'X': 'b'}]

def testFlora2(self):
- path="/Users/jpalanca/Downloads/flora/XSBFLORABUNDLE/flora2/runflora"
- if not os.path.exists(path): path="eclipse"
-
- self.a.configureKB("Flora2", None, path)
+ try:
+ from spade import Flora2KB
+ except:
+ self.fail("Could not import Flora2KB")
+ try:
+ self.a.configureKB("Flora2", None, "runflora")
+ except Flora2ExecutableNotFound:
+ self.fail('Flora-2 executable not found on the specified path. Try
installing flora2')

if not issubclass(self.a.kb.__class__,Flora2KB.Flora2KB):
- self.fail("Could not find Flora2.")
+ self.fail("Flora2 was not properly configured.")

self.a.addBelieve( 'a[ b->c ]' )
self.a.addBelieve( '( ?x[ c->?y ] :- ?x[ b->?y ] )', 'insertrule' )
@@ -90,34 +109,48 @@
assert len(result)==0

def testSWI(self):
- path="swipl"
-
- self.a.configureKB("SWI", None, path)
+ try:
+ from spade import SWIKB
+ except:
+ self.fail("Could not import SWIKB")
+
+ try:
+ self.a.configureKB("SWI", None, "swipl")
+ except SWIExecutableNotFound:
+ self.fail('SWI-Prolog executable not found on the specified path.
Try installing swi-prolog.')

if not issubclass(self.a.kb.__class__,SWIKB.SWIKB):
- self.fail("Could not find SWI pl.")
+ self.fail("SWI was not properly configured.")

self.a.addBelieve( 'a(b,c)' )
self.a.addBelieve( 'a(c,d)' )
self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Y) )' )
self.a.addBelieve( '( p(_X,_Y) :- a(_X,_Z), p(_Z,_Y) )' )

- self.assertFalse(self.a.askBelieve( 'p(X,Y)' ))
+ for result in self.a.askBelieve( 'p(X,Y)' ):
+ assert result in [{'Y': 'c', 'X': 'b'}, {'Y': 'd', 'X': 'c'},
{'Y': 'd', 'X': 'b'}]

self.a.removeBelieve( 'a(b,c)' )
- self.assertFalse(self.a.askBelieve( 'p(X,Y)' ))
+ for result in self.a.askBelieve( 'p(X,Y)' ):
+ assert result in [{'Y': 'd', 'X': 'c'}]

self.a.removeBelieve( 'a(c,d)' )
- self.assertTrue(self.a.askBelieve( 'p(X,Y)' ))
+ self.assertFalse(self.a.askBelieve( 'p(X,Y)' ))
+

def testXSB(self):
- path="/Users/jpalanca/Downloads/XSB/XSB/bin/xsb"
- if not os.path.exists(path): path="xsb"
-
- self.a.configureKB("XSB", None, path)
+ try:
+ from spade import XSBKB
+ except:
+ self.fail("Could not import XSBKB")
+
+ try:
+ self.a.configureKB("XSB", None, "xsb")
+ except XSBExecutableNotFound:
+ self.fail('XSB executable not found on the specified path. Try
installing xsb')

if not issubclass(self.a.kb.__class__,XSBKB.XSBKB):
- self.fail("Could not find XSB.")
+ self.fail("XSB was not properly configured.")

self.a.addBelieve( 'a(b,c)' )
self.a.addBelieve( 'a(c,d)' )
Reply all
Reply to author
Forward
0 new messages