001    package eu.lod2.nlp2rdf.schema.topic;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    import java.util.Iterator;
006    
007    import nl.tudelft.tbm.eeni.owl2java.model.jenautils.NullFilter;
008    
009    import org.apache.commons.logging.Log;
010    import org.apache.commons.logging.LogFactory;
011    
012    import com.hp.hpl.jena.enhanced.BuiltinPersonalities;
013    import com.hp.hpl.jena.enhanced.EnhGraph;
014    import com.hp.hpl.jena.enhanced.EnhNode;
015    import com.hp.hpl.jena.enhanced.Implementation;
016    import com.hp.hpl.jena.graph.Graph;
017    import com.hp.hpl.jena.graph.Node;
018    import com.hp.hpl.jena.ontology.Individual;
019    import com.hp.hpl.jena.ontology.OntClass;
020    import com.hp.hpl.jena.ontology.OntModel;
021    import com.hp.hpl.jena.ontology.Profile;
022    import com.hp.hpl.jena.ontology.impl.IndividualImpl;
023    import com.hp.hpl.jena.rdf.model.Resource;
024    import com.hp.hpl.jena.rdf.model.RDFNode;
025    import com.hp.hpl.jena.rdf.model.Statement;
026    import com.hp.hpl.jena.rdf.model.Literal;
027    import com.hp.hpl.jena.util.iterator.WrappedIterator;
028    import com.hp.hpl.jena.util.iterator.ExtendedIterator;
029    import com.hp.hpl.jena.util.iterator.Filter;
030    import com.hp.hpl.jena.util.iterator.Map1;
031    
032    // import interface
033    import eu.lod2.nlp2rdf.schema.topic.ITopic;
034    
035    /**
036     * Class http://nlp2rdf.lod2.eu/schema/topic/Topic
037     */
038    public class Topic extends IndividualImpl implements ITopic {
039    
040            private static Log log = LogFactory.getLog(Topic.class);
041    
042            /**
043             * Implementation factory for Topic
044             */
045            static final public Implementation factory = new Implementation() {
046    
047                    /**
048                     * Convert a Node into an instance of the class
049                     */
050                    public EnhNode wrap(Node n, EnhGraph eg) {
051                            if (canWrap(n, eg)) {
052                                    return new Topic(n, eg);
053                            } else {
054                                    log.warn("Cannot convert node " + n.toString() + " to  Topic");
055                                    return null;
056                            }
057                    }
058    
059                    /**
060                     * Return true iff the node can be converted to an instance of
061                     * this class (Topic)
062                     */
063                    public boolean canWrap(Node n, EnhGraph eg) {
064                            Profile profile;
065                            if (eg instanceof OntModel)
066                                    profile = ((OntModel) eg).getProfile();
067                            else
068                                    return false;
069    
070                            if (!profile.isSupported(n, eg, Individual.class)) {
071                                    return false;
072                            }
073    
074                            Graph graph = eg.asGraph();
075                            return graph.contains(n, com.hp.hpl.jena.vocabulary.RDF.type.asNode(), eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic.asNode());
076                    }
077            };
078    
079            /**
080             * Filtering support for Topic
081             */
082            static final public Filter<Topic> nullFilter = new NullFilter<Topic>();
083    
084            /**
085             * Mapping support for Topic
086             */
087            public static <From> Map1<From, Topic> mapperFrom(Class<From> from) {
088                    return new Map1<From, Topic>() {
089                            @Override
090                            public Topic map1(Object x) {
091                                    if (x instanceof Statement) {
092                                            Resource r = ((Statement) x).getResource();
093                                            if (r.canAs(Topic.class))
094                                                    return r.as(Topic.class);
095                                    } else if (x instanceof RDFNode) {
096                                            if (((RDFNode) x).canAs(Topic.class))
097                                                    return ((RDFNode) x).as(Topic.class);
098                                    }
099                                    return null;
100                            }
101                    };
102            }
103    
104            // Instantiate some mappers for general use
105            static final public Map1<Statement, Topic> statementMapper = mapperFrom(Statement.class);
106            static final public Map1<Individual, Topic> individualMapper = mapperFrom(Individual.class);
107            static final public Map1<RDFNode, Topic> nodeMapper = mapperFrom(RDFNode.class);
108    
109            /**
110             * Generic functions from parent class
111             */
112            public Topic(Node n, EnhGraph g) {
113                    super(n, g);
114            }
115    
116            /**
117             * Registers all custom classes with jena
118             */
119            public static void register() {
120                    log.debug("Registering custom class Topic with jena");
121                    BuiltinPersonalities.model.add(Topic.class, Topic.factory);
122                    BuiltinPersonalities.model.add(eu.lod2.nlp2rdf.schema.topic.Topic.class, Topic.factory);
123            }
124    
125            /**
126             * Static Functions for instance handling
127             */
128            public static Topic get(java.lang.String uri, OntModel ontModel) {
129                    Individual individual = ontModel.getIndividual(uri);
130                    return (eu.lod2.nlp2rdf.schema.topic.Topic) individual.as(eu.lod2.nlp2rdf.schema.topic.Topic.class);
131            }
132    
133            public static Topic get(java.lang.String uri) {
134                    return get(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
135            }
136    
137            public static Iterator<Topic> iterate(OntModel ontModel) {
138                    ExtendedIterator<Individual> it = ontModel.listIndividuals(eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic);
139                    return it.mapWith(individualMapper).filterDrop(nullFilter);
140            }
141    
142            public static Iterator<Topic> iterate() {
143                    return iterate(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
144            }
145    
146            public static List<Topic> list(OntModel ontModel) {
147                    List<Topic> list = new ArrayList<Topic>();
148                    Iterator<Topic> it = iterate(ontModel);
149                    while (it.hasNext()) {
150                            Topic cls = it.next();
151                            list.add(cls);
152                    }
153                    return list;
154            }
155    
156            public static List<Topic> list() {
157                    return list(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
158            }
159    
160            public static Iterator<Topic> iterate(boolean direct, OntModel ontModel) {
161                    OntClass cls = ontModel.getOntClass("http://nlp2rdf.lod2.eu/schema/topic/Topic");
162                    ExtendedIterator<? extends RDFNode> it = cls.listInstances(direct);
163                    ExtendedIterator<RDFNode> nodeIt = new WrappedIterator<RDFNode>(it) {
164                    };
165                    return nodeIt.mapWith(nodeMapper).filterDrop(nullFilter);
166            }
167    
168            public static Iterator<Topic> iterate(boolean direct) {
169                    return iterate(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
170            }
171    
172            public static List<Topic> list(boolean direct, OntModel ontModel) {
173                    List<Topic> list = new ArrayList<Topic>();
174                    Iterator<Topic> it = iterate(direct, ontModel);
175                    while (it.hasNext()) {
176                            Topic cls = it.next();
177                            list.add(cls);
178                    }
179                    return list;
180            }
181    
182            public static List<Topic> list(boolean direct) {
183                    return list(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
184            }
185    
186            public static int count(OntModel ontModel) {
187                    int count = 0;
188                    Iterator<Topic> it = iterate(ontModel);
189                    while (it.hasNext()) {
190                            it.next();
191                            count++;
192                    }
193                    return count;
194            }
195    
196            public static int count() {
197                    return count(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
198            }
199    
200            public static int count(boolean direct, OntModel ontModel) {
201                    int count = 0;
202                    Iterator<Topic> it = iterate(direct, ontModel);
203                    while (it.hasNext()) {
204                            it.next();
205                            count++;
206                    }
207                    return count;
208            }
209    
210            public static int count(boolean direct) {
211                    return count(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
212            }
213    
214            public static boolean exists(java.lang.String uri, OntModel ontModel) {
215                    Individual individual = ontModel.getIndividual(uri);
216                    if (individual != null)
217                            return true;
218                    return false;
219            }
220    
221            public static boolean exists(java.lang.String uri) {
222                    return exists(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
223            }
224    
225            public static Topic create(java.lang.String uri, OntModel ontModel) {
226                    return (Topic) ontModel.createOntResource(Topic.class, eu.lod2.nlp2rdf.schema.tools.Vocabulary.Topic, uri);
227            }
228    
229            public static Topic create(OntModel ontModel) {
230                    return create(null, ontModel);
231            }
232    
233            public static Topic create(java.lang.String uri) {
234                    return create(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
235            }
236    
237            public static Topic create() {
238                    return create(null, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
239            }
240    
241            public static void delete(java.lang.String uri, OntModel ontModel) {
242                    eu.lod2.nlp2rdf.schema.tools.Factory.deleteInstance(uri, ontModel);
243            }
244    
245            public static void delete(java.lang.String uri) {
246                    eu.lod2.nlp2rdf.schema.tools.Factory.deleteInstance(uri);
247            }
248    
249            /**
250             * Domain property occursIn
251             * with uri http://nlp2rdf.lod2.eu/schema/topic/occursIn
252             */
253            public boolean existsOccursIn() {
254                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
255            }
256    
257            public boolean hasOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
258                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
259            }
260    
261            public int countOccursIn() {
262                    int count = 0;
263                    Iterator<eu.lod2.nlp2rdf.schema.str.Document> it = iterateOccursIn();
264                    while (it.hasNext()) {
265                            it.next();
266                            count++;
267                    }
268                    return count;
269            }
270    
271            public Iterator<eu.lod2.nlp2rdf.schema.str.Document> iterateOccursIn() {
272                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
273                    return it.mapWith(eu.lod2.nlp2rdf.schema.str.Document.statementMapper).filterDrop(eu.lod2.nlp2rdf.schema.str.Document.nullFilter);
274            }
275    
276            public List<eu.lod2.nlp2rdf.schema.str.Document> listOccursIn() {
277                    List<eu.lod2.nlp2rdf.schema.str.Document> list = new ArrayList<eu.lod2.nlp2rdf.schema.str.Document>();
278                    Iterator<eu.lod2.nlp2rdf.schema.str.Document> it = iterateOccursIn();
279                    while (it.hasNext()) {
280                            eu.lod2.nlp2rdf.schema.str.Document inst = it.next();
281                            list.add(inst);
282                    }
283                    return list;
284            }
285    
286            public void addOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
287                    addProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
288            }
289    
290            public void addAllOccursIn(List<? extends eu.lod2.nlp2rdf.schema.str.IDocument> documentList) {
291                    for (eu.lod2.nlp2rdf.schema.str.IDocument o : documentList)
292                            addOccursIn(o);
293    
294            }
295    
296            public void removeOccursIn(eu.lod2.nlp2rdf.schema.str.IDocument documentValue) {
297                    removeProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn, documentValue);
298            }
299    
300            public void removeAllOccursIn() {
301                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.occursIn);
302            }
303    
304            /**
305             * Domain property characterticLemma
306             * with uri http://nlp2rdf.lod2.eu/schema/topic/characteristicLemma
307             */
308            public boolean existsCharacterticLemma() {
309                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
310            }
311    
312            public boolean hasCharacterticLemma(java.lang.String stringValue) {
313                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
314            }
315    
316            public int countCharacterticLemma() {
317                    int count = 0;
318                    Iterator<java.lang.String> it = iterateCharacterticLemma();
319                    while (it.hasNext()) {
320                            it.next();
321                            count++;
322                    }
323                    return count;
324            }
325    
326            public Iterator<java.lang.String> iterateCharacterticLemma() {
327                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
328                    return it.mapWith(nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.objectAsStringMapper).filterDrop(new NullFilter<java.lang.String>());
329            }
330    
331            public List<java.lang.String> listCharacterticLemma() {
332                    List<java.lang.String> list = new ArrayList<java.lang.String>();
333                    Iterator<java.lang.String> it = iterateCharacterticLemma();
334                    while (it.hasNext()) {
335                            java.lang.String inst = it.next();
336                            list.add(inst);
337                    }
338                    return list;
339            }
340    
341            public void addCharacterticLemma(java.lang.String stringValue) {
342                    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");
343                    setPropertyValue(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma, literal);
344            }
345    
346            public void addAllCharacterticLemma(List<java.lang.String> stringList) {
347                    for (java.lang.String o : stringList)
348                            addCharacterticLemma(o);
349            }
350    
351            public void removeCharacterticLemma(java.lang.String stringValue) {
352                    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");
353                    removeProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma, literal);
354            }
355    
356            public void removeAllCharacterticLemma() {
357                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.characterticLemma);
358    
359            }
360    
361    }