001    /***************************************************************************/
002    /*  Copyright (C) 2010-2011, Sebastian Hellmann                            */
003    /*  Note: If you need parts of NLP2RDF in another licence due to licence   */
004    /*  incompatibility, please mail hellmann@informatik.uni-leipzig.de        */
005    /*                                                                         */
006    /*  This file is part of NLP2RDF.                                          */
007    /*                                                                         */
008    /*  NLP2RDF is free software; you can redistribute it and/or modify        */
009    /*  it under the terms of the GNU General Public License as published by   */
010    /*  the Free Software Foundation; either version 3 of the License, or      */
011    /*  (at your option) any later version.                                    */
012    /*                                                                         */
013    /*  NLP2RDF is distributed in the hope that it will be useful,             */
014    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
015    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
016    /*  GNU General Public License for more details.                           */
017    /*                                                                         */
018    /*  You should have received a copy of the GNU General Public License      */
019    /*  along with this program. If not, see <http://www.gnu.org/licenses/>.   */
020    /***************************************************************************/
021    
022    package eu.lod2.nlp2rdf.schema;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Iterator;
027    
028    import nl.tudelft.tbm.eeni.owl2java.model.jenautils.NullFilter;
029    
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    import com.hp.hpl.jena.enhanced.BuiltinPersonalities;
034    import com.hp.hpl.jena.enhanced.EnhGraph;
035    import com.hp.hpl.jena.enhanced.EnhNode;
036    import com.hp.hpl.jena.enhanced.Implementation;
037    import com.hp.hpl.jena.graph.Graph;
038    import com.hp.hpl.jena.graph.Node;
039    import com.hp.hpl.jena.ontology.Individual;
040    import com.hp.hpl.jena.ontology.OntClass;
041    import com.hp.hpl.jena.ontology.OntModel;
042    import com.hp.hpl.jena.ontology.Profile;
043    import com.hp.hpl.jena.ontology.impl.IndividualImpl;
044    import com.hp.hpl.jena.rdf.model.Resource;
045    import com.hp.hpl.jena.rdf.model.RDFNode;
046    import com.hp.hpl.jena.rdf.model.Statement;
047    import com.hp.hpl.jena.rdf.model.Literal;
048    import com.hp.hpl.jena.util.iterator.WrappedIterator;
049    import com.hp.hpl.jena.util.iterator.ExtendedIterator;
050    import com.hp.hpl.jena.util.iterator.Filter;
051    import com.hp.hpl.jena.util.iterator.Map1;
052    
053    // import interface
054    import eu.lod2.nlp2rdf.schema.IThing;
055    
056    /**
057     * Class http://www.w3.org/2002/07/owl#Thing
058     */
059    public class Thing extends IndividualImpl implements IThing {
060    
061            private static Log log = LogFactory.getLog(Thing.class);
062    
063            /**
064             * Implementation factory for Thing
065             */
066            static final public Implementation factory = new Implementation() {
067    
068                    /**
069                     * Convert a Node into an instance of the class
070                     */
071                    public EnhNode wrap(Node n, EnhGraph eg) {
072                            if (canWrap(n, eg)) {
073                                    return new Thing(n, eg);
074                            } else {
075                                    log.warn("Cannot convert node " + n.toString() + " to  Thing");
076                                    return null;
077                            }
078                    }
079    
080                    /**
081                     * Return true iff the node can be converted to an instance of
082                     * this class (Thing)
083                     */
084                    public boolean canWrap(Node n, EnhGraph eg) {
085                            Profile profile;
086                            if (eg instanceof OntModel)
087                                    profile = ((OntModel) eg).getProfile();
088                            else
089                                    return false;
090    
091                            if (!profile.isSupported(n, eg, Individual.class)) {
092                                    return false;
093                            }
094    
095                            Graph graph = eg.asGraph();
096                            return graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Thing.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.String.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Phrase.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Word.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.StopWord.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Sentence.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.OffsetBasedString.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.ContextHashBasedString.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Document.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic.asNode()) || graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Error.asNode());
097                    }
098            };
099    
100            /**
101             * Filtering support for Thing
102             */
103            static final public Filter<Thing> nullFilter = new NullFilter<Thing>();
104    
105            /**
106             * Mapping support for Thing
107             */
108            public static <From> Map1<From, Thing> mapperFrom(Class<From> from) {
109                    return new Map1<From, Thing>() {
110                            @Override
111                            public Thing map1(Object x) {
112                                    if (x instanceof Statement) {
113                                            Resource r = ((Statement) x).getResource();
114                                            if (r.canAs(Thing.class))
115                                                    return r.as(Thing.class);
116                                    } else if (x instanceof RDFNode) {
117                                            if (((RDFNode) x).canAs(Thing.class))
118                                                    return ((RDFNode) x).as(Thing.class);
119                                    }
120                                    return null;
121                            }
122                    };
123            }
124    
125            // Instantiate some mappers for general use
126            static final public Map1<Statement, Thing> statementMapper = mapperFrom(Statement.class);
127            static final public Map1<Individual, Thing> individualMapper = mapperFrom(Individual.class);
128            static final public Map1<RDFNode, Thing> nodeMapper = mapperFrom(RDFNode.class);
129    
130            /**
131             * Generic functions from parent class
132             */
133            public Thing(Node n, EnhGraph g) {
134                    super(n, g);
135            }
136    
137            /**
138             * Registers all custom classes with jena
139             */
140            public static void register() {
141                    log.debug("Registering custom class Thing with jena");
142                    BuiltinPersonalities.model.add(Thing.class, Thing.factory);
143                    BuiltinPersonalities.model.add(eu.lod2.nlp2rdf.schema.Thing.class, Thing.factory);
144            }
145    
146            /**
147             * Static Functions for instance handling
148             */
149            public static Thing get(java.lang.String uri, OntModel ontModel) {
150                    Individual individual = ontModel.getIndividual(uri);
151                    return (eu.lod2.nlp2rdf.schema.Thing) individual.as(eu.lod2.nlp2rdf.schema.Thing.class);
152            }
153    
154            public static Thing get(java.lang.String uri) {
155                    return get(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
156            }
157    
158            public static Iterator<Thing> iterate(OntModel ontModel) {
159                    ExtendedIterator<Individual> it = ontModel.listIndividuals(eu.lod2.nlp2rdf.schema.tools.Vocabulary.Thing);
160                    return it.mapWith(individualMapper).filterDrop(nullFilter);
161            }
162    
163            public static Iterator<Thing> iterate() {
164                    return iterate(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
165            }
166    
167            public static List<Thing> list(OntModel ontModel) {
168                    List<Thing> list = new ArrayList<Thing>();
169                    Iterator<Thing> it = iterate(ontModel);
170                    while (it.hasNext()) {
171                            Thing cls = it.next();
172                            list.add(cls);
173                    }
174                    return list;
175            }
176    
177            public static List<Thing> list() {
178                    return list(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
179            }
180    
181            public static Iterator<Thing> iterate(boolean direct, OntModel ontModel) {
182                    OntClass cls = ontModel.getOntClass("http://www.w3.org/2002/07/owl#Thing");
183                    ExtendedIterator<? extends RDFNode> it = cls.listInstances(direct);
184                    ExtendedIterator<RDFNode> nodeIt = new WrappedIterator<RDFNode>(it) {
185                    };
186                    return nodeIt.mapWith(nodeMapper).filterDrop(nullFilter);
187            }
188    
189            public static Iterator<Thing> iterate(boolean direct) {
190                    return iterate(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
191            }
192    
193            public static List<Thing> list(boolean direct, OntModel ontModel) {
194                    List<Thing> list = new ArrayList<Thing>();
195                    Iterator<Thing> it = iterate(direct, ontModel);
196                    while (it.hasNext()) {
197                            Thing cls = it.next();
198                            list.add(cls);
199                    }
200                    return list;
201            }
202    
203            public static List<Thing> list(boolean direct) {
204                    return list(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
205            }
206    
207            public static int count(OntModel ontModel) {
208                    int count = 0;
209                    Iterator<Thing> it = iterate(ontModel);
210                    while (it.hasNext()) {
211                            it.next();
212                            count++;
213                    }
214                    return count;
215            }
216    
217            public static int count() {
218                    return count(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
219            }
220    
221            public static int count(boolean direct, OntModel ontModel) {
222                    int count = 0;
223                    Iterator<Thing> it = iterate(direct, ontModel);
224                    while (it.hasNext()) {
225                            it.next();
226                            count++;
227                    }
228                    return count;
229            }
230    
231            public static int count(boolean direct) {
232                    return count(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
233            }
234    
235            public static boolean exists(java.lang.String uri, OntModel ontModel) {
236                    Individual individual = ontModel.getIndividual(uri);
237                    if (individual != null)
238                            return true;
239                    return false;
240            }
241    
242            public static boolean exists(java.lang.String uri) {
243                    return exists(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
244            }
245    
246            public static Thing create(java.lang.String uri, OntModel ontModel) {
247                    return (Thing) ontModel.createOntResource(Thing.class, eu.lod2.nlp2rdf.schema.tools.Vocabulary.Thing, uri);
248            }
249    
250            public static Thing create(OntModel ontModel) {
251                    return create(null, ontModel);
252            }
253    
254            public static Thing create(java.lang.String uri) {
255                    return create(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
256            }
257    
258            public static Thing create() {
259                    return create(null, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
260            }
261    
262            public static void delete(java.lang.String uri, OntModel ontModel) {
263                    eu.lod2.nlp2rdf.schema.tools.Factory.deleteInstance(uri, ontModel);
264            }
265    
266            public static void delete(java.lang.String uri) {
267                    eu.lod2.nlp2rdf.schema.tools.Factory.deleteInstance(uri);
268            }
269    
270    }