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.topic;
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.topic.ITopic;
055    
056    /**
057     * Class http://nlp2rdf.lod2.eu/schema/topic/Topic
058     */
059    public class Topic extends IndividualImpl implements ITopic {
060    
061            private static Log log = LogFactory.getLog(Topic.class);
062    
063            /**
064             * Implementation factory for Topic
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 Topic(n, eg);
074                            } else {
075                                    log.warn("Cannot convert node " + n.toString() + " to  Topic");
076                                    return null;
077                            }
078                    }
079    
080                    /**
081                     * Return true iff the node can be converted to an instance of
082                     * this class (Topic)
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.Topic.asNode());
097                    }
098            };
099    
100            /**
101             * Filtering support for Topic
102             */
103            static final public Filter<Topic> nullFilter = new NullFilter<Topic>();
104    
105            /**
106             * Mapping support for Topic
107             */
108            public static <From> Map1<From, Topic> mapperFrom(Class<From> from) {
109                    return new Map1<From, Topic>() {
110                            @Override
111                            public Topic map1(Object x) {
112                                    if (x instanceof Statement) {
113                                            Resource r = ((Statement) x).getResource();
114                                            if (r.canAs(Topic.class))
115                                                    return r.as(Topic.class);
116                                    } else if (x instanceof RDFNode) {
117                                            if (((RDFNode) x).canAs(Topic.class))
118                                                    return ((RDFNode) x).as(Topic.class);
119                                    }
120                                    return null;
121                            }
122                    };
123            }
124    
125            // Instantiate some mappers for general use
126            static final public Map1<Statement, Topic> statementMapper = mapperFrom(Statement.class);
127            static final public Map1<Individual, Topic> individualMapper = mapperFrom(Individual.class);
128            static final public Map1<RDFNode, Topic> nodeMapper = mapperFrom(RDFNode.class);
129    
130            /**
131             * Generic functions from parent class
132             */
133            public Topic(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 Topic with jena");
142                    BuiltinPersonalities.model.add(Topic.class, Topic.factory);
143                    BuiltinPersonalities.model.add(eu.lod2.nlp2rdf.schema.topic.Topic.class, Topic.factory);
144            }
145    
146            /**
147             * Static Functions for instance handling
148             */
149            public static Topic get(java.lang.String uri, OntModel ontModel) {
150                    Individual individual = ontModel.getIndividual(uri);
151                    return (eu.lod2.nlp2rdf.schema.topic.Topic) individual.as(eu.lod2.nlp2rdf.schema.topic.Topic.class);
152            }
153    
154            public static Topic get(java.lang.String uri) {
155                    return get(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
156            }
157    
158            public static Iterator<Topic> iterate(OntModel ontModel) {
159                    ExtendedIterator<Individual> it = ontModel.listIndividuals(eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic);
160                    return it.mapWith(individualMapper).filterDrop(nullFilter);
161            }
162    
163            public static Iterator<Topic> iterate() {
164                    return iterate(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
165            }
166    
167            public static List<Topic> list(OntModel ontModel) {
168                    List<Topic> list = new ArrayList<Topic>();
169                    Iterator<Topic> it = iterate(ontModel);
170                    while (it.hasNext()) {
171                            Topic cls = it.next();
172                            list.add(cls);
173                    }
174                    return list;
175            }
176    
177            public static List<Topic> list() {
178                    return list(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
179            }
180    
181            public static Iterator<Topic> iterate(boolean direct, OntModel ontModel) {
182                    OntClass cls = ontModel.getOntClass("http://nlp2rdf.lod2.eu/schema/topic/Topic");
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<Topic> iterate(boolean direct) {
190                    return iterate(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
191            }
192    
193            public static List<Topic> list(boolean direct, OntModel ontModel) {
194                    List<Topic> list = new ArrayList<Topic>();
195                    Iterator<Topic> it = iterate(direct, ontModel);
196                    while (it.hasNext()) {
197                            Topic cls = it.next();
198                            list.add(cls);
199                    }
200                    return list;
201            }
202    
203            public static List<Topic> 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<Topic> 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<Topic> 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 Topic create(java.lang.String uri, OntModel ontModel) {
247                    return (Topic) ontModel.createOntResource(Topic.class, eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic, uri);
248            }
249    
250            public static Topic create(OntModel ontModel) {
251                    return create(null, ontModel);
252            }
253    
254            public static Topic create(java.lang.String uri) {
255                    return create(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
256            }
257    
258            public static Topic 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            /**
271             * Domain property occursIn
272             * with uri http://nlp2rdf.lod2.eu/schema/topic/occursIn
273             */
274            public boolean existsOccursIn() {
275                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
276            }
277    
278            public boolean hasOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
279                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
280            }
281    
282            public int countOccursIn() {
283                    int count = 0;
284                    Iterator<eu.lod2.nlp2rdf.schema.str.Document> it = iterateOccursIn();
285                    while (it.hasNext()) {
286                            it.next();
287                            count++;
288                    }
289                    return count;
290            }
291    
292            public Iterator<eu.lod2.nlp2rdf.schema.str.Document> iterateOccursIn() {
293                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
294                    return it.mapWith(eu.lod2.nlp2rdf.schema.str.Document.statementMapper).filterDrop(eu.lod2.nlp2rdf.schema.str.Document.nullFilter);
295            }
296    
297            public List<eu.lod2.nlp2rdf.schema.str.Document> listOccursIn() {
298                    List<eu.lod2.nlp2rdf.schema.str.Document> list = new ArrayList<eu.lod2.nlp2rdf.schema.str.Document>();
299                    Iterator<eu.lod2.nlp2rdf.schema.str.Document> it = iterateOccursIn();
300                    while (it.hasNext()) {
301                            eu.lod2.nlp2rdf.schema.str.Document inst = it.next();
302                            list.add(inst);
303                    }
304                    return list;
305            }
306    
307            public void addOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
308                    addProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
309            }
310    
311            public void addAllOccursIn(List<? extends eu.lod2.nlp2rdf.schema.str.IDocument> documentList) {
312                    for (eu.lod2.nlp2rdf.schema.str.IDocument o : documentList)
313                            addOccursIn(o);
314    
315            }
316    
317            public void removeOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
318                    removeProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
319            }
320    
321            public void removeAllOccursIn() {
322                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
323            }
324    
325            /**
326             * Domain property characterticLemma
327             * with uri http://nlp2rdf.lod2.eu/schema/topic/characteristicLemma
328             */
329            public boolean existsCharacterticLemma() {
330                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
331            }
332    
333            public boolean hasCharacterticLemma(java.lang.String stringValue) {
334                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
335            }
336    
337            public int countCharacterticLemma() {
338                    int count = 0;
339                    Iterator<java.lang.String> it = iterateCharacterticLemma();
340                    while (it.hasNext()) {
341                            it.next();
342                            count++;
343                    }
344                    return count;
345            }
346    
347            public Iterator<java.lang.String> iterateCharacterticLemma() {
348                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
349                    return it.mapWith(nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.objectAsStringMapper).filterDrop(new NullFilter<java.lang.String>());
350            }
351    
352            public List<java.lang.String> listCharacterticLemma() {
353                    List<java.lang.String> list = new ArrayList<java.lang.String>();
354                    Iterator<java.lang.String> it = iterateCharacterticLemma();
355                    while (it.hasNext()) {
356                            java.lang.String inst = it.next();
357                            list.add(inst);
358                    }
359                    return list;
360            }
361    
362            public void addCharacterticLemma(java.lang.String stringValue) {
363                    Literal literal = nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.createTypedLiteral((OntModel) getModel(), stringValue, "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
364                    setPropertyValue(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma, literal);
365            }
366    
367            public void addAllCharacterticLemma(List<java.lang.String> stringList) {
368                    for (java.lang.String o : stringList)
369                            addCharacterticLemma(o);
370            }
371    
372            public void removeCharacterticLemma(java.lang.String stringValue) {
373                    Literal literal = nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.createTypedLiteral((OntModel) getModel(), stringValue, "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral");
374                    removeProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma, literal);
375            }
376    
377            public void removeAllCharacterticLemma() {
378                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
379    
380            }
381    
382    }