Skip navigation

Instantiating a Fresnel parser to work with a Jena model

Before instantiating a parser, it is necessary to initialize two elements. First, the table of namespace prefix bindings that will be used to resolve qnames in FSL expressions. This table can be empty if you do not use any FSL expressions in your Fresnel documents, but it needs to be created nevertheless. Second, the table containing pointers to RDFS schemas or OWL ontologies that the FSL engine should be aware of when evaluating path expressions, if subproperty and subclass relationships should be taken into account when matching elements of a graph. Again, if you do not care about this feature, this element can be empty, but needs to be instantiated.

FSLNSResolver nsr = new FSLNSResolver(); nsr.addPrefixBinding("rdf", ""); nsr.addPrefixBinding("gn", ""); nsr.addPrefixBinding("wgs84_pos", ""); nsr.addPrefixBinding("foaf", ""); FSLHierarchyStore fhs = new FSLJenaHierarchyStore(); fhs.addOntology("", "file:///example/wine.owl");

Once those two elements have been created, it is possible to instantiate the FSL engine. This engine is not used during the Fresnel document parsing stage, but will be used in later stages, when lenses and formats are actually used on an actual Jena model to extract information from it and format it.

FSLJenaEvaluator fje = new FSLJenaEvaluator(nsr, fhs);

It is then possible to instantiate the parser, and ask for the lenses, formats and groups contained in a document as Java object instances.

// instantiate a Fresnel parser that will create lenses and formats to be applied on a Jena model FresnelParser fp = new FresnelParser(Constants.JENA_API, nsr, fhs); // actually parse a Fresnel document, written in Notation 3 FresnelDocument fd = fp.parse(LENS_FILE, Constants.N3_READER);

The lenses, formats and groups can then be obtained as follows:

Lens[] lenses = fd.getLenses(); Format[] formats = fd.getFormats();

They can also be obtained one at a time knowing their URI. See the API documentation for more information.

Working with lenses and formats on a Jena model

The lenses and formats can then be used directly on the Jena model. For instance, it is possible to test if a resource matches the domain of a lens (i.e., does this lens apply to this resource).

JenaLens lens = fd.getLens(""); Resource r = ...; // a Jena RDF resource boolean b = lens.selects(r, fje);

Once it has been tested that a lens does apply to a given resource, it is possible to ask the lens for the ordered list of property-value pairs to be displayed for this resource, by this lens:

// In the case of Jena, this Vector actually contains instances of Jena statement objects. // The predicate and object of these statements are the property-value pairs of interest. Vector statements = lens.getPropertyValuesToDisplay(r, fje);

It is possible to query a lens for formats associated with it (through groups), and for the best format among these for a given property value:

Format[] formatsAssociatedWithALens = lens.getAssociatedFormats(); // or Format format = lens.getBestFormatForProperty(r, fje);

As for lenses, it is possible to directly test if a property matches the domain of a Format.

The Format provides information about how to present the data (additional content between separate values, how to handle a value, etc.).

String explanatoryLabel = format.getLabel(); String whatContentToShowAfterEachSeparateValue = format.getContentAfter(); short howToHandleAValue = format.getValueType(); //e.g. Format.VALUE_TYPE_IMAGE

Using the FSL engine

More information is available on a separate page.