Mailing list ARC-DEV: Archives

Re: [arc-dev] SPARQL UNION

From: =?ISO-8859-1?Q?Michael_D=FCpjohann?= 
Subject: Re: [arc-dev] SPARQL UNION
Date: Thu, 27 Aug 2009 12:45:16 +0200


Hallo again,
after working, testing and understanding how to juse ARC2 i want to post 
my testfiles to the mailinglist for other people. I hope to give them an 
easy way to understand this system.
I jused two files:
-------------------------------------wichtig.inc.php--------------------
<?php

function config () {
//####################CONFIG##########
   include_once("../../arc/ARC2.php"); //pass to arc2
   $config = array(
     /* db */
     'db_name' => 'arctest1',
     'db_user' => 'root',
     'db_pwd' => '',
     /*  store */
     'store_name' => 'arc_tests',
   );
   /* instantiation */
   $store = ARC2::getStore($config);
   //Creating the MySQL tables
   if (!$store->isSetUp()) {
     $store->setUp();
   }
   echo "Konfiguration abgeschlossen";
   return $store;
//##################################
}

//prints a table with the rdf content of the sparql-query
function select_ausgabe ($rs) {

   $r = '';
   $variables = $rs[result][variables];
   $rows = $rs[result][rows];
     if (empty($variables)) {
     echo "<h1>Fehler</h1>";
     return;
   }

   //------ Table header
   foreach ($variables as $var)
     $r .= '<th>' . $var . '</th>';
   $r = '<tr>'. $r .'</tr>';     
   //------ Table content
   if ($rows) {
     foreach ($rows as $row) {
       $r .= '<tr>';
       foreach ($variables as $var)             $r .= '<td>' . 
$row[$var] . '</td>';
       $r .= '</tr>';      }
   }

   echo $r ? '<table border="1">' . $r . '</table>' : 'no named persons 
found';

}

?>
--------------------------------------end of wichtig.inc.php------------
second file:
------------------------------------------index.php--------------------
<html>
 <head>
   <title>ARC</title>
 </head>
 <body>
   <h1>ARC TEST 2</h1>
  <? include_once("wichtig.inc.php");
$store = config();

//--- als Tripel darstellen

$index = array(
 '_:john' => array(
   'http://xmlns.com/foaf/0.1/knows' => array(
     array('value' => '_:bill', 'type' => 'bnode'),
     array('value' => '_:bob', 'type' => 'bnode'),
   ),
 ),
);

$triples = ARC2::getTriplesFromIndex($index);

//$triples = $parser->getTriples();
//for ($i = 0, $i_max = count($triples); $i < $i_max; $i++) {
//  $triple = $triples[$i];
//}

//echo "<pre>SESSION\n"; print_r($triples); echo "</pre>\n";

//------ RDF in die DB einlesen aus externer Ressource

$parser = ARC2::getRDFXMLParser();
//$parser->parse('http://jena.sourceforge.net/ARQ/Tutorial/vc-db-1.rdf');
$parser->parse('http://jena.sourceforge.net/ARQ/Tutorial/vc-db-2.rdf');
//$parser->parse('http://scot-project.org/scot/ns');
$triples = $parser->getTriples();
$index = $parser->getSimpleIndex();


//-------------RDF speichern
echo "<pre>SESSION\n"; print_r($index); echo "</pre>\n";
//$doc = $ser->getSerializedTriples($triples);
//echo $doc;

array($subjekt);
array($praedikat);
$subjekt = array_keys($index);
$i_max_s = count($subjekt);

for ($i = 0; $i < $i_max_s; $i++) {
   $praedikat =  array_keys($index[$subjekt[$i]]);
   $i_max_p = count($praedikat);
   for ($j = 0; $j < $i_max_p; $j++) {
     $s = $subjekt[$i];
     $p = $praedikat[$j];
     $o = $index[$s][$p][0];
     $q= "
         INSERT INTO <http://www.md-technologie.de/> {
         <$subjekt[$i]> <$praedikat[$j]> \"$o\" .
         }";

     $q1= "
         INSERT INTO < http://www.md-technologie.de/> {
         < $subjekt[$i]> < $praedikat[$j]> \"$o\" .
         }";

     //$rs = $store->query($q);
//      echo $q1."<br>";
//      if (!$store->getErrors()) {
//        $rows = $rs['result']['rows'];
//        echo "Es sind Fehler aufgetreten";
//      }
     }

}

//--------------------RDF auslesen
array($qs);

$qs[0] = ' SELECT * WHERE { ?subjekt ?praedikat ?objekt .}';


