public void testDoesNotReify(String title, Resource r) {
   try {
     r.as(ReifiedStatement.class);
     fail(title + " (" + r + ")");
   } catch (DoesNotReifyException e) {
     /* that's what we expect */
   }
 }
Example #2
0
  /**
   * Answer the lowest common ancestor of two classes in a given ontology. This is the class that is
   * farthest from the root concept (defaulting to <code>owl:Thing</code> which is a super-class of
   * both <code>u</code> and <code>v</code>. The algorithm is based on <a
   * href="http://en.wikipedia.org/wiki/Tarjan's_off-line_least_common_ancestors_algorithm">Tarjan's
   * off-line LCA</a>. The current implementation expects that the given model:
   *
   * <ul>
   *   <li>is transitively closed over the <code>subClassOf</code> relation
   *   <li>can cheaply determine <em>direct sub-class</em> relations
   * </ul>
   *
   * <p>Both of these conditions are true of the built-in Jena OWL reasoners, such as {@link
   * OntModelSpec#OWL_MEM_MICRO_RULE_INF}, and external DL reasoners such as Pellet.
   *
   * @param m The ontology model being queried to find the LCA, which should conform to the reasoner
   *     capabilities described above
   * @param u An ontology class
   * @param v An ontology class
   * @return The LCA of <code>u</code> and <code>v</code>
   * @exception JenaException if the language profile of the given model does not define a top
   *     concept (e.g. <code>owl:Thing</code>)
   */
  public static OntClass getLCA(OntModel m, OntClass u, OntClass v) {
    Resource root = m.getProfile().THING();
    if (root == null) {
      throw new JenaException(
          "The given OntModel has a language profile that does not define a generic root class (such as owl:Thing)");
    }

    root = root.inModel(m);
    return getLCA(m, root.as(OntClass.class), u, v);
  }
 /**
  * the simplest case: if we assert all the components of a reification quad, we can get a
  * ReifiedStatement that represents the reified statement.
  */
 public void testBasicReification() {
   if (model.getReificationStyle() != ModelFactory.Minimal) {
     Resource R = model.createResource(aURI);
     model.add(R, RDF.type, RDF.Statement);
     model.add(R, RDF.subject, S);
     model.add(R, RDF.predicate, P);
     model.add(R, RDF.object, O);
     RDFNode rs = R.as(ReifiedStatement.class);
     assertEquals("can recover statement", SPO, ((ReifiedStatement) rs).getStatement());
   }
 }
 /**
  * check that, from a model with any combination of the statements given, we can convert R into a
  * ReifiedStatement iff the four components of the quad are in the model.
  */
 public void testReificationCombinations() {
   Resource RR = model.createResource(aURI), SS = model.createResource(anotherURI);
   Property PP = (Property) RR.as(Property.class);
   Object[][] statements = {
     {model.createStatement(RR, RDF.type, RDF.Statement), new Integer(1)},
     {model.createStatement(RR, RDF.subject, SS), new Integer(2)},
     {model.createStatement(RR, RDF.predicate, PP), new Integer(4)},
     {model.createStatement(RR, RDF.object, O), new Integer(8)},
     {model.createStatement(SS, PP, O), new Integer(16)},
     {model.createStatement(RR, PP, O), new Integer(32)},
     {model.createStatement(SS, RDF.subject, SS), new Integer(64)},
     {model.createStatement(SS, RDF.predicate, PP), new Integer(128)},
     {model.createStatement(SS, RDF.object, O), new Integer(256)},
     {model.createStatement(SS, RDF.type, RDF.Statement), new Integer(512)}
   };
   if (model.getReificationStyle() != ModelFactory.Minimal)
     testCombinations(model, RR, 0, statements, statements.length);
 }
 /**
  * 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);
   }
 }
 private static List<RDFNode> asJavaList(Resource resource) {
   return (resource.as(RDFList.class)).asJavaList();
 }