The ICS-FORTH SWKM Client: How to use programmatically...

1. General Information

The SWKM client  is a collection of Java classes and interfaces that can be used to contact the SWKM web services in a concise and natural way.

Each web service is modeled as a Java interface. These interfaces reside in swkm-services-api.jar , in the package gr.forth.ics.rdfsuite.services. The mapping of interface names and services is given below:


WEB SERVICES

WSDL URL PATHS

INTERFACES

Query Service

/query?wsdl

QueryHandler

Update Service

/update?wsdl

UpdateHandler

Delete Service

/deleter?wsdl

Deleter

Import Service

/importer?wsdl

Importer

Export Service

/exporter?wsdl

Exporter

Versioning Service

/versioning?wsdl

VersionManager

Comparison Service

/diffGenerator?wsdl

DiffGenerator

Change Impact Service

/changeImpact?wsdl

ChangeImpactAnalyzer

Registry Service

/registry?wsdl

Registry


The javadoc specification of the interfaces can be used as a reference for interacting with the respective web services. We will here provide examples of how to get instances of the interfaces, as well as several small examples of using them.

2. Bootstrapping

The first step to access the services is to create a client to them. The client can be found in the SwkmClient.jar, by importing the package gr.forth.ics.swkmclient. The goal is to create a gr.forth.ics.swkmclient.Client instance, which provides references to interface instances that can interact with the web services.


There are various ways to get a Client instance, all residing as static factories in class ClientFactory. For a detailed reference, see the specification of that class. The two most common ways are either through a properties file (“services.properties”) located on the classpath, or directly by providing the host’s URL, its port, and the web application context that provides the web services. To avoid proliferating configuration files in an application, the latter method is preferred, which allows an application having centralized configuration.


Another piece of configuration is which database settings to use. The SWKM services have the feature that a single deployment can talk to as many databases as required, without needing a separate deployment for each configuration. As the web services are strictly stateless for scalability reasons (i.e., they don’t maintain state between subsequent requests of the same client), this is implemented by including the relevant database configuration to be used by the service, inside the request. Each deployment of the web application defines its own default database configuration, and each setting can be overridden independently by a client – all not defined configuration settings are inherited from the default configuration. For example, one could simply define the database name to be used, and nothing else, so the user and password configured by default would be used to access this database.


Putting it all together:

import gr.forth.ics.swkmclient.*;

This creates a client by loading a “services.properties” file from the classpath, which will use the default database settings of the web application.

ClientFactory.newClientByConfiguration().withDefaultSettings();

Or to create a client by providing explicitly the endpoint location:

