kb module

class kb.EmbeddedKBClient(defaultontology=None)[source]
__init__(defaultontology=None)[source]
__module__ = 'kb'
call_server(method, *args, **kwargs)[source]
close()[source]
kb = None
kb_thread = None
kb_users = 0
process(defaultontology)[source]
sendmsg(msg)[source]
class kb.EventCallbackExecutor(in_event_queue, out_polled_event_queue, callback_queue)[source]

Bases: threading.Thread

__init__(in_event_queue, out_polled_event_queue, callback_queue)[source]
__module__ = 'kb'
close()[source]
run()[source]
class kb.KB(host='localhost', port=6969, embedded=False, defaultontology=None, sock=None)[source]
__contains__(pattern)[source]

This will return ‘True’ is either a concept - described by its ID or label- or a statement or a set of statement is present (or can be infered) in the ontology.

This allows syntax like:

if 'Toto' in kb:
    #...
if 'toto sees tata' in kb:
    #...
__del__()[source]
__enter__()[source]
__exit__(exc_type, exc_val, exc_tb)[source]
__getitem__(*args)[source]

This method introduces a different way of querying the ontology server. It uses the args (be it a string or a set of strings) to find concepts that match the pattern. An optional ‘models’ parameter can be given to specify the list of models the query is executed on.

Depending on the argument, 4 differents behaviours are possible:

  • with a string that can not be lexically split into 3 tokens (ie, a string that do not look like a s p o tuple), a lookup is performed, and matching resource are returned

  • with a single s p o pattern:
    • if only one of s, p, o is an unbound variable, returns the list of resources matching this pattern.
    • if 2 or 3 of the tokens are unbound variables (like kb["* * *"] or kb["* rdf:type *"]), a list of statements matching the pattern is returned.
  • with a list of patterns, a list of dictionaries is returned with possible combination of values for the different variables. For instance, kb[["?agent desires ?action", "?action rdf:type Jump"]] would return something like: [{"agent":"james", "action": "jumpHigh"}, {"agent": "laurel", "action":"jumpHigher"}]

Attention: if more than one argument is passed, and if the last argument is a list, this list is used as the set of models to execute the query on. If not such list is provided, the query is executed on all models.

Use example:

import kb

kb = KB()

for agent in kb["* rdf:type Agent"]:
    #...

if kb["* livesIn ?house", "?house isIn toulouse", ['GERALD']]:
    #...

#Assuming 'toulouse' has label "ville rose":
city_id = kb["ville rose"]
__iadd__(stmts)[source]

This method allows to easily add new statements to the ontology with the += operator. It can only add statement to the default robot’s model (other agents’ model are not accessible).

kb = KB(<host>, <port>)
kb += "toto likes icecream"
kb += ["toto loves tata", "tata rdf:type Robot"]
__init__(host='localhost', port=6969, embedded=False, defaultontology=None, sock=None)[source]
__isub__(stmts)[source]

This method allows to easily retract statements from the ontology with the -= operator. It can only add statement to the robot’s model (other agents’ model are not accessible). If a statement doesn’t exist, it is silently skipped.

kb = KB(<host>, <port>)
kb -= "toto likes icecream"
kb -= ["toto loves tata", "tata rdf:type Robot"]
__module__ = 'kb'
add_method(m)[source]
close()[source]
subscribe(pattern, callback=None, var=None, type='NEW_INSTANCE', trigger='ON_TRUE', models=None)[source]

Allows to subscribe to an event, and get notified when the event is triggered.

Example with callbacks:

>>> def onevent(evt):
>>>     print("In callback. Got evt %s" % evt)
>>>
>>> self.kb.subscribe(["?o isIn room"], onevent)
>>> self.kb += ["alfred isIn room"]
>>> # 'onevent' get called
In callback. Got evt [u'alfred']

Example with ‘polled’ events:

>>> evt_id = self.kb.subscribe(["?o isIn room"])
>>> self.kb += ["alfred isIn room"]
>>> print(str(self.kb.events.get()))
('evt_7694742461071211105', [u'alfred'])

If ‘callback’ is provided, the callback will be invoked with the result of the event (content depend on event type) in a separate thread. If not callback is provided, the incoming events are stored in the KB.events queue, and you can poll them yourself (which allow for better control of the execution flow).

The ‘var’ parameter can be used with the ‘NEW_INSTANCE’ type of event to tell which variable must be returned.

The ‘models’ parameter allows for registering an event in a specific list of models. By default, the pattern is monitored on every models.

Returns the event id of the newly created event.

exception kb.KbError(value)[source]

Bases: exceptions.Exception

__init__(value)[source]
__module__ = 'kb'
__str__()[source]
__weakref__

list of weak references to the object (if defined)

class kb.NullHandler(level=0)[source]

Bases: logging.Handler

Defines a NullHandler for logging, in case kb is used in an application that doesn’t use logging.

Initializes the instance - basically setting the formatter to None and the filter list to empty.

__module__ = 'kb'
emit(record)[source]
class kb.RemoteKBClient(event_queue, map, host='localhost', port=6969, sock=None)[source]

Bases: asynchat.async_chat

__init__(event_queue, map, host='localhost', port=6969, sock=None)[source]
__module__ = 'kb'
call_server(method, *args, **kwargs)[source]
collect_incoming_data(data)[source]
decode(raw)[source]
encode(method, *args, **kwargs)[source]
found_terminator()[source]
handle_error()[source]
initiate_send()[source]
use_encoding = 0