Ejemplo n.º 1
0
  private List<OntClass> listNamedClasses() {
    List<OntClass> result = new ArrayList<OntClass>();

    StmtIterator classes = model.listStatements(null, RDF.type, OWL.Class);
    while (classes.hasNext()) {
      Resource type = classes.next().getSubject();
      String uri = type.getURI();
      if (uri == null || manager.getDatatypeByUri(uri) != null) continue;
      if (!isStandard(uri)) {
        result.add(type.as(OntClass.class));
      }
    }
    classes = model.listStatements(null, RDF.type, RDFS.Class);
    while (classes.hasNext()) {
      Resource type = classes.next().getSubject();
      String uri = type.getURI();
      if (uri == null || manager.getDatatypeByUri(uri) != null) continue;
      if (!isStandard(uri)) {
        result.add(type.as(OntClass.class));
      }
    }

    List<Resource> rdfsClassList = model.listResourcesWithProperty(RDF.type, RDFS.Class).toList();
    for (Resource r : rdfsClassList) {
      if (r.canAs(OntClass.class)
          && r.getURI() != null
          && !isStandard(r.getURI())
          && manager.getDatatypeByUri(r.getURI()) == null) {
        result.add(r.as(OntClass.class));
      }
    }
    return result;
  }
Ejemplo n.º 2
0
 private void addTriplePatterns(Property predicate, Set<TriplePattern> results) {
   StmtIterator it = getModel().listStatements(null, predicate, this);
   while (it.hasNext()) {
     Resource subject = it.nextStatement().getSubject();
     results.add((TriplePattern) subject.as(TriplePattern.class));
   }
 }
 public void testDoesNotReify(String title, Resource r) {
   try {
     r.as(ReifiedStatement.class);
     fail(title + " (" + r + ")");
   } catch (DoesNotReifyException e) {
     /* that's what we expect */
   }
 }
Ejemplo n.º 4
0
 public RDFList getProcessingSteps(Resource identity) {
   if (_model.contains(identity, Vertere.process)) {
     Resource processResource = _model.getProperty(identity, Vertere.process).getResource();
     RDFList processSteps = processResource.as(RDFList.class);
     return processSteps;
   } else {
     return null;
   }
 }
Ejemplo n.º 5
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());
   }
 }
Ejemplo n.º 7
0
  private List<OntResource> listUnionMembers(OntProperty p, OntResource domain) {
    List<OntResource> list = new ArrayList<OntResource>();
    Resource union = domain.getPropertyResourceValue(OWL.unionOf);
    if (union != null && union.canAs(RDFList.class)) {

      RDFList rdfList = union.as(RDFList.class);
      Iterator<RDFNode> sequence = rdfList.iterator();
      while (sequence.hasNext()) {
        list.add(sequence.next().as(OntResource.class));
      }
    }
    return list;
  }
