wiki:howto_code_basic

Version 2 (modified by endres, 19 years ago) (diff)

--

TracNav?

Code Example 1: Basics

The following snippet demonstrates the simple use of the the PhaseLibs by performing the three main tasks

  • Instantiate two OntologyAdapters to access ontology data
  • Create a SimilarityMeasure object, capable of measuring distances between the elements of the ontologies.
  • Create an AlignmentGenerator object that will perform an ontology alignment on the given two ontologies.

The snippet's purpose is to demonstrate the general usage of the PhaseLibs, but it should, nevertheless, be executable.

// We want to align two ontologies
Ontology o1, o2;

// Please note, that the constructors are adapter dependent
	
// first: instantiate o1, the dublin core metadata types ontology 
o1 = new de.dfki.km.phaselib.adapters.rdfsJena.RDFSJenaOntology(DCT_URI, DCT_URI.toURL().openStream());

// second: a custom OWL-ontology
o2 = new de.dfki.km.phaselib.adapters.owl.OntologyAdapter(MYFILE_URI);

// now we need a measure. in this case we use a simple 3-gram measure
SimilarityMeasure measure = new StringBasedSimilarity();

// we can now either use the measure to calculate the distance between some classes:
SimilarityMatrix distances = measure.calcSimilarities(o1.getAllClasses(), o2.getAllClasses());
System.out.println(distances.getSortedNonzeroEntries());


// ... or we can use an AlignmentGenerator to calculate an alignment between the ontologies.
AlignmentGenerator generator;

// in this case a simple generator using the measure above will do:
generator = new de.dfki.km.phaselib.impl.alignments.SimpleEvidenceAlignment(new StringBasedSimilarity());
//  (We could have used the measure instatiated above, but to show that these two tasks are 
//     completely independet, we have created a new measure instance.)

// since AlignmentGenerators can only augment existing alignments, we have
//  to create an empty alignment
Alignment emptyAlignment = new AlignmentImpl(o1, o2);

// we might also set some options
HashMap<String, Object> options = new HashMap<String, Object>();
options.put(SimpleEvidenceAlignment.PARAM_THRESHOLD, 0.3d);
	
// now we can let it generate an alignment
Alignment generatedAlignment = generator.generateAlignment(emptyAlignment, options);
System.out.println(generatedAlignment);