SemanticWebApp

This commit is contained in:
M.Scholz 2011-12-05 12:39:56 +01:00
parent 1cf200b106
commit 61d111b2d7
17 changed files with 591 additions and 2 deletions

View File

@ -82,12 +82,12 @@ ma
Für eine bessere Nachvollziehbarkeit sind die Änderungen in diesem Dokument tabellarisch festgehalten.
\begin{tabular}{|| l || p{12cm} ||}
\begin{tabular}{ || l | p{12cm} || }
\hline
v. 0.0.0 - 01.12.2011 - MS & Dokument angelegt
\\ \hline
test & test2
test & dies soll nur ein langer sinnloser text werden, damit man überprüfen kann, ob auch der zeilenumbruch in der tabelle funktioniert.
\\ \hline
test & test2
\\ \hline

View File

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<div align=center>
This Page is only a small Semantic Web Application which allows you to find information
about movies using "Linked Movie Data Base" and "Freebase".
</p>
Which Movie are you interested in?</tab>
<form method="post" action="">
<input type="text" name="movieTitleByUser" size="45">
<input type="submit" name="buttonPressed" value="Search">
</form>
<?php
include 'logic/controller.php';
if(isset($_POST['buttonPressed'])){
$movieTitleByUser =$_POST['movieTitleByUser'];
echo "</p>Getting Infos for: " . $movieTitleByUser;
echo '</br><progress value=$counter max="100">searching . . .</p></progress>';
$mainController = new controller();
$mainController->getData($movieTitleByUser);
}
?>
</p>
<a href="http://www.linkedmdb.org/" target="_blank"><img src="logos/mdb.png" alt="linkedmdb.org"></a>
</br>
<a href="http://www.freebase.com/" target="_blank"><img src="logos/freebase.jpg" alt="freebase.com"></a>
</div>
</body>
</html>

View File

