plcopen/plcopen.py
changeset 383 25ffba02b6a8
parent 379 e4c26ee9c998
child 384 ed27a676d5c9
--- a/plcopen/plcopen.py	Fri Jul 24 09:55:11 2009 +0200
+++ b/plcopen/plcopen.py	Fri Jul 24 10:47:35 2009 +0200
@@ -1223,13 +1223,158 @@
 def sety(self, y):
     self.position.sety(y)
 
-cls = PLCOpenClasses.get("commonObjects_comment", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+def _updateElementName(self, old_name, new_name):
+    pass
+
+def _initElementClass(name, classname, connectionPointInType="none"):
+    cls = PLCOpenClasses.get(classname, None)
+    if cls:
+        setattr(cls, "getx", getx)
+        setattr(cls, "gety", gety)
+        setattr(cls, "setx", setx)
+        setattr(cls, "sety", sety)
+        setattr(cls, "updateElementName", _updateElementName)
+    return cls
+
+def _getexecutionOrder(instance, specific_values):
+    executionOrder = instance.getexecutionOrderId()
+    if executionOrder is None:
+        executionOrder = 0
+    specific_values["executionOrder"] = executionOrder
+    
+def _getdefaultmodifiers(instance, infos):
+    infos["negated"] = instance.getnegated()
+    infos["edge"] = instance.getedge()
+
+def _getinputmodifiers(instance, infos):
+    infos["negated"] = instance.getnegatedIn()
+    infos["edge"] = instance.getedgeIn()
+
+def _getoutputmodifiers(instance, infos):
+    infos["negated"] = instance.getnegatedOut()
+    infos["edge"] = instance.getedgeOut()
+
+MODIFIERS_FUNCTIONS = {"default": _getdefaultmodifiers,
+                       "input": _getinputmodifiers,
+                       "output": _getoutputmodifiers}
+
+def _getconnectioninfos(instance, connection, links=False, modifiers=None, parameter=False):
+    infos = {"position": connection.getrelPositionXY()}
+    if parameter:
+        infos["name"] = instance.getformalParameter()
+    MODIFIERS_FUNCTIONS.get(modifiers, lambda x, y: None)(instance, infos)
+    if links:
+        infos["links"] = []
+        connections = connection.getconnections()
+        if connections is not None:
+            for link in connections:
+                dic = {"refLocalId": link.getrefLocalId(),
+                       "points": link.getpoints(),
+                       "formalParameter": link.getformalParameter()}
+                infos["links"].append(dic)
+    return infos
+
+def _getelementinfos(instance):
+    return {"id": instance.getlocalId(),
+            "x": instance.getx(),
+            "y": instance.gety(),
+            "height": instance.getheight(),
+            "width": instance.getwidth(),
+            "specific_values": {},
+            "inputs": [],
+            "outputs": []}
+
+def _getvariableinfosFunction(type, input, output):
+    def getvariableinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = type
+        specific_values = infos["specific_values"]
+        specific_values["name"] = self.getexpression()
+        _getexecutionOrder(self, specific_values)
+        if input and output:
+            infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "input"))
+            infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "output"))
+        elif input:
+            infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "default"))
+        elif output:
+            infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "default"))
+        return infos
+    return getvariableinfos
+
+def _getconnectorinfosFunction(type):
+    def getvariableinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = type
+        infos["specific_values"]["name"] = self.getname()
+        if type == "connector":
+            infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        elif type == "continuation":
+            infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut))
+        return infos
+    return getvariableinfos
+
+def _getpowerrailinfosFunction(type):
+    def getpowerrailinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = type
+        if type == "rightPowerRail":
+            for connectionPointIn in self.getconnectionPointIn():
+                infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True))
+            infos["specific_values"]["connectors"] = [True for input in infos["inputs"]]
+        elif type == "leftPowerRail":
+            for connectionPointOut in self.getconnectionPointOut():
+                infos["outputs"].append(_getconnectioninfos(self, connectionPointOut))
+            infos["specific_values"]["connectors"] = [True for output in infos["outputs"]]
+        return infos
+    return getpowerrailinfos
+
+def _getldelementinfosFunction(type):
+    def getldelementinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = type
+        specific_values = infos["specific_values"]
+        specific_values["name"] = self.getvariable()
+        _getexecutionOrder(self, specific_values)
+        specific_values["negated"] = self.getnegated()
+        specific_values["edge"] = self.getedge()
+        if type == "coil":
+            specific_values["storage"] = self.getstorage()
+        infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut))
+        return infos
+    return getldelementinfos
+
+DIVERGENCE_TYPES = {(True, True): "simultaneousDivergence",
+                    (True, False): "selectionDivergence",
+                    (False, True): "simultaneousConvergence",
+                    (False, False): "selectionConvergence"}
+
+def _getdivergenceinfosFunction(divergence, simultaneous):
+    def getdivergenceinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = DIVERGENCE_TYPES[(divergence, simultaneous)]
+        if divergence:
+            infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+            for connectionPointOut in self.getconnectionPointOut():
+                infos["outputs"].append(_getconnectioninfos(self, connectionPointOut))
+            infos["specific_values"]["connectors"] = len(infos["outputs"])
+        else:
+            for connectionPointIn in self.getconnectionPointIn():
+                infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True))
+            infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut))
+            infos["specific_values"]["connectors"] = len(infos["inputs"])
+        return infos
+    return getdivergenceinfos
+
+cls = _initElementClass("comment", "commonObjects_comment")
+if cls:
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = "comment"
+        infos["specific_values"]["content"] = self.getcontentText()
+        return infos
+    setattr(cls, "getinfos", getinfos)
+    
     def setcontentText(self, text):
         self.content.settext(text)
     setattr(cls, "setcontentText", setcontentText)
