コード例 #1
0
ファイル: Example3.java プロジェクト: kodizant/java-skos-api
  public static void main(String[] args) {

    /*
     * This example recreates the SKOS dataset in Example2.java, but does so using some convenience methods in fewer lines of code
     *
     */

    try {

      SKOSManager manager = new SKOSManager();

      final String baseURI = "http://www.semanticweb.org/skos/example2.rdf";

      SKOSDataset dataset = manager.createSKOSDataset(URI.create(baseURI));

      SKOSDataFactory df = manager.getSKOSDataFactory();

      List<SKOSEntity> allEntities = new ArrayList<SKOSEntity>();

      // Create a concept scheme identified by a URI
      SKOSConceptScheme conceptScheme1 =
          df.getSKOSConceptScheme(URI.create(baseURI + "#conceptScheme1"));

      // create a set of concepts

      Set<SKOSConcept> concepts = new HashSet<SKOSConcept>();
      concepts.add(df.getSKOSConcept(URI.create(baseURI + "#conceptA")));
      concepts.add(df.getSKOSConcept(URI.create(baseURI + "#conceptB")));
      concepts.add(df.getSKOSConcept(URI.create(baseURI + "#conceptC")));
      concepts.add(df.getSKOSConcept(URI.create(baseURI + "#conceptD")));

      // add all the entities to this set
      allEntities.add(conceptScheme1);
      allEntities.addAll(concepts);

      List<SKOSEntityAssertion> entityAssertions = df.getSKOSEntityAssertions(allEntities);
      List<SKOSObjectRelationAssertion> relationAssertions =
          df.getSKOSObjectRelationAssertions(
              concepts, df.getSKOSInSchemeProperty(), conceptScheme1);

      List<SKOSChange> addAssertions = new ArrayList<SKOSChange>();

      for (SKOSEntityAssertion ass : entityAssertions) {
        addAssertions.add(new AddAssertion(dataset, ass));
      }

      for (SKOSObjectRelationAssertion ass : relationAssertions) {
        addAssertions.add(new AddAssertion(dataset, ass));
      }

      manager.applyChanges(addAssertions);

      manager.save(dataset, SKOSFormatExt.RDFXML, URI.create("file:/Users/simon/tmp/example3.rdf"));

    } catch (SKOSCreationException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (SKOSChangeException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (SKOSStorageException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }
コード例 #2
0
  private void _convert() throws Exception {

    List<?> list = standard_name_table.getChildren("entry");
    for (Iterator<?> iterator = list.iterator(); iterator.hasNext(); ) {
      Element ele = (Element) iterator.next();

      numEntries++;

      String id = ele.getAttribute("id").getValue().trim();

      String canonicalUnits = ele.getChildTextNormalize("canonical_units");
      String grib = ele.getChildTextNormalize("grib");
      String amip = ele.getChildTextNormalize("amip");

      String description = ele.getChildTextNormalize("description");

      String prefLabel = id.replace('_', ' ');

      URI conceptUri = URI.create(namespace + id);

      SKOSConcept concept = dataFactory.getSKOSConcept(conceptUri);
      concepts.add(concept);

      // skos:narrower
      objectRelationAssertions.add(
          dataFactory.getSKOSObjectRelationAssertion(
              topConcept, dataFactory.getSKOSNarrowerProperty(), concept));

      // skos:prefLabel
      dataRelationAssertions.add(
          dataFactory.getSKOSDataRelationAssertion(
              concept, dataFactory.getSKOSPrefLabelProperty(), prefLabel));

      ////////////////////////////////////////////////
      // OWL API stuff
      _addOwlChange(conceptUri, id, description, canonicalUnits, grib, amip);
    }

    props.put("entries", String.valueOf(numEntries));
    props.put("concepts", String.valueOf(concepts.size()));

    allEntities.add(conceptScheme);
    allEntities.addAll(concepts);

    List<SKOSChange> skosChanges = new ArrayList<SKOSChange>();

    // entities:
    List<SKOSEntityAssertion> entityAssertions = dataFactory.getSKOSEntityAssertions(allEntities);
    for (SKOSEntityAssertion ass : entityAssertions) {
      skosChanges.add(new AddAssertion(dataset, ass));
    }

    // skos:inScheme
    objectRelationAssertions.addAll(
        dataFactory.getSKOSObjectRelationAssertions(
            concepts, dataFactory.getSKOSInSchemeProperty(), conceptScheme));

    // object relations:
    for (SKOSObjectRelationAssertion assertion : objectRelationAssertions) {
      skosChanges.add(new AddAssertion(dataset, assertion));
    }

    // data relations:
    for (SKOSDataRelationAssertion assertion : dataRelationAssertions) {
      skosChanges.add(new AddAssertion(dataset, assertion));
    }

    manager.applyChanges(skosChanges);

    ////////////////////////////////////////////////////
    // OWL API assertions
    owlManager.applyChanges(owlChanges);
  }