Data Representation Example ContextEvent from Lighting Sample - universAAL/middleware GitHub Wiki

Example: ContextEvent from Lighting Sample

The Lighting Sample (see SVN for the complete code) is a small example for a service to control the lights in an environment and provides commands like "turn lamp X on". When this method is called, the server sends out a context event to the context bus indicating that the lamp with ID x at location y has changed its brightness to z percent (here: 100%).

To realize this event, all classes that may be serialized are derived from the base class Resource, as shown in the following figure.

Only a small number of lines of code is necessary to create such a context event as shown in the code snippet below. The details of context events are not the focus of this example; the important part to note is in the method lampStateChanged that creates the objects of interest (LightSource) and sets the required parameters (location and brightness). This information is then given to the class ContextEvent as RDF subject (LightSource) and RDF predicate (LightSource.PROP_SOURCE_BRIGHTNESS). There is no RDF object (which would be the brightness: '100') given but it is stored in the RDF subject and the ContextEvent will automatically get this data.

  LightingProvider(BundleContext context) {
    // The parent need to know the profiles of the available functions to register them
    super(context, ProvidedLightingService.profiles);
  
    // prepare for context publishing
    ContextProvider info =  new ContextProvider(ProvidedLightingService.LIGHTING_SERVER_NAMESPACE
       + "LightingContextProvider");
    info.setType(ContextProviderType.controller);
    cp = new DefaultContextPublisher(context, info);
  }
  
  public void lampStateChanged(int lampID, String loc, boolean isOn) {
    // Create an object that defines a specific lamp
    LightSource ls = new LightSource(LightingProvider.LAMP_URI_PREFIX + lampID);
    // Set the properties of the light (location and brightness)
    ls.setSourceLocation(new Room(LightingProvider.LOCATION_URI_PREFIX + loc));
    ls.setBrightness(isOn? 100 : 0);
    // finally create an context event and publish it with the light source
    // as subject and the property that changed as predicate
    cp.publish(new ContextEvent(ls, LightSource.PROP_SOURCE_BRIGHTNESS));
  }

