#!/usr/bin/python
# -*- coding: utf-8 -*-

#foaf http://xmlns.com/foaf/spec/index.rdf

from smart_m3.m3_kp import *

# connect to SS
node = KP("KP2_que")
ss_handle = ("X", (TCPConnector, ("127.0.0.1", 10010)))

if not node.join(ss_handle):
    sys.exit('Could not join to Smart Space')


print "KP2_que querying ..."
qs = node.CreateQueryTransaction(ss_handle)

try:
    result = qs.rdf_query([
	  Triple(URI("Saint-Petersburg"),URI("is a"),URI("Nothern capital")),
	  Triple(URI("Russia"),URI("has a town"), URI("Saint-Petersburg")),
	  Triple(URI("Nothern capital"),URI("of"),URI("Russia"))])
    for item in result:
        print "QUERY: Got triple(s): ", item
except M3Exception:
    print "RDF query failed:", M3Exception

node.CloseQueryTransaction(qs)

#print "Left smart space"
node.leave(ss_handle)

