| 1 | = Code Examples = |
| 2 | The following snippet demonstrates the simple use of the the PhaseLibs by performing the three main tasks |
| 3 | * Instantiate two OntologyAdapters to access ontology data |
| 4 | * Create a SimilarityMeasure object, capable of measuring distances between the elements of the ontologies. |
| 5 | * Create an AlignmentGenerator object that will perform an ontology alignment on the given two ontologies. |
| 6 | |
| 7 | The snippet's purpose is to demonstrate the general usage of the PhaseLibs, but it should, nevertheless, be executable. |
| 8 | |
| 9 | {{{ |
| 10 | // We want to align two ontologies |
| 11 | Ontology o1, o2; |
| 12 | |
| 13 | // Please note, that the constructors are adapter dependent |
| 14 | |
| 15 | // first: instantiate o1, the dublin core metadata types ontology |
| 16 | o1 = new de.dfki.km.phaselib.adapters.rdfsJena.RDFSJenaOntology(DCT_URI, DCT_URI.toURL().openStream()); |
| 17 | |
| 18 | // second: a custom OWL-ontology |
| 19 | o2 = new de.dfki.km.phaselib.adapters.owl.OntologyAdapter(MYFILE_URI); |
| 20 | |
| 21 | // now we need a measure. in this case we use a simple 3-gram measure |
| 22 | SimilarityMeasure measure = new StringBasedSimilarity(); |
| 23 | |
| 24 | // we can now either use the measure to calculate the distance between some classes: |
| 25 | SimilarityMatrix distances = measure.calcSimilarities(o1.getAllClasses(), o2.getAllClasses()); |
| 26 | System.out.println(distances.getSortedNonzeroEntries()); |
| 27 | |
| 28 | |
| 29 | // ... or we can use an AlignmentGenerator to calculate an alignment between the ontologies. |
| 30 | AlignmentGenerator generator; |
| 31 | |
| 32 | // in this case a simple generator using the measure above will do: |
| 33 | generator = new de.dfki.km.phaselib.impl.alignments.SimpleEvidenceAlignment(new StringBasedSimilarity()); |
| 34 | // (We could have used the measure instatiated above, but to show that these two tasks are |
| 35 | // completely independet, we have created a new measure instance.) |
| 36 | |
| 37 | // since AlignmentGenerators can only augment existing alignments, we have |
| 38 | // to create an empty alignment |
| 39 | Alignment emptyAlignment = new AlignmentImpl(o1, o2); |
| 40 | |
| 41 | // we might also set some options |
| 42 | HashMap<String, Object> options = new HashMap<String, Object>(); |
| 43 | options.put(SimpleEvidenceAlignment.PARAM_THRESHOLD, 0.3d); |
| 44 | |
| 45 | // now we can let it generate an alignment |
| 46 | Alignment generatedAlignment = generator.generateAlignment(emptyAlignment, options); |
| 47 | System.out.println(generatedAlignment); |
| 48 | }}} |