Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
easyrdf/examples/graphviz.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
85 lines (75 sloc)
2.61 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* GraphViz rendering example | |
* | |
* This example demonstrates converting an EasyRdf\Graph into the | |
* GraphViz graph file language. Using the 'Use Labels' option, you | |
* can have resource URIs replaced with text based labels and using | |
* 'Only Labelled' option, only the resources and properties with | |
* a label will be displayed. | |
* | |
* Rending a graph to an image will only work if you have the | |
* GraphViz 'dot' command installed. | |
* | |
* @package EasyRdf | |
* @copyright Copyright (c) 2012-2014 Nicholas J Humfrey | |
* @license http://unlicense.org/ | |
*/ | |
require_once realpath(__DIR__.'/..')."/vendor/autoload.php"; | |
require_once __DIR__."/html_tag_helpers.php"; | |
$formats = array( | |
'PNG' => 'png', | |
'GIF' => 'gif', | |
'SVG' => 'svg' | |
); | |
$format = \EasyRdf\Format::getFormat( | |
isset($_REQUEST['format']) ? $_REQUEST['format'] : 'png' | |
); | |
// Construct a graph of three people | |
$graph = new \EasyRdf\Graph(); | |
$graph->set('foaf:knows', 'rdfs:label', 'knows'); | |
$bob = $graph->resource('http://www.example.com/bob', 'foaf:Person'); | |
$alice = $graph->resource('http://www.example.com/alice', 'foaf:Person'); | |
$carol = $graph->resource('http://www.example.com/carol', 'foaf:Person'); | |
$bob->set('foaf:name', 'Bob'); | |
$alice->set('foaf:name', 'Alice'); | |
$carol->set('foaf:name', 'Carol'); | |
$bob->add('foaf:knows', $alice); | |
$bob->add('foaf:knows', $carol); | |
$alice->add('foaf:knows', $bob); | |
$alice->add('foaf:knows', $carol); | |
// Create a GraphViz serialiser | |
$gv = new \EasyRdf\Serialiser\GraphViz(); | |
$gv->setUseLabels(isset($_REQUEST['ul'])); | |
$gv->setOnlyLabelled(isset($_REQUEST['ol'])); | |
// If this is a request for the image, just render it and exit | |
if (isset($_REQUEST['image'])) { | |
header("Content-Type: ".$format->getDefaultMimeType()); | |
echo $gv->renderImage($graph, $format); | |
exit; | |
} | |
?> | |
<html> | |
<head><title>EasyRdf GraphViz Example</title></head> | |
<body> | |
<h1>EasyRdf GraphViz Example</h1> | |
<form action='' method='get'> | |
<?php | |
echo label_tag('format').' '.select_tag('format', $formats).tag('br'); | |
echo label_tag('ul', 'Use labels:').' '.check_box_tag('ul').tag('br'); | |
echo label_tag('ol', 'Only labelled:').' '.check_box_tag('ol').tag('br'); | |
echo submit_tag(); | |
?> | |
</form> | |
<div> | |
<img src='?image&<?=$_SERVER["QUERY_STRING"]?>' /> | |
</div> | |
<pre style="margin: 0.5em; padding:0.5em; background-color:#eee; border:dashed 1px grey;"> | |
<?php | |
print htmlspecialchars( | |
$gv->serialise($graph, 'dot') | |
); | |
?> | |
</pre> | |
</body> | |
</html> |