From 7fb47621a605c0062f08dccdb00d5ac62bb0130b Mon Sep 17 00:00:00 2001
From: npedot <nicola.pedot@gmail.com>
Date: Thu, 23 Jan 2020 22:22:56 +0100
Subject: [PATCH] adds xpath first horizontal decomposion alpha version

---
 README.md                                     | 32 +++++++++++++++----
 .../usecase/XPathTransformUseCase.kt          | 12 +++++--
 .../scenario/PersonXPathScenarioTI.kt         | 29 +++++++++++------
 .../decompose/horizontal_decompose_1.paths    |  5 +++
 .../decompose/horizontal_decompose_1.template | 31 ++++++++++++++++++
 .../decompose/vertical_decompose_1.paths      | 10 +++---
 6 files changed, 96 insertions(+), 23 deletions(-)
 create mode 100644 src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.paths
 create mode 100644 src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.template

diff --git a/README.md b/README.md
index b5ea283..03c89f1 100644
--- a/README.md
+++ b/README.md
@@ -68,14 +68,34 @@ technology depenent packages
 * A domain component has to depends on nothing. 
 
 
-## to do
+## v1.0.0
 
-* schema clone, builder, immutable
-* schema pattern matcher
-* schema variable extrator
-* sql view generator
+from object database meta representation as input
+apply kotlin transformer vertical or horizontal
+obtain a second database meta representation as output
 
-## references
+
+## v2.0.0
+
+from xml object database meta representation as input
+apply kotlin transformer vertical or horizontal
+obtain a changeset representation as output
+
+
+## v3.0.0
+
+from xml object database meta representation as input
+a generic xpath engine extraction plus freemarker template to generate
+obtain a database meta representation as output
+
+### to do
+
+* horizontal simple decomposition to remove null columns
+* merge selected input database out template process
+* (probably) xpath with use of result of previous xpath computation as parameter
+* (probably) sql xml representation to add database representation
+
+### references
 
 https://www.baeldung.com/java-xpath
 https://freemarker.apache.org/