$qs[1] = 'SELECT ?x
WHERE { ?x  <http://www.w3.org/2001/vcard-rdf/3.0#FN>  "Matt Jones"}';

$queryd = "DELETE FROM <http://www.md-technologie.de/>";

$qs[2] = "SELECT ?x ?fname
WHERE {?x  <http://www.w3.org/2001/vcard-rdf/3.0#FN>  ?fname}";

$qs[3] = 'PREFIX vcard:      <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?y ?givenName
WHERE
{ ?y vcard:Family "Smith" .
  ?y vcard:Given  ?givenName .
}';


$qs[4] = 'PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?g
WHERE
{ ?y vcard:Given ?g .
 FILTER regex(?g, "r", "i") }';

/*
$qi = 'Insert INTO <http://somewhere/RebeccaSmith/> WHERE {
   info:age "23"^^xsd:integer ;
   vCard:FN "Becky Smith" ;
   vCard:N [ vCard:Family "Smith" ;
             vCard:Given  "Rebecca" ] .}';
*/

$qs[5] = 'PREFIX info: <http://somewhere/peopleInfo#>

SELECT ?resource
WHERE
 {
   ?resource info:age ?age .
   FILTER (?age >= 20)
 }';


$qs[6] = '
PREFIX info:    <http://somewhere/peopleInfo#>
PREFIX vcard:   <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?name ?age
WHERE
{
   ?person vcard:FN  ?name .
   OPTIONAL { ?person info:age ?age }
}';

$qs[7] = 'PREFIX info:        <http://somewhere/peopleInfo#>
PREFIX vcard:      <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?name ?age
WHERE
{
   ?person vcard:FN  ?name .
   OPTIONAL { ?person info:age ?age . }
   FILTER ( !bound(?age) || ?age > 24 )
}';

$qs[8] = '
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT *
WHERE
{
[] ?p ?name
FILTER ( ?p = foaf:name || ?p = vCard:FN )

}';

$qs[9] = '
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT *
WHERE
{
 {[] vCard:Family ?name1} UNION { [] vCard:FN ?name2 }

}
';

$qs[10] = 'SELECT *
{
{ ?s ?p ?o } OPTIONAL { GRAPH ?g { ?s ?p ?o } }
}';

$qs[11] = '
PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT *
{
GRAPH <http://www.md-technologie.de/> { [] vCard:FN ?name2 }
}';

     $i = 0;
     $rs = $store->query($qs[$i]);
     echo "Query: <br />".$qs[$i]."<br><br>";         //echo 
"<pre>Query\n"; print_r($rs); echo "</pre>\n";
select_ausgabe($rs);

?>
 </body>
</head>
-------------------------------------end of index.php-------------------





-- 
Greetings
Michael Düpjohann


""" ;
         ns1:returnPath "<md22@zitmail.uni-paderborn.de>" ;
         ns1:xOriginalTo "arc-dev@semsol.org" ;
         ns1:deliveredTo "web11p1@p15192371.pureserver.info" ;
         ns1:received """from p4fc4c491.dip.t-dialin.net ([79.196.196.145] helo=[192.168.2.102])
	by mail.uni-paderborn.de with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32)
	(Exim 4.63 spheron)
	id 1MgcTo-0001Zi-Gd
	for arc-dev@semsol.org; Thu, 27 Aug 2009 12:45:21 +0200""" ;
         ns1:messageID "<4A9663BC.6010206@zitmail.uni-paderborn.de>" ;
         ns1:date "Thu, 27 Aug 2009 12:45:16 +0200" ;
         ns1:from "=?ISO-8859-1?Q?Michael_D=FCpjohann?= <md22@zitmail.uni-paderborn.de>" ;
         ns1:userAgent "Thunderbird 2.0.0.23 (Windows/20090812)" ;
         ns1:mIMEVersion "1.0" ;
         ns1:to "arc-dev <arc-dev@semsol.org>" ;
         ns1:subject "Re: [arc-dev] SPARQL UNION" ;
         ns1:references "<4A65D6B7.9090209@cn.ntua.gr>" ;
         ns1:inReplyTo "<4A65D6B7.9090209@cn.ntua.gr>" ;
         ns1:contentType "text/plain; charset=ISO-8859-1; format=flowed" ;
         ns1:contentTransferEncoding "8bit" ;
         ns1:xIMTSpamScore "0.0 ()" ;
         ns1:xPMXVersion "5.5.7.378829, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2009.8.27.103616" ;
         ns1:xIMTAuthenticatedSender "uid=md22,ou=People,o=upb,c=de" ;
         ns1:xSpamCheckerVersion """SpamAssassin 2.64 (2004-01-11) on 
	p15192371.pureserver.info""" ;
         ns1:xSpamLevel "" ;
         ns1:xSpamStatus """No, hits=-1.4 required=5.0 tests=AWL,BAYES_00,
	FROM_ENDS_IN_NUMS,HTML_MESSAGE autolearn=no version=2.64""" ;
         ns1:xUIDL """VCk"!3X=!!Q('!!TS6"!