Project

General

Profile

UserAPI » History » Version 6

Paula Gearon, 05/16/2008 01:41 AM

1 3 Paula Gearon
= Connection API =
2
== Overview ==
3 1 Paula Gearon
Mulgara now supports a new interface called a `Connection`. This will be replacing the current `ItqlInterpreterBean` interface as the full suite of utilities becomes available.
4
5
The existing `ItqlInterpreterBean` interface provides a completely automated connection mechanism, based on the URLs of graphs referenced in TQL queries. While convenient, this has several undesirable effects:
6 5 Paula Gearon
7
 * All queries issued to this interface must contain at least one URL for a graph on the server that will handle the query.
8
 * Arbitrary graph names are not permitted. Graph names must follow a particular format, and will include the hostname of the server containing the graph.
9
 * Queries against graphs must be changed when the server hosting the graph changes.
10
 * Connections to a server cannot be cached.
11
 * Distributed queries (queries where the requested data comes from more than one server) are sent to an arbitrary server and not necessarily the most desirable one.
12
 * Query languages which use a default graph name (like [http://www.w3.org/TR/rdf-sparql-query/ SPARQL]) cannot identify a server if no graph names are given.
13 1 Paula Gearon
14
The `Connection` interface avoids these issues by establishing a link to a specified server. This link can be to a server running in the current process, over inter-process-communication (IPC) to a server running on the same machine, or across a network. Queries and commands can be sent along this link, and all operations will be performed by the server that has been connected to.
15
16
The `Connection` interface and its supporting classes are found in the `org.mulgara.connection` package.
17
18
== Connection Factory ==
19
While Connections can be created directly, the `ConnectionFactory` class attempts to manage connections to ensure that the correct type is created, and reduce network overhead. The connections returned by a `ConnectionFactory` are only managed by that factory, and will not interfere with connections from a separate factory instance.
20
21
`Connection`s can be established based on the URL of a database (in the form ''protocol''://''host''/''service'') or by wrapping a `Session` instance. The `Session` interface has existed in Mulgara for some time, and it may be more appropriate for existing code to use its internal `Session` instances to create new `Connection`s.
22
23
For the moment, the only supported protocol is ''rmi''. Other protocols, including ''http'' are expected shortly.
24
25
== Using a Connection ==
26 2 Paula Gearon
=== Commands ===
27 1 Paula Gearon
Once a `Connection` has been created, commands can be sent on it using the `execute` method. Alternatively, commands also have their own `execute` method that can be applied to a connection, with the same effect.
28
29
The following is an example of using a Connection to create a graph, and load a file into that graph:
30
{{{
31
#!java
32
    // define the server we want to connect to
33
    URI serverURI = new URI("rmi://localhost/server1");
34
    // define the file we want to load
35
    URI dataFile = new File("data.rdf").toURI();
36
    // define the name of the graph to load the data into
37
    URI myData = new URI("test:data");
38
39
    // Create a factory, and connect to the server
40
    ConnectionFactory factory = new ConnectionFactory();
41
    Connection connection = factory.newConnection(serverUri);
42
43
    // execute a CREATE command
44
    connection.execute(new CreateGraph(myData));
45
    // execute a LOAD command
46
    connection.execute(new Load(dataFile, myData, true));
47 2 Paula Gearon
48
    // cleaning up the connection allows the network resources to be re-used
49
    connection.close();
50
}}}
51
52
Executing a command returns a String containing a message related to the success of the command. If commands are kept, then it is also possible to retrieve the last message from a command with the `getResultMessage` method.
53
54
=== Queries ===
55
Queries are a type of command that return an `Answer` instead of a message string. Queries can be created using either the TQL or SPARQL parsers:
56
{{{
57
#!java
58
    // define the server we want to connect to
59
    URI serverURI = new URI("rmi://localhost/server1");
60
61
    // Create a factory, and connect to the server
62
    ConnectionFactory factory = new ConnectionFactory();
63
    Connection connection = factory.newConnection(serverUri);
64 1 Paula Gearon
65
    // Use a SPARQL query
66 3 Paula Gearon
    Query query = new SparqlInterpreter().parseQuery(
67
                  "SELECT * FROM <test:data> WHERE { ?s ?p ?o }");
68 2 Paula Gearon
    Answer answer = connection.execute(query);
69
70
    // display the result
71
    answer.beforeFirst();
72
    while (answer.next()) {
73
      System.out.print(" " + answer.getObject(0));
74
      System.out.print(" " + answer.getObject(1));
75
      System.out.println(" " + answer.getObject(2));
76
    }
77
    answer.close();
78 1 Paula Gearon
79
    connection.close();
80
}}}
81 6 Paula Gearon
82
A working example program for issuing SPARQL queries is provided with the source code. It can be found on the following path:
83
  [http://mulgara.org/svn/mulgara/tags/release-2.0-alpha-final/tools/src/org/mulgara/tools/Sparql.java tools/src/org/mulgara/tools/Sparql.java]