As all classes are derived from Resource, the important information is contained in the attributes of Resource, i.e. the URI and the properties (realized as Hashtable with key-value pairs). To give a small impression of the data structure that is supposed to be serialized, the text below describes the object structure of the context event and its contained objects, together with their URI and properties. All URIs are given with their declaration and their definition. Class names are shown in bold. Below this textual representation, a RDF graph is given. The RDF triple of interest ("The LightSource 'controlledLamp1' has changed its brightness to 100") is marked with a green box. The ContextEvent (shown in the middle of the image) uses RDF reification to describe this statement and to add further information, e.g. a timestamp of this event.

  '''ContextEvent''' (org.universAAL.middleware.context.ContextEvent):
    URI: ContextEvent.CONTEXT_EVENT_URI_PREFIX + xxx
       = urn:org.universAAL.middleware.context.rdf:ContextEvent#_:0ffffffc00ffffffa80001:51
    Properties (Key-Value):
    * K Resource.PROP_RDF_TYPE = http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      V '''ArrayList'''
          '''Resource''' (org.universAAL.middleware.rdf)
            URI: ContextEvent.MY_URI = http://ontology.universAAL.org/Context.owl#ContextEvent
            Properties:
              -
    * K ContextEvent.PROP_RDF_SUBJECT = http://www.w3.org/1999/02/22-rdf-syntax-ns#subject
      V '''LightSource''' (org.universAAL.ontology.lighting.LightSource):
          URI: LightingProvider.LAMP_URI_PREFIX + lampID
             = http://ontology.igd.fhg.de/LightingServer.owl#controlledLamp1
          Properties (Key-Value):
          * K Resource.PROP_RDF_TYPE = http://www.w3.org/1999/02/22-rdf-syntax-ns#type
            V LightSource.MY_URI = http://ontology.persona.ima.igd.fhg.de/Lighting.owl#LightSource
          * K LightSource.PROP_SOURCE_BRIGHTNESS
                 = http://ontology.persona.ima.igd.fhg.de/Lighting.owl#srcBrightness
            V 100
          * K LightSource.PROP_SOURCE_LOCATION
                 = http://ontology.persona.ima.igd.fhg.de/Lighting.owl#srcLocation
            V '''Room''' (org.universAAL.ontology.location.indoor.Room)
                URI: LightingProvider.LOCATION_URI_PREFIX + location = urn:aal_space:myHome#loc2
                Properties (Key-Value):
                * K Resource.PROP_RDF_TYPE = http://www.w3.org/1999/02/22-rdf-syntax-ns#type
                  V Location.uAAL_LOCATION_NAMESPACE + "Room"
                       = http://ontology.universAAL.org/PhysicalWorld.owl#Room
    * K ContextEvent.PROP_RDF_PREDICATE = http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate
      V '''Resource''' (org.universAAL.middleware.rdf)
          URI: LightSource.PROP_SOURCE_BRIGHTNESS
             = http://ontology.persona.ima.igd.fhg.de/Lighting.owl#srcBrightness
          Properties:
            -
    * K ContextEvent.PROP_RDF_OBJECT = http://www.w3.org/1999/02/22-rdf-syntax-ns#object
      V 100
    * K ContextEvent.PROP_CONTEXT_TIMESTAMP = http://ontology.universAAL.org/Context.owl#hasTimestamp
      V timestamp = 1297261507805
    * K ContextEvent.PROP_CONTEXT_PROVIDER = http://ontology.universAAL.org/Context.owl#hasProvider
      V '''ContextProvider'''
          URI: ProvidedLightingService.LIGHTING_SERVER_NAMESPACE + "LightingContextProvider"
             = http://ontology.igd.fhg.de/LightingServer.owl#LightingContextProvider
          Properties (Key-Value):
          * K Resource.PROP_RDF_TYPE = http://www.w3.org/1999/02/22-rdf-syntax-ns#type
            V '''ArrayList'''
                '''Resource''' (org.universAAL.middleware.rdf)
                  URI: ContextProvider.MY_URI
                     = http://ontology.universAAL.org/Context.owl#ContextProvider
                  Properties:
                    -
          * K ContextProvider.PROP_CONTEXT_PROVIDER_TYPE
                 = http://ontology.universAAL.org/Context.owl#hasType
            V '''ContextProviderType'''
                URI: ContextProviderType.xxx = http://ontology.universAAL.org/Context.owl#controller
                Properties (Key-Value):
                * K Resource.PROP_RDF_TYPE = http://www.w3.org/1999/02/22-rdf-syntax-ns#type
                  V '''ArrayList'''
                      '''Resource''' (org.universAAL.middleware.rdf)
                        URI: ContextProviderType.MY_URI
                           = http://ontology.universAAL.org/Context.owl#ContextProviderType
                        Properties:
                          -

The following text shows the serialization output of Turtle.

@prefix ns: <http://ontology.universAAL.org/PhysicalWorld.owl#> .
@prefix ns1: <http://ontology.igd.fhg.de/LightingServer.owl#> .
@prefix ns2: <http://ontology.persona.ima.igd.fhg.de/Lighting.owl#> .
@prefix ns3: <urn:aal_space:myHome#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://ontology.universAAL.org/Context.owl#> .
<urn:org.universAAL.middleware.context.rdf:ContextEvent#_:0ffffffc00ffffffa80001:51> \
    :hasProvider ns1:LightingContextProvider ;
  a :ContextEvent ;
  rdf:subject ns1:controlledLamp1 ;
  :hasTimestamp "1297261507805"^^<http://www.w3.org/2001/XMLSchema#long> ;
  rdf:predicate ns2:srcBrightness ;
  rdf:object 100 .
:controller a :ContextProviderType .
ns3:loc2 a ns:Room ,
    ns:HomeArea ,
    ns:Place ,
    ns:Location .
ns1:LightingContextProvider a :ContextProvider ;
  :hasType :controller .
ns1:controlledLamp1 ns2:srcLocation ns3:loc2 ;
  a ns2:LightSource ,
    <http://ontology.universAAL.org/uAAL.owl#PhysicalThing> ;
  ns2:srcBrightness 100 .
⚠️ **GitHub.com Fallback** ⚠️