Examples of OntModel


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

   
    if ( log.isDebugEnabled() ) {
      log.debug("getOntologyContents(RegisteredOntologyInfo): loading model");
    }
   
    OntModel ontModel;
    try {
      ontModel = OntServiceUtil.retrieveModel(registeredOntologyInfo.getUri(), version);
    }
    catch (Exception e) {
      String error = "Error loading model: " +e.getMessage();
View Full Code Here

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

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

    OntModel model;
    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;
      }
     
      // 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;
      }
     
      uriForEmpty = model.getNsPrefixURI("");
      if ( uriForEmpty == null ) {
        // Shouldn't happen -- we're reading in an already registered version.
        String error = "error while getting URI for empty prefix for a registered version. " +
            "Please report this bug.";
        log.info(error);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }

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

     
    final String original_ns_ = uriForEmpty;
   
    String ns_;
    String base_;

   
    final String finalUri = namespaceRoot + "/" +
    orgAbbreviation + "/" +
    version + "/" +
    shortName;

    ns_ = JenaUtil2.appendFragment(finalUri);
    base_ = JenaUtil2.removeTrailingFragment(finalUri);


    log.info("Setting prefix \"\" for URI " + ns_);
    model.setNsPrefix("", ns_);


    if ( original_ns_ != null ) {
      // Update statements  according to the new namespace:
      log.info("createOntologyFullyHosted: original namespace: '" +uriForEmpty+ "'. " +
        "Elements here will be transferred to new namespace " +ns_
      );
      Util2.replaceNameSpace(model, original_ns_, ns_);
    }
    else {
      log.info("createOntologyFullyHosted: no original namespace, so no transfer will be done.");
    }
   

   
   
    /////////////////////////////////////////////////////////////////
    // Is there an existing OWL.Ontology individual?
    // TODO Note that ONLY the first OWL.Ontology individual is considered.
    Resource ontRes = JenaUtil2.getFirstIndividual(model, OWL.Ontology);
    List<Statement> prexistStatements = null;
    if ( ontRes != null ) {
      prexistStatements = new ArrayList<Statement>();
      log.info("Getting pre-existing properties for OWL.Ontology individual: " +ontRes.getURI());
      StmtIterator iter = ontRes.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 = _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() ) {
View Full Code Here

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

    }

   
    // 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);
        }
View Full Code Here

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

   
   
    ////////////////////////////////////////////
    // 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);
        }
      } 
     
    }
View Full Code Here

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

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

    OntModel model;
    String uriForEmpty;
    String newContentsFileName;

    if ( createOntologyResult.getFullPath() != null ) {
      //
      // new contents to check.
      // Get model from the new contents.
      //
      String 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;
      }
     
      uriForEmpty = Util2.getDefaultNamespace(model, file, createOntologyResult);

      if ( uriForEmpty == null ) {
        String error = "Cannot get base URI for the ontology";
        log.info(error);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }
     
      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;
      }
     
      uriForEmpty = model.getNsPrefixURI("");
      if ( uriForEmpty == null ) {
        // Shouldn't happen -- we're reading in an already registered version.
        String error = "error while getting URI for empty prefix for a registered version. " +
            "Please report this bug.";
        log.info(error);
        createOntologyResult.setError(error);
        return createOntologyResult;
      }

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

     

   
    log.info("createOntology: using '" +uriForEmpty+ "' as base URI");
   
    final String original_ns_ = uriForEmpty;
    log.info("original namespace: " +original_ns_);

   
    String ns_;
    String base_;

   
    if ( createOntologyResult.isPreserveOriginalBaseNamespace() ) {
      //pons:  just use original namespace
      ns_ = original_ns_;
      base_ = ns_;
//      base_ = JenaUtil2.removeTrailingFragment(ns_);
     
      ///////////////////////////////////////////////////////
     
      if ( ontologyId == null ) {
        // This is a new submission. We need to check for any conflict with a preexisting
        // ontology in the repository with the same URI, base_
        //
        if ( ! Util2.checkNoPreexistingOntology(base_, createOntologyResult) ) {
          return createOntologyResult;
        }
      }
      else {
        // This is a submission of a *new version* of an existing ontology.
        // NO check needed--the given URI (base_) is respected.
      }
      ///////////////////////////////////////////////////////
    }
    else {
     
      final String finalUri = namespaceRoot + "/" +
                    orgAbbreviation + "/" +
                    version + "/" +
                    shortName;
                   
      ns_ = JenaUtil2.appendFragment(finalUri);
      base_ = JenaUtil2.removeTrailingFragment(finalUri);
     
     
      log.info("Setting prefix \"\" for URI " + ns_);
      model.setNsPrefix("", ns_);
     
     
      // Update statements  according to the new namespace:
      Util2.replaceNameSpace(model, original_ns_, ns_);
     
    }
   
   
    /////////////////////////////////////////////////////////////////
    // Is there an existing OWL.Ontology individual?
    // TODO Note that ONLY the first OWL.Ontology individual is considered.
    Resource ontRes = JenaUtil2.getFirstIndividual(model, OWL.Ontology);
    List<Statement> prexistStatements = null;
    if ( ontRes != null ) {
      prexistStatements = new ArrayList<Statement>();
      log.info("Getting pre-existing properties for OWL.Ontology individual: " +ontRes.getURI());
      StmtIterator iter = ontRes.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 = _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() ) {
View Full Code Here

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

    String full_path = tempOntologyInfo.getFullPath();
    File file = new File(full_path );
    String uriFile = file.toURI().toString();
    log.info("Loading model: " +uriFile);

    OntModel ontModel;
    try {
      ontModel = JenaUtil2.loadModel(uriFile, false);
    }
    catch (Throwable ex) {
      String error = "Unexpected error: " +ex.getClass().getName()+ " : " +ex.getMessage();
View Full Code Here

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

   * @param ontModel
   * @return the found Ontology or null.
   */
  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();
      }

View Full Code Here

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

  // verbatim copy from JenaUtil
  public static OntModel createDefaultOntModel() {
    OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
    OntDocumentManager docMang = new OntDocumentManager();
    spec.setDocumentManager(docMang);
    OntModel model = ModelFactory.createOntologyModel(spec, null);
    // removeNotNeccesaryNamespaces(model);

    return model;
  }
View Full Code Here

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

  // adapted from JenaUtil
  public static OntModel loadModel(String uriModel, boolean processImports) {
    log.debug("Loading model '" + uriModel + "' with processImports=" +processImports);
    uriModel = removeTrailingFragment(uriModel);
    OntModel model = createDefaultOntModel();
    model.setDynamicImports(false);
    model.getDocumentManager().setProcessImports(processImports);
    model.read(uriModel);
    return model;
  }
View Full Code Here

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

  }

  private void _loadOntology(OntologyInfo ontology, String full_path) {
    final Model model2update = _infModel != null ? _infModel : _model;
    if ( USE_UNVERSIONED ) {
      OntModel model = JenaUtil2.loadModel("file:" +full_path, false);

      if ( OntUtil.isOntResolvableUri(ontology.getUri()) ) {
        MmiUri mmiUri;
        try {
          mmiUri = new MmiUri(ontology.getUri());
          OntModel unversionedModel = UnversionedConverter.getUnversionedModel(model, mmiUri);
          model2update.add(unversionedModel);
        }
        catch (URISyntaxException e) {
          log.error("shouldn't happen", e);
          return;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.