Ejemplo n.º 8
0
  private void handlePropertySubclassOf(OntProperty p) {

    List<RDFNode> list = p.listPropertyValues(RDFS.subClassOf).toList();
    for (RDFNode node : list) {
      if (!node.canAs(Resource.class)) continue;
      Resource resource = node.asResource();
      Resource onPropertyValue = resource.getPropertyResourceValue(OWL.onProperty);
      if (!RDFS.domain.equals(onPropertyValue)) continue;
      Resource someValuesFrom = resource.getPropertyResourceValue(OWL.someValuesFrom);
      if (someValuesFrom == null) continue;
      String uri = someValuesFrom.getURI();
      if (uri != null) {
        OntResource type = someValuesFrom.as(OntResource.class);
        addField(type, p, null);
      } else {
        Resource unionList = someValuesFrom.getPropertyResourceValue(OWL.unionOf);
        while (unionList != null) {
          Resource first = unionList.getPropertyResourceValue(RDF.first);
          if (first != null) {
            String typeURI = first.getURI();
            if (typeURI == null) {
              logger.warn(
                  "Cannot handle union that contains an anonymous class in the domain of "
                      + p.getURI());
            } else {
              OntResource type = first.as(OntResource.class);
              addField(type, p, null);
            }
          }
          unionList = unionList.getPropertyResourceValue(RDF.rest);
          if (RDF.nil.equals(unionList)) {
            break;
          }
        }
      }
    }
  }
 /* (non-Javadoc)
  * @see org.sadiframework.client.Registry#findServicesByConnectedClass(com.hp.hpl.jena.rdf.model.Resource)
  */
 @Override
 public Collection<? extends Service> findServicesByConnectedClass(Resource clazz)
     throws SADIException {
   Set<String> classURIs = new HashSet<String>();
   if (clazz.isURIResource()) classURIs.add(clazz.getURI());
   if (clazz.canAs(OntClass.class)) {
     for (Iterator<? extends OntClass> i = clazz.as(OntClass.class).listSubClasses();
         i.hasNext(); ) {
       OntClass c = i.next();
       if (c.isURIResource()) classURIs.add(c.getURI());
     }
   }
   return findServicesByConnectedClass(classURIs);
   // TODO
   //		return findServices(RegistrySearchCriteria.findService().addConnectedClass(clazz));
 }
 /**
  * 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);
 }
Ejemplo n.º 11
0
  private List<OntResource> getEnumeratedIndividuals(OntClass type) {
    Resource equivalentClass = type.getPropertyResourceValue(OWL.equivalentClass);
    if (equivalentClass == null) {
      return null;
    }
    Resource oneOf = equivalentClass.getPropertyResourceValue(OWL.oneOf);
    if (oneOf == null) return null;

    List<RDFNode> nodeList = oneOf.as(RDFList.class).asJavaList();

    List<OntResource> result = new ArrayList<OntResource>();

    for (RDFNode node : nodeList) {
      result.add(node.as(OntResource.class));
    }

    return result;
  }
Ejemplo n.º 12
0
 public int[] getSourceColumns(Resource resource) {
   if (_model.contains(resource, Vertere.source_column)) {
     Statement sourceColumn = _model.getProperty(resource, Vertere.source_column);
     return new int[] {sourceColumn.getInt()};
   } else if (_model.contains(resource, Vertere.source_columns)) {
     Statement sourceColumns = _model.getProperty(resource, Vertere.source_columns);
     Resource listResource = sourceColumns.getResource();
     RDFList list = listResource.as(RDFList.class);
     List<RDFNode> javalist = list.asJavaList();
     int[] sourceColumnNumbers = new int[javalist.size()];
     for (int i = 0; i < javalist.size(); i++) {
       RDFNode node = javalist.get(i);
       Literal value = node.asLiteral();
       sourceColumnNumbers[i] = value.getInt();
     }
     return sourceColumnNumbers;
   } else {
     return new int[0];
   }
 }
Ejemplo n.º 13
0
  private Field createListField(Frame frame, OntProperty p, OntResource range) {

    Resource intersection = range.getPropertyResourceValue(OWL.intersectionOf);
    if (intersection == null) return null;

    if (intersection.canAs(RDFList.class)) {
      List<RDFNode> intersectionList = intersection.as(RDFList.class).asJavaList();
      for (RDFNode node : intersectionList) {
        if (node.canAs(OntClass.class)) {
          OntClass intersectionMember = node.as(OntClass.class);

          if (RDF.first.equals(intersectionMember.getPropertyResourceValue(OWL.onProperty))) {
            // The intersectionMember has an owl:onProperty property whose value is rdf:first

            Resource elementRdfType =
                intersectionMember.getPropertyResourceValue(OWL.allValuesFrom);

            if (elementRdfType != null) {

              String elementTypeURI = elementRdfType.getURI();
              if (elementTypeURI != null) {
                RdfType elementType = manager.getTypeByURI(elementTypeURI);
                if (elementType != null) {

                  ListType listType = manager.getListTypeByElementUri(elementTypeURI);
                  if (listType == null) {
                    listType = new ListType(manager, intersectionMember, elementType);
                    manager.add(listType);
                  }

                  return new Field(frame, p, listType);
                }
              }
            }
          }
        }
      }
    }

    return null;
  }
 /**
  * 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);
   }
 }
Ejemplo n.º 15
0
  private void addFieldFromRestriction(
      Map<String, Field> fieldMap, Frame frame, OntClass restriction) {
    int minCardinality = 0;
    int maxCardinality = -1;
    OntResource range = null;

    Resource resource = restriction.getPropertyResourceValue(OWL2.onProperty);
    String uri = resource.getURI();
    Field priorField = fieldMap.get(uri);

    if (priorField != null) {
      maxCardinality = priorField.getMaxCardinality();
    }

    OntProperty property = null;

    if (restriction.getPropertyResourceValue(OWL2.onProperty).canAs(OntProperty.class)) {
      property = resource.as(OntProperty.class);
    } else {
      property = model.createOntProperty(resource.getURI());
    }

    if (property != null && property.canAs(FunctionalProperty.class)) {
      maxCardinality = 1;
    }

    if (restriction.hasProperty(OWL.minCardinality)) {
      minCardinality = restriction.getProperty(OWL.minCardinality).getInt();
    }
    if (restriction.hasProperty(OWL.maxCardinality)) {
      maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt();
    }
    Resource valueType = restriction.getPropertyResourceValue(OWL.allValuesFrom);
    if (valueType != null) {
      range = valueType.as(OntResource.class);
    }

    Resource hasValue = restriction.getPropertyResourceValue(OWL.hasValue);
    NamedIndividual individualValue = null;
    if (hasValue != null && hasValue.getURI() != null) {
      String individualURI = hasValue.getURI();
      individualValue = new NamedIndividual(hasValue.as(OntResource.class));
    }

    //      Resource onClass = restriction.getPropertyResourceValue(OWL2.onClass);
    //      if (onClass != null) {
    //        range = onClass.as(OntResource.class);
    //        if (restriction.hasProperty(OWL2.minQualifiedCardinality)) {
    //          minCardinality = restriction.getProperty(OWL2.minQualifiedCardinality).getInt();
    //        }
    //        if (restriction.hasProperty(OWL2.maxQualifiedCardinality)) {
    //          maxCardinality = restriction.getProperty(OWL2.maxQualifiedCardinality).getInt();
    //        }
    //
    //      } else {
    //        if (restriction.hasProperty(OWL.minCardinality)) {
    //          minCardinality = restriction.getProperty(OWL.minCardinality).getInt();
    //        }
    //        if (restriction.hasProperty(OWL.maxCardinality)) {
    //          maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt();
    //        }
    //      }

    if (range == null) {
      Resource value = property.getPropertyResourceValue(RDFS.range);
      if (value == null) {
        logger.warn(
            "Ignoring field "
                + resource.getLocalName()
                + " on class "
                + frame.getLocalName()
                + ": the range is not defined.");
        return;
      }
      range = property.getPropertyResourceValue(RDFS.range).as(OntResource.class);
    }

    String comment = restriction.getComment(null);
    if (priorField != null && priorField.getDeclaringFrame() == frame) {

      priorField.setComment(comment);
      priorField.setMinCardinality(minCardinality);
      priorField.setMaxCardinality(maxCardinality);
      priorField.setValueRestriction(individualValue);
      return;
    }
    Field field = new Field(frame, property, range, minCardinality, maxCardinality);
    field.setComment(comment);
    field.setValueRestriction(individualValue);
    fieldMap.put(uri, field);

    frame.getDeclaredFields().add(field);
  }
 /** @see com.hp.hpl.jena.rdf.model.RDFNode#as(java.lang.Class) */
 public RDFNode as(Class view) {
   // TODO implement
   logger.warn("Synchronization not implemented");
   return wrapped.as(view);
 }
