示例#1
0
 /**
  * Obtain a Model from the persistent TDB store.
  *
  * @param path2db (location of the TDB store).
  * @return {@link Model}
  */
 public static Model model(String path2db) {
   // TDB.setExecutionLoggin(InfoLevel.INFO);
   TDB.getContext().set(TDB.symLogExec, false);
   TDB.getContext().set(TDB.symUnionDefaultGraph, true);
   rdf_model = TDBFactory.createModel(path2db);
   return rdf_model;
 }
示例#2
0
 @After
 public void tearDown() {
   TDB.sync(dsGraph);
   dsGraph.close();
   f.delete();
   TDB.closedown();
 }
示例#3
0
 @Override
 protected Graph createGraph() throws IOException {
   TDB.init();
   f = new File(System.getProperty("java.io.tmpdir") + "/TDBTest");
   dsGraph = TDBFactory.createDatasetGraph(f.getCanonicalPath());
   return dsGraph.getDefaultGraph();
 }
示例#4
0
 /**
  * Remove a Resource from the specified model. If property is null, the entire resource will be
  * removed. Otherwise only the specified property will be removed from the resource.
  *
  * @param path2db (location of the TDB store).
  * @param uri (base URI of the resource):
  * @param id (resource id).
  * @param property (property of the resource). can be null.
  */
 public static void removeResource(String path2db, String uri, String id, Property property) {
   // TDB.setExecutionLogging(InfoLevel.INFO);
   TDB.getContext().set(TDB.symLogExec, true);
   rdf_model = TDBFactory.createModel(path2db);
   if (property == null) {
     rdf_model.removeAll(rdf_model.createResource(uri + id), null, null);
   } else {
     rdf_model.removeAll(rdf_model.createResource(uri + id), property, null);
   }
 }
示例#5
0
 public static void processGlobalSystemProperties() {
   Context context = processProperties(System.getProperties());
   TDB.getContext().setAll(context);
 }
示例#6
0
 /**
  * Remove all data (triples) from the specified TDB store.
  *
  * @param path2db (location of the TDB store).
  */
 public static void removeData(String path2db) {
   // TDB.setExecutionLogging(InfoLevel.INFO);
   TDB.getContext().set(TDB.symLogExec, true);
   rdf_model = TDBFactory.createModel(path2db);
   rdf_model.removeAll();
 }
示例#7
0
  public static void main(String args[]) {
    String SOURCE = "http://spitfire-project.eu/ontology.rdf",
        SOURCE1 = "http://spitfire-project.eu/sn.rdf",
        NS = "http://spitfire-project.eu/ontology/ns/",
        NS1 = "http://spitfire-project.eu/ontology/ns/sn/";

    // create a model using reasoner
    OntModel model1_reasoner =
        ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
    OntModel model_instances =
        ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
    // create a model which doesn't use a reasoner
    OntModel model2_noreasoner = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

    /** ===========1. STORE IN A NON-OwlFull MODEL==============* */
    // read the RDF/XML file
    model1_reasoner.read(SOURCE, "RDF/XML");
    model1_reasoner.read(SOURCE1, "RDF/XML");

    model2_noreasoner.read(SOURCE, "RDF/XML");

    model2_noreasoner.read(SOURCE1, "RDF/XML");
    model1_reasoner.add(
        model1_reasoner.createResource(NS + "containedIn"), RDF.type, OWL.TransitiveProperty);

    // add the instances
    //        model1_reasoner.add(model1_reasoner.createResource(NS+"fan123"), RDF.type,
    //        		model1_reasoner.createResource(NS1+"Fan"));
    //		model_instances.add(model_instances.createResource(NS+"fan123"), RDF.type,
    //				model_instances.createResource(NS1+"Fan"));

    model_instances.add(
        model_instances.getProperty(NS + "containedIn"),
        OWL.equivalentProperty,
        model_instances.createProperty(
            "http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#hasLocation"));
    model_instances.add(
        model_instances.createResource(NS + "desk_a"),
        model_instances.getProperty(NS + "containedIn"),
        model_instances.createResource(NS + "floor3"));
    model_instances.add(
        model2_noreasoner.createResource(NS + "floor3"),
        model_instances.getProperty(NS + "containedIn"),
        model_instances.createResource(NS + "cti"));
    //		model1_reasoner.add(model2_noreasoner);

    model1_reasoner.add(model2_noreasoner);

    // prints out the RDF/XML structure
    printModel(model1_reasoner, null);
    printModel(model1_reasoner, model1_reasoner.getProperty(NS + "containedIn"));
    //		printModel(model2_noreasoner);

    /** ===========2. STORE IN THE TDB==============* */
    // Direct way: Make a TDB-backed dataset
    String directory =
        System.getProperty("user.dir")
            + ".ld4s/tdb"
            + LD4SConstants.SYSTEM_SEPARATOR
            + "LD4SDataset1";
    File dirf = new File(directory);
    if (!dirf.exists()) {
      dirf.mkdirs();
    }
    Dataset dataset = TDBFactory.createDataset(directory);
    TDB.sync(dataset);

    Resource subj = model1_reasoner.listSubjects().next();
    dataset.begin(ReadWrite.WRITE);
    try {
      dataset.addNamedModel(subj.getURI(), model1_reasoner);
      dataset.addNamedModel(NS + "desk_a", model_instances);
      dataset.commit();

      // Or call .abort()
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      dataset.end();
      dataset.close();
    }

    /** ===========3. QUERY==============* */
    // Create a new query
    String queryString =
        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
            + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>  "
            + "PREFIX dul: <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>  "
            + "PREFIX spt: <"
            + NS
            + ">  "
            + "select ?uri "
            + "where { "
            + "?uri dul:hasLocation spt:cti  "
            + "} ";
    Query query = QueryFactory.create(queryString);

    System.out.println("----------------------");

    System.out.println("Query Result Sheet");

    System.out.println("----------------------");

    System.out.println("Direct&Indirect Descendants (model1)");

    System.out.println("-------------------");

    // Execute the query and obtain results
    QueryExecution qe = QueryExecutionFactory.create(query, model1_reasoner);
    com.hp.hpl.jena.query.ResultSet results = qe.execSelect();

    // Output query results
    ResultSetFormatter.out(System.out, results, query);

    qe.close();

    System.out.println("----------------------");
    System.out.println("Only Direct Descendants (model2)");
    System.out.println("----------------------");

    // Execute the query and obtain results
    qe = QueryExecutionFactory.create(query, model2_noreasoner);
    results = qe.execSelect();

    // Output query results
    ResultSetFormatter.out(System.out, results, query);
    qe.close();
  }