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;
  }
  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;
  }
 /* (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));
 }
  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;
  }
  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);
  }
 /**
  * Since multiple threads may access the model just because canAs returns true doesn't mean .as
  * will work.
  *
  * @see com.hp.hpl.jena.rdf.model.RDFNode#canAs(java.lang.Class)
  */
 public boolean canAs(Class view) {
   synchronized (model) {
     return wrapped.canAs(view);
   }
 }