Ejemplo n.º 17
0
  private void addField(OntResource type, OntProperty p, OntProperty ancestor) {
    int minCardinality = 0;
    int maxCardinality = -1;
    OntResource range = null;

    String typeURI = type.getURI();
    if (typeURI == null) {
      // We only add fields to named types.
      return;
    }

    // Do not add abstract properties.
    if (isAbstract(p)) return;

    Frame frame = manager.getFrameByUri(typeURI);
    if (frame == null) {
      if (isStandard(typeURI)) return;
      logger.warn(
          "Ignoring property "
              + p.getLocalName()
              + " on class "
              + type.getLocalName()
              + ": frame not found");
      return;
    }

    if (frame.getDeclaredFieldByPropertyURI(p.getURI()) != null) return;

    if (p.hasRDFType(OWL.FunctionalProperty)) {
      maxCardinality = 1;
    }

    OntClass restriction = frame.getRestriction(p.getURI());
    range = p.getRange();
    if (range == null && ancestor != null) {
      range = ancestor.getRange();
    }
    if (range == null) {
      //      logger.warn("Ignoring property " + p.getLocalName() + " on class " +
      // type.getLocalName() + ": range not defined");
      //      return;
      range = THING;
    }
    if (restriction != null) {
      Resource onClass = restriction.getPropertyResourceValue(OWL2.onClass);
      if (onClass != null) {
        range = onClass.as(OntResource.class);
        if (restriction.hasProperty(OWL2.minQualifiedCardinality)) {
          minCardinality = restriction.getProperty(OWL2.minQualifiedCardinality).getInt();
        }
        if (restriction.hasProperty(OWL2.maxQualifiedCardinality)) {
          maxCardinality = restriction.getProperty(OWL2.maxQualifiedCardinality).getInt();
        }

      } else {
        if (restriction.hasProperty(OWL.minCardinality)) {
          minCardinality = restriction.getProperty(OWL.minCardinality).getInt();
        }
        if (restriction.hasProperty(OWL.maxCardinality)) {
          maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt();
        }
      }
    }

    Field field = null;

    String rangeURI = range.getURI();
    if (rangeURI == null) {

      field = createListField(frame, p, range);

      if (field == null) {
        logger.warn(
            "Ignoring property "
                + p.getLocalName()
                + " on class "
                + type.getLocalName()
                + ": range has no URI");
        return;
      }
    } else {

      field = new Field(frame, p, range, minCardinality, maxCardinality);

      if (field.getRdfType() == null) {
        logger.warn(
            "Failed to create RdfType for field "
                + field.getLocalName()
                + " of type "
                + field.getType().getURI());
      }
    }

    Resource rawInverse = p.getPropertyResourceValue(OWL.inverseOf);
    if (rawInverse != null && rawInverse.canAs(OntProperty.class)) {
      field.setInverseOf(rawInverse.as(OntProperty.class));
    }
    frame.getDeclaredFields().add(field);
  }
Ejemplo n.º 18
0
 private static List<RDFNode> asJavaList(Resource resource) {
   return (resource.as(RDFList.class)).asJavaList();
 }