Ejemplo n.º 1
0
  private void _createNewOntology() throws Exception {

    assert namespace.matches(".*(/|#)");

    String baseUri = namespace.replaceAll("(/|#)+$", ""); // removing any trailing slash/hash

    manager = new SKOSManager();

    if (namespace.endsWith("#")) {
      // Ok, OWLAPI v2 works fine with hash separator:
      dataset = manager.createSKOSDataset(URI.create(baseUri));
    } else {
      /////////////////////////////////////////////////////////////////////////////////////////////
      // TODO NOTE: workaround for bug in OWLAPI v2:
      // Instead of using a baseUri without separator,
      // use the namespace itself (ie, w/ fragment separator):
      dataset = manager.createSKOSDataset(URI.create(namespace));
      // In this way, the resulting ontology looks OK except that the xml:base is
      // written with the separator:
      //
      //		<rdf:RDF xmlns="http://mmisw.org/ont/mmi/cf/parameter/"
      //		     xml:base="http://mmisw.org/ont/mmi/cf/parameter/"    <<<--- WITH trailing slash
      //		     xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#"
      //		     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
      //		     xmlns:owl2="http://www.w3.org/2006/12/owl2#"
      //		     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
      //		     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      //		     xmlns:owl="http://www.w3.org/2002/07/owl#"
      //		     xmlns:skos="http://www.w3.org/2004/02/skos/core#">
      //		    <owl:Ontology rdf:about=""/>
      //		...
      //	    <!-- http://mmisw.org/ont/mmi/cf/parameter/CfStdName -->
      //
      //	        <skos:ConceptScheme rdf:about="CfStdName"/>
      //
      //
      //	        <!-- http://mmisw.org/ont/mmi/cf/parameter/age_of_stratospheric_air -->
      //
      //	        <skos:Concept rdf:about="age_of_stratospheric_air">
      //	            <skos:inScheme rdf:resource="CfStdName"/>
      //	        </skos:Concept>
      //
      //
      //	        <!-- http://mmisw.org/ont/mmi/cf/parameter/air_density -->
      //
      //	        <skos:Concept rdf:about="air_density">
      //	            <skos:inScheme rdf:resource="CfStdName"/>
      //	        </skos:Concept>
      //
      //		...
      /////////////////////////////////////////////////////////////////////////////////////////////
    }

    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new StringReader(inputContents));

    standard_name_table = document.getRootElement();

    _getProperty(standard_name_table, "version_number");
    _getProperty(standard_name_table, "last_modified");
    _getProperty(standard_name_table, "institution");
    _getProperty(standard_name_table, "contact");

    // TODO assign version_number, last_modified as some properties to the ontology itself.

    dataFactory = manager.getSKOSDataFactory();

    allEntities = new ArrayList<SKOSEntity>();
    conceptScheme = dataFactory.getSKOSConceptScheme(URI.create(namespace + CONCEPT_SCHEME));
    concepts = new HashSet<SKOSConcept>();

    URI topConceptUri = URI.create(namespace + TOP_CONCEPT);
    topConcept = dataFactory.getSKOSConcept(topConceptUri);
    concepts.add(topConcept);

    objectRelationAssertions = new ArrayList<SKOSObjectRelationAssertion>();
    dataRelationAssertions = new ArrayList<SKOSDataRelationAssertion>();

    // skos:hasTopConcept
    objectRelationAssertions.add(
        dataFactory.getSKOSObjectRelationAssertion(
            conceptScheme, dataFactory.getSKOSHasTopConceptProperty(), topConcept));

    /////////////////////////////////////////////////
    // OWL API stuff
    owlManager = manager.getOWLManger();
    owlOntology = owlManager.getOntology(URI.create(namespace));
    owlDataFactory = owlManager.getOWLDataFactory();

    canonicalUnitsProp =
        owlDataFactory.getOWLDataProperty(URI.create(namespace + "canonical_units"));
    gribProp = owlDataFactory.getOWLDataProperty(URI.create(namespace + "grib"));
    amipProp = owlDataFactory.getOWLDataProperty(URI.create(namespace + "amip"));

    rdfsLabel =
        owlDataFactory.getOWLDataProperty(URI.create("http://www.w3.org/2000/01/rdf-schema#label"));
    rdfsComment =
        owlDataFactory.getOWLDataProperty(
            URI.create("http://www.w3.org/2000/01/rdf-schema#comment"));

    owlChanges = new ArrayList<AddAxiom>();

    _addOwlChange(topConceptUri, TOP_CONCEPT.replace('_', ' '), "", null, null, null);
  }