Virtuoso Open-Source Wiki
Virtuoso Open-Source, OpenLink Data Spaces, and OpenLink Ajax Toolkit
Advanced Search
Help?
Location: / Dashboard / Main / VirtProgrammerGuideRDFCartridge

RDF Cartridges (RDF Mappers or RDFizers)

This article explains how to develop and test a custom RDF Cartridge.

Concept

RDF cartridges provide a modular approach to RDF oriented entity extraction and ontology mapping as part of a Linked Data production pipeline. Typical sources include (X)HTML pages, images, Office documents and PDF documents amongst others.

Cartridges expose their functionality to service consumers via the Virtuoso Sponger ("Sponger"), a middleware layer that extracts and delivers RDF to other Virtuoso components such as the Web Crawler and SPARQL Query Process. In addition it is directly exposed as a REST-style Web Service for external applications and services to exploit.

A Cartridges is comprised of an initialization PL procedure (hook) and an entity extractor and mapper. The entity extractor and mapping component can be developed using PL, C or any external language supported by Virtuoso via the Virtuoso Server Extensions APIs.

Once the cartridge has been developed, it is plugged into the Virtuoso Sponger by adding a record to the table DB.DBA.SYS_RDF_MAPPERS.

How Cartridges Work

SPARQL Query Processing:

When a SPARQL query is dispatched to Virtuoso, it invokes the Sponger during the act of graph or resource URI dereferencing i.e. it actually crawls the Web, locates a resource via it's URI/IRI (using a variety of RDF discovery heuristics), and then differences the data that the URI exposes. If RDF is discovered, the cartridges play no role. On the other hand, if RDF isn't discovered the Sponger will look in the DB.DBA.SYS_RDF_MAPPERS table (in RM_ID order), and for every matching URI or MIME type pattern (depending on RM_TYPE column value) it will invoke the associated cartridge by invoking the hook procedure. If the hook returns zero the next cartridge will be tried. If the result is negative the process stops, instructing the SPARQL engine that nothing was retrieved. If the result is positive the process stops, this time instructing the SPARQL engine that RDF data was successfully retrieved.

Sponger Cartridge RDF Extractor PL Requirements

PL hook requirements

Every PL function used to plug a cartridge into the SPARQL engine must have the following parameter signature:

  • in graph_iri varchar: the local storage graph IRI
  • in new_origin_uri varchar: target information resource URI
  • in destination varchar: the target graph IRI
  • inout content any: the content of the information resource retrieved for dispatch to the Sponger
  • inout async_queue any: an asynchronous queue, can be used for background processing (if required)
  • inout ping_service any: the value in the [SPARQL] section of a Virtuoso instance (i.e PingService INI parameter) which is enables RDF triple propagation and notification to pinger services such as http://pingthesemanticweb.com
  • inout api_key any: a plain text id single key value or serialized vector of keys, basically the value of RM_KEY column of the DB.DBA.SYS_RDF_MAPPERS table, which is used for handling of API keys for 3rd party Web Services.

Note: the names of the parameters are not important, but their order and presence are vital.

Implementation

In the example script we implement a basic cartridge which maps a text/plain mime type to an imaginary ontology, which extends the class Document from FOAF with properties 'txt:UniqueWords' and 'txt:Chars', where the prefix 'txt:' we specify as 'urn:txt:v0.0:'.

use DB;

create procedure DB.DBA.RDF_LOAD_TXT_META
 (
  in graph_iri varchar,
  in new_origin_uri varchar,
  in dest varchar,
  inout ret_body any,
  inout aq any,
  inout ps any,
  inout ser_key any
  )
{
  declare words, chars int;
  declare vtb, arr, subj, ses, str any;
  declare ses any;
  -- if any error we just say nothing can be done
  declare exit handler for sqlstate '*'
    {
      return 0;
    };
  subj := coalesce (dest, new_origin_uri);
  vtb := vt_batch ();
  chars := length (ret_body);




  -- using the text index procedures we get a list of words
  vt_batch_feed (vtb, ret_body, 1);
  arr := vt_batch_strings_array (vtb);

  -- the list has 'word' and positions array, so we must divide by 2
  words := length (arr) / 2;
  ses := string_output ();

  -- we compose a N3 literal
  http (sprintf ('<%s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Document> .\n', subj), ses);
  http (sprintf ('<%s> <urn:txt:v0.0:UniqueWords> "%d" .\n', subj, words), ses);
  http (sprintf ('<%s> <urn:txt:v0.0:Chars> "%d" .\n', subj, chars), ses);
  str := string_output_string (ses);

  -- we push the N3 text into the local store
  DB.DBA.TTLP (str, new_origin_uri, subj);
  return 1;
};

delete from DB.DBA.SYS_RDF_MAPPERS where RM_HOOK = 'DB.DBA.RDF_LOAD_TXT_META';

insert soft DB.DBA.SYS_RDF_MAPPERS (RM_PATTERN, RM_TYPE, RM_HOOK, RM_KEY, RM_DESCRIPTION)
values ('(text/plain)', 'MIME', 'DB.DBA.RDF_LOAD_TXT_META', null, 'Text Files (demo)');

-- here we set order to some large number so don't break existing mappers
update DB.DBA.SYS_RDF_MAPPERS set RM_ID = 2000 where RM_HOOK = 'DB.DBA.RDF_LOAD_TXT_META';

To test the cartridge we just use /sparql endpoint with option 'Retrieve remote RDF data for all missing source graphs' to execute:

      select * from <URL-of-a-txt-file> where { ?s ?p ?o }

It is important that the SPARQL_SPONGE role needs to be granted to "SPARQL" user account (or any other account bound to SPARQL functionality) in order to enable persistence to local storage.

If the above is set correctly then you can just hit this link.

More complex examples can be found in the cartridges package implementation.

Authentication in Sponger

To enable usage of user defined authentication, there are added more parameters to the /proxy and /sparql endpoints. So to use it, the RDF browser and iSPARQL should send following url parameters:

  • for /proxy endpoint:

    'login=<account name>'

  • for /sparql endpoint:

    get-login=<account name>

References

Powered By Virtuoso