@@ -1242,81 +1387,97 @@
         self.content.updateElementName(old_name, new_name)
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("fbdObjects_block", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
+cls = _initElementClass("block", "fbdObjects_block")
+if cls:
+    def getBoundingBox(self):
+        bbox = _getBoundingBox(self)
+        for input in self.inputVariables.getvariable():
+            bbox.union(_getConnectionsBoundingBox(input.connectionPointIn))
+        return bbox
+    setattr(cls, "getBoundingBox", getBoundingBox)
+
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = self.gettypeName()
+        specific_values = infos["specific_values"]
+        specific_values["name"] = self.getinstanceName()
+        _getexecutionOrder(self, specific_values)
+        for variable in self.inputVariables.getvariable():
+            infos["inputs"].append(_getconnectioninfos(variable, variable.connectionPointIn, True, "default", True))
+        for variable in self.outputVariables.getvariable():
+            infos["outputs"].append(_getconnectioninfos(variable, variable.connectionPointOut, False, "default", True))
+        return infos
+    setattr(cls, "getinfos", getinfos)
 
     def updateElementName(self, old_name, new_name):
         if self.typeName == old_name:
             self.typeName = new_name
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("ldObjects_leftPowerRail", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("ldObjects_rightPowerRail", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("ldObjects_contact", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+cls = _initElementClass("leftPowerRail", "ldObjects_leftPowerRail")
+if cls:
+    setattr(cls, "getinfos", _getpowerrailinfosFunction("leftPowerRail"))
+
+cls = _initElementClass("rightPowerRail", "ldObjects_rightPowerRail", "multiple")
+if cls:
+    setattr(cls, "getinfos", _getpowerrailinfosFunction("rightPowerRail"))
+
+cls = _initElementClass("contact", "ldObjects_contact", "single")
+if cls:
+    setattr(cls, "getinfos", _getldelementinfosFunction("contact"))
+    
     def updateElementName(self, old_name, new_name):
         if self.variable == old_name:
             self.variable = new_name
     setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("ldObjects_coil", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+    
+cls = _initElementClass("coil", "ldObjects_coil", "single")
+if cls:
+    setattr(cls, "getinfos", _getldelementinfosFunction("coil"))
+    
     def updateElementName(self, old_name, new_name):
         if self.variable == old_name:
             self.variable = new_name
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("sfcObjects_step", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("sfcObjects_transition", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
+cls = _initElementClass("step", "sfcObjects_step", "single")
+if cls:
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = "step"
+        specific_values = infos["specific_values"]
+        specific_values["name"] = self.getname()
+        specific_values["initial"] = self.getinitialStep()
+        if self.connectionPointIn:
+            infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        if self.connectionPointOut:
+            infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut))
+        if self.connectionPointOutAction:
+            specific_values["action"] = _getconnectioninfos(self, self.connectionPointOutAction)
+        return infos
+    setattr(cls, "getinfos", getinfos)
+
+cls = _initElementClass("transition", "sfcObjects_transition", "single")
+if cls:
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = "transition"
+        specific_values = infos["specific_values"]
+        priority = self.getpriority()
+        if priority is None:
+            priority = 0
+        specific_values["priority"] = priority
+        condition = self.getconditionContent()
+        specific_values["condition_type"] = condition["type"]
+        if specific_values["condition_type"] == "connection":
+            
+            specific_values["connection"] = _getconnectioninfos(self, self, True)
+        else:
+            specific_values["condition"] = condition["value"]
+        infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut))
+        return infos
+    setattr(cls, "getinfos", getinfos)
 
     def setconditionContent(self, type, value):
         if not self.condition:
@@ -1355,6 +1516,14 @@
                 content["value"].updateElementName(old_name, new_name)
     setattr(cls, "updateElementName", updateElementName)
 
+    def setrelPositionXY(self, x, y):
+        pass
+    setattr(cls, "setrelPositionXY", setrelPositionXY)
+
+    def getrelPositionXY(self):
+        return None
+    setattr(cls, "getrelPositionXY", getrelPositionXY)
+
     def addconnection(self):
         if self.condition:
             content = self.condition.getcontent()
@@ -1385,7 +1554,7 @@
             if content["name"] == "connection":
                 return content["value"]
     setattr(cls, "getconnections", getconnections)
-    
+
     def setconnectionId(self, idx, id):
         if self.condition:
             content = self.condition.getcontent()
@@ -1427,64 +1596,35 @@
         if self.condition:
             content = self.condition.getcontent()
             if content["name"] == "connection":
-                return content["value"][idx].getformalParameter()
+                content["value"][idx].getformalParameter()
         return None
     setattr(cls, "getconnectionParameter", getconnectionParameter)
-
-cls = PLCOpenClasses.get("sfcObjects_selectionDivergence", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("sfcObjects_selectionConvergence", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-    
-cls = PLCOpenClasses.get("sfcObjects_simultaneousDivergence", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-    
-cls = PLCOpenClasses.get("sfcObjects_simultaneousConvergence", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("sfcObjects_jumpStep", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
+    
+cls = _initElementClass("selectionDivergence", "sfcObjects_selectionDivergence", "single")
+if cls:
+    setattr(cls, "getinfos", _getdivergenceinfosFunction(True, False))
+
+cls = _initElementClass("selectionConvergence", "sfcObjects_selectionConvergence", "multiple")
+if cls:
+    setattr(cls, "getinfos", _getdivergenceinfosFunction(False, False))
+
+cls = _initElementClass("simultaneousDivergence", "sfcObjects_simultaneousDivergence", "single")
+if cls:
+    setattr(cls, "getinfos", _getdivergenceinfosFunction(True, True))
+
+cls = _initElementClass("simultaneousConvergence", "sfcObjects_simultaneousConvergence", "multiple")
+if cls:
+    setattr(cls, "getinfos", _getdivergenceinfosFunction(False, True))
+
+cls = _initElementClass("jumpStep", "sfcObjects_jumpStep", "single")
+if cls:
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = "jump"
+        infos["specific_values"]["target"] = self.gettargetName()
+        infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        return infos
+    setattr(cls, "getinfos", getinfos)
 
 cls = PLCOpenClasses.get("actionBlock_action", None)
 if cls:
@@ -1518,13 +1658,22 @@
             self.inline.updateElementName(old_name, new_name)
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("commonObjects_actionBlock", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+cls = _initElementClass("actionBlock", "commonObjects_actionBlock", "single")
+if cls:
+    def compatibility(self, tree):
+        for child in tree.childNodes[:]:
+            if child.nodeName == "connectionPointOut":
+                tree.childNodes.remove(child)
+    setattr(cls, "compatibility", compatibility)
+    
+    def getinfos(self):
+        infos = _getelementinfos(self)
+        infos["type"] = "actionBlock"
+        infos["specific_values"]["actions"] = self.getactions()
+        infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True))
+        return infos
+    setattr(cls, "getinfos", getinfos)
+    
     def setactions(self, actions):
         self.action = []
         for params in actions:
@@ -1571,63 +1720,40 @@
             action.updateElementName(old_name, new_name)
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("fbdObjects_inVariable", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
+cls = _initElementClass("inVariable", "fbdObjects_inVariable")
+if cls:
+    setattr(cls, "getinfos", _getvariableinfosFunction("input", False, True))
     
     def updateElementName(self, old_name, new_name):
         if self.expression == old_name:
             self.expression = new_name
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("fbdObjects_outVariable", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+cls = _initElementClass("outVariable", "fbdObjects_outVariable", "single")
+if cls:
+    setattr(cls, "getinfos", _getvariableinfosFunction("output", True, False))
+    
     def updateElementName(self, old_name, new_name):
         if self.expression == old_name:
             self.expression = new_name
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("fbdObjects_inOutVariable", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-
+cls = _initElementClass("inOutVariable", "fbdObjects_inOutVariable", "single")
+if cls:
+    setattr(cls, "getinfos", _getvariableinfosFunction("inout", True, True))
+    
     def updateElementName(self, old_name, new_name):
         if self.expression == old_name:
             self.expression = new_name
     setattr(cls, "updateElementName", updateElementName)
 
-cls = PLCOpenClasses.get("commonObjects_continuation", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
-
-cls = PLCOpenClasses.get("commonObjects_connector", None)
-if cls:
-    setattr(cls, "getx", getx)
-    setattr(cls, "gety", gety)
-    setattr(cls, "setx", setx)
-    setattr(cls, "sety", sety)
-    
-    def updateElementName(self, old_name, new_name):
-        pass
-    setattr(cls, "updateElementName", updateElementName)
+cls = _initElementClass("continuation", "commonObjects_continuation")
+if cls:
+    setattr(cls, "getinfos", _getconnectorinfosFunction("continuation"))
+
+cls = _initElementClass("connector", "commonObjects_connector", "single")
+if cls:
+    setattr(cls, "getinfos", _getconnectorinfosFunction("connector"))
 
 cls = PLCOpenClasses.get("connection", None)
 if cls: