diff --git a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java new file mode 100644 index 0000000000000000000000000000000000000000..626f85581bad9d0ee0e60c51ad04dae934081d69 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntology.java @@ -0,0 +1,204 @@ +package fr.inrialpes.exmo.align.onto.jena25; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.semanticweb.owl.align.AlignmentException; + +import com.hp.hpl.jena.ontology.DatatypeProperty; +import com.hp.hpl.jena.ontology.Individual; +import com.hp.hpl.jena.ontology.ObjectProperty; +import com.hp.hpl.jena.ontology.OntClass; +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntProperty; +import com.hp.hpl.jena.ontology.OntResource; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.impl.LiteralImpl; + +import fr.inrialpes.exmo.align.onto.BasicOntology; +import fr.inrialpes.exmo.align.onto.LoadedOntology; + +public class JENAOntology extends BasicOntology<OntModel> implements LoadedOntology<OntModel>{ + + + protected Set<Object> listResources(Class<? extends OntResource> type) { + Set<Object> resources = new HashSet<Object>(); + Iterator i = onto.listObjects(); + while (i.hasNext()) { + Object r = i.next(); + if (type.isInstance(r)) + resources.add(r); + } + return resources; + + + } + + public Set<Object> getClasses() { + return onto.listNamedClasses().toSet(); + } + + public Set<Object> getDataProperties() { + return onto.listDatatypeProperties().toSet(); + } + + @SuppressWarnings("unchecked") + public Set<Object> getEntities() { + Set<Object> entities = new HashSet<Object>(); + entities.addAll(onto.listNamedClasses().toSet()); + entities.addAll(onto.listDatatypeProperties().toSet()); + entities.addAll(onto.listObjectProperties().toSet()); + entities.addAll(onto.listIndividuals().toSet()); + return entities; + } + + public Object getEntity(URI u) throws AlignmentException { + return onto.getOntResource(u.toString()); + } + + public Set<String> getEntityAnnotations(Object o) throws AlignmentException { + Set<String> annots = new HashSet<String>(); + OntResource or = (OntResource) o; + Iterator i = or.listComments(null); + while (i.hasNext()) { + annots.add(((LiteralImpl) i.next()).getLexicalForm()); + } + i = or.listLabels(null); + while (i.hasNext()) { + annots.add(((LiteralImpl) i.next()).getLexicalForm()); + } + return annots; + } + + public Set<String> getEntityComments(Object o, String lang) throws AlignmentException { + Set<String> comments = new HashSet<String>(); + OntResource or = (OntResource) o; + Iterator i = or.listComments(lang); + while (i.hasNext()) { + String comment = ((LiteralImpl) i.next()).getLexicalForm(); + comments.add(comment); + } + return comments; + } + + public Set<String> getEntityComments(Object o) throws AlignmentException { + return getEntityComments(o,null); + } + + + public String getEntityName(Object o) throws AlignmentException { + // TODO Auto-generated method stub + return null; + } + + public Set<String> getEntityNames(Object o, String lang) throws AlignmentException { + Set<String> labels = new HashSet<String>(); + OntResource or = (OntResource) o; + Iterator i = or.listLabels(lang); + while (i.hasNext()) { + String label = ((LiteralImpl) i.next()).getLexicalForm(); + labels.add(label); + } + return labels; + } + + public Set<String> getEntityNames(Object o) throws AlignmentException { + return getEntityNames(o,null); + } + + public URI getEntityURI(Object o) throws AlignmentException { + try { + OntResource or = (OntResource) o; + return new URI(or.getURI()); + } catch (Exception e) { + throw new AlignmentException(o.toString()+" do not have uri", e ); + } + } + + public Set<Object> getIndividuals() { + /*Set individuals = new HashSet(); + Iterator i = onto.listIndividuals(); + while (i.hasNext()) { + Individual ind = (Individual) i.next(); + individuals.add(ind); + Iterator j = ind.listProperties(); + while (j.hasNext()) { + Statement s = (Statement) j.next(); + if (s.getPredicate().canAs(ObjectProperty.class)) { + System.out.println(s.getObject()); + individuals.add(s.getObject()); + } + } + } + return individuals;*/ + //return listResources(Individual.class); + return onto.listIndividuals().toSet(); + } + + public Set<Object> getObjectProperties() { + return onto.listObjectProperties().toSet(); + } + + public Set<Object> getProperties() { + Set properties = new HashSet(); + properties.addAll(onto.listDatatypeProperties().toSet()); + properties.addAll(onto.listObjectProperties().toSet()); + return properties; + } + + public boolean isClass(Object o) { + return o instanceof OntClass; + } + + public boolean isDataProperty(Object o) { + return o instanceof DatatypeProperty; + } + + public boolean isEntity(Object o) { + return isClass(o)||isProperty(o)||isIndividual(o); + } + + public boolean isIndividual(Object o) { + return o instanceof Individual; + } + + public boolean isObjectProperty(Object o) { + return o instanceof ObjectProperty; + } + + public boolean isProperty(Object o) { + return o instanceof OntProperty; + } + + public int nbClasses() { + return onto.listNamedClasses().toSet().size(); + } + + public int nbDataProperties() { + return onto.listDatatypeProperties().toList().size(); + } + + public int nbInstances() { + //return onto.listIndividuals().toList().size(); + return this.getIndividuals().size(); + } + + public int nbObjectProperties() { + return onto.listObjectProperties().toList().size(); + } + + public int nbProperties() { + return onto.listOntProperties().toList().size(); + } + + public void unload() { + + } + + +} diff --git a/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..dd8444349b4c78a1de3044296b9831ba5de83d61 --- /dev/null +++ b/src/fr/inrialpes/exmo/align/onto/jena25/JENAOntologyFactory.java @@ -0,0 +1,36 @@ +package fr.inrialpes.exmo.align.onto.jena25; + +import java.net.URI; +import java.util.Iterator; + +import org.semanticweb.owl.align.AlignmentException; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntModelSpec; +import com.hp.hpl.jena.ontology.Ontology; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +import fr.inrialpes.exmo.align.onto.LoadedOntology; +import fr.inrialpes.exmo.align.onto.OntologyFactory; + +public class JENAOntologyFactory extends OntologyFactory{ + + @Override + public LoadedOntology loadOntology(URI uri) throws AlignmentException { + try { + OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null ); + m.read(uri.toString()); + JENAOntology onto = new JENAOntology(); + onto.setFile(uri); + // to be checked : why several ontologies in a model ??? + onto.setURI(new URI(((Ontology)m.listOntologies().next()).getURI())); + //onto.setURI(new URI(m.listOntologies()getOntology(null).getURI())); + onto.setOntology(m); + return onto; + } + catch (Exception e) { + throw new AlignmentException("Cannot load "+uri, e ); + } + } + +}