- 更新日時:
- 2014/02/17 19:05:46 (11 年 前)
- パス:
- BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL
- ファイル:
-
- 2 変更
凡例:
- 変更なし
- 追加
- 削除
-
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/ClassLink.java
r46 r49 15 15 private int numOfLinkedInstances = 0; 16 16 private int numOfOriginInstances = 0; 17 private int numOfOriginClassInstances = 0; 18 private int numOfLinkedClassInstances = 0; 17 19 18 20 /** … … 26 28 * @since 28.01.2014 27 29 */ 28 public ClassLink(String propertyURI, String linkedClassURI, Direction direction, int numLinks, int numofOriginInstances, int numOfLinkedInstances){ 30 public ClassLink(String propertyURI, String linkedClassURI, Direction direction, 31 int numLinks, int numOfOriginInstances, int numOfLinkedInstances, 32 int numOfOriginClassInstances, int numofLinkedClassInstances){ 29 33 this.propertyURI = propertyURI; 30 34 this.linkedClassURI = linkedClassURI; … … 33 37 this.numOfLinkedInstances = numOfLinkedInstances; 34 38 this.numOfOriginInstances = numOfOriginInstances; 39 this.numOfOriginClassInstances = numOfOriginClassInstances; 40 this.numOfLinkedClassInstances = numofLinkedClassInstances; 35 41 } 36 42 … … 69 75 sb.append(linkedClassURI); 70 76 sb.append(" ["); 77 sb.append(numOfOriginInstances); 78 sb.append("/"); 79 sb.append(numOfOriginClassInstances); 80 sb.append("]縲---"); 81 82 83 sb.append(" ["); 71 84 sb.append(numOfLinks); 85 sb.append("] --->"); 86 87 sb.append(" ["); 88 sb.append(numOfLinkedInstances); 89 sb.append("/"); 90 sb.append(numOfLinkedClassInstances); 72 91 sb.append("]"); 73 92 return sb.toString(); 74 93 } 75 94 … … 110 129 this.numOfOriginInstances = numOfOriginInstances; 111 130 } 131 132 133 public final int getNumOfOriginClassInstances() { 134 return numOfOriginClassInstances; 135 } 136 137 138 public final void setNumOfOriginClassInstances(int numOfOriginClassInstances) { 139 this.numOfOriginClassInstances = numOfOriginClassInstances; 140 } 141 142 143 public final int getNumOfLinkedClassInstances() { 144 return numOfLinkedClassInstances; 145 } 146 147 148 public final void setNumOfLinkedClassInstances(int numOfLinkedClassInstances) { 149 this.numOfLinkedClassInstances = numOfLinkedClassInstances; 150 } 112 151 113 152 -
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilderImpl.java
r46 r49 73 73 */ 74 74 75 75 76 System.out.println("CLS"); 76 77 … … 95 96 } 96 97 98 /* 97 99 100 String uri = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry"; 98 101 102 ClassLink[] cls = null; 99 103 100 /*101 uri = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry";102 103 104 System.out.println("CLS-INS"); 104 105 cls = null; … … 356 357 // SELECT 357 358 if (countLinks) { 358 queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(? s) AS ?numOfLinks) (COUNT(DISTINCT(?s)) AS ?numOfLinkedInstances) \n");359 queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?oci) AS ?numOfOriginalClassInstances) (COUNT(?lci) AS ?numOfLinkedClassInstances) (COUNT(?s) AS ?numOfLinks) (COUNT(DISTINCT(?s)) AS ?numOfLinkedInstances) \n"); 359 360 } else { 360 361 queryStr.append("SELECT DISTINCT ?p ?pDirection ?c \n"); … … 477 478 } 478 479 ClassLink cl = new ClassLink(proURI, clsURI, direction, 479 numOfLinks, 0, 0 );480 numOfLinks, 0, 0, 0, 0 ); 480 481 solCLs.add(cl); 481 482 } … … 509 510 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 510 511 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 511 queryStr.append("SELECT DISTINCT ?pf ?pr ?c \n");512 queryStr.append("SELECT DISTINCT ?pf ?pr (COUNT(?pf) AS ?numOfForwardLinks) (COUNT(?pr) AS ?numOfReverseLinks) (COUNT(DISTINCT(?insOrg)) AS ?numOfOriginInstances) (COUNT(DISTINCT(?ins)) AS ?numOfLinkedInstances) ?c \n"); 512 513 if (graphURIs != null) { 513 514 for (String graphURI : graphURIs) { … … 524 525 queryStr.append(" { ?ins ?pr ?insOrg. }\n UNION { ?insOrg ?pf ?ins. }\n"); 525 526 queryStr.append("}\n"); 527 queryStr.append("GROUP BY ?pf ?pr ?c\n"); 526 528 if (limit > 0) { 527 529 queryStr.append("limit "); … … 536 538 query); 537 539 538 ResultSet results = qexec.execSelect(); 540 ResultSet results = null; 541 try{ 542 long start = System.currentTimeMillis(); 543 results = qexec.execSelect(); 544 long end = System.currentTimeMillis(); 545 System.out.println("EXEC TIME: " + (end-start)); 546 }catch(Exception ex){ 547 ex.printStackTrace(); 548 throw ex; 549 } 550 539 551 ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>(); 540 552 for (; results.hasNext();) { … … 545 557 Direction direction = null; 546 558 String propURI = null; 559 int numOfLinks = 0; 560 int numOfLinkedInstances = 0; 561 int numOfOriginInstances = 0; 547 562 if (proForward != null) { 548 563 if (proReverse != null) { 564 numOfLinks = sol.getLiteral("numOfForwardLinks").getInt(); 565 numOfLinks += sol.getLiteral("numOfReverseLinks").getInt(); 566 numOfLinkedInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 567 numOfOriginInstances = sol.getLiteral("numOfOriginInstances").getInt(); 549 568 direction = Direction.both; 550 569 } else { 570 numOfLinkedInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 571 numOfOriginInstances = sol.getLiteral("numOfOriginInstances").getInt(); 572 numOfLinks = sol.getLiteral("numOfForwardLinks").getInt(); 551 573 direction = Direction.forward; 552 574 } … … 555 577 direction = Direction.reverse; 556 578 propURI = proReverse.getURI(); 579 numOfLinkedInstances = sol.getLiteral("numOfOriginInstances").getInt(); 580 numOfOriginInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 581 numOfLinks = sol.getLiteral("numOfReverseLinks").getInt(); 557 582 } 558 583 String clsURI = cls.getURI(); 559 584 // System.out.println(propURI + " " + clsURI + " " + direction); 560 585 561 ClassLink cl = new ClassLink(propURI, clsURI, direction, 0, 0, 0);586 ClassLink cl = new ClassLink(propURI, clsURI, direction, numOfLinks, numOfOriginInstances, numOfLinkedInstances, 0, 0); 562 587 solCLs.add(cl); 563 588 } … … 834 859 classLink.setNumOfOriginInstances(numOfOriginInstances); 835 860 } 861 lit = sol.getLiteral("numOfOriginClassInstances"); 862 if (lit != null) { 863 int numOfOriginClassInstances = lit.getInt(); 864 classLink.setNumOfOriginClassInstances(numOfOriginClassInstances); 865 } 866 lit = sol.getLiteral("numOfLinkedClassInstances"); 867 if (lit != null) { 868 int numOfLinkedClassInstances = lit.getInt(); 869 classLink.setNumOfLinkedClassInstances(numOfLinkedClassInstances); 870 } 836 871 } 837 872 qexec.close(); 873 874 // count instances 875 queryStr = new StringBuffer(); 876 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); 877 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 878 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 879 880 queryStr.append("SELECT (COUNT(?ics) AS ?numOfOriginClassInstances) \n"); 881 if (graphURIs != null) { 882 for (String graphURI : graphURIs) { 883 queryStr.append("FROM <"); 884 queryStr.append(graphURI); 885 queryStr.append(">\n"); 886 } 887 } 888 889 queryStr.append("WHERE{\n"); 890 if (classLink.getDirection() == Direction.forward) { 891 queryStr.append("?ics rdf:type <"); 892 queryStr.append(startClassURI); 893 queryStr.append(">.\n"); 894 queryStr.append("}"); 895 } else { 896 if (classLink.getDirection() == Direction.forward) { 897 queryStr.append("?ics rdf:type <"); 898 queryStr.append(classLink.getLinkedClassURI()); 899 queryStr.append(">.\n"); 900 queryStr.append("}"); 901 } else { 902 queryStr.append("?ics rdf:type <"); 903 queryStr.append(classLink.getLinkedClassURI()); 904 queryStr.append(">.\n"); 905 queryStr.append("}"); 906 } 907 } 908 909 System.out.println(queryStr.toString()); 910 911 query = QueryFactory.create(queryStr.toString()); 912 qexec = QueryExecutionFactory.sparqlService( 913 endpointURI, query); 914 915 results = qexec.execSelect(); 916 if (results.hasNext()) { 917 QuerySolution sol = results.next(); 918 Literal lit = null; 919 lit = sol.getLiteral("numOfOriginClassInstances"); 920 if (lit != null) { 921 int numOfOriginClassInstances = lit.getInt(); 922 classLink.setNumOfOriginClassInstances(numOfOriginClassInstances); 923 } 924 } 925 qexec.close(); 926 927 // count instances 928 queryStr = new StringBuffer(); 929 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); 930 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 931 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 932 933 queryStr.append("SELECT (COUNT(?ocs) AS ?numOfLinkedClassInstances) \n"); 934 if (graphURIs != null) { 935 for (String graphURI : graphURIs) { 936 queryStr.append("FROM <"); 937 queryStr.append(graphURI); 938 queryStr.append(">\n"); 939 } 940 } 941 942 queryStr.append("WHERE{\n"); 943 if (classLink.getDirection() == Direction.forward) { 944 queryStr.append("?ocs rdf:type <"); 945 queryStr.append(classLink.getLinkedClassURI()); 946 queryStr.append(">.\n"); 947 queryStr.append("}"); 948 } else { 949 if (classLink.getDirection() == Direction.forward) { 950 queryStr.append("?ocs rdf:type <"); 951 queryStr.append(startClassURI); 952 queryStr.append(">.\n"); 953 queryStr.append("}"); 954 } else { 955 queryStr.append("?ocs rdf:type <"); 956 queryStr.append(startClassURI); 957 queryStr.append(">.\n"); 958 queryStr.append("}"); 959 } 960 } 961 962 System.out.println(queryStr.toString()); 963 964 query = QueryFactory.create(queryStr.toString()); 965 qexec = QueryExecutionFactory.sparqlService( 966 endpointURI, query); 967 968 results = qexec.execSelect(); 969 if (results.hasNext()) { 970 QuerySolution sol = results.next(); 971 Literal lit = null; 972 lit = sol.getLiteral("numOfLinkedClassInstances"); 973 if (lit != null) { 974 int numOfLinkedClassInstances = lit.getInt(); 975 classLink.setNumOfLinkedClassInstances(numOfLinkedClassInstances); 976 } 977 } 978 qexec.close(); 979 838 980 } 839 981 return classLinks; 840 982 } 841 983 984 842 985 public SClass[] countInstances(String[] graphURIs, SClass[] classes) 843 986 throws Exception {