Skip to content
This repository
branch: master
Nicholas Humfrey March 04, 2013
file 87 lines (76 sloc) 2.695 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?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-2013 Nicholas J Humfrey
* @license http://unlicense.org/
*/

    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
    require_once "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>
Something went wrong with that request. Please try again.