Package com.hp.hpl.jena.ontology

Examples of com.hp.hpl.jena.ontology.Ontology


 
  public OntModel toOWL(File target, String location, boolean redefinition, Context ctx)
  throws Exception {
    if (processed) return ont;
    // ontology header resource
    Ontology ontology = null;
    if (ctx.getBaseMap()!=null && !redefinition) {
      ontology = ont.getOntology(ctx.getBaseMap().toString()) ;
      if (ontology==null) ontology = ont.createOntology(ctx.getBaseMap().toString());
    }
   
View Full Code Here


  public static String getDefaultNamespace(OntModel model, File file, Errorable baseResult) {

    // See issues #213, #174
   
    // try the first ontology resource, if any:
    Ontology ont = JenaUtil2.getOntology(model);
    if ( ont != null ) {
      String namespace = ont.getURI()
      return namespace;
    }
   
    // try namespace of the empty prefix, if any:
    String namespace = model.getNsPrefixURI("");
View Full Code Here

    // The new OntModel that will contain the pre-existing attributes (if any),
    // plus the new and updated attributes:
    final OntModel newOntModel = ModelFactory.createOntologyModel(model.getSpecification(), model);
    // Note: previously, newOntModel = new OwlModel(model); but the OwlModel extension was not used
    // in any particular way, so the new call should be equivalent.
    final Ontology ont_ = newOntModel.createOntology(base_);
    if ( log.isDebugEnabled() ) {
      log.debug("New ontology created with namespace " + ns_ + " base " + base_);
    }
    newOntModel.setNsPrefix("", ns_);
   
   
    //////////////////////////////////////////////////////////////////////////
    // set new values for the unversioned version:
    Map<String, String> newValues = new HashMap<String, String>();
   
    // Set internal attributes, which are updated in the newValues map itself
    // so we facilite the processing below:
    newValues.put(Omv.uri.getURI(), base_);
    if ( version != null ) {
      newValues.put(Omv.version.getURI(), version);
    }
   
    newValues.put(Omv.creationDate.getURI(), creationDate);


    //////////////////////////////////////////////////
    // transfer any preexisting attributes, and then remove all properties from
    // pre-existing ontRes so just the new OntModel gets added.
    if ( ontRes != null ) {
      for ( Statement st : prexistStatements ) {
        Property prd = st.getPredicate();

        //
        // Do not tranfer pre-existing/pre-assigned-above attributes
        //
        String newValue = newValues.get(prd.getURI());
        if ( newValue == null || newValue.trim().length() == 0 ) {
          // not assigned above.
         
          if ( ALTERATE_ANNOTATIONS ) {
            // See if it's one of the ones to be modified:
            if ( Omv.description.getURI().equals(prd.getURI()) ) {
              // transfer modified description:
              String description =
                "An unversioned ontology containing the latest terms as of the request time, " +
                "for the ontology containing: " +st.getObject();
//              log.info("  Transferring modified description: " +st.getSubject()+ " :: " +prd+ " :: " +description);
              newOntModel.add(ont_, st.getPredicate(), description);
            }
            else if ( Omv.name.getURI().equals(prd.getURI()) ) {
              // transfer modified title:
              String title =
                "Unversioned form of: " +st.getObject();
//              log.info("  Transferring modified title: " +st.getSubject()+ " :: " +prd+ " :: " +title);
              newOntModel.add(ont_, st.getPredicate(), title);
            }
            else {
              // transfer as it comes:
//              log.info("  Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
              newOntModel.add(ont_, st.getPredicate(), st.getObject());
            }
          }
          else {
            // just transfer as it comes:
//            log.info("  Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
            newOntModel.add(ont_, st.getPredicate(), st.getObject());
          }
        }
        else {
//          log.info(" Not Transferring: " +prd+ " from previous version because new value " +newValue);
        }
      } 
     
//      log.info("Removing original OWL.Ontology individual");
      ontRes.removeProperties();
      // TODO the following may be unnecesary but doesn't hurt:
      model.remove(ontRes, RDF.type, OWL.Ontology);
    }

   
   
    ///////////////////////////////////////////////////////
    // Update attributes in model:
   
    ont_.addProperty(Omv.uri, base_);
    if ( version != null ) {
      ont_.addProperty(Omv.version, version);
    }
    ont_.addProperty(Omv.creationDate, creationDate);

    ////////////////////////////////////////////////////////////////////////
    // Done with the model.
    ////////////////////////////////////////////////////////////////////////
   
View Full Code Here

   
    // The new OntModel that will contain the pre-existing attributes (if any),
    // plus the new and updated attributes:
    final OntModel newOntModel = _createOntModel(model);
    final Ontology ont_ = newOntModel.createOntology(base_);
    log.info("New ontology created with namespace " + ns_ + " base " + base_);
    newOntModel.setNsPrefix("", ns_);
   
    // set preferred prefixes:
    Map<String, String> preferredPrefixMap = MdHelper.getPreferredPrefixMap();
    for ( String uri : preferredPrefixMap.keySet() ) {
      String prefix = preferredPrefixMap.get(uri);
      newOntModel.setNsPrefix(prefix, uri);
    }
   
   
    // Set internal attributes, which are updated in the newValues map itself
    // so we facilite the processing below:
    newValues.put(Omv.version.getURI(), version);
   
    newValues.put(Omv.creationDate.getURI(), creationDate);
   
   
    // set some properties from the explicit values
    newValues.put(OmvMmi.origMaintainerCode.getURI(), orgAbbreviation);


    //////////////////////////////////////////////////
    // transfer any preexisting attributes, and then remove all properties from
    // pre-existing ontRes so just the new OntModel gets added.
    if ( ontRes != null ) {
      for ( Statement st : prexistStatements ) {
        Property prd = st.getPredicate();

        //
        // Do not tranfer pre-existing/pre-assigned-above attributes
        //
        String newValue = newValues.get(prd.getURI());
        if ( newValue == null || newValue.trim().length() == 0 ) {
          log.info("  Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
          newOntModel.add(ont_, prd, st.getObject());
        }
        else {
          log.info(" Removing pre-existing values for predicate: " +prd+ " because of new value " +newValue);
          newOntModel.removeAll(ont_, prd, null);
        }
      } 
     
     
      if ( ! createOntologyResult.isPreserveOriginalBaseNamespace() ) {
       
        //
        // Only, when we're creating a new model, ie., per the new namespace, do the following removals.
        // (If we did this on a model with the same original namespace, we would remove the owl:Ontology
        // entry altogether and get an "rdf:Description" instead.
        //
       
        log.info("Removing original OWL.Ontology individual");
        ontRes.removeProperties();
        // TODO the following may be unnecesary but doesn't hurt:
        model.remove(ontRes, RDF.type, OWL.Ontology);
      }
    }

   
   
    ///////////////////////////////////////////////////////
    // Update attributes in model:

    Map<String, Property> uriPropMap = MdHelper.getUriPropMap();
    for ( String uri : newValues.keySet() ) {
      String value = newValues.get(uri).trim();
      if ( value.length() > 0 ) {
        Property prop = uriPropMap.get(uri);
        if ( prop == null ) {
          log.info("No property found for uri='" +uri+ "'");
          continue;
        }

        log.info(" Assigning: " +uri+ " = " +value);
        ont_.addProperty(prop, value);
      }
    }
   

View Full Code Here

   
    ////////////////////////////////////////////
    // load  model

    OntModel model;
    Ontology ont;
    String uriForEmpty;
    String newContentsFileName;

    if ( createOntologyResult.getFullPath() != null ) {
      //
      // new contents to check.
      // Get model from the new contents.
      //
      full_path = createOntologyResult.getFullPath();
      log.info("Loading model: " +full_path);

      File file = new File(full_path);
     
      try {
        model = Util2.loadModelWithCheckingUtf8(file, null);
      }
      catch ( Throwable ex ) {
        String error = "Unexpected error: " +ex.getClass().getName()+ " : " +ex.getMessage();
        log.info(error);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }
     
      ont = JenaUtil2.getOntology(model);
     
      // get original namespace associated with the ontology, if any:
      uriForEmpty = Util2.getDefaultNamespace(model, file, createOntologyResult);
      // 2009-12-21: previously returning error if uriForEmpty==null. Not anymore; see below.
     
      newContentsFileName = file.getName();
    }
    else {
      // NO new contents.
      // Use contents from prior version.
      PriorOntologyInfo priorVersionInfo = createOntologyInfo.getPriorOntologyInfo();
     
      try {
        model = OntServiceUtil.retrieveModel(createOntologyInfo.getUri(), priorVersionInfo.getVersionNumber());
      }
      catch (Exception e) {
        String error = "error while retrieving registered ontology: " +e.getMessage();
        log.info(error, e);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }
     
      ont = JenaUtil2.getOntology(model);
      if ( ont == null ) {
        // Shouldn't happen -- we're reading in an already registered version.
        String error = "error while getting Ontology resource a registered version. " +
            "Please report this bug.";
        log.info(error);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }
      uriForEmpty = ont.getURI();

      // replace ':', '/', or '\' for '_'
      newContentsFileName = uriForEmpty.replaceAll(":|/|\\\\", "_");
    }

    log.debug("uriForEmpty = '" +uriForEmpty+ "'");
   
    final String original_base_ = uriForEmpty;
//    final String original_base_ = JenaUtil2.removeTrailingFragment(uriForEmpty);

    // and this is the info for the requested URI:
    final String ontUri = createOntologyInfo.getUri();
    final String ns_ = JenaUtil2.appendFragment(ontUri);
    final String base_ = ontUri;
//    final String base_ = JenaUtil2.removeTrailingFragment(ontUri);

    log.info("createOntologyReHosted: original namespace: '" +original_base_+ "'");
    if ( ! original_base_.equals(base_) ) {
      // In this re-hosted case, we force the original URI and the new URI to be the same.
      // This may happen only in the case of a submission of a new version.
      String error = "The new base URI (" +base_+ ") is not equal to the registered " +
        "base URI (" +original_base_+ ") "
      ;
      log.debug(error);
      createOntologyResult.setError(error);
      return createOntologyResult;
    }
   
    /////////////////////////////////////////////////////////////////
    // If there is an pre-existing Ontology resource, get the associated statements:
    List<Statement> prexistStatements = null;
    if ( ont != null ) {
      prexistStatements = new ArrayList<Statement>();
      if ( log.isDebugEnabled() ) {
        log.debug("Getting pre-existing properties from Ontology: " +ont.getURI());
      }
      StmtIterator iter = ont.listProperties();
      while ( iter.hasNext() ) {
        Statement st = iter.nextStatement();
        prexistStatements.add(st);
     
    }

   
    // The new OntModel that will contain the pre-existing attributes (if any),
    // plus the new and updated attributes:
    final OntModel newOntModel = OntModelUtil.createOntModel(base_, true, model);
    JenaUtil2.setOntologyUriForGetOntology(base_);
    final Ontology ont_ = JenaUtil2.getOntology(newOntModel);
    if ( log.isDebugEnabled() ) {
      log.debug("New ontology created with namespace " + ns_ + " base " + base_);
    }
   
    // Set internal attributes, which are updated in the newValues map itself
    // so we facilite the processing below:
    newValues.put(Omv.version.getURI(), version);
    newValues.put(Omv.creationDate.getURI(), creationDate);
   

    //////////////////////////////////////////////////
    // transfer any preexisting attributes, and then remove all properties from
    // pre-existing ont so just the new OntModel gets added.
    if ( prexistStatements != null ) {
      for ( Statement st : prexistStatements ) {
        Property prd = st.getPredicate();

        //
        // Do not tranfer pre-existing/pre-assigned-above attributes
        //
        String newValue = newValues.get(prd.getURI());
        if ( newValue == null || newValue.trim().length() == 0 ) {
          if ( log.isDebugEnabled() ) {
            log.debug("  Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
          }
          newOntModel.add(ont_, prd, st.getObject());
        }
        else {
          if ( log.isDebugEnabled() ) {
            log.debug(" Removing pre-existing values for predicate: " +prd+ " because of new value " +newValue);
          }
          newOntModel.removeAll(ont_, prd, null);
        }
      } 
     
    }

   
   
    ///////////////////////////////////////////////////////
    // Update attributes in model:

    Map<String, Property> uriPropMap = MdHelper.getUriPropMap();
    for ( String uri : newValues.keySet() ) {
      String value = newValues.get(uri);
      if ( value != null && value.trim().length() > 0 ) {
        Property prop = uriPropMap.get(uri);
        if ( prop == null ) {
          log.info("No property found for uri='" +uri+ "'");
          continue;
        }

        log.info(" Assigning: " +uri+ " = " +value);
        ont_.addProperty(prop, value);
      }
    }
   

View Full Code Here

   
    // The new OntModel that will contain the pre-existing attributes (if any),
    // plus the new and updated attributes:
    final OntModel newOntModel = _createOntModel(model);
    final Ontology ont_ = newOntModel.createOntology(base_);
    log.info("New ontology created with namespace " + ns_ + " base " + base_);
    newOntModel.setNsPrefix("", ns_);
   
    // set preferred prefixes:
    Map<String, String> preferredPrefixMap = MdHelper.getPreferredPrefixMap();
    for ( String uri : preferredPrefixMap.keySet() ) {
      String prefix = preferredPrefixMap.get(uri);
      newOntModel.setNsPrefix(prefix, uri);
    }
   
   
    // Set internal attributes, which are updated in the newValues map itself
    // so we facilite the processing below:
    newValues.put(Omv.uri.getURI(), base_);
    newValues.put(Omv.version.getURI(), version);
   
    newValues.put(Omv.creationDate.getURI(), creationDate);


    //////////////////////////////////////////////////
    // transfer any preexisting attributes, and then remove all properties from
    // pre-existing ontRes so just the new OntModel gets added.
    if ( ontRes != null ) {
      for ( Statement st : prexistStatements ) {
        Property prd = st.getPredicate();

        //
        // Do not tranfer pre-existing/pre-assigned-above attributes
        //
        String newValue = newValues.get(prd.getURI());
        if ( newValue == null || newValue.trim().length() == 0 ) {
          log.info("  Transferring: " +st.getSubject()+ " :: " +prd+ " :: " +st.getObject());
          newOntModel.add(ont_, prd, st.getObject());
        }
        else {
          log.info(" Removing pre-existing values for predicate: " +prd+ " because of new value " +newValue);
          newOntModel.removeAll(ont_, prd, null);
        }
      } 
     
     
      if ( ! createOntologyResult.isPreserveOriginalBaseNamespace() ) {
       
        //
        // Only, when we're creating a new model, ie., per the new namespace, do the following removals.
        // (If we did this on a model with the same original namespace, we would remove the owl:Ontology
        // entry altogether and get an "rdf:Description" instead.
        //
       
        log.info("Removing original OWL.Ontology individual");
        ontRes.removeProperties();
        // TODO the following may be unnecesary but doesn't hurt:
        model.remove(ontRes, RDF.type, OWL.Ontology);
      }
    }

   
   
    ///////////////////////////////////////////////////////
    // Update attributes in model:

    Map<String, Property> uriPropMap = MdHelper.getUriPropMap();
    for ( String uri : newValues.keySet() ) {
      String value = newValues.get(uri).trim();
      if ( value.length() > 0 ) {
        Property prop = uriPropMap.get(uri);
        if ( prop == null ) {
          log.info("No property found for uri='" +uri+ "'");
          continue;
        }

        log.info(" Assigning: " +uri+ " = " +value);
        ont_.addProperty(prop, value);
      }
    }
   

View Full Code Here

   * @param ontModel
   * @param prop Property
   * @return value of the property, or null if missing.
   */
  public static String getOntologyPropertyValue(OntModel ontModel, Property prop) {
    Ontology ontology = getOntology(ontModel);
    if ( ontology == null ) {
      return null;
    }
    RDFNode node = ontology.getPropertyValue(prop);
    if ( node == null ) {
      return null;
    }
    return node.toString();
  }
View Full Code Here

  public static Ontology getOntology(OntModel ontModel) {
   
    OntModel mBase = ModelFactory.createOntologyModel(
                OntModelSpec.OWL_MEM, ontModel.getBaseModel() );

    Ontology ont = null;
   
    ExtendedIterator<Ontology> iter = mBase.listOntologies();
    try {
      if ( iter.hasNext() ) {
        ont = iter.next();
      }

      if ( log.isDebugEnabled() ) {
        if ( ont != null ) {
          if ( iter.hasNext() ) {
            Ontology ont2 = iter.next();
            log.debug("WARNING: more than one Ontology resource in OntModel. " +
                "Second one found (but ignored): " +ont2.getURI()
            );
          }
          log.debug("Returning Ontology with URI: " +ont.getURI());
        }
        else {
View Full Code Here

   * @return the value of omv.version assigned to the ontology; null if no
   * assignment was performed.
   */
  // Method created as part of the fix to issue #252: "omv:version gone?".
  public static String setVersionFromCreationDateIfNecessary(OntModel ontModel) {
    Ontology ontology = getOntology(ontModel);
    if ( ontology == null ) {
      return null;
    }
    RDFNode node = ontology.getPropertyValue(Omv.version);
    if ( node != null ) {
      // already assigned; keep it:
      return null;
    }
    // omv.version not assigned; try to use omv.creationDate:
    node = ontology.getPropertyValue(Omv.creationDate);
    if ( node == null ) {
      // not available, return with no changes:
      return null;
    }
   
    String creationDate = node.toString();
    String version = _getVersionFromCreationDate(creationDate);
    if ( version != null ) {
      Literal versionLit = ResourceFactory.createPlainLiteral(version);
      ontology.setPropertyValue(Omv.version, versionLit);
      return version;
    }
    return null;
  }
View Full Code Here

 
 
  private static OntModel setOrAddProperties(OntModel ontModel, boolean doSet, Map<String, String> values) {
    ExtendedIterator<Ontology> iter = ontModel.listOntologies();
    if ( iter.hasNext() ) {
      Ontology ont = (Ontology) iter.next();
      for ( String uri : values.keySet() ) {
        String value = values.get(uri);
        if ( value.trim().length() > 0 ) {
          Property prop = ResourceFactory.createProperty(uri);
          if ( doSet  &&  ont.getPropertyValue(prop) != null ) {
            ont.removeAll(prop);
          }
          ont.addProperty(prop, value.trim());
        }
      }
    }
    return ontModel;
  }
View Full Code Here

TOP

Related Classes of com.hp.hpl.jena.ontology.Ontology

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.