PageRenderTime 28ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

/IncrementalMaintenanceOfRDFViews/src/br/ufc/mcc/arida/rdb2rdfmb/mapping/ViewsGen.java

https://github.com/luiseufrasio/rdb2rdfMB
Java | 281 lines | 244 code | 24 blank | 13 comment | 63 complexity | cfd6e79ef20ba92dd70a4ad1094c4535 MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package br.ufc.mcc.arida.rdb2rdfmb.mapping;
  6. import br.ufc.mcc.arida.rdb2rdfmb.model.AttAlias;
  7. import br.ufc.mcc.arida.rdb2rdfmb.model.CA;
  8. import br.ufc.mcc.arida.rdb2rdfmb.model.CCA;
  9. import br.ufc.mcc.arida.rdb2rdfmb.model.DCA;
  10. import br.ufc.mcc.arida.rdb2rdfmb.model.DataProperty;
  11. import br.ufc.mcc.arida.rdb2rdfmb.model.Fk;
  12. import br.ufc.mcc.arida.rdb2rdfmb.model.MappingConfiguration;
  13. import br.ufc.mcc.arida.rdb2rdfmb.model.OCA;
  14. import br.ufc.mcc.arida.rdb2rdfmb.model.ObjProperty;
  15. import br.ufc.mcc.arida.rdb2rdfmb.model.Pair;
  16. import br.ufc.mcc.arida.rdb2rdfmb.model.Property;
  17. import br.ufc.mcc.arida.rdb2rdfmb.model.TableAtt;
  18. import de.fuberlin.wiwiss.d2rq.algebra.Attribute;
  19. import de.fuberlin.wiwiss.d2rq.algebra.Join;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import javafx.scene.control.ListView;
  25. import rdb2rdfmappingbuilder.TemplateUtil;
  26. /**
  27. *
  28. * @author Luis
  29. */
  30. public class ViewsGen {
  31. public static List<String> buildViews(ListView<CA> assertionsList, HashMap<String, Fk> mapFks, MappingConfiguration mc) throws Exception {
  32. List<String> listViews = new ArrayList<>();
  33. for (CA ca : assertionsList.getItems()) {
  34. if (ca instanceof CCA) {
  35. CCA cca = (CCA) ca;
  36. String viewName = cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + "_view";
  37. List<String> tables = new ArrayList<>();
  38. List<AttAlias> atts = new ArrayList<>();
  39. List<TableAtt> parentAtts = new ArrayList<>();
  40. List<Pair> pairs = new ArrayList<>();
  41. Map<String, Object> param = new HashMap<>();
  42. param.put("viewName", viewName);
  43. param.put("childTable", cca.getRelationName());
  44. param.put("tables", tables);
  45. param.put("atts", atts);
  46. param.put("parentAtts", parentAtts);
  47. param.put("pairs", pairs);
  48. param.put("filter", cca.getSelCondition());
  49. param.put("db", mc.getDatabaseDriver());
  50. tables.add(cca.getRelationName());
  51. int i = 1;
  52. for (String att : cca.getAttributes()) {
  53. String index = (i == 1 ? "" : "" + i);
  54. atts.add(new AttAlias(att, "ID" + index));
  55. i++;
  56. }
  57. for (DCA dca : cca.getDcaList()) {
  58. if (assertionsList.getItems().contains(dca)) {
  59. DataProperty dp = dca.getdProperty();
  60. if (dp.getMaxCardinality() == 1) {
  61. if (dca.getFks().isEmpty()) {
  62. i = 1;
  63. for (String att : dca.getAttributes()) {
  64. String index = (i == 1 ? "" : "" + i);
  65. atts.add(new AttAlias(att, dp.getPrefix() + "_" + dp.getName() + index));
  66. i++;
  67. }
  68. } else {
  69. addTablesJoinsDP(dca, mapFks, tables, pairs, parentAtts, dp);
  70. }
  71. } else {
  72. // Create another view
  73. List<String> tables2 = new ArrayList<>();
  74. List<AttAlias> atts2 = new ArrayList<>();
  75. List<TableAtt> parentAtts2 = new ArrayList<>();
  76. List<Pair> pairs2 = new ArrayList<>();
  77. Map<String, Object> param2 = new HashMap<>();
  78. setParamsUri(param2, cca, dp, dca, tables2, atts2, parentAtts2, pairs2, mc);
  79. addTablesJoinsDP(dca, mapFks, tables2, pairs2, parentAtts2, dp);
  80. if (cca.getSelCondition() == null) {
  81. listViews.add(TemplateUtil.applyTemplate("views/pathView", param2));
  82. } else {
  83. listViews.add(TemplateUtil.applyTemplate("views/pathFilterView", param2));
  84. }
  85. }
  86. }
  87. }
  88. for (OCA oca : cca.getOcaList()) {
  89. if (assertionsList.getItems().contains(oca)) {
  90. ObjProperty op = oca.getoProperty();
  91. String alias = op.getPrefix() + "_" + op.getName();
  92. if (op.getMaxCardinality() == 1) {
  93. if (oca.getFks().isEmpty()) {
  94. CCA ccaRange = CCA.getCcaFromClass(assertionsList, op.getRange());
  95. int q = ccaRange.getAttributes().size();
  96. i = 1;
  97. while (i <= q) {
  98. String index = (i == 1 ? "" : "" + i);
  99. atts.add(new AttAlias(ccaRange.getAttributes().get(i-1), alias + index));
  100. i++;
  101. }
  102. } else if (oca.getFks().size() == 1) {
  103. String fkStr = oca.getFks().get(0);
  104. Fk fk = mapFks.get(fkStr);
  105. if (!fk.isInverse()) {
  106. Join j = fk.getJoin();
  107. i = 1;
  108. while (i <= j.attributes1().size()) {
  109. String index = (i == 1 ? "" : "" + i);
  110. Attribute a1 = (Attribute) j.attributes1().get(i-1);
  111. atts.add(new AttAlias(a1.attributeName(), alias + index));
  112. i++;
  113. }
  114. }
  115. } else {
  116. // Add tables and joins
  117. addTablesJoinsOP(oca, mapFks, tables, pairs, parentAtts, op);
  118. }
  119. } else {
  120. // Create another view
  121. List<String> tables2 = new ArrayList<>();
  122. List<AttAlias> atts2 = new ArrayList<>();
  123. List<TableAtt> parentAtts2 = new ArrayList<>();
  124. List<Pair> pairs2 = new ArrayList<>();
  125. Map<String, Object> param2 = new HashMap<>();
  126. setParamsUri(param2, cca, op, oca, tables2, atts2, parentAtts2, pairs2, mc);
  127. addTablesJoinsOP(oca, mapFks, tables2, pairs2, parentAtts2, op);
  128. if (cca.getSelCondition() == null) {
  129. listViews.add(TemplateUtil.applyTemplate("views/pathView", param2));
  130. } else {
  131. listViews.add(TemplateUtil.applyTemplate("views/pathFilterView", param2));
  132. }
  133. }
  134. }
  135. }
  136. if (parentAtts.isEmpty()) {
  137. if (cca.getSelCondition() == null) {
  138. listViews.add(TemplateUtil.applyTemplate("views/simpleView", param));
  139. } else {
  140. listViews.add(TemplateUtil.applyTemplate("views/filterView", param));
  141. }
  142. } else {
  143. if (cca.getSelCondition() == null) {
  144. listViews.add(TemplateUtil.applyTemplate("views/pathView", param));
  145. } else {
  146. listViews.add(TemplateUtil.applyTemplate("views/pathFilterView", param));
  147. }
  148. }
  149. }
  150. }
  151. return listViews;
  152. }
  153. private static void addTablesJoinsDP(DCA dca, HashMap<String, Fk> mapFks, List<String> tables,
  154. List<Pair> pairs, List<TableAtt> parentAtts, DataProperty dp) {
  155. // Add tables, join pairs
  156. for (String fkStr : dca.getFks()) {
  157. Fk fk = mapFks.get(fkStr);
  158. Join j = fk.getJoin();
  159. int i = 0;
  160. while (i < j.attributes1().size()) {
  161. Pair p;
  162. Attribute a1 = (Attribute) j.attributes1().get(i);
  163. Attribute a2 = (Attribute) j.attributes2().get(i);
  164. if (!fk.isInverse()) {
  165. p = new Pair(a1.attributeName(), a1.tableName(), a2.attributeName(), a2.tableName());
  166. if (!tables.contains(a1.tableName())) {
  167. tables.add(a1.tableName());
  168. }
  169. if (!tables.contains(a2.tableName())) {
  170. tables.add(a2.tableName());
  171. }
  172. } else {
  173. p = new Pair(a2.attributeName(), a2.tableName(), a1.attributeName(), a1.tableName());
  174. if (!tables.contains(a2.tableName())) {
  175. tables.add(a2.tableName());
  176. }
  177. if (!tables.contains(a1.tableName())) {
  178. tables.add(a1.tableName());
  179. }
  180. }
  181. pairs.add(p);
  182. i++;
  183. }
  184. }
  185. // Add atts from last table
  186. String alias = dp.getPrefix() + "_" + dp.getName();
  187. int i = 1;
  188. for (String att : dca.getAttributes()) {
  189. TableAtt ta = new TableAtt(tables.get(tables.size() - 1), new AttAlias(att, alias));
  190. parentAtts.add(ta);
  191. i++;
  192. alias = dp.getPrefix() + "_" + dp.getName() + i;
  193. }
  194. }
  195. private static void addTablesJoinsOP(OCA oca, HashMap<String, Fk> mapFks, List<String> tables,
  196. List<Pair> pairs, List<TableAtt> parentAtts, ObjProperty op) {
  197. String alias = op.getPrefix() + "_" + op.getName();
  198. int k = 0;
  199. for (String fkStr : oca.getFks()) {
  200. k++;
  201. boolean isLastFk = (k == oca.getFks().size());
  202. Fk fk = mapFks.get(fkStr);
  203. Join j = fk.getJoin();
  204. int i = 0;
  205. while (i < j.attributes1().size()) {
  206. Pair p;
  207. Attribute a1 = (Attribute) j.attributes1().get(i);
  208. Attribute a2 = (Attribute) j.attributes2().get(i);
  209. if (isLastFk) {
  210. TableAtt ta = new TableAtt(a1.tableName(), new AttAlias(a1.attributeName(), alias));
  211. parentAtts.add(ta);
  212. alias = op.getPrefix() + "_" + op.getName() + (i + 2);
  213. }
  214. if (!fk.isInverse()) {
  215. p = new Pair(a1.attributeName(), a1.tableName(), a2.attributeName(), a2.tableName());
  216. if (!tables.contains(a1.tableName())) {
  217. tables.add(a1.tableName());
  218. }
  219. if (!tables.contains(a2.tableName())) {
  220. tables.add(a2.tableName());
  221. }
  222. } else {
  223. p = new Pair(a2.attributeName(), a2.tableName(), a1.attributeName(), a1.tableName());
  224. if (!tables.contains(a2.tableName())) {
  225. tables.add(a2.tableName());
  226. }
  227. if (!tables.contains(a1.tableName())) {
  228. tables.add(a1.tableName());
  229. }
  230. }
  231. pairs.add(p);
  232. i++;
  233. }
  234. }
  235. }
  236. private static void setParamsUri(Map<String, Object> param2,
  237. CCA cca, Property p, CA ca, List<String> tables2,
  238. List<AttAlias> atts2, List<TableAtt> parentAtts2,
  239. List<Pair> pairs2, MappingConfiguration mc) {
  240. param2.put("viewName", cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + "_"
  241. + p.getPrefix() + "_" + p.getName() + "_view");
  242. param2.put("childTable", ca.getRelationName());
  243. param2.put("tables", tables2);
  244. param2.put("atts", atts2);
  245. param2.put("parentAtts", parentAtts2);
  246. param2.put("pairs", pairs2);
  247. param2.put("filter", cca.getSelCondition());
  248. param2.put("db", mc.getDatabaseDriver());
  249. tables2.add(cca.getRelationName());
  250. int i = 1;
  251. for (String att : cca.getAttributes()) {
  252. String index = (i == 1 ? "" : "" + i);
  253. atts2.add(new AttAlias(att, "ID_" + cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + index));
  254. i++;
  255. }
  256. }
  257. }