diff --git a/src/main/kotlin/unibz.cs.semint.kprime/usecase/XPathTransformUseCase.kt b/src/main/kotlin/unibz.cs.semint.kprime/usecase/XPathTransformUseCase.kt
index 315bacd..b26cfd0 100644
--- a/src/main/kotlin/unibz.cs.semint.kprime/usecase/XPathTransformUseCase.kt
+++ b/src/main/kotlin/unibz.cs.semint.kprime/usecase/XPathTransformUseCase.kt
@@ -13,7 +13,7 @@ import javax.xml.xpath.XPathFactory
 
 class XPathTransformUseCase {
 
-    fun transform(dbFilePath: String, trasformerName: String, trasformerDirection: String, trasformerVersion: String) {
+    fun transform(dbFilePath: String, trasformerName: String, trasformerDirection: String, trasformerVersion: String, tranformerParmeters: MutableMap<String, Any>) {
 
         val vdecomposeFilePath = "/transformer/${trasformerName}/${trasformerDirection}/${trasformerName}_${trasformerDirection}_${trasformerVersion}.paths"
         val vdecomposeTemplatePath = "transformer/${trasformerName}/${trasformerDirection}/${trasformerName}_${trasformerDirection}_${trasformerVersion}.template"
@@ -43,7 +43,7 @@ class XPathTransformUseCase {
         for (entryNameas in xPaths.propertyNames()) {
             val name = entryNameas as String
             val pathTokens = xPaths.getProperty(name).split(" ")
-            val value = pathTokens[0]
+            val value = parametrized(pathTokens[0],tranformerParmeters)
             if (!(value.startsWith("-") || value.startsWith("+"))) {
                 templModel[name] = asValueList(xpath.compile(value).evaluate(doc, XPathConstants.NODESET) as NodeList)
                 println(" ${name} = ${value}")
@@ -81,6 +81,14 @@ class XPathTransformUseCase {
         templ.process(templModel, OutputStreamWriter(System.out))
     }
 
+    private fun parametrized(line: String, tranformerParmeters: MutableMap<String, Any>): String {
+        var newline = line
+        for (key in tranformerParmeters.keys) {
+            newline = newline.replace("%%${key}%%", tranformerParmeters[key] as String)
+        }
+        return newline
+    }
+
 
     private fun computeDerivedList(templModel: MutableMap<String, List<String>>, derivationRule: String): List<String> {
         var derivedList = mutableListOf<String>()
diff --git a/src/test/kotlin/unibz.cs.semint.kprime/scenario/PersonXPathScenarioTI.kt b/src/test/kotlin/unibz.cs.semint.kprime/scenario/PersonXPathScenarioTI.kt
index 953cf05..cf29a81 100644
--- a/src/test/kotlin/unibz.cs.semint.kprime/scenario/PersonXPathScenarioTI.kt
+++ b/src/test/kotlin/unibz.cs.semint.kprime/scenario/PersonXPathScenarioTI.kt
@@ -1,27 +1,36 @@
 package unibz.cs.semint.kprime.scenario
 
-import freemarker.cache.ClassTemplateLoader
-import freemarker.template.Configuration
 import org.junit.Test
-import org.w3c.dom.NodeList
 import unibz.cs.semint.kprime.usecase.XPathTransformUseCase
-import java.io.OutputStreamWriter
-import java.util.*
-import javax.xml.parsers.DocumentBuilderFactory
-import javax.xml.xpath.XPathConstants
-import javax.xml.xpath.XPathFactory
 
 class PersonXPathScenarioTI {
 
     @Test
-    fun test_xpath_extraction_on_person_db() {
+    fun test_xpath_vertical_decomposition_on_person_db() {
         // given
         val dbFilePath = "db/person.xml"
         val trasformerName = "vertical"
         val trasformerDirection = "decompose"
         val trasformerVersion = "1"
+        val tranformerParmeters = mutableMapOf<String,Any>()
+        tranformerParmeters["table"]="person"
         // when
-        XPathTransformUseCase().transform(dbFilePath, trasformerName, trasformerDirection, trasformerVersion)
+        XPathTransformUseCase().transform(dbFilePath, trasformerName, trasformerDirection, trasformerVersion,tranformerParmeters)
+        // then
+        // print to console output
+    }
+
+    @Test
+    fun test_xpath_horizontal_decomposition_on_person_db() {
+        // given
+        val dbFilePath = "db/person.xml"
+        val trasformerName = "horizontal"
+        val trasformerDirection = "decompose"
+        val trasformerVersion = "1"
+        val tranformerParmeters = mutableMapOf<String,Any>()
+        tranformerParmeters["table"]="person"
+        // when
+        XPathTransformUseCase().transform(dbFilePath, trasformerName, trasformerDirection, trasformerVersion,tranformerParmeters)
         // then
         // print to console output
     }
diff --git a/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.paths b/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.paths
new file mode 100644
index 0000000..89b90db
--- /dev/null
+++ b/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.paths
@@ -0,0 +1,5 @@
+all=/database/schema/tables/tables[@name='%%table%%']/columns/columns/@name = 4
+keys=/database/schema/constraints/constraints[@type='PRIMARY_KEY']/source/columns/columns/@name > 0
+nullable=/database/schema/constraints/constraints[@type='FUNCTIONAL']/source/columns/columns/@name
+rests=- all keys nullable
+table=/database/schema/tables/tables[@name='%%table%%']/@name = 1
\ No newline at end of file
diff --git a/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.template b/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.template
new file mode 100644
index 0000000..0a838bf
--- /dev/null
+++ b/src/test/resources/transformer/horizontal/decompose/horizontal_decompose_1.template
@@ -0,0 +1,31 @@
+<database name="" id="">
+  <schema name="" id="">
+    <tables>
+      <tables name="pure_person" id="" view="person" condition="person.T=null AND person.S=null">
+        <columns>
+          <columns name="SSN" id="id.SSN" dbname="dbname.SSN" nullable="false" dbtype=""/>
+        </columns>
+      </tables>
+      <tables name="person_with_T" id="" view="person" condition="person.T NOT null AND person.S=null">
+        <columns>
+          <columns name="SSN" id="id.SSN" dbname="dbname.SSN" nullable="false" dbtype=""/>
+          <columns name="T" id="id.T" dbname="dbname.T" nullable="false" dbtype=""/>
+        </columns>
+      </tables>
+      <tables name="person_with_TS" id="" view="person" condition="person.T = null AND person.S NOT null">
+        <columns>
+          <columns name="SSN" id="id.SSN" dbname="dbname.SSN" nullable="false" dbtype=""/>
+          <columns name="S" id="id.S" dbname="dbname.S" nullable="false" dbtype=""/>
+        </columns>
+      </tables>
+      <tables name="person_with_S" id="" view="person" condition="person.T NOT null AND person.S NOT null">
+        <columns>
+          <columns name="SSN" id="id.SSN" dbname="dbname.SSN" nullable="false" dbtype=""/>
+          <columns name="T" id="id.T" dbname="dbname.T" nullable="false" dbtype=""/>
+          <columns name="S" id="id.S" dbname="dbname.S" nullable="false" dbtype=""/>
+        </columns>
+      </tables>
+    </tables>
+    <constraints/>
+  </schema>
+</database>
\ No newline at end of file
diff --git a/src/test/resources/transformer/vertical/decompose/vertical_decompose_1.paths b/src/test/resources/transformer/vertical/decompose/vertical_decompose_1.paths
index 27b6e2d..419229d 100644
--- a/src/test/resources/transformer/vertical/decompose/vertical_decompose_1.paths
+++ b/src/test/resources/transformer/vertical/decompose/vertical_decompose_1.paths
@@ -1,6 +1,6 @@
-all=/database/schema/tables/tables[@name='person']/columns/columns/@name = 4
-keys=/database/schema/constraints/constraints[@type='PRIMARY_KEY']/source/columns/columns/@name > 0
-lhss=/database/schema/constraints/constraints[@type='FUNCTIONAL']/source/columns/columns/@name
-rhss=/database/schema/constraints/constraints[@type='FUNCTIONAL']/target/columns/columns/@name
+all=/database/schema/tables/tables[@name='%%table%%']/columns/columns/@name = 4
+keys=/database/schema/constraints/constraints[@type='PRIMARY_KEY']/source[@table='%%table%%']/columns/columns/@name > 0
+lhss=/database/schema/constraints/constraints[@type='FUNCTIONAL']/source[@table='%%table%%']/columns/columns/@name
+rhss=/database/schema/constraints/constraints[@type='FUNCTIONAL']/target[@table='%%table%%']/columns/columns/@name
 rests=- all keys lhss rhss
-table=/database/schema/tables/tables[@name='person']/@name = 1
\ No newline at end of file
+table=/database/schema/tables/tables[@name='%%table%%']/@name = 1
\ No newline at end of file
-- 
GitLab