graphical information added to boxes and testbed descriptions
authorAlina Quereilhac <alina.quereilhac@inria.fr>
Thu, 17 Feb 2011 15:42:57 +0000 (16:42 +0100)
committerAlina Quereilhac <alina.quereilhac@inria.fr>
Thu, 17 Feb 2011 15:42:57 +0000 (16:42 +0100)
examples/design1.py
src/nepi/core/description.py
src/nepi/testbeds/netns/metadata_v01.py
src/nepi/util/parser/_xml.py
src/nepi/util/parser/base.py

index 1765b21..3c6cb76 100644 (file)
@@ -29,26 +29,10 @@ app = netns_desc.create("Application")
 app.set_attribute_value("command", "ping -qc10 10.0.0.2")
 app.connector("node").connect(node1.connector("apps"))
 
-from nepi.util.parser.base import ExperimentParser
-p = ExperimentParser()
-data = p.to_data(exp_desc)
-print data.data
+xml = exp_desc.to_xml()
 exp_desc2 = ExperimentDescription()
-p.from_data(exp_desc2, data)
-data2 = p.to_data(exp_desc2)
-print data2.data
-print data.data == data2.data
-
-from nepi.util.parser._xml import XmlExperimentParser
-p = XmlExperimentParser()
-xml = p.to_xml(exp_desc)
-print xml
-exp_desc2 = ExperimentDescription()
-p.from_xml(exp_desc2, xml)
-xml2 = p.to_xml(exp_desc2)
-print xml2
-print xml == xml2
-
-#print experiment.xml_description
+exp_desc2.from_xml(xml)
+xml2 = exp_desc2.to_xml()
+assert xml == xml2
 
 
index cc3cb07..baae03f 100644 (file)
@@ -4,6 +4,7 @@
 from nepi.core.attributes import AttributesMap, Attribute
 from nepi.util import validation
 from nepi.util.guid import GuidGenerator
+from nepi.util.graphical_info import GraphicalInfo
 from nepi.util.parser._xml import XmlExperimentParser
 import sys
 
@@ -218,6 +219,8 @@ class Box(AttributesMap):
         # factory attributes for box construction
         self._factory_attributes = list()
 
+        self.graphical_info = GraphicalInfo(str(self._guid))
+
         for connector_type in factory.connector_types:
             connector = Connector(self, connector_type)
             self._connectors[connector_type.name] = connector
@@ -428,6 +431,10 @@ class TestbedFactoriesProvider(object):
     def testbed_version(self):
         return self._testbed_version
 
+    @property
+    def factories(self):
+        return self._factories.values()
+
     def factory(self, factory_id):
         return self._factories[factory_id]
 
@@ -437,9 +444,6 @@ class TestbedFactoriesProvider(object):
     def remove_factory(self, factory_id):
         del self._factories[factory_id]
 
-    def list_factories(self):
-        return self._factories.keys()
-
 class TestbedDescription(AttributesMap):
     def __init__(self, guid_generator, provider):
         super(TestbedDescription, self).__init__()
@@ -447,6 +451,7 @@ class TestbedDescription(AttributesMap):
         self._guid = guid_generator.next()
         self._provider = provider
         self._boxes = dict()
+        self.graphical_info = GraphicalInfo(str(self._guid))
 
     @property
     def guid(self):
@@ -484,7 +489,6 @@ class TestbedDescription(AttributesMap):
 class ExperimentDescription(object):
     def __init__(self, guid = 0):
         self._guid_generator = GuidGenerator(guid)
-        # testbed design instances
         self._testbed_descriptions = dict()
 
     @property
index a0faef3..bfa8caa 100644 (file)
@@ -46,7 +46,7 @@ def get_metadata():
                                 "Connector from P2PInterface to Node", 
                                 "node", 1, 1, 
                                 ["netns_node_devs"]),
