function renderCablingPlan() { // Build cabling plan // Select edges $sql = "SELECT oa.id AS source, ob.id AS target, CONCAT(pa.name, _utf8' <> ', pb.name) AS label, 0 AS weight " . "FROM ((Link l JOIN Port pa ON l.porta = pa.id) JOIN RackObject oa ON pa.object_id = oa.id " . "JOIN Port pb ON l.portb = pb.id JOIN RackObject ob ON pb.object_id = ob.id)"; $result = usePreparedSelectBlade($sql); $edges = array(); while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $found = FALSE; foreach ($edges as $key => $edge) { if ($edge['source'] == $row['source'] && $edge['target'] == $row['target']) { // Edge already exists ("Parallel"). Alter label and add weight $edges[$key]['label'] .= "\n" . $row['label']; $edges[$key]['weight']++; $found = TRUE; } } if (!$found) { $edges[] = $row; } } // Select nodes $sql = "SELECT DISTINCT o.id AS id, o.name AS label, '' AS url " . "FROM Port p JOIN RackObject o ON p.object_id = o.id " . "WHERE (p.id IN (SELECT Link.porta AS porta FROM Link) OR p.id IN " . "(SELECT Link.portb AS portb FROM Link))"; $result = usePreparedSelectBlade($sql); $nodes = $result->fetchAll(PDO::FETCH_ASSOC); $graph = new Image_GraphViz(TRUE, array(), 'Cabling Plan', FALSE, FALSE); foreach ($nodes as $node) { $graph->addNode($node['id'], array('label' => $node['label'], 'shape' => 'box3d')); } foreach ($edges as $edge) { $graph->addEdge(array($edge['source'] => $edge['target']), array('label' => $edge['label'], 'weight' => floatval($edge['weight']), 'fontsize' => 8.0, 'arrowhead' => 'dot', 'arrowtail' => 'dot', 'arrowsize' => 0.5)); } if (in_array($_REQUEST['format'], array('svg', 'png'))) { $graph->image($_REQUEST['format']); } }
function show_bgpmap($data) { // return a bgp map in a png file global $graph, $nodes, $edges; $graph = new Image_GraphViz(true, array(), "BGPMAP"); $nodes = array(); $edges = array(); function escape($label) { $label = str_replace("&", "&", $label); $label = str_replace(">", ">", $label); $label = str_replace("<", "<", $label); return $label; } function add_node($_as, $fillcolor = NULL, $label = NULL, $shape = NULL) { global $nodes, $graph; if (!array_key_exists($_as, $nodes)) { if (!$label) { $label = "AS" . $_as; } $label = '<table cellborder="0" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">' . str_replace("\r", "<br/>", escape($label)) . '</td></tr></table>'; $attrs = array('style' => 'filled', 'fontsize' => '10', 'label' => $label); if ($shape) { $attrs['shape'] = $shape; } if ($fillcolor) { $attrs['fillcolor'] = $fillcolor; } $nodes[$_as] = $attrs; $graph->addNode($_as, $nodes[$_as]); } return $nodes[$_as]; } function add_edge($_previous_as, $_as, $attrs = array()) { global $edges, $graph; $edge_array = array($_previous_as => $_as); if (!array_key_exists(gek($_previous_as, $_as), $edges)) { $attrs['splines'] = "true"; $edge = array($edge_array, $attrs); $graph->addEdge($edge_array, $attrs); $edges[gek($_previous_as, $_as)] = $edge; } else { if (array_key_exists('label', $attrs)) { $e =& $edges[gek($_previous_as, $_as)]; $label_without_star = str_replace("*", "", $attrs['label']); $labels = explode("\r", $e[1]['label']); if (!in_array($label_without_star . "*", $labels)) { $tmp_labels = array(); foreach ($labels as $l) { if (!startsWith($l, $label_without_star)) { $labels[] = $l; } } $labels = array_merge(array($attrs['label']), $tmp_labels); $cmp = function ($a, $b) { return endsWith($a, "*") ? -1 : 1; }; usort($labels, $cmp); $label = escape(implode("\r", $labels)); $e[1]['label'] = $label; } } } return $edges[gek($_previous_as, $_as)]; } function gek($_previous_as, $_as) { // Generate Edge Key return $_previous_as . '_' . $_as; } foreach ($data as $host => $asmaps) { add_node($host, "#F5A9A9", strtoupper($host), "box"); if ($host == Config::NODE_NAME) { $node = add_node(Config::ASN, "#F5A9A9"); $edge = add_edge(Config::ASN, $host, array('color' => "red", 'style' => "bold")); } } $previous_as = NULL; $hosts = array_keys($data); foreach ($data as $host => $asmaps) { $first = true; foreach ($asmaps as $asmap) { $previous_as = $host; $color = "#" . dechex(rand(0, 16777215)); $hop = false; $hop_label = ""; foreach ($asmap as $_as) { if ($_as == $previous_as) { continue; } if (!$hop) { $hop = true; if (!in_array($_as, $hosts)) { $hop_label = $_as; if ($first) { $hop_label = $hop_label . "*"; } continue; } else { $hop_label = ""; } } if (!strpos($_as, '.')) { add_node($_as, $first ? "#F5A9A9" : "white"); } else { add_node($_as, $first ? "#F5A9A9" : "white", NULL, "box"); } if ($hop_label) { $attrs = array('fontsize' => "7", 'label' => $hop_label); } else { $attrs = array('fontsize' => "7"); } $hop_label = ""; if ($first) { $attrs['style'] = "bold"; $attrs['color'] = "red"; } else { if (!array_key_exists(gek($previous_as, $_as), $edges) || $edges[gek($previous_as, $_as)][1]['color'] != "red") { $attrs['style'] = "dashed"; $attrs['color'] = $color; } } add_edge($previous_as, $_as, $attrs); $previous_as = $_as; } $first = false; } } return $graph->image("jpeg"); }
$as_e_tmp = "AS" . $as; $as_info_dns = dns_get_record($as_e_tmp . ".asn.cymru.com", DNS_TXT); list($as_info['as'], $as_info['country'], $as_info['rir'], $as_info['date'], $as_info['desc']) = explode("|", $as_info_dns[0]['txt']); $asinfo = explode(" ", $as_info['desc']); $as_e = $as_e_tmp . " " . $asinfo[1]; $gv->addEdge(array($as_s => $as_e)); $as_s = $as_e; } } } } } mysqli_close($mid); switch ($gixlg['graphviz_mode']) { // Direct function call case "direct": $gv->image('jpg'); break; // Executing dot parser from cmd line // Executing dot parser from cmd line case "cmd": $dot = $gv->parse(); header("Content-type: image/jpg"); passthru("echo '{$dot}' | /usr/local/bin/dot -Tjpg"); break; case "cmd_svg": $dot = $gv->parse(); header("Content-type: image/svg+xml"); passthru("echo '{$dot}' | /usr/local/bin/dot -Tsvg"); break; }
public function getOutput() { //echo get_include_path (); require_once 'Image/GraphViz.php'; $race = Dolumar_Races_Race::getFromId(Neuron_Core_Tools::getInput('_GET', 'race', 'int', 1)); $default_settings = array('fontsize' => 8); $default_node_settings = $default_settings; $default_node_settings['shape'] = 'box'; $tech_atts = $default_node_settings; $tech_atts['bgcolor'] = 'ff0000'; $tech_atts['color'] = 'blue'; $equip_atts = $default_node_settings; $equip_atts['bgcolor'] = 'ff0000'; $equip_atts['color'] = 'red'; $equip_atts['rankdir'] = 'tb'; $equip_atts['constraint'] = false; $unit_atts = $default_node_settings; $unit_atts['bgcolor'] = 'ff0000'; $unit_atts['color'] = 'green'; $arrow_atts = $default_settings; $arrow_atts['arrowType'] = 'normal'; $arrow_atts['fontsize'] = '6'; $grayarrow_atts = $arrow_atts; $grayarrow_atts['color'] = 'gray'; define('UNIT_PREFIX', '[U] '); define('TECHNOLOGY_PREFIX', '[T] '); define('BUILDING_PREFIX', '[B] '); define('EQUIPMENT_PREFIX', '[E] '); $show_equipment = Neuron_Core_Tools::getInput('_GET', 'equipment', 'int', 0) == 1; $show_technology = Neuron_Core_Tools::getInput('_GET', 'technology', 'int', 0) == 1; $show_units = Neuron_Core_Tools::getInput('_GET', 'units', 'int', 0) == 1; $gv = new Image_GraphViz(true, array('label' => $race->getName(), 'labelloc' => 't')); // All buildings $gv->addCluster("BUILDINGS", "Buildings"); if ($show_technology) { $gv->addCluster("TECHNOLOGY", "Technology"); } //$gv->addCluster ("EQUIPMENT", "Equipment"); if ($show_equipment) { $gv->addCluster("weapon", "Weapons", array('rotate' => '90')); $gv->addCluster("armour", "Armour", array()); } if ($show_units) { $gv->addCluster("UNITS", "Units"); } $buildings = Dolumar_Buildings_Building::getBuildingObjects($race); foreach ($buildings as $building) { $building->setVillage(new Dolumar_Players_DummyVillage($race)); //$building->setRace ($race); // Add building $gv->addNode(BUILDING_PREFIX . $building->getName(), $default_node_settings, "BUILDINGS"); // Add building requirements foreach ($building->getRequiredBuildings() as $req) { $label = $req['amount'] . '+'; $gv->addEdge(array(BUILDING_PREFIX . $req['building']->getName() => BUILDING_PREFIX . $building->getName()), array_merge($arrow_atts, array('label' => $label))); } // Technologies if ($show_technology) { foreach ($building->getTechnologies() as $tech) { $label = 'Level ' . $tech->getMinLevel(); $gv->addNode(TECHNOLOGY_PREFIX . $tech->getName(), $tech_atts, "TECHNOLOGY"); $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => TECHNOLOGY_PREFIX . $tech->getName()), array_merge($arrow_atts, array('label' => $label))); // requirements for the technologies? foreach ($tech->getRequiredTechnologies() as $req) { $label = null; $gv->addEdge(array(TECHNOLOGY_PREFIX . $req->getName() => TECHNOLOGY_PREFIX . $tech->getName()), array_merge($arrow_atts, array('label' => $label))); } } } // Equipment if ($building instanceof Dolumar_Buildings_Crafting && $show_equipment) { foreach ($building->getEquipment() as $equip) { //$gv->addNode (EQUIPMENT_PREFIX . $equip->getName (), $equip_atts, "EQUIPMENT"); $gv->addNode(EQUIPMENT_PREFIX . $equip->getName(), $equip_atts, $equip->getItemType()); // Arrow to this building $label = $equip->getRequiredLevel() > 0 ? 'Level ' . $equip->getRequiredLevel() : null; //$gv->addEdge (array (BUILDING_PREFIX . $building->getName () => EQUIPMENT_PREFIX . $equip->getName ()), array_merge ($arrow_atts, array ('label' => $label))); $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => EQUIPMENT_PREFIX . $equip->getName()), array_merge($grayarrow_atts, array('label' => $label, 'ltail' => 'EQUIPMENT'))); // Required technologies? foreach ($equip->getRequiredTechnologies() as $tech) { $label = null; $gv->addEdge(array(TECHNOLOGY_PREFIX . $tech->getName() => EQUIPMENT_PREFIX . $equip->getName()), array_merge($grayarrow_atts, array('label' => $label))); } } } // Units if ($building instanceof Dolumar_Buildings_Training && $show_units) { foreach ($building->getUnits() as $unit) { // Units! $gv->addNode(UNIT_PREFIX . $unit->getName(), $unit_atts, "UNITS"); // Arrow to this building $label = null; $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => UNIT_PREFIX . $unit->getName()), array_merge($grayarrow_atts, array('label' => $label))); // Required technologies? foreach ($unit->getRequiredTechnologies() as $tech) { $label = null; $gv->addEdge(array(TECHNOLOGY_PREFIX . $tech->getName() => UNIT_PREFIX . $unit->getName()), array_merge($grayarrow_atts, array('label' => $label))); } } } } // All equipment /* $gv->addCluster ("EQUIPMENT", "Equipment"); $eqs = Dolumar_Players_Equipment::getAllEquipment(); foreach ($eqs as $building) { $gv->addNode ($building->getName (), $equip_atts, "EQUIPMENT"); // Required technologies? foreach ($building->getRequiredTechnologies () as $tech) { $label = null; $gv->addEdge (array ($tech->getName () => $building->getName ()), array_merge ($arrow_atts, array ('label' => $label))); } } */ // All units $output = Neuron_Core_Tools::getInput('_GET', 'engine', 'varchar'); switch ($output) { case "dot": case "neato": break; default: $output = "dot"; break; } if (!$gv->image("png", $output)) { echo "Error... Is Graphviz installed?"; } }
$graph->addNode($nodeName, array('label' => $nodeLabel, 'shape' => 'octagon', 'fontsize' => '8')); if (sizeof($contact->relations) > 0) { foreach ($contact->relations as $relation) { if ($relation->meetInPerson) { $colorRelation = 'black'; } else { $colorRelation = 'grey'; } if (strlen($relation->nombre) > 0) { $nodeNameRelation = camelCase($relation->nombre); $nodeLabelRelation = $relation->nombre; if (strlen($relation->empresa) > 0) { $nodeLabelRelation .= "\n" . $relation->empresa; } } elseif (strlen($relation->empresa) > 0) { $nodeNameRelation = camelCase($relation->empresa); $nodeLabelRelation = $relation->empresa; } else { $nodeNameRelation = "node{$i}"; $nodeLabelRelation = '(nada)'; } if (!$graph->hasNode($nodeNameRelation)) { $graph->addNode($nodeNameRelation, array('label' => $nodeLabelRelation)); } $graph->addEdge(array($nodeName => $nodeNameRelation), array('color' => $colorRelation)); } } $i++; } $graph->image();
/** * drawRequirementsGraph Will draw a requirement graph if PEAR::Image_GraphViz is installed * * @param boolean $pInstallVersion Use the actual installed version instead of the version that will be in bitweaver after the upgrade * @param string $pFormat dot output format * @param string $pCommand dot or neato * @access public * @return boolean TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function drawRequirementsGraph($pInstallVersion = FALSE, $pFormat = 'png', $pCommand = 'dot') { global $gBitSmarty, $gBitThemes; // only do this if we can load PEAR GraphViz interface if (@(include_once 'Image/GraphViz.php')) { ksort($this->mPackages); $deps = $this->calculateRequirements($pInstallVersion); $delKeys = $matches = array(); // crazy manipulation of hash to remove duplicate version matches. // we do this that we can use double headed arrows in the graph below. foreach ($deps as $key => $req) { foreach ($deps as $k => $d) { if ($req['requires'] == $d['package'] && $req['package'] == $d['requires'] && $req['result'] == 'ok' && $d['result'] == 'ok') { $deps[$key]['dir'] = 'both'; $matches[$key] = $k; } } } // get duplicates foreach ($matches as $key => $match) { unset($delKeys[$match]); $delKeys[$key] = $match; } // remove dupes from hash foreach ($delKeys as $key) { unset($deps[$key]); } // start drawing stuff $graph = new Image_GraphViz(TRUE, $gBitThemes->getGraphvizGraphAttributes(), 'Requirements', TRUE); $fromattributes = $toattributes = $gBitThemes->getGraphvizNodeAttributes(); foreach ($deps as $node) { //$fromNode = ucfirst( $node['package'] )."\n".$node['package_version']; //$toNode = ucfirst( $node['requires'] )."\n".$node['required_version']['min']; $fromNode = ucfirst($node['package']); $toNode = ucfirst($node['requires']); switch ($node['result']) { case 'max_dep': $edgecolor = 'chocolate3'; $label = 'Maximum version\\nexceeded'; $toNode .= "\n" . $node['required_version']['min'] . " - " . $node['required_version']['max']; $toattributes['fillcolor'] = 'khaki'; break; case 'min_dep': $edgecolor = 'crimson'; $label = 'Minimum version\\nnot met'; $toNode .= "\n" . $node['required_version']['min']; if (!empty($node['required_version']['max'])) { $toNode .= " - " . $node['required_version']['max']; } $toattributes['fillcolor'] = 'pink'; break; case 'missing': $edgecolor = 'crimson'; $label = 'Not installed\\nor activated'; $toNode .= "\n" . $node['required_version']['min']; if (!empty($node['required_version']['max'])) { $toNode .= " - " . $node['required_version']['max']; } $toattributes['fillcolor'] = 'pink'; break; default: $edgecolor = ''; $label = ''; $toattributes['fillcolor'] = 'white'; break; } $fromattributes['URL'] = "http://www.bitweaver.org/wiki/" . ucfirst($node['package']) . "Package"; $graph->addNode($fromNode, $fromattributes); $toattributes['URL'] = "http://www.bitweaver.org/wiki/" . ucfirst($node['requires']) . "Package"; $graph->addNode($toNode, $toattributes); $graph->addEdge(array($fromNode => $toNode), $gBitThemes->getGraphvizEdgeAttributes(array('dir' => !empty($node['dir']) ? $node['dir'] : '', 'color' => $edgecolor, 'fontcolor' => $edgecolor, 'label' => $label))); } if (preg_match("#(png|gif|jpe?g|bmp|svg|tif)#i", $pFormat)) { $graph->image($pFormat, $pCommand); } else { return $graph->fetch($pFormat, $pCommand); } } else { return FALSE; } }
function imprimir_afd() { $this->layout = 'empty'; $this->thompson->generarAFND(); $this->thompson->AFNDaAFD(); $AFD = $this->thompson->getAFD(); $estados = $AFD->getEstados(); $graph = new Image_GraphViz(true, array(), 'AFD', false); $graph->setAttributes(array('size' => '8,5', 'rankdir' => 'LR')); $transiciones = array(); foreach ($estados as $estado) { if (!$estado->esFinal()) { $graph->addNode('q' . $estado->getID(), array('shape' => 'circle')); } else { $graph->addNode('q' . $estado->getID(), array('shape' => 'doublecircle')); } if ($estado->esInicial()) { $graph->addNode('null', array('shape' => 'plaintext', 'label' => '')); $graph->addEdge(array('null' => 'q' . $estado->getID()), array('label' => '')); } $transiciones = $estado->getArregloTransiciones(); if (!empty($transiciones)) { foreach ($transiciones as $simbolo => $destinos) { foreach ($destinos as $destino) { $graph->addEdge(array('q' . $estado->getID() => 'q' . $destino), array('label' => $simbolo)); } } } } $graph->image(); }
} } // All equipment /* $gv->addCluster ("EQUIPMENT", "Equipment"); $eqs = Dolumar_Players_Equipment::getAllEquipment(); foreach ($eqs as $building) { $gv->addNode ($building->getName (), $equip_atts, "EQUIPMENT"); // Required technologies? foreach ($building->getRequiredTechnologies () as $tech) { $label = null; $gv->addEdge (array ($tech->getName () => $building->getName ()), array_merge ($arrow_atts, array ('label' => $label))); } } */ // All units $output = Neuron_Core_Tools::getInput('_GET', 'engine', 'varchar'); switch ($output) { case "dot": case "neato": break; default: $output = "dot"; break; } if (!$gv->image("png", $output)) { echo "Error... Is Graphviz installed?"; }