You are here

protected function EasyRdf_Serialiser_GraphViz::serialiseDot in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/GraphViz.php \EasyRdf_Serialiser_GraphViz::serialiseDot()

Internal function to serialise an EasyRdf_Graph into a DOT formatted string

@ignore

2 calls to EasyRdf_Serialiser_GraphViz::serialiseDot()
EasyRdf_Serialiser_GraphViz::renderImage in vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/GraphViz.php
Internal function to render a graph into an image
EasyRdf_Serialiser_GraphViz::serialise in vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/GraphViz.php
Serialise an EasyRdf_Graph into a GraphViz dot document.

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Serialiser/GraphViz.php, line 252

Class

EasyRdf_Serialiser_GraphViz
Class to serialise an EasyRdf_Graph to GraphViz

Code

protected function serialiseDot($graph) {
  $result = "digraph {\n";

  // Write the graph attributes
  foreach ($this->attributes as $k => $v) {
    $result .= '  ' . $this
      ->escape($k) . '=' . $this
      ->escape($v) . ";\n";
  }

  // Go through each of the properties and write the edges
  $nodes = array();
  $result .= "\n  // Edges\n";
  foreach ($graph
    ->resources() as $resource) {
    $name1 = $this
      ->nodeName($resource);
    foreach ($resource
      ->propertyUris() as $property) {
      $label = null;
      if ($this->useLabels) {
        $label = $graph
          ->resource($property)
          ->label();
      }
      if ($label === null) {
        if ($this->onlyLabelled == true) {
          continue;
        }
        else {
          $label = EasyRdf_Namespace::shorten($property);
        }
      }
      foreach ($resource
        ->all("<{$property}>") as $value) {
        $name2 = $this
          ->nodeName($value);
        $nodes[$name1] = $resource;
        $nodes[$name2] = $value;
        $result .= $this
          ->serialiseRow($name1, $name2, array(
          'label' => $label,
        ));
      }
    }
  }
  ksort($nodes);
  $result .= "\n  // Nodes\n";
  foreach ($nodes as $name => $node) {
    $type = substr($name, 0, 1);
    $label = '';
    if ($type == 'R') {
      if ($this->useLabels) {
        $label = $node
          ->label();
      }
      if (!$label) {
        $label = $node
          ->shorten();
      }
      if (!$label) {
        $label = $node
          ->getURI();
      }
      $result .= $this
        ->serialiseRow($name, null, array(
        'URL' => $node
          ->getURI(),
        'label' => $label,
        'shape' => 'ellipse',
        'color' => 'blue',
      ));
    }
    elseif ($type == 'B') {
      if ($this->useLabels) {
        $label = $node
          ->label();
      }
      $result .= $this
        ->serialiseRow($name, null, array(
        'label' => $label,
        'shape' => 'circle',
        'color' => 'green',
      ));
    }
    else {
      $result .= $this
        ->serialiseRow($name, null, array(
        'label' => strval($node),
        'shape' => 'record',
      ));
    }
  }
  $result .= "}\n";
  return $result;
}