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);
  }
  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);
  }