Virtlab:Řídící server/ParserTopology.php.inc

Z VirtlabWiki

(Rozdíly mezi verzemi)
Přejít na: navigace, hledání
Verze z 12:34, 21. 2. 2007
Vav166 (Diskuse | příspěvky)
(Popis funkcí)
← Předchozí porovnání
Verze z 12:40, 21. 2. 2007
Vav166 (Diskuse | příspěvky)
(doplněn zdrojový kod)
Následující porovnání →
Řádka 79: Řádka 79:
[0] => rd [0] => rd
) )
 +
 +== Zdrojový kód ==
 +<pre>
 +<?php
 +
 +class virtlabParserTopology {
 + private $parsed = array();
 + private $edges = array();
 + private $vertexes = array();
 + private $cached_edges = 0;
 + private $cached_vertexes = 0;
 +
 + function __construct($file, $is_file=0, $cache_ed=1, $cache_ve=1) {
 + $this->virtlabParserTopology($file, $is_file, $cache_ed, $cache_ve);
 + }//konstruktor
 +
 + private function virtlabParserTopology($file, $is_file=0, $cache_ed=1, $cache_ve=1) {
 + $parser = new virtlabXmlParser(1);
 +
 + if($is_file) $parser->parse($file);
 + else $parser->parse_data($file);
 +
 + $this->parsed = $parser->output;
 + unset($parser);
 +
 + if($cache_ed) {
 + $this->edges = $this->getEdges();
 + $this->cached_edges = 1;
 + }
 +
 + if($cache_ve) {
 + $this->vertexes = $this->getVertexes();
 + $this->cached_vertexes = 1;
 + }
 + }//function
 +
 +
 + public function getEdges() {
 + if(!$this->cached_edges) {
 + $temp = $this->parsed[0]["child"];
 + do {
 + $temp2 = array_pop($temp);
 + } while($temp2[name]=="VERTEX_DETAIL");
 + array_push($temp, $temp2);
 + return $temp;
 + }
 + else return $this->edges;
 + }//function
 +
 +
 + public function getEdgesCount() {
 + if(!$this->cached_edges)
 + {
 + $temp = $this->getEdges();
 + return count($temp);
 + }
 + else return count($this->edges);
 + }//function
 +
 +
 + public function getEdge($index) {
 + $temp = $this->getEdges();
 + return $temp[$index];
 + }//function
 +
 +
 + public function getEdgeName($index) {
 + $temp = $this->getEdge($index);
 + return $temp["attribs"]["NAME"];
 + }//function
 +
 +
 + public function getEdgeByName($name, &$index) {
 + for($i = $this->getEdgesCount(); $i>=0; $i--)
 + {
 + if($this->getEdgeName($i) == $name) {
 + $index = $i;
 + return $this->getEdge($i);
 + }
 + }
 + return NULL;
 + }//function
 +
 +
 + public function getEdgeTechnology($index) {
 + $temp = $this->getEdge($index);
 + return $temp["attribs"]["TECHNOLOGY"];
 + }//function
 +
 +
 + public function getEdgeEthertype($index) {
 + $temp = $this->getEdge($index);
 + if($temp["attribs"]["ETHER_TYPE"])
 + return $temp["attribs"]["ETHER_TYPE"];
 + else return NULL;
 + }//function
 +
 +
 + public function getEdgeMinbps($index) {
 + $temp = $this->getEdge($index);
 + if($temp["child"][2]["name"]=="MIN_BPS")
 + return $temp["child"][2]["content"];
 + else return NULL;
 + }//function
 +
 +
 + public function getEdgeVertexes($index) {
 + $temp = $this->getEdge($index);
 + $vertexes = array();
 + array_push($vertexes, $temp["child"][0]["attribs"]["NAME"]);
 + array_push($vertexes, $temp["child"][1]["attribs"]["NAME"]);
 + return $vertexes;
 + }//function
 +
 +
 + public function getEdgesByVertex($name) {
 + $result = array();
 + for($i = $this->getEdgesCount(); $i>=0; $i--) {
 + $vertexes = $this->getEdgeVertexes($i);
 + foreach($vertexes as $hodnota) {
 + if($hodnota == $name) {
 + //array_push($result, $this->getEdgeName($i));
 + $result[$i] = $this->getEdgeName($i);
 + break;
 + }//if
 + }//foreach
 + }//for
 + if(!$result) return NULL;
 + else return $result;
 + }//function
 +
 +
 + public function getEdgeFeatures($index) {
 + $temp = $this->getEdge($index);
 + $result = array();
 + foreach($temp["child"] as $hodnota) {
 + if($hodnota["name"] == "EDGE_FEATURE") {
 + array_push($result, $hodnota["content"]);
 + }
 + }
 + if(!$result[0]) return NULL;
 + else return $result;
 + }//function
 +
 +
 + public function getVertexes() {
 + if(!$this->cached_vertexes) {
 + $temp3 = array();
 + $temp = $this->parsed[0]["child"];
 + $temp2 = array_pop($temp);
 + while($temp2[name]=="VERTEX_DETAIL") {
 + array_push($temp3, $temp2);
 + $temp2 = array_pop($temp);
 + }
 + return $temp3;
 + }
 + else return $this->vertexes;
 + }//function
 +
 +
 + public function getVertexesCount() {
 + return(count($this->getVertexes()));
 + }//function
 +
 +
 + public function getVertex($index) {
 + $temp = $this->getVertexes();
 + return $temp[$index];
 + }//function
 +
 +
 + public function getVertexName($index) {
 + $temp = $this->getVertex($index);
 + return $temp["attribs"]["NAME"];
 + }//function
 +
 +
 + public function getVertexByName($name) {
 + $temp = $this->getVertexes();
 + foreach($temp as $hodnota) {
 + if($hodnota["attribs"]["NAME"] == $name)
 + return $hodnota;
 + }
 + return NULL;
 + }//function
 +
 +
 + public function getVertexPlatforms($name, $variant=1) {
 + $temp = $this->getVertexByName($name);
 + if($temp["child"][0]["name"] == "POSS_PLATFORMS") {
 + if($variant) {
 + $temp2 = array();
 + foreach($temp["child"][0]["child"] as $hodnota) {
 + array_push($temp2, $hodnota["content"]);
 + }//foreach
 + return $temp2;
 + }//if-variant
 + else return $temp["child"][0]["child"];
 + }
 + else return NULL;
 + }//function
 +
 +
 + public function getVertexOS($name, $variant=1) {
 + $temp = $this->getVertexByName($name);
 + if($temp["child"]) {
 + foreach($temp["child"] as $hodnota) {
 + if($hodnota["name"] == "OS") {
 + $temp2 = $hodnota;
 + break;
 + }//if
 + }//foreach
 + if(!isset($temp2)) return NULL;
 + if($variant) {
 + //$temp3["major"] = $temp2["child"][0]["content"];
 + //if($temp2["child"][1]) $temp3["minor"] = $temp2["child"][1]["content"];
 + //if($temp2["child"][2]) $temp3["other"] = $temp2["child"][2]["content"];
 + //return $temp3;
 + return $temp2["content"];
 + }
 + else return $temp2;
 + }//if
 + else return NULL;
 + }//function
 +
 +
 + public function getVertexOSRelation($name) {
 + $temp = $this->getVertexOS($name,0);
 + if(!is_null($temp)) {
 + return $temp["attribs"]["RELATION"];
 + }
 + else return NULL;
 + }//function
 +
 +
 + public function getVertexType($name) {
 + $temp = $this->getVertexByName($name);
 + if(!is_null($temp)) {
 + return $temp["attribs"]["TYPE"];
 + }
 + else return NULL;
 + }//function
 +
 +
 + public function getVertexesByType($type) {
 + $temp = $this->getVertexes();
 + $temp2 = array();
 + foreach($temp as $hodnota) {
 + if($hodnota["attribs"]["TYPE"] == $type)
 + array_push($temp2, $hodnota["attribs"]["NAME"]);
 + }
 + if(!$temp2[0]) return NULL;
 + return Unique($temp2);
 + }//function
 +
 +
 + public function getVertexesTypes() {
 + $temp = $this->getVertexes();
 + $temp2 = array();
 + foreach($temp as $hodnota) {
 + array_push($temp2, $hodnota["attribs"]["TYPE"]);
 + }
 + if(!$temp2[0]) return NULL;
 + return Unique($temp2);
 + }//function
 +
 +
 + public function getVertexFeatures($name) {
 + $temp = $this->getVertexByName($name);
 + if(!is_null($temp) && isset($temp["child"])) {
 + $temp2 = array();
 + foreach($temp["child"] as $hodnota) {
 + if($hodnota["name"]=="VERTEX_FEATURE")
 + array_push($temp2, $hodnota["content"]);
 + }//foreach
 + if(!$temp2[0]) return NULL;
 + return $temp2;
 + }//if
 + else return NULL;
 + }//function
 +
 +
 + public function getVertexesFeatures() {
 + $feat = array();
 + for($i = $this->getVertexesCount() - 1; $i >= 0; $i--) {
 + $jmeno = $this->getVertexName($i);
 + $temp = $this->getVertexFeatures($jmeno);
 + if(is_array($temp))
 + foreach($temp as $value)
 + array_push($feat, $value);
 + }//for
 + return $feat;
 + }//function
 +
 +
 + public function getEdgesFeatures() {
 + $feat = array();
 + for($i = $this->getEdgesCount() - 1; $i >= 0; $i--) {
 + $temp = $this->getEdgeFeatures($i);
 + if(is_array($temp))
 + foreach($temp as $value)
 + array_push($feat, $value);
 + }//for
 + return $feat;
 + }//function
 +
 + public function getEdgesList($variant=1) {
 + $temp = array();
 + for($i = $this->getEdgesCount() - 1; $i >= 0; $i--) {
 + if($variant) $temp[$i] = $this->getEdgeName($i);
 + else $temp[$this->getEdgeName($i)] = $i;
 + }//for
 + return $temp;
 +
 + }//function
 +
 + public function getVertexesList($variant=1) {
 + $temp = array();
 + for($i = $this->getVertexesCount() - 1; $i >= 0; $i--) {
 + if($variant) $temp[$i] = $this->getVertexName($i);
 + else $temp[$this->getVertexName($i)] = $i;
 + }//for
 + return $temp;
 + }//function
 +}//class
 +?>
 +</pre>

