Java KPI for Smart M3 Brief introduction Fabio Viola University of Bologna Master Degrees in computer and electronic engineering October 1, 2014 Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Introduction The present document is a brief introduction to the Java KPI, libraries needed to access a Smart-M3 Smart Space. In this tutorial Eclipse is the environment taken into account. Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Importing the libraries Once a new Java project has been created, it must be configured to load the required libraries. Right click on the project name and then click on Properties. In the Java Build Path section, Libraries tab, a click on the Add External JARs... button allows to load the files sofia kp.jar and jdom-2.0.5.jar. Now the libraries must be imported into the classes in the following way: import import import import import import import java . sofia sofia sofia sofia sofia sofia u t i l . Vector ; k p . KPICore ; k p . SIBResponse ; k p . SSAP XMLTools ; kp . SSAP sparql response ; kp . iKPIC subscribeHandler ; kp . iKPIC subscribeHandler2 ; Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Creating a KP To create a new KP, a new object of class KPICore must be instantiated. KPICore mykp = new KPICore ( s i b i p , s i b p o r t , s s n a m e ) ; where: sib ip is a String containing the IP address (or hostname) of the server running the SIB; sib port is an Integer containing the port number of the SIB; ss name is a String representing the name of the smart space. It is useful to declare a SIBResponse object to check the response of the SIB to the future commands: SIBResponse r e s p = n u l l ; Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Join To join a smart space simply invoke the join method of the instantiated object: r e s p = mykp . j o i n ( ) ; i f (! resp . isConfirmed () ) { System . e r r . p r i n t l n ( ” E r r o r j o i n i n g t h e SIB ” ) ; System . e x i t ( 0 ) ; } Introduction Preliminary steps Joining/Leaving a SS CRUD operations Leave Leaving a smart space is as easy as joining it: r e s p = mykp . l e a v e ( ) ; i f (! resp . isConfirmed () ) { System . e r r . p r i n t l n ( ” E r r o r l e a v i n g t h e SIB ” ) ; System . e x i t ( 0 ) ; } Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Defining triples // d e c l a r e a l i s t o f t r i p l e s V e c t o r<V e c t o r<S t r i n g >> t r i p l e s = new V e c t o r<V e c t o r<S t r i n g >>() ; // d e c l a r e a t r i p l e V e c t o r<S t r i n g > t r i p l e = new V e c t o r<S t r i n g >() ; // d e c l a r i n g t h e f i e l d s S t r i n g s u b j e c t = ” h t t p : / / n s#s u b j e c t ” ; S t r i n g p r e d i c a t e = ” h t t p : / / n s#p r e d i c a t e ” ; S t r i n g o b j e c t = ” h t t p : / / n s#o b j e c t ” ; // f i l l t h e t r i p l e t r i p l e . add ( s u b j e c t ) ; t r i p l e . add ( p r e d i c a t e ) ; t r i p l e . add ( o b j e c t ) ; t r i p l e . add ( ” u r i ” ) ; t r i p l e . add ( ” u r i ” ) ; // add t h e t r i p l e t o t h e t r i p l e s . add ( t r i p l e ) ; list Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Insertion Now that a list of triples has been defined, the insertion can be done invoking the method insert of the mykp object. r e s p = mykp . i n s e r t ( t r i p l e s ) ; i f (! resp . isConfirmed () ) { System . e r r . p r i n t l n ( ” I n s e r t f a i l e d ! ” ) ; } else { System . o u t . p r i n t l n ( ” I n s e r t s u c c e s s f u l ! ” ) ; } The insert method can also be invoked with the following parameters: subject (String); predicate (String); object (String); subject type ("uri" or "literal"); object type ("uri" or "literal"). Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Removal In the same way of the insert: r e s p = mykp . remove ( t r i p l e s ) ; i f ( ! r e s p . i s C o n f i r m e d ( ) ){ System . o u t . p r i n t l n ( ”Remove e r r o r ! ” ) ; } else { System . o u t . p r i n t l n ( ”Remove s u c c e s s f u l l ” ) ; } Even the remove method can be invoked specifying subject, predicate, object, subject type and object type. Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Update The update method accept a Vector of Vector of String to express the new triples and another one for the old triples. r e s p = mykp . u p d a t e ( n e w t r i p l e s , old triples ); Alternatively it takes ten arguments: the new subject (String); the new predicate (String); the new object (String); the type of the new subject (a String that can be "uri" or "literal"); the type of the new object (a String that can be "uri" or "literal"); the old subject (String); the old predicate (String); the old object (String); the type of the old subject (a String that can be "uri" or "literal"); the type of the old object (a String that can be "uri" or "literal"); An example: r e s p = mykp . u p d a t e ( s u b j e c t 2 , p r e d i c a t e 2 , o b j e c t 2 , ” u r i ” , ” u r i ” , subject1 , predicate1 , object1 , ” u r i ” , ” u r i ”) ; And then the usual check on the resp variable can be done. Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions RDF Query In the following listing, the method queryRDF is used. Its arguments are: the subject (String or null to match everything); the predicate (String or null to match everything); the object (String or null to match everything); the type of the subject ("uri" or "literal"); the type of the object ("uri" or "literal"); // e x e c u t e t h e q u e r y r e s p = mykp . queryRDF ( s u b j e c t , p r e d i c a t e , o b j e c t , ” u r i ” , ” u r i ” ) ; // p r i n t t h e r e s u l t V e c t o r<V e c t o r<S t r i n g >> q u e r y r e s p o n s e = r e s p . q u e r y r e s u l t s ; f o r ( V e c t o r<S t r i n g > t : q u e r y r e s p o n s e ) { System . o u t . p r i n t l n ( t ) ; } Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links SPARQL Query The following listings shows how to query the SIB with the SPARQL language: // D e f i n e t h e q u e r y S t r i n g q u e r y = ”SELECT ? s ? p ? o WHERE { ? s ? p ? o }” ; // E x e c u t e t h e q u e r y r e s p = mykp . querySPARQL ( q u e r y ) ; // Check t h e r e s u l t s i f ( ! r e s p . i s C o n f i r m e d ( ) ){ System . o u t . p r i n t l n ( ”SPARQL Query e r r o r ! ” ) ; } else { // Get t h e r e s u l t s V e c t o r<S t r i n g [] > row = n u l l ; SSAP sparql response sparql query response = resp . s p a r q l q u e r y r e s u l t s ; // C y c l e t h r o u g h t h e r e s u l t s w h i l e ( s p a r q l q u e r y r e s p o n s e . hasNext ( ) ){ s p a r q l q u e r y r e s p o n s e . next () ; row = s p a r q l q u e r y r e s p o n s e . getRow ( ) ; f o r ( S t r i n g [ ] r : row ) { System . o u t . p r i n t l n ( r [ 0 ] + ” , ” + r [ 1 ] + ” , ” + r [ 2 ] ) ; } } } Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Subscribe With a subscription, a program receives a notification for each triple that matches a pattern or a query. Once the notification is received, an handler is triggered. The definition of an handler class is shown in the following slide, before introducing the activation of a subscription. Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links Defining the handler class The handler class is a class that implements the iKPIC subscribeHandler2 interface. p u b l i c c l a s s MyHandler i m p l e m e n t s i K P I C s u b s c r i b e H a n d l e r 2 { p u b l i c MyHandler ( ) { // I n i t i a l i z a t i o n c o d e } @Override p u b l i c v o i d k p i c R D F E v e n t H a n d l e r ( V e c t o r<V e c t o r<S t r i n g >> n e w T r i p l e s , V e c t o r<V e c t o r<S t r i n g >> o l d T r i p l e s , S t r i n g indSequence , S t r i n g subID ) { // P r o c e s s added and removed t r i p l e s ! } } Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Reference Links RDF Subscription Once that the handler class has been defined (and imported), a subscription can be executed with: r e s p = mykp . s u b s c r i b e R D F ( s u b j e c t , p r e d i c a t e , o b j e c t , o b j e c t t y p e , MyHandler ) ; String sub id = resp . subscription id ; where object type can be "uri" or "literal". Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions SPARQL Subscription In a SPARQL subscription, the pattern is expressed with a String variable: S t r i n g p a t t e r n = ”SELECT ? s ? p ? o WHERE { ? s ? p ? o }” ; r e s p = mykp . subscribeSPARQL ( p a t t e r n , MyHandler ) ; String sub id = resp . subscription id ; Reference Links Introduction Preliminary steps Joining/Leaving a SS CRUD operations Subscriptions Unsubscription To close a subscription (nevermind if it is a RDF or a SPARQL one) the required steps are: mykp . u n s u b s c r i b e ( s u b i d ) ; Reference Links Introduction Preliminary steps Joining/Leaving a SS Table of Contents 1 Introduction Introduction 2 Preliminary steps 3 Joining/Leaving a SS Join Leave 4 CRUD operations Defining triples Insertion Removal Update Query 5 Subscriptions Introduction to subscriptions Defining the handler Subscription Unsubscription 6 Reference Links CRUD operations Subscriptions Reference Links Introduction Preliminary steps Reference Links Software Smart-M3 Java KPI Joining/Leaving a SS CRUD operations Subscriptions Reference Links
© Copyright 2024 ExpyDoc