This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
forked from easyrdf/easyrdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphviz.php
85 lines (75 loc) · 2.61 KB
/
graphviz.php
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
<?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>