From f5a711d9c3f8c29d1e1f959fec545ecdca84dec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20David?= <Jerome.David@univ-grenoble-alpes.fr> Date: Tue, 12 Oct 2010 17:25:34 +0000 Subject: [PATCH] Add method Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException in ontowrap API. This method return a map annotation->language. Implementations for all APIs except owlapi1 are provided --- .../exmo/ontowrap/LoadedOntology.java | 3 ++ .../exmo/ontowrap/jena25/JENAOntology.java | 45 ++++++++++-------- .../ontowrap/owlapi10/OWLAPIOntology.java | 9 +++- .../ontowrap/owlapi30/OWLAPI3Ontology.java | 22 +++++++++ .../exmo/ontowrap/skosapi/SKOSThesaurus.java | 5 ++ .../skoslite/SKOSLiteOntologyFactory.java | 18 +++++++ .../ontowrap/skoslite/SKOSLiteThesaurus.java | 47 +++++++++++++++++-- 7 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java b/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java index 785b5a7f..a857e067 100644 --- a/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/LoadedOntology.java @@ -21,6 +21,7 @@ package fr.inrialpes.exmo.ontowrap; import java.net.URI; +import java.util.Map; import java.util.Set; import org.semanticweb.owl.model.OWLEntity; @@ -106,6 +107,8 @@ public interface LoadedOntology<O> extends Ontology<O> { * @throws OntowrapException */ public Set<String> getEntityAnnotations( Object o ) throws OntowrapException; + + public Map<String,String> getEntityAnnotationsL( Object o ) throws OntowrapException; /** * Returns all the values of the "owl:AnnotationProperty" property for a given entity expressed in the required language. diff --git a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java index 3584c2f3..24e8cfc2 100644 --- a/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/jena25/JENAOntology.java @@ -21,8 +21,10 @@ package fr.inrialpes.exmo.ontowrap.jena25; import java.net.URI; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Set; @@ -60,7 +62,6 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded } public void getEntityAnnotations( Object o, Set<String> annots, String lang) throws OntowrapException { - StmtIterator stmtIt = onto.listStatements((Resource)o,null,(RDFNode)null); while (stmtIt.hasNext()) { Statement st = stmtIt.next(); @@ -72,28 +73,30 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded if (lang==null || lang.equals(l.getLanguage())) { annots.add(l.getLexicalForm()); } - else if (obj.isResource()) { + } + else if (obj.isResource()) { getEntityAnnotations(obj, annots, lang); - } } } } - - // THIS SHOULD BE STATIC NOW!! - /*Iterator<AnnotationProperty> z = onto.listAnnotationProperties(); - while (z.hasNext()) { - AnnotationProperty ap = z.next(); - // System.out.println("hjhjhjh"+ap); - NodeIterator j = ((OntResource)o).listPropertyValues((Property) ap); - while (j.hasNext()) { - RDFNode n = j.nextNode(); - if (n.isResource()) - getEntityAnnotations(n, annots); - else if (n.isLiteral()) { - annots.add( ((Literal)n.as(Literal.class)).getLexicalForm() ); + } + + public void getEntityAnnotations( Object o, Map<String,String> annots) throws OntowrapException { + StmtIterator stmtIt = onto.listStatements((Resource)o,null,(RDFNode)null); + while (stmtIt.hasNext()) { + Statement st = stmtIt.next(); + + if ( st.getPredicate().canAs(AnnotationProperty.class)) { + RDFNode obj= st.getObject(); + if (obj.isLiteral()) { + Literal l =obj.as(Literal.class); + annots.put(l.getLexicalForm(),l.getLanguage()); } + else if (obj.isResource()) { + getEntityAnnotations(obj, annots); + } } - }*/ + } } public Set<String> getEntityAnnotations(Object o) throws OntowrapException { @@ -101,6 +104,12 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded getEntityAnnotations(o,annots,null); return annots; } + + public Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException { + Map<String,String> annots = new HashMap<String,String>(); + getEntityAnnotations(o,annots); + return annots; + } public Set<String> getEntityAnnotations( Object o, String lang ) throws OntowrapException { Set<String> annots = new HashSet<String>(); @@ -335,8 +344,6 @@ public class JENAOntology extends BasicOntology<OntModel> implements HeavyLoaded public Set<? extends OntProperty> getSuperProperties(Object p, int local, int asserted, int named) { return ((OntProperty) p).listSuperProperties(asserted==OntologyFactory.DIRECT).toSet(); } - - } /*class JENAEntityIt<R extends OntResource> implements Iterator<R> { diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java index 8c2db442..bf86251d 100644 --- a/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java +++ b/src/fr/inrialpes/exmo/ontowrap/owlapi10/OWLAPIOntology.java @@ -36,6 +36,7 @@ package fr.inrialpes.exmo.ontowrap.owlapi10; import java.net.URI; import java.util.AbstractSet; import java.util.Iterator; +import java.util.Map; import java.util.Set; import java.util.HashSet; @@ -154,7 +155,7 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL } catch (OWLException oex) { return null; } - }; + } public Set<String> getEntityComments( Object o ) throws OntowrapException { try { @@ -163,7 +164,11 @@ public class OWLAPIOntology extends BasicOntology<OWLOntology> implements HeavyL } catch (OWLException oex) { return null; } - }; + } + + public Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException { + throw new UnsupportedOperationException(); + } protected Set<String> getAnnotations(final OWLEntity e , final String lang , final String typeAnnot ) throws OWLException { final OWLOntology o = this.onto; diff --git a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java index 45ade41e..a82462d1 100644 --- a/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java +++ b/src/fr/inrialpes/exmo/ontowrap/owlapi30/OWLAPI3Ontology.java @@ -21,7 +21,9 @@ package fr.inrialpes.exmo.ontowrap.owlapi30; import java.net.URI; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; @@ -136,11 +138,31 @@ public class OWLAPI3Ontology extends BasicOntology<OWLOntology> implements Heavy } return annotations; } + + protected Map<String,String> getEntityAnnotationsL( Object o, URI type ) { + OWLEntity entity = (OWLEntity) o; + Map<String,String> annotations = new HashMap<String,String>(); + for ( OWLAnnotation annot : entity.getAnnotations( onto ) ) { + OWLAnnotationValue c = annot.getValue(); + OWLAnnotationProperty p = annot.getProperty(); + if ( c instanceof OWLLiteral ) { + if ( type == null || + ( type.equals(OWLRDFVocabulary.RDFS_LABEL.getURI()) && p.isLabel() ) || + ( type.equals(OWLRDFVocabulary.RDFS_COMMENT.getURI()) && p.isComment() ) ) + annotations.put( ((OWLLiteral)c).getLiteral(), ((OWLLiteral)c).getLang()); + } + } + return annotations; + } public Set<String> getEntityAnnotations(Object o) throws OntowrapException { return getEntityAnnotations(o, null, null); } + public Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException { + return getEntityAnnotationsL(o, null); + } + public Set<String> getEntityAnnotations( Object o, String lang ) throws OntowrapException { return getEntityAnnotations(o, null, lang); } diff --git a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSThesaurus.java b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSThesaurus.java index 3859adae..294841d2 100644 --- a/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSThesaurus.java +++ b/src/fr/inrialpes/exmo/ontowrap/skosapi/SKOSThesaurus.java @@ -24,6 +24,7 @@ import java.net.URI; import java.util.AbstractSet; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Set; import org.semanticweb.skos.SKOSDataFactory; @@ -199,6 +200,10 @@ lang = untypedLiteral.getLang(); */ return comments; } + + public Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException { + throw new UnsupportedOperationException(); + } // TODO /** diff --git a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java index 5975cde8..d6df4906 100644 --- a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java +++ b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteOntologyFactory.java @@ -1,3 +1,21 @@ +/* + * + * Copyright (C) INRIA, 2008-2010 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ package fr.inrialpes.exmo.ontowrap.skoslite; import java.net.URI; diff --git a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteThesaurus.java b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteThesaurus.java index c950841c..56804168 100644 --- a/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteThesaurus.java +++ b/src/fr/inrialpes/exmo/ontowrap/skoslite/SKOSLiteThesaurus.java @@ -1,8 +1,27 @@ +/* + * Copyright (C) INRIA, 2008-2010 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ package fr.inrialpes.exmo.ontowrap.skoslite; import java.net.URI; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import com.hp.hpl.jena.graph.Node; @@ -209,6 +228,30 @@ public class SKOSLiteThesaurus implements HeavyLoadedOntology<Model> { return annots; } + public Map<String,String> getEntityAnnotationsL(Object o, String[] types) throws OntowrapException { + HashMap<String,String> annots=new HashMap<String,String>(); + ExtendedIterator<RDFNode> it=null; + for (String t : types) { + if (it==null) + it=ontoInf.listObjectsOfProperty((Resource) o,ontoInf.getProperty(t)); + else + it.andThen(ontoInf.listObjectsOfProperty((Resource) o,ontoInf.getProperty(t))); + } + while (it.hasNext()) { + Node n = it.next().asNode(); + if (n.isLiteral()) { + //System.out.println(n.getLiteralLexicalForm()); + annots.put(n.getLiteralLexicalForm(),n.getLiteralLanguage()); + } + } + return annots; + } + + @Override + public Map<String, String> getEntityAnnotationsL(Object o) throws OntowrapException { + return getEntityAnnotationsL(o,new String[]{RDFS.label.toString(),SKOS_NOTE,SKOS_NOTATION}); + } + public Set<String> getEntityAnnotations(Object o) throws OntowrapException { return getEntityAnnotations(o,null); } @@ -441,8 +484,4 @@ public class SKOSLiteThesaurus implements HeavyLoadedOntology<Model> { public void setURI(URI uri) { this.uri=uri; } - - - - } -- GitLab