@ -0,0 +1,120 @@
<?php
/**
* Description of controller
*
* @author Michael
*/
include_once './rdfLib/sparqllib.php';
class controller {
static $resultsReady = false;
private function buildNamespaces(){
sparql_ns("owl", "http://www.w3.org/2002/07/owl#");
sparql_ns("xsd", "http://www.w3.org/2001/XMLSchema#");
sparql_ns("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
sparql_ns("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
sparql_ns("foaf", "http://xmlns.com/foaf/0.1/");
sparql_ns("dc", "http://purl.org/dc/elements/1.1/");
sparql_ns("linkedmdbRes", "http://data.linkedmdb.org/resource/movie/");
}
/**
* Funktion organisiert die verschiedenen Quellen und gibt diese
* an die View weiter.
*
*
*/
public function getData($userInput){
//$mdbC = new mdbController();
//$db = sparql_connect( "http://dbpedia.org/sparql" ); //connect to dbpedia's sparql service
$db = sparql_connect("http://data.linkedmdb.org/sparql");
$this->buildNamespaces();
if( !$db ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
sparql_ns("prop", "http://dbpedia.org/property/");
sparql_ns("type", "http://dbpedia.org/class/yago/");
sparql_ns("test1", "http://dbpedia.org/ontology/");
sparql_ns("test2", "http://www.w3.org/2000/01/");
// sparql_ns("test3", "");
//folgende Abfragen funktionieren:
// $sparql = "SELECT ?country_name ?population WHERE {
// ?country a type:LandlockedCountries ;
// rdfs:label ?country_name ;
// prop:populationEstimate ?population .
// FILTER (?population > 15000000) .
// }";
//
//$sparql = "select DISTINCT ?label2 WHERE { ?country <http://dbpedia.org/ontology/capital> ?city . ?country <http://www.w3.org/2000/01/rdf-schema#label> ?label1 . ?city <http://www.w3.org/2000/01/rdf-schema#label> ?label2 . FILTER (str(?label1) = \"".$userInput."\" && LANG(?label2) = \"en\") }";
//$sparql = "SELECT ?movie WHERE { ?arni linkedmdbRes:actor_name \"Arnold Schwarzeneger\" . ?movie linkedmdbRes:actor ?arni . ";
$sparql = "SELECT ?actorName WHERE { ?woody linkedmdbRes:director_name \"Woody Allen\" .
?movie <http://data.linkedmdb.org/resource/movie/director> ?woody;
<http://data.linkedmdb.org/resource/movie/actor> ?actor.
?actor <http://data.linkedmdb.org/resource/movie/actor_name> ?actorName .
}";
$result = $db->query( $sparql );
$resultsReady = true;
if( !$result )
{ print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
$fields = $result->field_array( $result );
// if($userInput == "Test"){
// echo "No way! I won't search infos about " . $userInput;
// }else{
print "<p>Found: ".sparql_num_rows( $result )." entries.</p>";
print "<table border='1'>";
print "<tr>";
foreach( $fields as $field )
{
print "<th>$field</th>";
}
print "</tr>";
while( $row = sparql_fetch_array( $result ) )
{
print "<tr>";
foreach( $fields as $field )
{
print "<td>$row[$field]</td>";
}
print "</tr>";
}
print "</table>";
// }
}
}
?>

View File

@ -0,0 +1,24 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of freebaseController
*
* @author Michael
*/
class freebaseController {
//put your code here
public function getPersonData(){
}
}
?>

View File

@ -0,0 +1,28 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of mdbController
*
* @author Michael
*/
class mdbController {
//put your code here
public function getMovieData(){
}
}
?>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -0,0 +1,5 @@
copy.src.files=true
copy.src.target=/Applications/XAMPP/htdocs/SemanticWebApp
index.file=index.php
run.as=LOCAL
url=http://localhost/SemanticWebApp/

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1">
<file>
<url>logic/controller.php</url>
<line>15</line>
</file>
</editor-bookmarks>
</project-private>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
</project-private>

View File

@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_53
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=true
web.root=.

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>SemanticWebApp</name>
</data>
</configuration>
</project>

View File

@ -0,0 +1,335 @@
<?
###############################
# Christopher Gutteridge 2010
# cjg@ecs.soton.ac.uk
# cc-by License
# http://graphite.ecs.soton.ac.uk/sparqllib/
###############################
function sparql_connect( $endpoint ) { return new sparql_connection( $endpoint ); }
function sparql_ns( $short, $long, $db = null ) { return _sparql_a_connection( $db )->ns( $short, $long ); }
function sparql_query( $sparql, $db = null ) { return _sparql_a_connection( $db )->query( $sparql ); }
function sparql_errno( $db = null ) { return _sparql_a_connection( $db )->errno(); }
function sparql_error( $db = null ) { return _sparql_a_connection( $db )->error(); }
function sparql_fetch_array( $result ) { return $result->fetch_array(); }
function sparql_num_rows( $result ) { return $result->num_rows(); }
function sparql_field_array( $result ) { return $result->field_array(); }
function sparql_field_name( $result, $i ) { return $result->field_name( $i ); }
function sparql_fetch_all( $result ) { return $result->fetch_all(); }
function sparql_get( $endpoint, $sparql )
{
$db = sparql_connect( $endpoint );
if( !$db ) { return; }
$result = $db->query( $sparql );
if( !$result ) { return; }
return $result->fetch_all();
}
function _sparql_a_connection( $db )
{
global $sparql_last_connection;
if( !isset( $db ) )
{
if( !isset( $sparql_last_connection ) )
{
print( "No currect SPARQL connection (connection) in play!" );
return;
}
$db = $sparql_last_connection;
}
return $db;
}
# $timeout = 20;
# $old = ini_set('default_socket_timeout', $timeout);
# ini_set('default_socket_timeout', $old);
class sparql_connection
{
var $db;
var $debug = false;
var $errno = null;
var $error = null;
var $ns = array();
function __construct( $endpoint )
{
$this->endpoint = $endpoint;
global $sparql_last_connection;
$sparql_last_connection = $this;
}
function ns( $short, $long )
{
$this->ns[$short] = $long;
}
function errno() { return $this->errno; }
function error() { return $this->error; }
function query( $query )
{
$this->errno = null;
$this->error = null;
$prefixes = "";
foreach( $this->ns as $k=>$v )
{
$prefixes .= "PREFIX $k: <$v>\n";
}
$url = $this->endpoint."?query=".urlencode( $prefixes.$query );
if( $this->debug ) { print "<div class='debug'><a href='".htmlspecialchars($url)."'>".htmlspecialchars($prefixes.$query)."</a></div>\n"; }
$ch = curl_init($url);
#curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if(curl_errno($ch))
{
$this->errno = curl_errno( $ch );
$this->error = 'Curl error: ' . curl_error($ch);
return;
}
if( $output === '' )
{
$this->errno = "-1";
$this->error = 'URL returned no data';
return;
}
if( $info['http_code'] != 200)
{
$this->errno = $info['http_code'];
$this->error = 'Bad response, '.$info['http_code'].': '.$output;
return;
}
curl_close($ch);
$parser = new xx_xml($output, 'contents');
return new sparql_result( $this, $parser->rows, $parser->fields );
}
}
class sparql_result
{
var $rows;
var $fields;
var $db;
var $i = 0;
function __construct( $db, $rows, $fields )
{
$this->rows = $rows;
$this->fields = $fields;
$this->db = $db;
}
function fetch_array()
{
if( !@$this->rows[$this->i] ) { return; }
$r = array();
foreach( $this->rows[$this->i++] as $k=>$v )
{
$r[$k] = $v["value"];
}
return $r;
}
function fetch_all()
{
$r = new sparql_results();
$r->fields = $this->fields;
foreach( $this->rows as $i=>$row )
{
$r []= $this->fetch_array();
}
return $r;
}
function num_rows()
{
return sizeof( $this->rows );
}
function field_array()
{
return $this->fields;
}
function field_name($i)
{
return $this->fields[$i];
}
}
# class xx_xml adapted code found at http://php.net/manual/en/function.xml-parse.php
# class is cc-by
# hello at rootsy dot co dot uk / 24-May-2008 09:30
class xx_xml {
// XML parser variables
var $parser;
var $name;
var $attr;
var $data = array();
var $stack = array();
var $keys;
var $path;
// either you pass url atau contents.
// Use 'url' or 'contents' for the parameter
var $type;
// function with the default parameter value
function xx_xml($url='http://www.opocot.com', $type='url') {
$this->type = $type;
$this->url = $url;
$this->parse();
}
// parse XML data
function parse()
{
$this->rows = array();
$this->fields = array();
$data = '';
$this->parser = xml_parser_create ("UTF-8");
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startXML', 'endXML');
xml_set_character_data_handler($this->parser, 'charXML');
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
if ($this->type == 'url') {
// if use type = 'url' now we open the XML with fopen
if (!($fp = fopen($this->url, 'rb'))) {
$this->error("Cannot open {$this->url}");
}
while (($data = fread($fp, 8192))) {
if (!xml_parse($this->parser, $data, feof($fp))) {
$this->error(sprintf('XML error at line %d column %d',
xml_get_current_line_number($this->parser),
xml_get_current_column_number($this->parser)));
}
}
} else if ($this->type == 'contents') {
// Now we can pass the contents, maybe if you want
// to use CURL, SOCK or other method.
$lines = explode("\n",$this->url);
foreach ($lines as $val) {
$data = $val . "\n";
if (!xml_parse($this->parser, $data)) {
echo $data.'<br />';
$this->error(sprintf('XML error at line %d column %d',
xml_get_current_line_number($this->parser),
xml_get_current_column_number($this->parser)));
}
}
}
}
function startXML($parser, $name, $attr)
{
if( $name == "result" )
{
$this->result = array();
}
if( $name == "binding" )
{
$this->part = $attr["name"];
}
if( $name == "uri" || $name == "bnode" )
{
$this->part_type = "uri";
$this->chars = "";
}
if( $name == "literal" )
{
$this->part_type = "literal";
if( isset( $attr["datatype"] ) )
{
$this->part_datatype = $attr["datatype"];
}
if( isset( $attr["xml:lang"] ) )
{
$this->part_lang = $attr["xml:lang"];
}
$this->chars = "";
}
if( $name == "variable" )
{
$this->fields[] = $attr["name"];
}
}
function endXML($parser, $name) {
if( $name == "result" )
{
$this->rows[] = $this->result;
$this->result = array();
}
if( $name == "uri" || $name == "bnode" || $name == "literal" )
{
$this->result[$this->part] = array( "type"=>$name, "value"=>$this->chars );
if( isset( $this->part_lang ) )
{
$this->result[$this->part]["lang"] = $this->part_lang;
}
if( isset( $this->part_datatype ) )
{
$this->result[$this->part]["datatype"] = $this->part_datatype;
}
$this->part_datatype = null;
$this->part_lang = null;
}
}
function charXML($parser, $data) {
@$this->chars .= $data;
}
function error($msg) {
echo "<div align=\"center\">
<font color=\"red\"><b>Error: $msg</b></font>
</div>";
exit();
}
}
class sparql_results extends ArrayIterator
{
var $fields;
function fields() { return $this->fields; }
function render_table()
{
$html = "<table class='sparql-results'><tr>";
foreach( $this->fields as $i=>$field )
{
$html .= "<th>?$field</th>";
}
$html .= "</tr>";
foreach( $this as $row )
{
$html.="<tr>";
foreach( $row as $cell )
{
$html .= "<td>".htmlspecialchars( $cell )."</td>";
}
$html.="</tr>";
}
$html.="</table>
<style>
table.sparql-results { border-collapse: collapse; }
table.sparql-results tr td { border: solid 1px #000 ; padding:4px;vertical-align:top}
table.sparql-results tr th { border: solid 1px #000 ; padding:4px;vertical-align:top ; background-color:#eee;}
</style>
";
return $html;exit;
}
}