Verze z 12:40, 21. 2. 2007

Tato třída pomáhá vytahovat data z XML souboru. Ten je naparsován třídou virtlabXmlParser.

Popis funkcí

function __construct($file, $is_file=0, $cache_ed=1, $cache_ve=1) 
konstruktor třídy v PHP5
private function virtlabParserTopology($file, $is_file=0, $cache_ed=1, $cache_ve=1)
konstruktor třídy. Jednotlivé parametry mají tento význam:
$file 
obsahuje cestu k XML souboru s popisem topologie, nebo jsu v této proměnné uložena přímo XML data (o variantě rozhoduje parametr $is_file)
$is_file 
udává jestli je atribut $file casta k souboru, nebo přímo XML data
$cache_ed 
nastavuje, jestli se maji data o linkách uložit do proměnné, ze které si při potřebě načítají, nebo se vždy čtou z původního výstupu třídy virtlabXmlParser
$cache_ve 
jako u $cache_ed, ale pro jednotlivé prvky
public function getEdges()
vrátí část původního pole, která popisuje jen linky virtuální topologie
public function getEdgesCount()
vrátí počet linek v topologii
public function getEdge($index)
vrátí určenou linku (podle parametru $index, který odpovídá jejímu indexu v celkovém poli včech linek)
public function getEdgeName($index)
vrátí název zadané linky
public function getEdgeByName($name, &$index)
vrátí požadovanou linku (určenou jejím názvem). Do parametru $index se uloží její index
public function getEdgeTechnology($index)
vrátí technologii linky
public function getEdgeEthertype($index)
vrátí verzi ethernetu linky. Pokud je technologie serial vrací NULL.
public function getEdgeMinbps($index)
vrátí minimální rychlost linky. Pokud je technologie ethernet vrací NULL.
public function getEdgeVertexes($index)
vrátí zařízení určené linky
public function getEdgesByVertex($name)
vrátí linky, na kterých leží zadané zařízení. Pokud na žádné vrací NULL.
public function getEdgeFeatures($index)
vrátí speciální vlastnosti linky. Pokud nejsou zadány, vrací NULL.
public function getEdgesFeatures() 
vrátí všechny speciální vlastnosti všech linek v topologii
public function getEdgesList($variant=1) 
vrátí seznam linek v topologii
public function getVertexes()
vrátí část původního pole, která popisuje jen zařízení virtuální topologie
public function getVertexesCount()
vrátí počet zařízení v topologii
public function getVertex($index)
vrátí určené zařízení
public function getVertexName($index)
vrátí název zařízení
public function getVertexByName($name) 
vrátí požadované zařízení (určené jménem)
public function getVertexPlatforms($name, $variant=1) 
vrácí zadané platformy zařízení. Parametr $variant určuje jestli výstup je část původního pole, nebo jde o jednoduche pole hodnot (viz příklady)
public function getVertexOS($name, $variant=1) 
vrátí zadanou verzi OS
public function getVertexType($name) 
vrátí typ zařízení
public function getVertexesByType($type) 
vrátí zařízení určeného typu
public function getVertexesTypes() 
vrátí všechny typy zařízení v topologii
public function getVertexFeatures($name) 
vrátí speciální vlastnosti zařízení
public function getVertexesFeatures() 
vrátí všechny speciální vlastnosti všech zařízení v topologii
public function getVertexesList($variant=1) 
vrátí seznam zařízení v topologii

