| 66 | System.out.println("CLS"); |
| 67 | ClassLink[] cls = builder.getNextClass(null, clz[1].getClassURI(), 100, |
| 68 | false); |
| 69 | if (cls != null) { |
| 70 | for (ClassLink cl : cls) { |
| 71 | System.out.println(cl.toString()); |
| 72 | } |
| 73 | cls = builder.countLinks(null, clz[1].getClassURI(), cls); |
| 74 | for (ClassLink cl : cls) { |
| 75 | System.out.println(cl.toString()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 651 | public ClassLink[] countLinks(String[] graphURIs, String startClassURI, |
| 652 | ClassLink[] classLinks) throws Exception { |
| 653 | if (classLinks == null || classLinks.length == 0) { |
| 654 | return new ClassLink[0]; |
| 655 | } |
| 656 | for (ClassLink classLink : classLinks) { |
| 657 | StringBuffer queryStr = new StringBuffer(); |
| 658 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); |
| 659 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
| 660 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
| 661 | |
| 662 | queryStr.append("SELECT (COUNT(?os) AS ?numOfLinks) \n"); |
| 663 | if (graphURIs != null) { |
| 664 | for (String graphURI : graphURIs) { |
| 665 | queryStr.append("FROM <"); |
| 666 | queryStr.append(graphURI); |
| 667 | queryStr.append(">\n"); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | queryStr.append("WHERE{\n"); |
| 672 | if (classLink.getDirection() == Direction.forward) { |
| 673 | queryStr.append("?is rdf:type <"); |
| 674 | queryStr.append(startClassURI); |
| 675 | queryStr.append(">.\n"); |
| 676 | queryStr.append("?os rdf:type <"); |
| 677 | queryStr.append(classLink.getLinkedClassURI()); |
| 678 | queryStr.append(">.\n"); |
| 679 | queryStr.append("?is ?p ?os.\n"); |
| 680 | queryStr.append("}"); |
| 681 | } else { |
| 682 | if (classLink.getDirection() == Direction.forward) { |
| 683 | queryStr.append("?os rdf:type <"); |
| 684 | queryStr.append(startClassURI); |
| 685 | queryStr.append(">.\n"); |
| 686 | queryStr.append("?is rdf:type <"); |
| 687 | queryStr.append(classLink.getLinkedClassURI()); |
| 688 | queryStr.append(">.\n"); |
| 689 | queryStr.append("?is ?p ?os.\n"); |
| 690 | queryStr.append("}"); |
| 691 | } else { |
| 692 | queryStr.append("?os rdf:type <"); |
| 693 | queryStr.append(startClassURI); |
| 694 | queryStr.append(">.\n"); |
| 695 | queryStr.append("?is rdf:type <"); |
| 696 | queryStr.append(classLink.getLinkedClassURI()); |
| 697 | queryStr.append(">.\n"); |
| 698 | queryStr.append("{?is ?p ?os.}\n"); |
| 699 | queryStr.append("UNION\n"); |
| 700 | queryStr.append("{?os ?p ?is.}\n"); |
| 701 | queryStr.append("}"); |
| 702 | } |
| 703 | } |
| 704 | System.out.println(queryStr.toString()); |
| 705 | |
| 706 | Query query = QueryFactory.create(queryStr.toString()); |
| 707 | QueryExecution qexec = QueryExecutionFactory.sparqlService( |
| 708 | endpointURI, query); |
| 709 | |
| 710 | ResultSet results = qexec.execSelect(); |
| 711 | if (results.hasNext()) { |
| 712 | QuerySolution sol = results.next(); |
| 713 | Literal lit = sol.getLiteral("numOfLinks"); |
| 714 | if (lit != null) { |
| 715 | int numOfLinks = lit.getInt(); |
| 716 | classLink.setNumOfLinks(numOfLinks); |
| 717 | } |
| 718 | } |
| 719 | qexec.close(); |
| 720 | } |
| 721 | return classLinks; |
| 722 | } |
| 723 | |