Fixed pou instance path list computing stylesheet
authorLaurent Bessard
Wed, 09 Oct 2013 23:06:47 +0200
changeset 1350 7280349a3375
parent 1349 be6c3a036844
child 1351 a546a63ce1bf
Fixed pou instance path list computing stylesheet
PLCControler.py
plcopen/instances_path.xslt
plcopen/instances_path.ysl2
--- a/PLCControler.py	Wed Oct 09 22:01:21 2013 +0200
+++ b/PLCControler.py	Wed Oct 09 23:06:47 2013 +0200
@@ -242,35 +242,13 @@
 #            Helpers object for generating instances path list
 #-------------------------------------------------------------------------------
 
-class InstanceDefinition(etree.XSLTExtension):
-    
-    def __init__(self, controller, debug):
-        etree.XSLTExtension.__init__(self)
-        self.Controller = controller
-        self.Debug = debug
-    
-    def execute(self, context, self_node, input_node, output_parent):
-        instance_infos = etree.Element('infos')
-        self.process_children(context, instance_infos)
-        
-        pou_infos = self.Controller.GetPou(instance_infos.get("name"), self.Debug)
-        if pou_infos is not None:
-            pou_instance = etree.Element('pou_instance',
-                pou_path=instance_infos.get("path"))
-            pou_instance.append(deepcopy(pou_infos))
-            self.apply_templates(context, pou_instance, output_parent)
-            return
-            
-        datatype_infos = self.Controller.GetDataType(instance_infos.get("name"), self.Debug)
-        if datatype_infos is not None:
-            datatype_instance = etree.Element('datatype_instance',
-                datatype_path=instance_infos.get("path"))
-            datatype_instance.append(deepcopy(datatype_infos))
-            self.apply_templates(context, datatype_instance, output_parent)
-            return
-
-instances_path_xslt = etree.parse(
-    os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"))
+class InstancesPathFactory:
+    
+    def __init__(self, instances):
+        self.Instances = instances
+        
+    def AddInstance(self, context, *args):
+        self.Instances.append(args[0][0])
 
 #-------------------------------------------------------------------------------
 #            Helpers object for generating instance tagname
@@ -795,14 +773,25 @@
     def GetInstanceList(self, root, name, debug = False):
         project = self.GetProject(debug)
         if project is not None:
+            instances = []
+            factory = InstancesPathFactory(instances)
+            
+            parser = etree.XMLParser()
+            parser.resolvers.add(LibraryResolver(self, debug))
+            
             instances_path_xslt_tree = etree.XSLT(
-                instances_path_xslt, 
+                etree.parse(
+                    os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
+                    parser), 
                 extensions = {
-                    ("instances_ns", "instance_definition"): 
-                    InstanceDefinition(self, debug)})
+                    ("instances_ns", "AddInstance"): factory.AddInstance})
             
-            return instances_path_xslt_tree(root, 
-                instance_type=etree.XSLT.strparam(name)).getroot()
+            instances_path_xslt_tree(root, 
+                instance_type=etree.XSLT.strparam(name))
+            
+            if len(instances) > 0:
+                return instances
+        
         return None
 
     def SearchPouInstances(self, tagname, debug = False):
@@ -810,10 +799,7 @@
         if project is not None:
             words = tagname.split("::")
             if words[0] == "P":
-                result = self.GetInstanceList(project, words[1])
-                if result is not None:
-                    return [instance.get("path") for instance in result]
-                return []
+                return self.GetInstanceList(project, words[1])
             elif words[0] == 'C':
                 return [words[1]]
             elif words[0] == 'R':
--- a/plcopen/instances_path.xslt	Wed Oct 09 22:01:21 2013 +0200
+++ b/plcopen/instances_path.xslt	Wed Oct 09 23:06:47 2013 +0200
@@ -1,38 +1,55 @@
-<xsl:stylesheet version="1.0"
-    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
-    xmlns:ppx="http://www.plcopen.org/xml/tc6_0201"
-    xmlns:ns="instances_ns"
-    extension-element-prefixes="ns"
-    exclude-result-prefixes="ns">
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:dyn="http://exslt.org/dynamic" xmlns:str="http://exslt.org/strings" xmlns:math="http://exslt.org/math" xmlns:exsl="http://exslt.org/common" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:yml="http://fdik.org/yml" xmlns:set="http://exslt.org/sets" xmlns:ppx="http://www.plcopen.org/xml/tc6_0201" xmlns:ns="instances_ns" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="ns" version="1.0" exclude-result-prefixes="ns">
+  <xsl:output method="xml"/>
+  <xsl:variable name="space" select="'                                                                                                                                                                                                        '"/>
+  <xsl:param name="autoindent" select="4"/>
   <xsl:param name="instance_type"/>