Příklady

$hrany = $parser->getEdgeName(2);
Kacena
$hrany = $parser->getEdgeVertexes(2);
Array
(
   [0] => ra
   [1] => rd
)
$hrany = $parser->getEdgesList(0);
Array
(
   [Kacena] => 2
   [Krokodyl] => 1
   [Kocour] => 0
)
$hrany = $parser->getEdgesList();
Array
(
   [2] => Kacena
   [1] => Krokodyl
   [0] => Kocour
)
$hrany = $parser->getVertexesList(0);
Array
(
   [ra] => 3
   [rb] => 2
   [rc] => 1
   [rd] => 0
)
$hrany = $parser->getVertexesList();
Array
(
   [3] => ra
   [2] => rb
   [1] => rc
   [0] => rd
)

Zdrojový kód

<?php

class virtlabParserTopology {
    private $parsed = array();
    private $edges = array();
    private $vertexes = array();
    private $cached_edges = 0;
    private $cached_vertexes = 0;
 
    function __construct($file, $is_file=0, $cache_ed=1, $cache_ve=1) {
        $this->virtlabParserTopology($file, $is_file, $cache_ed, $cache_ve);
    }//konstruktor
       
    private function virtlabParserTopology($file, $is_file=0, $cache_ed=1, $cache_ve=1) {
        $parser = new virtlabXmlParser(1);

        if($is_file) $parser->parse($file);
        else $parser->parse_data($file);

        $this->parsed = $parser->output;
        unset($parser);
        
        if($cache_ed) {
            $this->edges = $this->getEdges();
            $this->cached_edges = 1;
        }

        if($cache_ve) {
            $this->vertexes = $this->getVertexes();
            $this->cached_vertexes = 1;
        }
    }//function


