readme.md 2.59 KiB
Newer Older
Sergei Mikhailov's avatar
Sergei Mikhailov committed
# Project skeleton for Smart Space application

## How to install

0. _Use the virtualenv, Luke!_ (c) 
1. `pip install -r requirements.txt`
Sergei Mikhailov's avatar
Sergei Mikhailov committed


# Examples

See `examples/basics.py` for some working examples.
Sergei Mikhailov's avatar
Sergei Mikhailov committed

## Smart-M3 operations

`from smart_m3.m3_kp_api import *` - import all package data

### Triplets

`` Subject, Predicate, Object ``
`` Triple(URI(...), URI(...), URI(...)/Literal(...)) ``

```
Triple(
URI("http://www.ducatienergia.com/SIIP2P.owl#SensorData_829889475"), 
URI("http://www.ducatienergia.com/SIIP2P.owl#HasSensorDataValue"), 
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 and remove operations !!!
Sergei Mikhailov's avatar
Sergei Mikhailov committed
```

### Join

``kp = m3_kp_api(... options here ...)``

### Leave

`` kp.leave() ``


### Insert

```
kp.load_rdf_insert(Triple(...))
OR
kp.load_rdf_insert([Triple(...), Triple(...), ..., Triple(...)])
Sergei Mikhailov's avatar
Sergei Mikhailov committed
```

### Remove

```
kp.load_rdf_remove(Triple(...))
OR
kp.load_rdf_remove([Triple(...), Triple(...), ..., Triple(...)])
Sergei Mikhailov's avatar
Sergei Mikhailov committed

### 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')
```

### Query

```
### rdf-triples

## 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
Sergei Mikhailov's avatar
Sergei Mikhailov committed
for result in kp.result_rdf_query:
    print(result)
    
Sergei Mikhailov's avatar
Sergei Mikhailov committed
kp.load_query_sparql('....')
    for res in kp_test.result_sparql_query:
	    for result in res:
	        print(result)
```


### Subscribe / Unsubscribe

At first, you need to declare your handler class

```
class KP_Handler():
    def __init__(self, kp=None)
        self.kp = kp
    
    def handle(self, added, removed):
        """"
            added - list of information which was added
            removed - list of information which was removed
        """"
        
        for result in added:
            print(added)
            
        for result in removed:
            print(removed)
```

After that, you can create your subscription (e.g. which will listen all data):

```
    handler = KP_Handler(kp)
    rdf_subs_triple = Triple(None, None, None) # will listen ALL data
    rdf_subscription = kp.load_subscribe_RDF(rdf_subs_triple, handler) # handler will work in separate thread
    
    
    ### some inserting, pausing, and so on ###
    
    
    # leave smart space
    kp.load_unsubscribe(rdf_subscription)
    kp.leave()
```