+  <xsl:template match="text()">
+    <xsl:param name="_indent" select="0"/>
+  </xsl:template>
+  <xsl:variable name="project">
+    <xsl:copy-of select="document('project')/project/*"/>
+  </xsl:variable>
+  <xsl:variable name="stdlib">
+    <xsl:copy-of select="document('stdlib')/stdlib/*"/>
+  </xsl:variable>
+  <xsl:variable name="extensions">
+    <xsl:copy-of select="document('extensions')/extensions/*"/>
+  </xsl:variable>
   <xsl:template match="ppx:project">
-   <instances>
-     <xsl:apply-templates select="ppx:instances/ppx:configurations/ppx:configuration"/>
-   </instances>
+    <xsl:param name="_indent" select="0"/>
+    <instances>
+      <xsl:apply-templates select="ppx:instances/ppx:configurations/ppx:configuration">
+        <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      </xsl:apply-templates>
+    </instances>
   </xsl:template>
   <xsl:template match="ppx:configuration">
-    <xsl:apply-templates select="ppx:globalVars/ppx:variable[ppx:type/ppx:derived]">
-      <xsl:with-param name="parent_path" select="@name"/>
-    </xsl:apply-templates>
-    <xsl:apply-templates select="ppx:resource">
-      <xsl:with-param name="parent_path" select="@name"/>
+    <xsl:param name="_indent" select="0"/>
+    <xsl:apply-templates select="ppx:globalVars/ppx:variable[ppx:type/ppx:derived] | ppx:resource">
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="parent_path">
+        <xsl:value-of select="@name"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="ppx:resource">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="parent_path"/>
     <xsl:variable name="resource_path">
       <xsl:value-of select="$parent_path"/>
       <xsl:text>.</xsl:text>
       <xsl:value-of select="@name"/>
     </xsl:variable>
-    <xsl:apply-templates select="ppx:globalVars/ppx:variable[ppx:type/ppx:derived]">
-      <xsl:with-param name="parent_path" select="$resource_path"/>
-    </xsl:apply-templates>
-    <xsl:apply-templates select="ppx:pouInstance | ppx:task/ppx:pouInstance">
-      <xsl:with-param name="parent_path" select="$resource_path"/>
+    <xsl:apply-templates select="ppx:globalVars/ppx:variable[ppx:type/ppx:derived] | ppx:pouInstance | ppx:task/ppx:pouInstance">
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="parent_path">
+        <xsl:value-of select="$resource_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="ppx:pouInstance">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="parent_path"/>
     <xsl:variable name="pou_instance_path">
       <xsl:value-of select="$parent_path"/>
@@ -41,37 +58,43 @@
     </xsl:variable>
     <xsl:choose>
       <xsl:when test="@typeName=$instance_type">
-        <instance>
-          <xsl:attribute name="path">
-            <xsl:value-of select="$pou_instance_path"/>
-          </xsl:attribute>
-        </instance>
+        <xsl:value-of select="ns:AddInstance($pou_instance_path)"/>
       </xsl:when>
       <xsl:otherwise>
-        <ns:instance_definition>
-          <xsl:attribute name="name">
-            <xsl:value-of select="@typeName"/>
-          </xsl:attribute>
-          <xsl:attribute name="path">
+        <xsl:variable name="type_name">
+          <xsl:value-of select="@typeName"/>
+        </xsl:variable>
+        <xsl:apply-templates select="exsl:node-set($project)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($project)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |&#10;                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |&#10;                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name]">
+          <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+          <xsl:with-param name="instance_path">
             <xsl:value-of select="$pou_instance_path"/>
-          </xsl:attribute>
-        </ns:instance_definition>
+          </xsl:with-param>
+        </xsl:apply-templates>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
   <xsl:template match="ppx:pou">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="instance_path"/>
     <xsl:apply-templates select="ppx:interface/*/ppx:variable[ppx:type/ppx:derived]">
-      <xsl:with-param name="parent_path" select="$instance_path"/>
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="parent_path">
+        <xsl:value-of select="$instance_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="ppx:dataType">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="instance_path"/>
     <xsl:apply-templates select="ppx:baseType/*[self::ppx:derived or self::ppx:struct or self::ppx:array]">
-      <xsl:with-param name="parent_path" select="$instance_path"/>
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="parent_path">
+        <xsl:value-of select="$instance_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="ppx:variable">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="parent_path"/>
     <xsl:variable name="variable_path">
       <xsl:value-of select="$parent_path"/>
@@ -79,32 +102,34 @@
       <xsl:value-of select="@name"/>
     </xsl:variable>
     <xsl:apply-templates select="ppx:type/ppx:derived">
-      <xsl:with-param name="variable_path" select="$variable_path"/>
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="variable_path">
+        <xsl:value-of select="$variable_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="ppx:derived">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="variable_path"/>
     <xsl:choose>
       <xsl:when test="@name=$instance_type">
-        <instance>
-          <xsl:attribute name="path">
-            <xsl:value-of select="$variable_path"/>
-          </xsl:attribute>
-        </instance>
+        <xsl:value-of select="ns:AddInstance($variable_path)"/>
       </xsl:when>
       <xsl:otherwise>
-        <ns:instance_definition>
-          <xsl:attribute name="name">
-            <xsl:value-of select="@name"/>
-          </xsl:attribute>
-          <xsl:attribute name="path">
+        <xsl:variable name="type_name">
+          <xsl:value-of select="@name"/>
+        </xsl:variable>
+        <xsl:apply-templates select="exsl:node-set($project)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($project)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |&#10;                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |&#10;                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |&#10;                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name]">
+          <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+          <xsl:with-param name="instance_path">
             <xsl:value-of select="$variable_path"/>
-          </xsl:attribute>
-        </ns:instance_definition>
+          </xsl:with-param>
+        </xsl:apply-templates>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
-  <xsl:template name="ppx:struct">
+  <xsl:template match="ppx:struct">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="variable_path"/>
     <xsl:for-each select="ppx:variable[ppx:type/ppx:derived or ppx:type/ppx:struct or ppx:type/ppx:array]">
       <xsl:variable name="element_path">
@@ -112,26 +137,40 @@
         <xsl:text>.</xsl:text>
         <xsl:value-of select="@name"/>
       </xsl:variable>
-      <xsl:apply-templates select="ppx:type/*[self::ppx:derived or self::ppx:struct or self::ppx:array]">
-        <xsl:with-param name="variable_path" select="$element_path"/>
-      </xsl:apply-templates>
     </xsl:for-each>
+    <xsl:apply-templates select="ppx:type/*[self::ppx:derived or self::ppx:struct or self::ppx:array]">
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="variable_path">
+        <xsl:value-of select="$element_path"/>
+      </xsl:with-param>
+    </xsl:apply-templates>
   </xsl:template>
-  <xsl:template name="ppx:array">
+  <xsl:template match="ppx:array">
+    <xsl:param name="_indent" select="0"/>
     <xsl:param name="variable_path"/>
     <xsl:apply-templates select="ppx:baseType/*[self::ppx:derived or self::ppx:struct or self::ppx:array]">
-      <xsl:with-param name="variable_path" select="$variable_path"/>
-    </xsl:apply-templates> 
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="variable_path">
+        <xsl:value-of select="$variable_path"/>
+      </xsl:with-param>
+    </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="pou_instance">
+    <xsl:param name="_indent" select="0"/>
     <xsl:apply-templates>
-      <xsl:with-param name="instance_path" select="@pou_path"/>
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="instance_path">
+        <xsl:value-of select="@pou_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
   <xsl:template match="datatype_instance">
+    <xsl:param name="_indent" select="0"/>
     <xsl:apply-templates>