    public function getEdges() {
        if(!$this->cached_edges) {
            $temp = $this->parsed[0]["child"];
            do {
                $temp2 = array_pop($temp);
            } while($temp2[name]=="VERTEX_DETAIL");
            array_push($temp, $temp2);
            return $temp;
        }
        else return $this->edges;
    }//function


    public function getEdgesCount() {
        if(!$this->cached_edges)
        {    
            $temp = $this->getEdges();
            return count($temp);
        }
        else return count($this->edges);
    }//function


    public function getEdge($index) {
        $temp = $this->getEdges();
        return $temp[$index];
    }//function    


    public function getEdgeName($index) {
        $temp = $this->getEdge($index);
        return $temp["attribs"]["NAME"];
    }//function


    public function getEdgeByName($name, &$index) {
        for($i = $this->getEdgesCount(); $i>=0; $i--)
        {
            if($this->getEdgeName($i) == $name) {
                $index = $i;
                return $this->getEdge($i);
            }
        }
        return NULL;
    }//function


    public function getEdgeTechnology($index) {
        $temp = $this->getEdge($index);
        return $temp["attribs"]["TECHNOLOGY"];
    }//function


    public function getEdgeEthertype($index) {
        $temp = $this->getEdge($index);
        if($temp["attribs"]["ETHER_TYPE"])
            return $temp["attribs"]["ETHER_TYPE"];
        else return NULL;
    }//function


