Advertisement
asanchez75

virtuoso/java/insertion

Feb 12th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. package fr.inrialpes.exmo.evolution;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. import org.slf4j.Logger;
  11.  
  12. import com.hp.hpl.jena.util.URIref;
  13.  
  14. import virtuoso.jdbc4.VirtuosoException;
  15.  
  16.  
  17. public class XXXXXXX {
  18. private static final Logger logger = org.slf4j.LoggerFactory.getLogger(Evolution.class);
  19.  
  20. public static void main(String[] args) throws SQLException {
  21. //getSameAsLinks("<http://umbel.org>");
  22. //realization("INSTANCE2", "<http://umbel.org>");
  23. //realization("INSTANCE1","<http://dbpedia.org>");
  24. getRealisedSameAsLinks("<http://umbel.org>", "<http://dbpedia.org>", "<http://umbel.org>");
  25. }
  26.  
  27. public static void getRealisedSameAsLinks(String graphnameSameAs, String graphnameLeft, String graphnameRight) throws SQLException {
  28.  
  29. try {
  30.  
  31. logger.info("starting process getRealisedSameAsLinks...");
  32.  
  33. //Connection conn = DriverManager.getConnection("jdbc:virtuoso://localhost:1111/exmo.DBA/log_enable=2", "dba", "dba");
  34. Connection conn = DriverManager.getConnection("jdbc:virtuoso://localhost:1111/", "dba", "dba");
  35.  
  36. String query1 = "SELECT instance1, instance2 FROM exmo.DBA.SAMEAS WHERE graphname = iri_to_id(?)";
  37.  
  38. // S = instance
  39. // O = classname
  40. // P = rdf:type (very often)
  41. // G = graphname
  42.  
  43. String query2 = "SELECT DISTINCT O AS classname FROM exmo.DBA.INSTANCES WHERE S = iri_id_from_num(?) AND G = iri_to_id(?)";
  44.  
  45. String query3 = "SELECT DISTINCT O AS classname FROM exmo.DBA.INSTANCES WHERE S = iri_id_from_num(?) AND G = iri_to_id(?)";
  46.  
  47. //String sql = "INSERT INTO exmo.DBA.SAMEAS_REALIZATION (instance1, classname1, instance2, classname2, graphname) VALUES(iri_id_from_num(?),iri_id_from_num(?),iri_id_from_num(?),iri_id_from_num(?),iri_to_id(?))";
  48. //String sql = "INSERT INTO exmo.DBA.SAMEAS_REALIZATION2 (instance1, classname1, graphname) VALUES(iri_id_from_num(?),iri_id_from_num(?),iri_to_id(?))";
  49. String sql = "INSERT INTO exmo.DBA.SAMEAS_REALIZATION2 (instance1, classname1, graphname) VALUES(?,?,?)";
  50.  
  51. PreparedStatement preparedStatement1 = conn.prepareStatement(query1);
  52. preparedStatement1.setString(1, graphnameSameAs);
  53. ResultSet rows1 = preparedStatement1.executeQuery();
  54. PreparedStatement preparedStatement4 = conn.prepareStatement(sql);
  55.  
  56. // disabling log_enable
  57.  
  58. String log_enable = "log_enable(2)";
  59. Statement st = conn.createStatement();
  60. st.executeQuery(log_enable);
  61.  
  62. double total = 729306;
  63. double i = 0;
  64. while (rows1.next()) {
  65. i++;
  66. int instance1 = rows1.getInt("instance1");
  67. //System.out.println(instance1);
  68. int instance2 = rows1.getInt("instance2");
  69. //System.out.println(instance2);
  70.  
  71. ArrayList<Integer> classes1 = new ArrayList<Integer>();
  72. ArrayList<Integer> classes2 = new ArrayList<Integer>();
  73.  
  74. PreparedStatement preparedStatement2 = conn.prepareStatement(query2);
  75. preparedStatement2.setInt(1, instance1);
  76. preparedStatement2.setString(2, graphnameLeft);
  77.  
  78. ResultSet rows2 = preparedStatement2.executeQuery();
  79.  
  80. while (rows2.next()) {
  81. classes1.add(rows2.getInt("classname"));
  82. }
  83.  
  84. //System.out.println("classes1 = " + classes1);
  85.  
  86. preparedStatement2.close();
  87.  
  88. PreparedStatement preparedStatement3 = conn.prepareStatement(query3);
  89. preparedStatement3.setInt(1, instance2);
  90. preparedStatement3.setString(2, graphnameRight);
  91.  
  92. ResultSet rows3 = preparedStatement3.executeQuery();
  93.  
  94. while (rows3.next()) {
  95. classes2.add(rows3.getInt("classname"));
  96. }
  97.  
  98. //System.out.println("classes2 = " + classes2);
  99.  
  100. preparedStatement3.close();
  101.  
  102. for (int class1 : classes1) {
  103. for (int class2 : classes2) {
  104. //System.out.println(instance1 + "|" + class1 + "|" + instance2 + "|" + class2);
  105. preparedStatement4.setInt(1, instance1);
  106. preparedStatement4.setInt(2, class1);
  107. preparedStatement4.setString(3, graphnameSameAs);
  108. preparedStatement4.executeUpdate();
  109. }
  110. }
  111. if ((i % 10000) == 0) {
  112. logger.info("progress " + (i / total) * 100 + "%");
  113. }
  114. }
  115. preparedStatement4.close();
  116. conn.close();
  117. logger.info("getRealisedSameAsLinks done...");
  118. } catch (VirtuosoException e) {
  119. // TODO Auto-generated catch block
  120. e.printStackTrace();
  121. }
  122.  
  123. }
  124.  
  125. public static void realization(String columname, String graphname) throws SQLException {
  126. try {
  127. String query1 = "SELECT DISTINCT id_to_iri(%s) FROM exmo.DBA.SAMEAS";
  128. String query2 = "SPARQL SELECT ?instance ?class FROM %s {\n" +
  129. "?class rdf:type owl:Class.\n" +
  130. "?instance a ?class.\n" +
  131. " FILTER NOT EXISTS {\n" +
  132. " ?subtype ^a ?instance ;\n" +
  133. " rdfs:subClassOf ?class .\n" +
  134. " FILTER ( ?subtype != ?class )\n" +
  135. " }\n" +
  136. "FILTER(?instance = <%s>)\n" +
  137. "}";
  138.  
  139. Connection conn = DriverManager.getConnection("jdbc:virtuoso://localhost:1111/", "dba", "dba");
  140. Statement st1 = conn.createStatement();
  141. ResultSet rows1 = st1.executeQuery(String.format(query1,columname));
  142.  
  143. String sql = "INSERT INTO exmo.DBA.INSTANCES (P, S, G, O) VALUES(iri_to_id(?),iri_to_id(?),iri_to_id(?),iri_to_id(?))";
  144. PreparedStatement preparedStatement3 = conn.prepareStatement(sql);
  145. double total = 729306;
  146. double i = 0;
  147. while (rows1.next()) {
  148. i++;
  149. Statement st2 = conn.createStatement();
  150. ResultSet rows2 = st2.executeQuery(String.format(query2, graphname, URIref.encode(rows1.getString(1))));
  151. while (rows2.next()) {
  152. preparedStatement3.setString(1, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
  153. preparedStatement3.setString(2, rows2.getString(1));
  154. preparedStatement3.setString(3, graphname);
  155. preparedStatement3.setString(4, rows2.getString(2));
  156. preparedStatement3.executeUpdate();
  157. if ((i % 10000) == 0) {
  158. logger.info("progress " + (i / total) * 100 + "%");
  159. }
  160. }
  161. st2.close();
  162. }
  163. st1.close();
  164. conn.close();
  165. } catch (Exception e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. }
  170.  
  171. /**
  172. * Change the select parameters to ?s ?o
  173. * @param graphname
  174. */
  175. public static void getSameAsLinks(String graphname){
  176. try {
  177. Connection conn = DriverManager.getConnection("jdbc:virtuoso://localhost:1111/", "dba", "dba");
  178. Statement st = conn.createStatement();
  179. String query = "SPARQL SELECT ?s ?s {GRAPH %s {\n" +
  180. "?o rdf:type owl:Class.\n" +
  181. "?s rdf:type ?o.\n" +
  182. "}}\n" +
  183. "";
  184. ResultSet rows = st.executeQuery(String.format(query, graphname));
  185. String sql = "INSERT INTO exmo.DBA.SAMEAS (instance1, instance2, graphname) VALUES(iri_to_id(?),iri_to_id(?),iri_to_id(?))";
  186. PreparedStatement preparedStatement = conn.prepareStatement(sql);
  187.  
  188. while (rows.next()) {
  189. preparedStatement.setString(1, rows.getString(1));
  190. preparedStatement.setString(2, rows.getString(2));
  191. preparedStatement.setString(3, graphname);
  192. preparedStatement.executeUpdate();
  193. }
  194. conn.close();
  195.  
  196. } catch (SQLException ex) {
  197. ex.printStackTrace();
  198. }
  199. }
  200.  
  201. public static String realisation_umbel_dbpedia(String instance1, String instance2) {
  202. String query = "DEFINE sql:log-enable 2 "
  203. + "SELECT ?instance1 ?class1 ?instance2 ?class2 {\n" +
  204. "OPTIONAL\n" +
  205. "{GRAPH <http://dbpedia.org> {\n" +
  206. "?class1 rdf:type owl:Class.\n" +
  207. "?instance1 rdf:type ?class1.\n" +
  208. "FILTER NOT EXISTS {?subclass1 rdfs:subClassOf ?class1. ?instance1 rdf:type ?subclass1}\n" +
  209. "FILTER (REGEX(?class1, 'http://dbpedia.org/ontology') && ?class1 != <http://dbpedia.org/ontology/Wikidata:Q532>)\n" +
  210. "}}\n" +
  211. "OPTIONAL\n" +
  212. "{GRAPH <http://umbel.org> {\n" +
  213. "?class2 rdf:type owl:Class.\n" +
  214. "?instance2 rdf:type ?class2.\n" +
  215. "FILTER NOT EXISTS {?subclass2 rdfs:subClassOf ?class2. ?instance2 rdf:type ?subclass2}\n" +
  216. "FILTER (REGEX(?class2, 'http://umbel.org'))\n" +
  217. "\n" +
  218. "}}\n" +
  219. "FILTER(?instance1 = <" + instance1 + "> && ?instance2 = <" + instance2 + ">)\n" +
  220. "}";
  221. return query;
  222. }
  223.  
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement