diff --git a/examples/basics.py b/examples/basics.py new file mode 100644 index 0000000000000000000000000000000000000000..8d042e74566b47ca4bd9eeedf9bc7288f7b07da0 --- /dev/null +++ b/examples/basics.py @@ -0,0 +1,102 @@ +import os +import random +from datetime import datetime + +from smart_m3.m3_kp_api import * + + +class KP_Handler: + def __init__(self, kp=None): + self.kp = kp + + def handle(self, added, removed): + # in case if you want to react on added/removed data - just use self.kp.your_function(....) here + print('Agent_X reporting: {}'.format(datetime.now())) + print(' added', added) + print(' removed', removed) + + for data in added: + if str(data[1]) == 'trash_value': + print("!!! KP_sub_reaction !!! Garbage in the smart space :(") + break + + for data in removed: + if str(data[1]) == 'trash_value': + print("!!! KP_sub_reaction !!! Some garbage was removed! :)") + break + + +if __name__ == '__main__': + kp = m3_kp_api() # connection to the smart space, PrintDebug=True parameter will print debug messages + + # this condition means that kp will listen all data in the smart space + subscription_triple = Triple(None, None, None) + handler = KP_Handler(kp) + # this is how we create subscription at certain triple pattern in the smart space + handler_subscription = kp.load_subscribe_RDF(subscription_triple, handler) + + insert_list = [ + Triple(URI('Agent_X'), URI('has_temperature'), Literal(10)), + Triple(URI('Agent_X'), URI('wind_speed_measurement'), Literal(2.34)), + Triple(URI('Agent_X'), URI('wind_direction'), Literal('SW')), + Triple(URI('Agent_X'), URI('send_data'), URI('Agent_Y')), + ] + + # insert example + print('---Insert example---') + kp.load_rdf_insert(insert_list) + + # query example - agent try to get information about Object=URI('Agent_Y') + print('---Query example---') + kp.load_query_rdf(Triple(None, None, URI('Agent_Y'))) + print('Query result about Agent_Y in the smart space: {}'.format(kp.result_rdf_query)) + + # update example - the weather became warmer - so Agent_X need to update information about it + # 1. Search for the weather triplet from Agent_X + # 2. If we successfully find data - replace it! + print('---Update example---') + + kp.load_query_rdf(Triple(URI('Agent_X'), URI('has_temperature'), None)) + if len(kp.result_rdf_query) > 0: + new_triples = [] + for old_triple in kp.result_rdf_query: + new_triples.append(Triple(old_triple[0], old_triple[1], Literal(random.randint(-10, 20)))) + + # N.B! If you want to update only one triple - you still need to make wrapping around array + # kp.load_rdf_update([Triple(...)], [Triple(...)]) + kp.load_rdf_update(new_triples, kp.result_rdf_query) + + # remove example + # Create 'trash' triplets for Agent_X and Agent_Y and delete Agent_X 'trash' triples afterwards + # also, if kp meets 'trash_value' predicate - he will send information about it + print('---Remove example---') + + trash_triples = [] + for i in range(0, 10): + trash_triples.append(Triple(URI('Agent_X') if i % 2 == 0 else URI('Agent_Y'), URI('trash_value'), + Literal(random.randint(-10, 20)))) + + # insert and remove + kp.load_rdf_insert(trash_triples) + kp.load_rdf_remove(Triple(URI('Agent_X'), URI('trash_value'), None)) + + # check 'trash' triples which are left in the smart space + kp.load_query_rdf(Triple(None, URI('trash_value'), None)) + print('Trash triples in the smart space: {}'.format(kp.result_rdf_query)) + + # unsubscribe from any data from the smart space + kp.load_unsubscribe(handler_subscription) + + # remove all data from Smart Space by applying Triplet(None, None, None) + kp.clean_sib() + + # if you want to see how subscription handler show information about sib cleaning, + # you need to add sleep(...) function BEFORE kp.leave(), because leave will cut down connection with broker + + # sleep(5) # main thread will sleep 5 seconds and subscription thread will be able to do something + + kp.leave() + + # Unfortunately, subscription threads will work after kp.leave and block main program. + # So, it's okay just to send 0 signal manually + raise os._exit(0) diff --git a/main.py b/main.py index ed092a67c52ea30ccc9062451a9e9dff8b627f6f..32c1b60757a67f01d0df3101d00f838689a54286 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ from smart_m3.m3_kp_api import * if __name__ == '__main__': + # basic program, which connects to the smart space and clears it kp = m3_kp_api(PrintDebug=True) kp.clean_sib() # remove all data from Smart Space kp.leave() diff --git a/readme.md b/readme.md index 5b2f27be11f6e15f32b81411c6eb59459ad6cf9e..c5006bfefdb607de4b2e444d46758c0cf652f5a0 100644 --- a/readme.md +++ b/readme.md @@ -3,9 +3,12 @@ ## How to install 0. _Use the virtualenv, Luke!_ (c) -1. `pip install -r requitements.txt` +1. `pip install -r requirements.txt` +# Examples + +See `examples/basics.py` for some working examples. ## Smart-M3 operations @@ -25,7 +28,7 @@ Literal(10)) Triple( URI("http://www.ducatienergia.com/SIIP2P.owl#SensorData_829889475"), URI("http://www.ducatienergia.com/SIIP2P.owl#HasSensorDataValue"), -None) # only for query/subscription !!! +None) # only for query/subscription and remove operations !!! ``` ### Join @@ -40,13 +43,17 @@ None) # only for query/subscription !!! ### Insert ``` -kp.load_rdf_insert(Triple | List[Triple]) +kp.load_rdf_insert(Triple(...)) +OR +kp.load_rdf_insert([Triple(...), Triple(...), ..., Triple(...)]) ``` ### Remove ``` -kp.load_rdf_remove(Triple | List[Triple]) +kp.load_rdf_remove(Triple(...)) +OR +kp.load_rdf_remove([Triple(...), Triple(...), ..., Triple(...)]) ### Also it is possible to use NONE (wildcard) for ingnoring some on the triplet parts ### Triple(URI('test'), None, None) will remove all triplets, where subject == URI('test') @@ -56,11 +63,19 @@ kp.load_rdf_remove(Triple | List[Triple]) ``` ### rdf-triples -kp.load_query_rdf(Triple(...)) # may contain wildcards ! + +## Query triples may contain wildcards ! + +kp.load_query_rdf(Triple(...)) +OR +kp.load_query_rdf([Triple(...), Triple(...), ..., Triple(...)]) # answer will include union of all results + +## Query result retrieveng for result in kp.result_rdf_query: print(result) -### sparql + +### sparql example kp.load_query_sparql('....') for res in kp_test.result_sparql_query: for result in res: