public void testBulkByModel(Model m) {
   assertEquals("precondition: model must be empty", 0, m.size());
   Model A = modelWithStatements("clouds offer rain; trees offer shelter");
   Model B = modelWithStatements("x R y; y Q z; z P x");
   m.add(A);
   assertIsoModels(A, m);
   m.add(B);
   m.remove(A);
   assertIsoModels(B, m);
   m.remove(B);
   assertEquals("", 0, m.size());
 }
 public void testMBU(Model m) {
   Statement[] sArray = statements(m, "moon orbits earth; earth orbits sun");
   List<Statement> sList = Arrays.asList(statements(m, "I drink tea; you drink coffee"));
   m.add(sArray);
   testContains(m, sArray);
   m.add(sList);
   testContains(m, sList);
   testContains(m, sArray);
   /* */
   m.remove(sArray);
   testOmits(m, sArray);
   testContains(m, sList);
   m.remove(sList);
   testOmits(m, sArray);
   testOmits(m, sList);
 }
  @Test
  public void testUpdate() throws Exception {
    Resource rhmRes = model.getResource(FakeRDFModel.rhm);
    Property name = model.getProperty(foaf + "name");

    Statement nameSt = rhmRes.listProperties(name).nextStatement();
    String originalName = nameSt.getLiteral().getString();

    // assume update is always first remove and then add
    model.remove(nameSt);
    model.add(rhmRes, name, "TESTNAME");

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME");

    changes.undo();

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes
        .listProperties(name)
        .nextStatement()
        .getLiteral()
        .getString()
        .equals(originalName);

    changes.redo();

    assert changes.size() == 2;
    assert rhmRes.listProperties(name).toList().size() == 1;
    assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME");
  }
  @Test
  public void testDelete() throws Exception {
    Resource rhmRes = model.getResource(FakeRDFModel.rhm);

    Model toRemove = ModelFactory.createDefaultModel();
    toRemove.add(model.listStatements(rhmRes, null, (RDFNode) null));
    toRemove.add(model.listStatements(null, null, rhmRes));

    model.remove(toRemove);

    Model foo = changes.get(0).getChange();

    assert changes.size() == 1;
    assert !model.containsResource(rhmRes);

    changes.undo();

    assert changes.size() == 1;
    assert model.containsResource(rhmRes);
    assert model.containsAll(foo);

    changes.redo();

    assert changes.size() == 1;
    assert !model.containsResource(rhmRes);
  }
 /**
  * Example the conclusions graph for introduction of restrictions which require a comprehension
  * rewrite and declare new (anon) classes for those restrictions.
  */
 public void comprehensionAxioms(Model premises, Model conclusions) {
   // Comprehend all restriction declarations and note them in a map
   Map<Resource, Resource> comprehension = new HashMap<>();
   StmtIterator ri = conclusions.listStatements(null, RDF.type, OWL.Restriction);
   while (ri.hasNext()) {
     Resource restriction = ri.nextStatement().getSubject();
     StmtIterator pi = restriction.listProperties(OWL.onProperty);
     while (pi.hasNext()) {
       Resource prop = (Resource) pi.nextStatement().getObject();
       StmtIterator vi = restriction.listProperties();
       while (vi.hasNext()) {
         Statement rs = vi.nextStatement();
         if (!rs.getPredicate().equals(OWL.onProperty)) {
           // Have a restriction on(prop) of type rs in the conclusions
           // So assert a premise that such a restriction could exisit
           Resource comp =
               premises
                   .createResource()
                   .addProperty(RDF.type, OWL.Restriction)
                   .addProperty(OWL.onProperty, prop)
                   .addProperty(rs.getPredicate(), rs.getObject());
           comprehension.put(restriction, comp);
         }
       }
     }
   }
   // Comprehend any intersectionOf lists. Introduce anon class which has the form
   // of the intersection expression.
   // Rewrite queries of the form (X intersectionOf Y) to the form
   //   (X equivalentClass ?CC) (?CC intersectionOf Y)
   StmtIterator ii = conclusions.listStatements(null, OWL.intersectionOf, (RDFNode) null);
   List<Statement> intersections = new ArrayList<>();
   while (ii.hasNext()) {
     intersections.add(ii.nextStatement());
   }
   for (Statement is : intersections) {
     // Declare in the premises that such an intersection exists
     Resource comp =
         premises
             .createResource()
             .addProperty(RDF.type, OWL.Class)
             .addProperty(
                 OWL.intersectionOf, mapList(premises, (Resource) is.getObject(), comprehension));
     // Rewrite the conclusions to be a test for equivalence between the class being
     // queried and the comprehended interesection
     conclusions.remove(is);
     conclusions.add(is.getSubject(), OWL.equivalentClass, comp);
   }
   // Comprehend any oneOf lists
   StmtIterator io = conclusions.listStatements(null, OWL.oneOf, (RDFNode) null);
   while (io.hasNext()) {
     Statement s = io.nextStatement();
     Resource comp = premises.createResource().addProperty(OWL.oneOf, s.getObject());
   }
 }
 public void testBulkDeleteByModelReifying(boolean suppress) {
   Model target = modelWithStatements(ReificationStyle.Minimal, "");
   addReification(target, "x", "S P O");
   addReification(target, "y", "A P B");
   Model remove = modelWithStatements("");
   addReification(remove, "y", "A P B");
   Model answer = modelWithStatements("");
   addReification(answer, "x", "S P O");
   if (suppress) addReification(answer, "y", "A P B");
   target.remove(remove, suppress);
   assertIsoModels(answer, target);
 }
Exemple #7
0
  /** Removes all triples which have something to do with the given namespace */
  public static Model removeNamespace(String ns, Model m) throws ModelException {

    Model res = m.duplicate();
    for (Enumeration en = m.duplicate().elements(); en.hasMoreElements(); ) {

      Statement t = (Statement) en.nextElement();
      if (t.subject().toString().startsWith(ns)
          || t.predicate().toString().startsWith(ns)
          || t.object().toString().startsWith(ns)) {
        //      System.err.println("REMOVING TRIPLE: " + t);
        res.remove(t);
      }
    }
    return res;
  }
 /**
  * walk down the set of statements (represented as an array), recursing with and without each
  * statement being present. The mask bits record those statements that are in the model. At the
  * bottom of the recursion (n == 0), check that R can be reified exactly when all four quad
  * components are present; the other statements don't matter.
  */
 private void testCombinations(Model m, Resource R, int mask, Object[][] statements, int n) {
   if (n == 0) {
     try {
       // System.err.println( "| hello. mask = " + mask );
       ReifiedStatement rs = (ReifiedStatement) R.as(ReifiedStatement.class);
       // System.err.println( "+  we constructed " + rs );
       assertTrue(
           "should not reify: not all components present [" + mask + "]: " + rs,
           (mask & 15) == 15);
       // System.err.println( "+  and we passed the assertion." );
     } catch (DoesNotReifyException e) { // System.err.println( "+  we exploded" );
       assertFalse("should reify: all components present", mask == 15);
     }
   } else {
     int i = n - 1;
     Statement s = (Statement) statements[i][0];
     int bits = ((Integer) statements[i][1]).intValue();
     testCombinations(m, R, mask, statements, i);
     m.add(s);
     testCombinations(m, R, mask + bits, statements, i);
     m.remove(s);
   }
 }
 public void testBulkRemoveSelf() {
   Model m = modelWithStatements("they sing together; he sings alone");
   m.remove(m);
   assertEquals("", 0, m.size());
 }