If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!
Showing posts with label sparql. Show all posts
Showing posts with label sparql. Show all posts

Thursday, August 23, 2012

Using SPARQL describe queries

This post will show how to use describe queries from the stardog CLI but the query is not database specific. You can actually export the triples in different formats:
  • NTRIPLES
  • RDFXML
  • TURTLE
  • TRIG
  • TRIX
  • N3
  • NQUADS
Let's try out a simple describe query in 2 formats.

$ ./stardog query  -c http://localhost:5822/nxp -q "DESCRIBE <http://data.nxp.com/basicTypes/PH3330L>" -f RDFXML

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:owl="http://www.w3.org/2002/07/owl#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
  xmlns:skos="http://www.w3.org/2004/02/skos/core#"
  xmlns:nxp="http://purl.org/nxp/schema/v1/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:about="http://data.nxp.com/basicTypes/PH3330L">
  <rdf:type rdf:resource="http://purl.org/nxp/schema/v1/BasicType"/>
  <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-28</nxp:productStatusDate>
  <skos:prefLabel xml:lang="en-us">N-channel TrenchMOS logic level FET</skos:prefLabel>
  <nxp:productStatus rdf:resource="http://purl.org/nxp/schema/v1/endOfLife"/>
  <foaf:homepage rdf:resource="http://www.nxp.com/pip/PH3330L"/>
  <nxp:typeNumber>PH3330L</nxp:typeNumber>
  <nxp:mechanicalOutline rdf:resource="http://data.nxp.com/packageOutlineVersion/SOT669"/>
</rdf:Description>

</rdf:RDF>

$ ./stardog query  -c http://localhost:5822/nxp -q "DESCRIBE <http://data.nxp.com/basicTypes/PH3330L>" -f TURTLE

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix nxp: <http://purl.org/nxp/schema/v1/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://data.nxp.com/basicTypes/PH3330L> a nxp:BasicType ;
        nxp:productStatusDate "2011-10-28"^^xsd:date ;
        skos:prefLabel "N-channel TrenchMOS logic level FET"@en-us ;
        nxp:productStatus nxp:endOfLife ;
        foaf:homepage <http://www.nxp.com/pip/PH3330L> ;
        nxp:typeNumber "PH3330L" ;
        nxp:mechanicalOutline <http://data.nxp.com/packageOutlineVersion/SOT669> .

You can also return ALL object graphs of a specific type. This will wrap all descriptions in a rdf:RDF tag.
./stardog query  -c http://localhost:5822/nxp -f RDFXML -q "
PREFIX nxp:   <http://purl.org/nxp/schema/v1/>
DESCRIBE ?s
WHERE {
  ?s a nxp:BasicType.
}
" 

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
 xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 xmlns:owl="http://www.w3.org/2002/07/owl#"
 xmlns:foaf="http://xmlns.com/foaf/0.1/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
 xmlns:skos="http://www.w3.org/2004/02/skos/core#"
 xmlns:nxp="http://purl.org/nxp/schema/v1/"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<rdf:Description rdf:about="http://data.nxp.com/basicTypes/74AUP1G57GW">
 <rdf:type rdf:resource="http://purl.org/nxp/schema/v1/BasicType"/>
 <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-20</nxp:productStatusDate>
    ...
</rdf:Description>

<rdf:Description rdf:about="http://data.nxp.com/basicTypes/74AUP1G58GW">
 <rdf:type rdf:resource="http://purl.org/nxp/schema/v1/BasicType"/>
 <nxp:productStatusDate rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2011-10-14</nxp:productStatusDate>
    ...
</rdf:Description>

</rdf:RDF>

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> |
-------------------------------------------