This note is a development of Ian's original note on the enhanced node and graph design of Jena 2.
One problem with the Jena 1 design was that both the DAML layer and the RDB layer independantly extended Resource with domain-specific information. That made it impossible to have a DAML-over-RDB implementation. While this could have been fixed by using the "enhanced resource" meachanism of Jena 1, that would have left a second problem.
In Jena 1.0, once a resource has been determined to be a DAML Class (for instance),
that remains true for the lifetime of the model. If a resource starts out not qualifying
as a DAML Class (no rdf:type daml:Class
) then adding the type assertion
later doesn't make it a Class. Similarly, of a resource is a DAML Class, but then the
type assertion is retracted, the resource is still apparently a class.
Hence being a DAMLClass is a view of the
resource that may change over time.
Moreover, a given resource may validly have a number of different views simultaneously.
Using the current DAMLClass
implementation method means that a given resource is limited to a single such view.
A key objective of the new design is to allow different views, or facets, to be used dynamically when accessing a node. The new design allows nodes to be polymorphic, in the sense that the same underlying node from the graph can present different encapsulations - thus different affordances to the programmer - on request.
In summary, the enhanced node design in Jena 2.0 allows programmers to:
To assist the following discussion, the key terms are introduced first.
Some key features of the design are:
If en
is an enhanced node representing some resource we wish to be able to view as
being of some (Java) class/interface T
, the expression en.as(T.class)
will
either deliver an EnhNode of type C
, if it is possible to do so, or throw an
exception if not.
To check if the conversion is allowed, without having to catch exceptions, the expression
en.canAs(T.class)
delivers true
iff the conversion is possible.
as()
would have nothing
to work on. Subclasses of enhanced node provide constructors (perhaps hidden behind factories)
which wrap plain nodes up in enhanced graphs. Eventually these invoke the constructor
EnhNode(Node,EnhGraph)
It's up to the constructors for the enhanced node subclasses to ensure that they are called
with appropriate arguments.
as(Class T)
is defined on EnhNode to invoke asInternal(T)
in Polymorphic
.
If the original enhanced node en
is already a valid instance of T
,
it is returned as
the result. Validity is checked by the method isValue()
.
If en
is not already of type T
, then a cache of alternative views
of en
is consulted to see if a suitable alternative exists. The cache is implemented
as a sibling ring of enhanced nodes - each enhanced node has a link to its next sibling,
and the "last" node links back to the "first". This makes it cheap to find alternative views if
there are not too many of them, and avoids caches filling up with dead nodes and having to be
flushed.
If there is no existing suitable enhanced node, the node's personality is consulted. The personality
maps the desired class type to an Implementation
object, which is a factory with
a wrap
method which takes a (plain) node and an enhanced graph and delivers the
new enhanced node after checking that its conditions apply. The new enhanced node is then linked
into the sibling ring.
What you have to do to define an enhanced node/graph implementation:
I
for the new enhanced node.
(You could use just the implementation class, but we've stuck with the interface,
because there might be different implementations)C
. This is just a front for the enhanced node.
All the state of C
is reflected in the graph (except for caching; but beware
that the graph can change without notice).
Implementation
class for the factory. This class defines methods canWrap
and wrap
, which test a node to see if it is allowed to represent I
and
construct an implementation of C
respectively.
I
to the factory. At the moment
we do this by using (a copy of) the built-in graph personality as the personality for the enhanced
graph.
ReifiedStatementImpl
.