If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Friday, June 29, 2012

Experimenting with Jena SPARQL processor

Today I started playing with Jena ARQ, a SPARQL processor. First thing I needed to do was producing some RDF data from our (Sedna) XMLDB.
import module namespace basictypes = "http://www.nxp.com/basictypes";
import module namespace packages = "http://www.nxp.com/packages";

declare function local:toRDF() {
  <rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"  
    xmlns:bt="http://www.nxp.com/bt"
    xmlns:pkg="http://www.nxp.com/pkg">
    {
       for $product in basictypes:getBasicTypes()[ProductInformation/MagCode = ('R73', 'R01', 'R02')]
       let $prodInfo := $product/ProductInformation
       let $btn := data($prodInfo/Name)
       let $pkgId := data($prodInfo/PackageID)
       return
       <bt:BasicType rdf:about="http://www.nxp.com/bt/{$btn}">
         <bt:name>{$btn}</bt:name>
         <bt:magcode>{data($prodInfo/MagCode)}</bt:magcode>
         <bt:piptype rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">
           {data($prodInfo/PIPType)}</bt:piptype>
         <bt:status>{data($prodInfo/Status)}</bt:status> 
         <bt:maturity>{data($prodInfo/Maturity)}</bt:maturity>
         <bt:package rdf:resource="http://www.nxp.com/pkg/{$pkgId}"/>
       </bt:BasicType>
   }
   {
      for $pkg in packages:getPackages()
      let $pkgInfo := $pkg/PackageInformation
      let $pkgn := data($pkgInfo/Name)
      return
      <pkg:Package rdf:about="http://www.nxp.com/pkg/{$pkgn}">
        <pkg:name>{$pkgn}</pkg:name>
        <pkg:status>{data($pkgInfo/Status)}</pkg:status>
        <pkg:maturity>{data($pkgInfo/Maturity)}</pkg:maturity>
      </pkg:Package>
   }
 </rdf:RDF>
};

local:toRDF()

Below a short extract from the generated RDF testdata
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF 
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
  xmlns:bt="http://www.nxp.com/bt" 
  xmlns:pkg="http://www.nxp.com/pkg">
  <bt:BasicType rdf:about="http://www.nxp.com/bt/BGF802-20">
    <bt:name>BGF802-20</bt:name>
    <bt:magcode>R02</bt:magcode>
    <bt:piptype 
      rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">0</bt:piptype>
    <bt:status>OBS</bt:status>
    <bt:maturity>Product</bt:maturity>
    <bt:package rdf:resource="http://www.nxp.com/pkg/SOT365C"/>
  </bt:BasicType>
  <bt:BasicType rdf:about="http://www.nxp.com/bt/BLC6G10LS-160RN">
    <bt:name>BLC6G10LS-160RN</bt:name>
    <bt:magcode>R02</bt:magcode>
    <bt:piptype rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">1</bt:piptype>
    <bt:status>ACT</bt:status>
    <bt:maturity/>
    <bt:package rdf:resource="http://www.nxp.com/pkg/SOT896B"/>
  </bt:BasicType>
  <pkg:Package rdf:about="http://www.nxp.com/pkg/SOT365C">
    <pkg:name>SOT365C</pkg:name>
    <pkg:status>DEV</pkg:status>
    <pkg:maturity>Product</pkg:maturity>
  </pkg:Package>
  <pkg:Package rdf:about="http://www.nxp.com/pkg/SOT896B">
    <pkg:name>SOT896B</pkg:name>
    <pkg:status>DEV</pkg:status>
    <pkg:maturity>Product</pkg:maturity>
  </pkg:Package>
</rdf:RDF>  

Next I saved the file to disk in order to write a unit test querying this data.
@Test
public void executeQuery() throws IOException {
    InputStream in = new FileInputStream(new File("c:/tmp/rdfdata.xml"));
    Model model = ModelFactory.createMemModelMaker().createModel("TestData");
    model.read(in, null);
    in.close();
    String sQuery =
            "PREFIX bt: <http://www.nxp.com/bt>\n" +
            "SELECT ?s \n" +
            "WHERE\n" +
            "{\n" +
            "?s bt:package <http://www.nxp.com/pkg/SOT365C>" +
            "}";

    Query query = QueryFactory.create(sQuery);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(System.out, results, query);
    qexec.close();
}

The unit test runs a query listing all basictypes that have a package SOT365C.
-------------------------------------------
| s                                       |
===========================================
| <http://www.nxp.com/bt/BGF802-20> |
-------------------------------------------

2 comments:

  1. It's a good start, btw i spotted your deliberate mistake ^_^
    Namespaces are missing a trailing / or #, e.g.:

    xmlns:bt="http://www.nxp.com/bt/"
    xmlns:pkg="http://www.nxp.com/pkg#"

    Otherwise you URIs for the predicate/property will be like:
    http://www.nxp.com/btname

    ReplyDelete
  2. yep... missed that one. Will fix it

    ReplyDelete