The Knowledge Base

Each agent has a Knowledge Base where it stores its acquired knowledge items. The technology to store these items can be selected from a set of different prolog engines: SWI-Prolog, XSB, Flora2, Eclipse and SPARQL. SPADE does also support an internal Prolog engine, which is less powerful but has no external requirements. This is the SpadeKB engine, wich supports first-order logic.

Accessing the KB is really simple. As said, you can add knowledge, delete it, or make queries.

        #First configure the KB for using SWI-Prolog. The default KB is SpadeKB
        $ agent.configureKB("SWI", None, "swipl") #third argument is the path to the swipl executable

        #Insert some knowledge items
        $ agent.addBelieve( 'a(b,c)' )
        $ agent.addBelieve( 'a(c,d)' )
        $ agent.addBelieve( '( p(_X,_Y) :- a(_X,_Y) )' ) 
        $ agent.addBelieve( '( p(_X,_Y) :- a(_X,_Z), p(_Z,_Y) )' )

        #Ask for a Knowledge
        $ agent.askBelieve( 'p(X,Y)' )
        > [{'Y': 'c', 'X': 'b'}, {'Y': 'd', 'X': 'c'}, {'Y': 'd', 'X': 'b'}]

        #Deletes a Knowledge
        $ agent.removeBelieve( 'a(b,c)' )

        #Check deletion
        $ agent.askBelieve( 'p(X,Y)' )
        > [{'Y': 'd', 'X': 'c'}]

    
        #XSB
        $ agent.configureKB("XSB", None, "xsb") #third argument is the path to the xsb executable
        $ agent.addBelieve( 'a(b,c)' )
        $ agent.removeBelieve( 'a(c,d)' )
        $ agent.askBelieve( 'p(X,Y)' )

        #Flora2
        $ agent.configureKB("Flora2", None, "runflora") #third argument is the path to the flora2 executable
        $ agent.addBelieve( 'a[ b->c ]' ) #Beware, Flora2 notation is different
        $ agent.removeBelieve( 'a[ b->c ]' )
        $ agent.askBelieve( '?x[ ?y->?z ]' )

        #Eclipse
        $ agent.configureKB("ECLiPSe", None, "eclipse") #third argument is the path to the eclipse executable
        $ agent.addBelieve( 'a(b,c)' )
        $ agent.askBelieve( 'p(X,Y)' )

        #SPARQL
        $ agent.configureKB("SPARQL", sentence=None, path='http://dbpedia.org/sparql')
        $ sparql_query = '''
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                SELECT ?name
                WHERE { 
                <http://dbpedia.org/resource/Croatia> 
                rdfs:name 
                ?name 
                }'''
        $ result = agent.askBelieve(sparql_query)
        $ for item in result:
            assert item["name"] in ["Croatia","Republic of Croatia","Republika Hrvatska"]   

    

The SpadeKB is a FOL knowledge base, which is the default KB in SPADE. This KB stores believes in the same format as the rest of types of KBs, using addBelieve, removeBelieve and askBelieve. But the SpadeKB has also the ability of storing Facts in the KB. This is useful when you want to store variables with a name and a value. The types of value currently supported are: integer, string, float, list and dictionaries.

        $ agent.saveFact("MyFact", 1000) #parameters are name and value
        $ agent.getFact("MyFact")
        > 1000
        $ agent.askBelieve("Var(MyFact,1000,Int)")
        > True

        $ agent.saveFact("MyFloat",1.34)
        $ agent.getFact("MyFloat")
        > 1.34

        $ agent.saveFact("MyString","LoremIpsumAdHisScriptaBlanditPartiendo") #spaces still not supported, sorry
        $ agent.getFact("MyString")
        > 'LoremIpsumAdHisScriptaBlanditPartiendo'

        $ agent.saveFact("MyList",[5,6,7,8])
        $ agent.getFact("MyList")
        > [5,6,7,8]

        $ agent.saveFact("MyDict",{'a':123,'b':456,789:"c"})
        $ agent.getFact("MyDict")
        > {'a':123,'b':456,789:"c"}

        $ agent.saveFact("MyMixedFact",{'a':[123.25],'b':[4,5,6],789:{'a':1,'b':2}})
        $ agent.getFact("MyMixedFact")
        > {'a':[123.25],'b':[4,5,6],789:{'a':1,'b':2}}