-                            ("netns_p2pinterface_p2p", 
+                            ("netns_p2piface_p2p", 
                                 "Connector to another P2PInterface", 
                                 "p2p", 1, 0, 
                                 ["netns_p2piface_p2p"])
index fa21d0a..5f2e3d6 100644 (file)
@@ -31,6 +31,7 @@ class XmlExperimentParser(ExperimentParser):
         testbed_tag.setAttribute("testbed_id", str(testbed_id))
         testbed_tag.setAttribute("testbed_version", str(testbed_version))
         parent_tag.appendChild(testbed_tag)
+        self.graphical_info_data_to_xml(doc, testbed_tag, guid, data)
         self.attributes_data_to_xml(doc, testbed_tag, guid, data)
         elements_tag = doc.createElement("elements")
         testbed_tag.appendChild(elements_tag)
@@ -43,15 +44,26 @@ class XmlExperimentParser(ExperimentParser):
         parent_tag.appendChild(element_tag)
         element_tag.setAttribute("factory_id", factory_id)
         element_tag.setAttribute("guid", str(guid))
+        self.graphical_info_data_to_xml(doc, element_tag, guid, data)
         self.factory_attributes_data_to_xml(doc, element_tag, guid, data)
         self.attributes_data_to_xml(doc, element_tag, guid, data)
         self.traces_data_to_xml(doc, element_tag, guid, data)
         self.addresses_data_to_xml(doc, element_tag, guid, data)
         self.routes_data_to_xml(doc, element_tag, guid, data)
         self.connections_data_to_xml(doc, element_tag, guid, data)
-        
+
+    def graphical_info_data_to_xml(self, doc, parent_tag, guid, data):
+        graphical_info_tag = doc.createElement("graphical_info") 
+        parent_tag.appendChild(graphical_info_tag)
+        (x, y, width, height, label) = data.get_graphical_info_data(guid)
+        graphical_info_tag.setAttribute("x", str(x))
+        graphical_info_tag.setAttribute("y", str(y))
+        graphical_info_tag.setAttribute("width", str(width))
+        graphical_info_tag.setAttribute("height", str(height))
+        graphical_info_tag.setAttribute("label", str(label))
+
     def factory_attributes_data_to_xml(self, doc, parent_tag, guid, data):
-        factory_attributes_tag = doc.createElement("factory_attributes") 
+        factory_attributes_tag = doc.createElement("factory_attributes")
         parent_tag.appendChild(factory_attributes_tag)
         for (name, value) in data.get_factory_attribute_data(guid):
             factory_attribute_tag = doc.createElement("factory_attribute") 
@@ -142,19 +154,36 @@ class XmlExperimentParser(ExperimentParser):
         testbed_id = str(tag.getAttribute("testbed_id"))
         testbed_version = str(tag.getAttribute("testbed_version"))
         data.add_testbed_data(testbed_guid, testbed_id, testbed_version)
+        self.graphical_info_data_from_xml(tag, testbed_guid, data)
         self.attributes_data_from_xml(tag, testbed_guid, data)
 
     def box_data_from_xml(self, tag, testbed_guid, data):
         guid = int(tag.getAttribute("guid"))
         factory_id = str(tag.getAttribute("factory_id"))
         data.add_box_data(guid, testbed_guid, factory_id)
+        self.graphical_info_data_from_xml(tag, guid, data)
         self.factory_attributes_data_from_xml(tag, guid, data)
         self.attributes_data_from_xml(tag, guid, data)
         self.traces_data_from_xml(tag, guid, data)
         self.addresses_data_from_xml(tag, guid, data)
         self.routes_data_from_xml(tag, guid, data)
         self.connections_data_from_xml(tag, guid, data)
-        
+
+    def graphical_info_data_from_xml(self, tag, guid, data):
+        graphical_info_tag_list = tag.getElementsByTagName(
+                "graphical_info")
+        if len(graphical_info_tag_list) == 0:
+            return
+
+        graphical_info_tag = graphical_info_tag_list[0]
+        if graphical_info_tag.nodeType == tag.ELEMENT_NODE:
+            x = int(graphical_info_tag.getAttribute("x"))
+            y = int(graphical_info_tag.getAttribute("y"))
+            width = int(graphical_info_tag.getAttribute("width"))
+            height = int(graphical_info_tag.getAttribute("height"))
+            label = str(graphical_info_tag.getAttribute("label"))
+            data.add_graphical_info_data(guid, x, y, width, height, label)
+
     def factory_attributes_data_from_xml(self, tag, guid, data):
         factory_attributes_tag_list = tag.getElementsByTagName(
                 "factory_attributes")
index feef6ab..15c18ad 100644 (file)
@@ -23,6 +23,17 @@ class ExperimentData(object):
         box_data["factory_id"] = factory_id
         self.data[guid] = box_data
 
+    def add_graphical_info_data(self, guid, x, y, width, height, label):
+        data = self.data[guid]
+        if not "graphical_info" in data:
+            data["graphical_info"] = dict()
+        graphical_info_data = data["graphical_info"]
+        graphical_info_data["x"] = x
+        graphical_info_data["y"] = y
+        graphical_info_data["width"] = width
+        graphical_info_data["height"] = height
+        graphical_info_data["label"] = label
+
     def add_factory_attribute_data(self, guid, name, value):
         data = self.data[guid]
         if not "factory_attributes" in data:
@@ -100,6 +111,17 @@ class ExperimentData(object):
         box_data = self.data[guid]
         return (box_data["testbed_guid"], box_data["factory_id"])
 
+    def get_graphical_info_data(self, guid):
+        data = self.data[guid]
+        if not "graphical_info" in data:
+            return (0, 0, 0, 0, "") 
+        graphical_info_data = data["graphical_info"]
+        return (graphical_info_data["x"],
+                graphical_info_data["y"],
+                graphical_info_data["width"],
+                graphical_info_data["height"],
+                graphical_info_data["label"])
+
     def get_factory_attribute_data(self, guid):
         data = self.data[guid]
         if not "factory_attributes" in data:
@@ -165,9 +187,12 @@ class ExperimentParser(object):
             testbed_id = testbed_description.provider.testbed_id
             testbed_version = testbed_description.provider.testbed_version
             data.add_testbed_data(guid, testbed_id, testbed_version)
+            self.graphical_info_to_data(data, guid, 
+                    testbed_description.graphical_info)
             self.attributes_to_data(data, guid, testbed_description.attributes)
             for box in testbed_description.boxes:
                 data.add_box_data(box.guid, guid, box.factory_id)
+                self.graphical_info_to_data(data, box.guid, box.graphical_info)
                 self.factory_attributes_to_data(data, box.guid, 
                         box.factory_attributes)
                 self.attributes_to_data(data, box.guid, box.attributes)
@@ -177,6 +202,10 @@ class ExperimentParser(object):
                 self.routes_to_data(data, box.guid, box.routes)
         return data
 
+    def graphical_info_to_data(self, data, guid, g_info):
+        data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width, 
+                g_info.height, g_info.label)
+
     def factory_attributes_to_data(self, data, guid, attributes):
         for attribute in attributes:
             if attribute.modified:
@@ -256,11 +285,21 @@ class ExperimentParser(object):
         self.factory_attributes_from_data(testbed_description, factory_id,
                 guid, data)
         box = testbed_description.create(factory_id)
+        self.graphical_info_from_data(box, data)
         self.attributes_from_data(box, data)
         self.traces_from_data(box, data)
         self.addresses_from_data(box, data)
         self.routes_from_data(box, data)
 
+    def graphical_info_from_data(self, element, data):
+        (x, y, width, height, label) =  data.get_graphical_info_data(
+                element.guid)
+        element.graphical_info.x = x
+        element.graphical_info.y = y
+        element.graphical_info.width = width
+        element.graphical_info.height = height
+        element.graphical_info.label = label
+
     def factory_attributes_from_data(self, testbed_description, factory_id, 
             guid, data):
         factory = testbed_description.provider.factory(factory_id)