Exemplo n.º 1
0
 /**
  * Utility to load a file into a model a Model. Files are assumed to be relative to the BASE_URI.
  *
  * @param file the file name, relative to baseDir
  * @return the loaded Model
  */
 public static Model loadFile(String file, Model model) throws IOException {
   String langType = "RDF/XML";
   if (file.endsWith(".nt")) {
     langType = "N-TRIPLE";
   } else if (file.endsWith("n3")) {
     langType = "N3";
   }
   String fname = file;
   if (fname.startsWith(BASE_URI)) {
     fname = fname.substring(BASE_URI.length());
   }
   Reader reader = new BufferedReader(new FileReader(BASE_TESTDIR + fname));
   model.read(reader, BASE_URI + fname, langType);
   return model;
 }
Exemplo n.º 2
0
 /** Load all of the known manifest files into a single model */
 public static Model loadAllTestDefinitions() {
   System.out.print("Loading manifests ");
   System.out.flush();
   Model testDefs = ModelFactory.createDefaultModel();
   int count = 0;
   for (String TEST_DIR : TEST_DIRS) {
     File dir = new File(BASE_TESTDIR + TEST_DIR);
     String[] manifests =
         dir.list(
             new FilenameFilter() {
               @Override
               public boolean accept(File df, String name) {
                 if (name.startsWith("Manifest") && name.endsWith(".rdf")) {
                   return includeModified || !name.endsWith("-mod.rdf");
                 } else {
                   return false;
                 }
               }
             });
     for (String manifest : manifests) {
       File mf = new File(dir, manifest);
       try {
         testDefs.read(new FileInputStream(mf), "file:" + mf);
         count++;
         if (count % 8 == 0) {
           System.out.print(".");
           System.out.flush();
         }
       } catch (FileNotFoundException e) {
         System.out.println("File not readable - " + e);
       }
     }
   }
   System.out.println("loaded");
   return testDefs;
 }
  public static void main(String[] args) {

    List<String> obj = new ArrayList<String>();

    Scanner input = new Scanner(System.in);

    System.out.print("Enter URI: ");

    String userIn = input.nextLine();

    // create an empty Model
    Model model = ModelFactory.createDefaultModel();

    // read the RDF/XML file
    model.read(userIn);

    // write it to standard out
    // model.write(System.out);

    // list the statements in the Model
    StmtIterator iter = model.listStatements();

    System.out.println();

    // print out the predicate, subject and object of each statement
    while (iter.hasNext()) {
      Statement stmt = iter.nextStatement(); // get next statement
      Resource subject = stmt.getSubject(); // get the subject
      Property predicate = stmt.getPredicate(); // get the predicate
      RDFNode object = stmt.getObject(); // get the object

      System.out.print(subject.toString());
      System.out.print(" -> " + predicate.toString() + " -> ");
      if (object instanceof Resource) {
        System.out.print(object.toString() + "\n");
      } else {
        // object is a literal
        System.out.print(" \"" + object.toString() + "\"\n");
      }
    }

    /* for(int i = 0; i < (obj.size()); i++){

    	String sparqlQueryString1=
    								"SELECT ?s ?o "+
    								"WHERE {"+
    								"?s ?p ?o ."+
    								"?o <bif:contains> \""+obj.get(i)+"\" ."+
    								"}"+
    								"limit 10";

    		      Query query = QueryFactory.create(sparqlQueryString1);
    		      QueryExecution qexec = QueryExecutionFactory.sparqlService("http://pubmed.bio2rdf.org/sparql", query);

    		      ResultSet results = qexec.execSelect();
    		      System.out.println("Query: "+obj.get(i));
    		      ResultSetFormatter.out(System.out, results, query);

    		     qexec.close() ;
    } */

  }