PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/luiseufrasio/rdb2rdfMB
Java | 184 lines | 156 code | 22 blank | 6 comment | 32 complexity | f2816659b81274756505c78a23c504ee MD5 | raw file
  1. package br.ufc.mcc.arida.rdb2rdfmb.mapping;
  2. import br.ufc.mcc.arida.rdb2rdfmb.model.CA;
  3. import br.ufc.mcc.arida.rdb2rdfmb.model.CCA;
  4. import br.ufc.mcc.arida.rdb2rdfmb.model.Class_;
  5. import br.ufc.mcc.arida.rdb2rdfmb.model.DCA;
  6. import br.ufc.mcc.arida.rdb2rdfmb.model.DataProperty;
  7. import br.ufc.mcc.arida.rdb2rdfmb.model.Fk;
  8. import br.ufc.mcc.arida.rdb2rdfmb.model.OCA;
  9. import br.ufc.mcc.arida.rdb2rdfmb.model.ObjProperty;
  10. import br.ufc.mcc.arida.rdb2rdfmb.model.Pair;
  11. import br.ufc.mcc.arida.rdb2rdfmb.model.Property;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import javafx.scene.control.ListView;
  17. import rdb2rdfmappingbuilder.TemplateUtil;
  18. /**
  19. *
  20. * @author Luis
  21. */
  22. public class R2RMLGen {
  23. public static String buildR2RML(ListView<CA> assertionsList, HashMap<String, Fk> mapFks, HashMap<String, String> mapPrefixes) throws Exception {
  24. StringBuilder r2rml = new StringBuilder("");
  25. r2rml.append("@prefix rr: &lt;http://www.w3.org/ns/r2rml#&gt; .\n");
  26. r2rml.append("@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .\n");
  27. r2rml.append("@prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt; .\n");
  28. r2rml.append("@prefix xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt; .");
  29. List<String> prefixes = new ArrayList<>();
  30. for (CA ca : assertionsList.getItems()) {
  31. if (ca instanceof CCA) {
  32. CCA cca = (CCA) ca;
  33. List<String> specialTriplesMap = new ArrayList<>();
  34. String prefixClass = cca.getClass_().getPrefix();
  35. if (!prefixes.contains(prefixClass)) {
  36. prefixes.add(prefixClass);
  37. }
  38. String viewName = cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + "_view";
  39. List<String> atts = new ArrayList<>();
  40. Map<String, Object> param = new HashMap<>();
  41. param.put("mapName", cca.getClass_().getName());
  42. param.put("className", cca.getClass_().getName());
  43. param.put("prefixClass", prefixClass);
  44. param.put("table", viewName);
  45. param.put("atts", atts);
  46. int i = 1;
  47. while (i <= cca.getAttributes().size()) {
  48. String index = (i == 1 ? "" : "" + i);
  49. atts.add("ID" + index);
  50. i++;
  51. }
  52. r2rml.append(TemplateUtil.applyTemplate("r2rml/subjectMap", param));
  53. for (DCA dca : cca.getDcaList()) {
  54. if (assertionsList.getItems().contains(dca)) {
  55. DataProperty dp = dca.getdProperty();
  56. Map<String, Object> param2 = new HashMap<>();
  57. String prefix = dp.getPrefix();
  58. if (!prefixes.contains(prefix)) {
  59. prefixes.add(prefix);
  60. }
  61. param2.put("prefix", prefix);
  62. param2.put("propertyName", dp.getName());
  63. if (dca.getAttributes().size() == 1) {
  64. param2.put("type", 1);
  65. param2.put("columnName", dp.getPrefix() + "_" + dp.getName());
  66. } else {
  67. param2.put("type", 3);
  68. List<String> cols = new ArrayList<>();
  69. i = 1;
  70. while (i <= dca.getAttributes().size()) {
  71. String index = (i == 1 ? "" : "" + i);
  72. cols.add(dp.getPrefix() + "_" + dp.getName() + index);
  73. i++;
  74. }
  75. param2.put("cols", cols);
  76. }
  77. if (dp.getMaxCardinality() == 1) {
  78. r2rml.append(TemplateUtil.applyTemplate("r2rml/predicateObjectMap", param2));
  79. } else {
  80. // Create another triples map
  81. specialTriplesMap.add(createSpecialTriplesMap(param2, cca, dp));
  82. }
  83. }
  84. }
  85. for (OCA oca : cca.getOcaList()) {
  86. if (assertionsList.getItems().contains(oca)) {
  87. ObjProperty op = oca.getoProperty();
  88. Map<String, Object> param2 = new HashMap<>();
  89. String prefix = op.getPrefix();
  90. if (!prefixes.contains(prefix)) {
  91. prefixes.add(prefix);
  92. }
  93. param2.put("prefix", prefix);
  94. param2.put("propertyName", op.getName());
  95. param2.put("type", 2);
  96. String rangeClass = oca.getoProperty().getRange().getName();
  97. param2.put("rangeClass", rangeClass);
  98. List<Pair> pairs = new ArrayList<>();
  99. param2.put("pairs", pairs);
  100. int size = oca.getFks().size();
  101. if (size > 0) {
  102. String fkStr = oca.getFks().get(size - 1);
  103. Fk fk = mapFks.get(fkStr);
  104. int qtdeAtts = fk.getJoin().attributes1().size();
  105. i = 1;
  106. while (i <= qtdeAtts) {
  107. String index = (i == 1 ? "" : "" + i);
  108. pairs.add(new Pair(op.getPrefix() + "_" + op.getName() + index, "ID" + index));
  109. i++;
  110. }
  111. } else {
  112. CCA ccaRange = CCA.getCcaFromClass(assertionsList, op.getRange());
  113. int q = ccaRange.getAttributes().size();
  114. i = 1;
  115. while (i <= q) {
  116. String index = (i == 1 ? "" : "" + i);
  117. pairs.add(new Pair(op.getPrefix() + "_" + op.getName() + index, "ID" + index));
  118. i++;
  119. }
  120. }
  121. if (op.getMaxCardinality() == 1) {
  122. r2rml.append(TemplateUtil.applyTemplate("r2rml/predicateObjectMap", param2));
  123. } else {
  124. // Create another triples map
  125. specialTriplesMap.add(createSpecialTriplesMap(param2, cca, op));
  126. }
  127. }
  128. }
  129. int idx = r2rml.lastIndexOf(";");
  130. r2rml.replace(idx, idx + 1, ".");
  131. for (String tm : specialTriplesMap) {
  132. r2rml.append(tm);
  133. idx = r2rml.lastIndexOf(";");
  134. r2rml.replace(idx, idx + 1, ".");
  135. }
  136. }
  137. }
  138. for (String prefix : prefixes) {
  139. r2rml.insert(0, "@prefix " + prefix + ": &lt;" + mapPrefixes.get(prefix) + "&gt; .\n");
  140. }
  141. return r2rml.toString();
  142. }
  143. private static String createSpecialTriplesMap(Map<String, Object> param2, CCA cca, Property p) throws Exception {
  144. param2.put("mapName", cca.getClass_().getName() + "_" + p.getName());
  145. param2.put("className", cca.getClass_().getName());
  146. param2.put("prefixClass", cca.getClass_().getPrefix());
  147. param2.put("table", cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + "_"
  148. + p.getPrefix() + "_" + p.getName() + "_view");
  149. List<String> atts2 = new ArrayList<>();
  150. param2.put("atts", atts2);
  151. int i = 1;
  152. while (i <= cca.getAttributes().size()) {
  153. String index = (i == 1 ? "" : "" + i);
  154. atts2.add("ID_" + cca.getClass_().getPrefix() + "_" + cca.getClass_().getName() + index);
  155. i++;
  156. }
  157. StringBuilder r2rml2 = new StringBuilder("");
  158. r2rml2.append(TemplateUtil.applyTemplate("r2rml/subjectMap", param2));
  159. r2rml2.append(TemplateUtil.applyTemplate("r2rml/predicateObjectMap", param2));
  160. return r2rml2.toString();
  161. }
  162. }