Client client = ClientFactory.newClientByConfiguration(
“http://mielikki.mobile.evtek.fi”, 3027,“SwkmMiddlewareWS”).withDefaultSettings();


The Client interface is an access point to the interfaces specified in package “gr.forth.ics.rdfsuite.services”.

In order to create a client that creates requests for a specific database, this would be used:


import gr.forth.ics.swkmclient.*;

import gr.forth.ics.rdfsuite.services.*;


DbSettings customDbSettings = new DbSettings();

customDbSettings.setDbName(“repositorydb”);


Client client = ClientFactory.newClientByConfiguration(

“http://mielikki.mobile.evtek.fi”, 3027,

“SwkmMiddlewareWS”).with(customDbSettings);

3. Client’s methods

As mentioned earlier, a Client instance is an access point to several interfaces in “gr.forth.ics.rdfsuite.services”. These group the operations of each web service. They are:


4. Knowledge Repository and Mediator Usage Examples

All examples assume the following import statements:


import gr.forth.ics.swkmclient.*;

import gr.forth.ics.rdfsuite.services.*;

import gr.forth.ics.rdfsuite.services.utils.*;

import java.util.*;

1. Importer Example 1

This example loads in memory the CIDOC.rdfs document, along with a URI to describe it.

RdfDocument cidoc = new RdfDocument("http://cidoc.ics.forth.gr/rdfs/CIDOC4.3.rdfs#",
IOUtils.readFileAsString(new File("data/cidoc/CIDOC.rdfs")),
Format.RDF_XML);

Creating the client and using the default configuration…

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Start with a clean database, for demonstration purposes. Of course, this functionality can be disabled through configuration (by setting the EraseDatabase property to false).

c.debug().eraseDatabase();

Getting a handle to the Importer interface.

Importer importer = c.importer();

Lets store TLO. It does not have any dependency that must be looked up on the network, so we specify Deps.WITHOUT (“without dependencies”).

importer.store(Arrays.asList(http://www.kp-lab.org/system-model/TLO#), Deps.WITHOUT);

2. Importer Example 2

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Load in memory the SMAT schema file, that extends TLO.

RdfDocument smat = new RdfDocument(
"http://www.kp-lab.org/ontologies/SMAT#",IOUtils.readFileAsString(new File("data/smat/SMAT.rdfs")),
Format.RDF_XML);


Start with a clean database, for demonstration purposes. As mentioned, this functionality can be disabled through configuration.

c.debug().eraseDatabase();

Getting a handle to the Importer interface.

Importer importer = c.importer();

Lets store SMAT. SMAT needs TLO, and we didn't store it yet, nor we provide it to the importer service. Instead, we will request any missing dependencies to be looked up on the network (but using their base URIs as URLs), by specifying Deps.WITH. CIDOC will also be stored after this operation.

importer.store(Arrays.asList(smat), Deps.WITH);


3. Exporter Example 1

Let’s store CIDOC first, to have something to export.

ImporterExample1.main(args);

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Get a handle to the Exporter interface.

Exporter exporter = c.exporter();

The base URI of TLO.

String tloUri = "http://www.kp-lab.org/system-model/TLO#";

Get CIDOC document as TriG. We don't want any dependencies to be fetched, or data instances of the specified schema belonging to other named graphs, so we specify Format.TRIG, Deps.WITHOUT and Data.WITHOUT respectively.

Map<String, RdfDocument> docs = exporter.fetch(
Arrays.asList(tloUri),
Format.TRIG, Deps.WITHOUT, Data.WITHOUT);
RdfDocument tlo = docs.get(tloUri);

Print out exported TLO.

System.out.println(tlo.getContent());

4. Exporter Example 2

Lets store TLO and SMAT first, to have something to export.

ImporterExample2.main(args);

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Get a handle to the Exporter interface.

Exporter exporter = c.exporter();

The base URI of TLO:

String tloUri = "http://www.kp-lab.org/system-model/TLO#";

The base URI of SMAT:

String smatUri = "http http://www.kp-lab.org/ontologies/SMAT#";


We will fetch SMAT, and specify that we also want its dependencies (Deps.WITH). This will cause TLO to be fetched as well.

Map<String, RdfDocument> docs = exporter.fetch(
Arrays.asList(smatUri),
Format.TRIG, Deps.WITH, Data.WITHOUT);
RdfDocument frbr = docs.get(smatUri);
RdfDocument cidoc = docs.get(tloUri);

Print out exported TLO:

System.out.println(tlo.getContent());

5. Query Example

Lets store TLO first, to have something to query.

ImporterExample1.main(args);

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Get a handle to the QueryHandler interface.

QueryHandler queryHandler = c.query();

Find all available classes by a trivial RQL query.

String result = queryHandler.query(Format.RDF_XML, "classes");
System.out.println(result);

Get a list with the classes and print it.

List<String> classes = QueryUtils.list(result,Format.RDF_XML);
System.out.println(classes);

6. Update Example

Lets store TLO and some class instances first, to have something to update.

ImporterExample1.main(args);

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Get a handle to the UpdateHandler interface.

UpdateHandler updateHandler = c.update();

Insert, through a RUL query, an event.

boolean succeeded = updateHandler.update("Insert Task(&somethingHappened#)");

Let’s verify that it was stored.

String queryResult = c.query().query(Format.RDF_XML, "Task");
List<String> instances = QueryUtils.list(queryResult, Format.RDF_XML);

Now this list will contain an element “somethingHappened#”.

System.out.println(instances);

7. Versioning Example

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Start with a clean database, for demonstration purposes. Of course, this functionality can be disabled through configuration.

c.debug().eraseDatabase();
RdfDocument firstVersion = new RdfDocument("http://example.org#",
IOUtils.readFileAsString(new File("data/diff/AddClass1_a.rdf")),Format.RDF_XML);

Get a handle to the VersionManager interface.

VersionManager vm = c.versionManager();

Store the first version. It has no parents.

VersionedUri v1 = vm.importVersion(firstVersion,
Collections.<VersionedUri>emptySet());


RdfDocument secondVersion = new RdfDocument("http://example.org#", 
IOUtils.readFileAsString(new File("data/diff/AddClass1_b.rdf")),
Format.RDF_XML);


Store the second version. It is marked as a subsequent (child) version of the previous one.

VersionedUri v2 = vm.importVersion(secondVersion,Arrays.asList(v1));

Get both versions through the Exporter service.

Map<String, RdfDocument> docs = c.exporter().fetch(
Arrays.asList(v1.toString(), v2.toString()),Format.TRIG, Deps.WITHOUT, Data.WITHOUT);


Simply print the URIs of the documents. They should be:
{ http://example.org~_~1#, http://example.org~_~2# }

System.out.println(docs.keySet());

8. Comparison and ChangeImpact Example

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Start with a clean database, for demonstration purposes. Of course, this functionality can be disabled through configuration.

c.debug().eraseDatabase();

Assume two different versions of a model, described in two RDF/XML files. Store them using the Importer.

RdfDocument firstVersion = new
RdfDocument("http://example~_~v1#",IOUtils.readFileAsString(new File("data/diff/AddClass1_a.rdf")),
Format.RDF_XML);
c.importer().store(Arrays.asList(firstVersion), Deps.WITHOUT);

RdfDocument secondVersion = new RdfDocument("http://example~_~v2#", IOUtils.readFileAsString(new
File("data/diff/AddClass1_b.rdf")),
Format.RDF_XML);
c.importer().store(Arrays.asList(secondVersion),Deps.WITHOUT);

Now we will try to compare these stored documents. These are the different modes that the Comparison service supports.

DeltaFunction[] functions = {
DeltaFunction.Delta_explicit,
DeltaFunction.Delta_closure,
DeltaFunction.Delta_dense,
DeltaFunction.Delta_dense_and_closure
};

Lets try all of them, and see the generated (through the comparison) Delta. Store the generated deltas, to also show the Change Impact service.

Map<DeltaFunction, Delta> deltas = new EnumMap<DeltaFunction, Delta>(DeltaFunction.class);
DiffGenerator diffGenerator = c.diffGenerator();

for (DeltaFunction function : functions) {
System.out.println("Using: " + function);
Delta delta = diffGenerator.diff(Arrays.asList(firstVersion.getURI()),
Arrays.asList(secondVersion.getURI()),function);
printDelta(delta);
deltas.put(function, delta);
}

ChangeImpactAnalyzer impactAnalyzer = c.changeImpact();
for (DeltaFunction function : deltas.keySet()) {
System.out.println("Using delta derived by: " + function);
Delta delta = deltas.get(function);

Delta impact = impactAnalyzer.changeImpact(delta,Arrays.asList(firstVersion.getURI()),
NamespaceClosure.OFF);
printDelta(impact);
}

A couple of extra printing method used above…

static void printDelta(Delta delta) {
System.out.println("Delta:\nAdded triples");

for (Triple triple : delta.getAdded()) {
printTriple(triple);
}

System.out.println("\nDeleted triples");

for (Triple triple : delta.getAdded()) {
printTriple(triple);
}
}

static void printTriple(Triple triple) {
System.out.printf("<%s> <%s> <%s>\n",triple.getSubject(), triple.getPredicate(),triple.getObject());
}

9. Registry Example

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Get a handle to the Registry interface.

Registry registry = c.registry();

Record a schema and a first version of it into the registry. The versions of the schema need not be stored into the repository.

String schemaUri = "http://example.org/mySchema#";
String firstVersion = "firstVersion";
registry.insertSchemaURI(Registry.SchemaSubClasses.SCHEMA,schemaUri, new String[] { firstVersion });


The full URI of the first version now would be:
“http://example.org/mySchema~_~firstVersion#”
This string is equal to the following object's #toString() result:

VersionedUri firstVersionURI = new VersionedUri(schemaUri,firstVersion, "");

Record a second version of the same schema.

String secondVersion = "secondVersion";
registry.insertSchemaURI(Registry.SchemaSubClasses.SCHEMA,schemaUri, new String[] { secondVersion.toString() });

The full URI of the second version now would be:

“http://example.org/mySchema~_~secondVersion#”

This string is equal to the following object's #toString() result:

VersionedUri secondVersionURI = new VersionedUri(schemaUri,secondVersion, "");

Specify that the second version has the first version as a parent.

registry.insertProperty(Registry.ClassName.VERSION,secondVersionURI.toString(), "previousVersion",
new String[] { firstVersionURI.toString() },Registry.PropertyRangeType.RESOURCE);

Now verify that the parental relationship between first and second version is recorded. This requires first to assert that both versions are registered.

#getSimpleUri returns the URI by omitting the final '#' character .

assert registry.existInstanceURI(Registry.ClassName.VERSION,firstVersionURI.getSimpleUri());
assert registry.existInstanceURI(Registry.ClassName.VERSION,secondVersionURI.getSimpleUri());

Then, we must find the parents of the second version (which should yield the first one). This will be done through a RQL query.

String queryResult = registry.evaluatePredefinedQuery("Versioning", "findParents", new String[] {
secondVersionURI.getSimpleUri() }, Format.RDF_XML);

List<String> parents = QueryUtils.list(queryResult,Format.RDF_XML);
System.out.println(parents);


10. Deleter Example

Create a client by reading services.properties from classpath and using the default database configuration of the server.

Client c = ClientFactory.newClientByConfiguration().withDefaultSettings();

Instantiate a Deleter object

Deleter deleter = c.deleter();

Delete a specific namespace.

deleter.store(Arrays.asList("http://myschemauri:8080/schema1.rdfs#"), Data.WITHOUT);

Delete a specific namespace and all the data classified under it.

deleter.store(Arrays.asList("http://myschemauri:8080/schema1.rdfs#"), Data.WITH);