Previous topic  Top  Next topic  Print this Topic
 

Sessions in OntoBroker

 

As of version 6.1, OntoBroker now supports sessions with private session facts. This means that the session user can add facts to any already existing modules, but nobody but him/her can see these additional facts. On querying, OntoBroker does not differentiate between static facts and session facts, so evaluation of rules works on both static and session facts. Besides storing facts, a session can also store additional session settings such as the default module, default namespace and additional namespace prefix definitions.

An OntoBroker session can be started using various interfaces

Socket client: A new session starts automatically on opening an OntoBrokerConnection,  by calling the method.
 
OntoBrokerConnectionManager.getConnection(…)
OntoBroker Web service: Calls the operation startSession(…)
OntoBroker API: If your code runs in the same VM as OntoBroker, call the method startSession(…)  of the SessionManager from the OntoBroker API
 
import org.semanticweb.kaon2.api.OntologyManager;
import org.semanticweb.kaon2.api.SessionManager;
import org.semanticweb.kaon2.api.SessionFacade;
 
OntologyManager om = …;
SessionManager sm = om.getSessionManager();
SessionFacade sf = sm.createSession(…);

 

NOTE: In OntoBroker 6.1 the sessions of the Collaboration server are a different kind of session and cannot be used in the way described above.

Using Sessions with OntoBroker API

Sessions have two main interfaces in the OntoBroker API. The SessionManager interface is used to create new sessions or to retrieve existing sessions by the session id.

// create new session

SessionFacade sf1 =  

 sm.createSession(user,password,params);

 

// retrieve existing session by id

SessionFacade sf2 = sm.getSession(sessionId);

The SessionFacade interface provides all session functionalities:

Adding and removing facts to/from the session
Running queries within the session
Executing commands within the session
Closing sessions
Getting/setting session attributes like default module, default namespace and namespace prefixes
Information about the session like session id, session user, host, last access timestamp, creation timestamp
Checking session validity

See JavaDoc for org.semanticweb.kaon2.api.SessionFacade, for details about its methods. The SessionFacade interface is not the session itself, i.e. there can be multiple SessionFacade objects referencing the same session.

Commands

Several commands have been extended for usage with sessions. The INSERT, DELETE and MODIFY commands can now optionally work on the session facts by adding a "INTO SESSION" or "FROM SESSION" to the command.

Examples:

INSERT INTO SESSION { alice:Person[age->18]. }
DELETE FROM SESSION { alice:Person[age->18]. }

 MODIFY DELETE FROM SESSION { ?P[age->?X0]. }
          INSERT INTO SESSION { ?P[age->?X1]. }
          WHERE { ?- ?P[age->?X0], ?X1 = ?X0 + 1. }

Variants of modify can also be used for moving data between session and static storage

MODIFY DELETE {...} INSERT INTO SESSION {...} WHERE {...}
MODIFY DELETE FROM SESSION {...} INSERT {...} WHERE {...}

For setting sessions attribute there is a new SET command:

set default prefix = "http://ontoprise.de/".

set prefix a = "http://ontoprise.de/sg#".

set default module = inferencelayer.

These default namespace, namespace prefix and default module attributes can be used in subsequent commands and queries without needing to state them again.

New Query Options

When running queries within a session, there are three "data sources" for facts:

Static facts (i.e. the storage EDB defined by the storage parameter in the OntoConfig.prp)
Session facts (i.e. the session EDB)
Temporary facts (facts and rules provided together with the query)

In some situations queries should exclude one of these data sources. For this purpose there are now two new query options:

ignoreSession - ignore all session facts

Example
@{options[ignoreSession]} ?- ?P:Person.
 

ignoreStorage - ignore all facts from the storage EDB

Example
@{options[ignoreStorage]} ?- ?P:Person.

Built-ins

There are two built-ins to deal with sessions: _activeSessions/4 and _sessionInfo/1

_activeSessions/4

This built-in returns all OntoBroker and Collaboration server sessions.

Example:

?- _activeSessions(?Id,?Type,?User,?Host).

 

The variables are bound as follows:

?Id                - session id

?Type        - "session" or "collab"

?User        - user name (may be null if user is unknown)

?Host        - host the client is connected from (may be null if host is unknown)

_sessionInfo/1

This built-in returns information on the current session, like the default module, default namespace and other namespace prefixes, and number of facts in the session EDB.

Example:

?- _sessionInfo(?InfoMap).

The built-in returns a map with following key value pairs:

Key

Value

"id"

session id

"user"

user name

"host"

host

"startTimestamp"

start timestamp of session

"defaultModule"

default module

"namespaces"

map with namespace prefixes

"facts"

number of facts stored in this session

_loginUser/2

This built-in is not new, but if the user name was provided on session creation, it returns user name and host.

Example:

?- _loginUser(?User, ?Host).

 

The variables are bound as follows:

?User        - user name of the current session (may be null if the user is unknown)

?Host        - host of the client of the current session (may be null if the host is unknown)

Limitations

Sessions can only store facts, i.e. it cannot store rules, queries or constraints.
It is only possible to delete facts of the session, but not to hide static facts. i.e. if a fact is stored in the static storage of OntoBroker, deleting it from the session has no effect.
Sessions can only store facts in existing modules, i.e. at least one fact or rule must exist in this module in the static storage of OntoBroker.
Persistence of sessions is not supported in OntoBroker 6.1, i.e. session facts are only held in the memory.