You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

In some situations you may want to make sure write operations aren't performed against a Context. For instance, you may be using a UnionContext to combine metadata from a Tupelo Server with local annotations, and ensure that new annotations are stored locally instead of on the server.

To make a Context read-only, you can "wrap" it in a ReadOnlyContextFacade. Operations against the facade are performed against the wrapped Context, except for write operations, which throw OperatorUnavailableException.

The following example shows how to combine metadata from a Tupelo Server with local annotations, and to prevent new local annotations from being written to a server. It sets up a UnionContext to read metadata from a Tupelo Server and from a BasicLocalContext in the user's home directory. The client Context is wrapped in a ReadOnlyContextFacade to prevent writes.

Context server = ...
 
File blcLocation = new File(System.getProperty("user.home"),".roetblc");
BasicLocalContext local = new BasicLocalContext();
local.setPath(blcLocation.getAbsolutePath());
 
UnionContext context = new UnionContext();
context.addChild(new ReadOnlyContextFacade(server)); // server is read-only
context.addChild(local);
context.setComplexMerging(true);
 
// print out all the triples about "joe"
Resource joe = Resource.uriRef("http://joe.org#joe");
RdfXml.write(context.getSubject(joe).getTriples(), System.out);
 
// assert that joe's foaf:name is "Joe Futrelle"
context.addTriple(joe, Foaf.NAME, "Joe Futrelle");
 
// compare the merged output:
RdfXml.write(context.getSubject(joe).getTriples(), System.out);
// with what's on the server, which doesn't have the foaf:name triple:
RdfXml.write(server.getSubject(joe).getTriples(), System.out);

That's all there is to it. One more detail: if you propose a write operation to a ReadOnlyContextFacade, it will return Bid.UNSUPPORTED.

  • No labels