-      <xsl:with-param name="instance_path" select="@datatype_path"/>
+      <xsl:with-param name="_indent" select="$_indent + (1) * $autoindent"/>
+      <xsl:with-param name="instance_path">
+        <xsl:value-of select="@datatype_path"/>
+      </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
-  <xsl:template match="text()"/>
-</xsl:stylesheet>
\ No newline at end of file
+</xsl:stylesheet>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plcopen/instances_path.ysl2	Wed Oct 09 23:06:47 2013 +0200
@@ -0,0 +1,135 @@
+include yslt.yml2
+estylesheet xmlns:ppx="http://www.plcopen.org/xml/tc6_0201"
+            xmlns:xhtml="http://www.w3.org/1999/xhtml"
+            xmlns:ns="instances_ns" 
+            extension-element-prefixes="ns" 
+            exclude-result-prefixes="ns" {
+    
+    param "instance_type";
+    
+    template "text()";
+    
+    variable "project" {
+        copy "document('project')/project/*";
+    }
+    
+    variable "stdlib" {
+        copy "document('stdlib')/stdlib/*";
+    }
+    variable "extensions" {
+        copy "document('extensions')/extensions/*";
+    }
+    
+    template "ppx:project" {
+        instances {
+            apply "ppx:instances/ppx:configurations/ppx:configuration";
+        }
+    }
+    
+    template "ppx:configuration" {
+        apply "ppx:globalVars/ppx:variable[ppx:type/ppx:derived] | ppx:resource" {
+            with "parent_path" > «@name»
+        }
+    }
+    
+    template "ppx:resource" {
+        param "parent_path";
+        variable "resource_path" > «$parent_path».«@name»
+        apply "ppx:globalVars/ppx:variable[ppx:type/ppx:derived] | ppx:pouInstance | ppx:task/ppx:pouInstance" {
+            with "parent_path" > «$resource_path»
+        }
+    }
+    
+    template "ppx:pouInstance" {
+        param "parent_path";
+        variable "pou_instance_path" > «$parent_path».«@name»
+        choose {
+            when "@typeName=$instance_type" {
+                value "ns:AddInstance($pou_instance_path)";
+            }
+            otherwise {
+                variable "type_name" > «@typeName»
+                apply """exsl:node-set($project)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($project)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |
+                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |
+                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name]""" {
+                    with "instance_path" > «$pou_instance_path»
+                }
+            }
+        }
+    }
+    
+    template "ppx:pou" {
+        param "instance_path";
+        apply "ppx:interface/*/ppx:variable[ppx:type/ppx:derived]" {
+            with "parent_path" > «$instance_path»
+        }
+    }
+    
+    template "ppx:dataType" {
+        param "instance_path";
+        apply "ppx:baseType/*[self::ppx:derived or self::ppx:struct or self::ppx:array]" {
+            with "parent_path" > «$instance_path»
+        }
+    }
+    
+    template "ppx:variable" {
+        param "parent_path";
+        variable "variable_path" > «$parent_path».«@name»
+        apply "ppx:type/ppx:derived" {
+            with "variable_path" > «$variable_path»
+        }
+    }
+    
+    template "ppx:derived" {
+        param "variable_path";
+        choose {
+            when "@name=$instance_type" {
+                value "ns:AddInstance($variable_path)";
+            }
+            otherwise {
+                variable "type_name" > «@name»
+                apply """exsl:node-set($project)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($project)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |
+                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($stdlib)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name] |
+                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:pous/ppx:pou[@name=$type_name] |
+                         exsl:node-set($extensions)/ppx:project/ppx:types/ppx:dataTypes/ppx:dataType[@name=$type_name]""" {
+                    with "instance_path" > «$variable_path»
+                }
+            }
+        }
+    }
+    
+    template "ppx:struct" {
+        param "variable_path";
+        foreach "ppx:variable[ppx:type/ppx:derived or ppx:type/ppx:struct or ppx:type/ppx:array]" {
+            variable "element_path" > «$variable_path».«@name»
+        }
+        apply "ppx:type/*[self::ppx:derived or self::ppx:struct or self::ppx:array]" {
+            with "variable_path" > «$element_path»
+        }
+    }
+    
+    template "ppx:array" {
+        param "variable_path";
+        apply "ppx:baseType/*[self::ppx:derived or self::ppx:struct or self::ppx:array]" {
+            with "variable_path" > «$variable_path»
+        }
+    }
+    
+    template "pou_instance" {
+        apply {
+            with "instance_path" > «@pou_path»
+        }
+    }
+    
+    template "datatype_instance" {
+        apply {
+            with "instance_path" > «@datatype_path»
+        }
+    }
+    
+}