    public function getEdgeMinbps($index) {
        $temp = $this->getEdge($index);
        if($temp["child"][2]["name"]=="MIN_BPS")
            return $temp["child"][2]["content"];
        else return NULL;
    }//function


    public function getEdgeVertexes($index) {
        $temp = $this->getEdge($index);
        $vertexes = array();
        array_push($vertexes, $temp["child"][0]["attribs"]["NAME"]);
        array_push($vertexes, $temp["child"][1]["attribs"]["NAME"]);
        return $vertexes;
    }//function


    public function getEdgesByVertex($name) {
        $result = array();
        for($i = $this->getEdgesCount(); $i>=0; $i--) {
            $vertexes = $this->getEdgeVertexes($i);
            foreach($vertexes as $hodnota) {
                if($hodnota == $name) {
                    //array_push($result, $this->getEdgeName($i));
                    $result[$i] = $this->getEdgeName($i);
                    break;
                }//if
            }//foreach
        }//for
        if(!$result) return NULL;
        else return $result;
    }//function


    public function getEdgeFeatures($index) {
        $temp = $this->getEdge($index);
        $result = array();
        foreach($temp["child"] as $hodnota) {
            if($hodnota["name"] == "EDGE_FEATURE") {
                array_push($result, $hodnota["content"]);
            }
        }
        if(!$result[0]) return NULL;
        else return $result;
    }//function


    public function getVertexes() {
        if(!$this->cached_vertexes) {
            $temp3 = array();
            $temp = $this->parsed[0]["child"];
            $temp2 = array_pop($temp);
            while($temp2[name]=="VERTEX_DETAIL") {
                array_push($temp3, $temp2);
                $temp2 = array_pop($temp);
            }
            return $temp3;
        }
        else return $this->vertexes;
    }//function


    public function getVertexesCount() {
        return(count($this->getVertexes()));
    }//function


    public function getVertex($index) {
        $temp = $this->getVertexes();
        return $temp[$index];
    }//function


    public function getVertexName($index) {
        $temp = $this->getVertex($index);
        return $temp["attribs"]["NAME"];
    }//function


    public function getVertexByName($name) {
        $temp = $this->getVertexes();
        foreach($temp as $hodnota) {
            if($hodnota["attribs"]["NAME"] == $name)
                return $hodnota;
        }
        return NULL;
    }//function


    public function getVertexPlatforms($name, $variant=1) {
        $temp = $this->getVertexByName($name);
        if($temp["child"][0]["name"] == "POSS_PLATFORMS") {
            if($variant) {
                $temp2 = array();
                foreach($temp["child"][0]["child"] as $hodnota) {
                    array_push($temp2, $hodnota["content"]);
                }//foreach
                return $temp2;
            }//if-variant
            else return $temp["child"][0]["child"];
        }
        else return NULL;
    }//function


    public function getVertexOS($name, $variant=1) {
        $temp = $this->getVertexByName($name);
        if($temp["child"]) {
            foreach($temp["child"] as $hodnota) {
                if($hodnota["name"] == "OS") {
                    $temp2 = $hodnota;
                    break;
                }//if
            }//foreach
            if(!isset($temp2)) return NULL;
            if($variant) {
                //$temp3["major"] = $temp2["child"][0]["content"];
                //if($temp2["child"][1]) $temp3["minor"] = $temp2["child"][1]["content"];
                //if($temp2["child"][2]) $temp3["other"] = $temp2["child"][2]["content"];
                //return $temp3;
                return $temp2["content"];
            }
            else return $temp2;
        }//if
        else return NULL;
    }//function


    public function getVertexOSRelation($name) {
        $temp = $this->getVertexOS($name,0);
        if(!is_null($temp)) {
            return $temp["attribs"]["RELATION"];
        }
        else return NULL;
    }//function


    public function getVertexType($name) {
        $temp = $this->getVertexByName($name);
        if(!is_null($temp)) {
            return $temp["attribs"]["TYPE"];
        }
        else return NULL;
    }//function


    public function getVertexesByType($type) {
        $temp = $this->getVertexes();
        $temp2 = array();
        foreach($temp as $hodnota) {
            if($hodnota["attribs"]["TYPE"] == $type)
                array_push($temp2, $hodnota["attribs"]["NAME"]);
        }
        if(!$temp2[0]) return NULL;
        return Unique($temp2);
    }//function


    public function getVertexesTypes() {
        $temp = $this->getVertexes();
        $temp2 = array();
        foreach($temp as $hodnota) {
            array_push($temp2, $hodnota["attribs"]["TYPE"]);
        }
        if(!$temp2[0]) return NULL;
        return Unique($temp2);
    }//function
        
        
    public function getVertexFeatures($name) {
        $temp = $this->getVertexByName($name);
        if(!is_null($temp) && isset($temp["child"])) {
            $temp2 = array();
            foreach($temp["child"] as $hodnota) {
                if($hodnota["name"]=="VERTEX_FEATURE")
                    array_push($temp2, $hodnota["content"]);
            }//foreach
            if(!$temp2[0]) return NULL;
            return $temp2;
        }//if
        else return NULL;
    }//function


    public function getVertexesFeatures() {
        $feat = array();
        for($i = $this->getVertexesCount() - 1; $i >= 0; $i--) {
            $jmeno = $this->getVertexName($i);
            $temp = $this->getVertexFeatures($jmeno);
            if(is_array($temp))
                foreach($temp as $value)
                    array_push($feat, $value);
        }//for
        return $feat;
    }//function


    public function getEdgesFeatures() {
        $feat = array();
        for($i = $this->getEdgesCount() - 1; $i >= 0; $i--) {
            $temp = $this->getEdgeFeatures($i);
            if(is_array($temp))
                foreach($temp as $value)
                    array_push($feat, $value);
        }//for
        return $feat;
    }//function

    public function getEdgesList($variant=1) {
        $temp = array();
        for($i = $this->getEdgesCount() - 1; $i >= 0; $i--) {
            if($variant) $temp[$i] = $this->getEdgeName($i);
            else $temp[$this->getEdgeName($i)] = $i;
        }//for
        return $temp;

    }//function

    public function getVertexesList($variant=1) {
        $temp = array();
        for($i = $this->getVertexesCount() - 1; $i >= 0; $i--) {
            if($variant) $temp[$i] = $this->getVertexName($i);
            else $temp[$this->getVertexName($i)] = $i;
        }//for
        return $temp;
    }//function
}//class
?>
Osobní nástroje