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.error;
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.error.IError;
055    
056    /**
057     * Class http://nlp2rdf.lod2.eu/schema/error/Error
058     */
059    public class Error extends IndividualImpl implements IError {
060    
061            private static Log log = LogFactory.getLog(Error.class);
062    
063            /**
064             * Implementation factory for Error
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 Error(n, eg);
074                            } else {
075                                    log.warn("Cannot convert node " + n.toString() + " to  Error");
076                                    return null;
077                            }
078                    }
079    
080                    /**
081                     * Return true iff the node can be converted to an instance of
082                     * this class (Error)
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.Error.asNode());
097                    }
098            };
099    
100            /**
101             * Filtering support for Error
102             */
103            static final public Filter<Error> nullFilter = new NullFilter<Error>();
104    
105            /**
106             * Mapping support for Error
107             */
108            public static <From> Map1<From, Error> mapperFrom(Class<From> from) {
109                    return new Map1<From, Error>() {
110                            @Override
111                            public Error map1(Object x) {
112                                    if (x instanceof Statement) {
113                                            Resource r = ((Statement) x).getResource();
114                                            if (r.canAs(Error.class))
115                                                    return r.as(Error.class);
116                                    } else if (x instanceof RDFNode) {
117                                            if (((RDFNode) x).canAs(Error.class))
118                                                    return ((RDFNode) x).as(Error.class);
119                                    }
120                                    return null;
121                            }
122                    };
123            }
124    
125            // Instantiate some mappers for general use
126            static final public Map1<Statement, Error> statementMapper = mapperFrom(Statement.class);
127            static final public Map1<Individual, Error> individualMapper = mapperFrom(Individual.class);
128            static final public Map1<RDFNode, Error> nodeMapper = mapperFrom(RDFNode.class);
129    
130            /**
131             * Generic functions from parent class
132             */
133            public Error(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 Error with jena");
142                    BuiltinPersonalities.model.add(Error.class, Error.factory);
143                    BuiltinPersonalities.model.add(eu.lod2.nlp2rdf.schema.error.Error.class, Error.factory);
144            }
145    
146            /**
147             * Static Functions for instance handling
148             */
149            public static Error get(java.lang.String uri, OntModel ontModel) {
150                    Individual individual = ontModel.getIndividual(uri);
151                    return (eu.lod2.nlp2rdf.schema.error.Error) individual.as(eu.lod2.nlp2rdf.schema.error.Error.class);
152            }
153    
154            public static Error get(java.lang.String uri) {
155                    return get(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
156            }
157    
158            public static Iterator<Error> iterate(OntModel ontModel) {
159                    ExtendedIterator<Individual> it = ontModel.listIndividuals(eu.lod2.nlp2rdf.schema.tools.Vocabulary.Error);
160                    return it.mapWith(individualMapper).filterDrop(nullFilter);
161            }
162    
163            public static Iterator<Error> iterate() {
164                    return iterate(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
165            }
166    
167            public static List<Error> list(OntModel ontModel) {
168                    List<Error> list = new ArrayList<Error>();
169                    Iterator<Error> it = iterate(ontModel);
170                    while (it.hasNext()) {
171                            Error cls = it.next();
172                            list.add(cls);
173                    }
174                    return list;
175            }
176    
177            public static List<Error> list() {
178                    return list(eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
179            }
180    
181            public static Iterator<Error> iterate(boolean direct, OntModel ontModel) {
182                    OntClass cls = ontModel.getOntClass("http://nlp2rdf.lod2.eu/schema/error/Error");
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<Error> iterate(boolean direct) {
190                    return iterate(direct, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
191            }
192    
193            public static List<Error> list(boolean direct, OntModel ontModel) {
194                    List<Error> list = new ArrayList<Error>();
195                    Iterator<Error> it = iterate(direct, ontModel);
196                    while (it.hasNext()) {
197                            Error cls = it.next();
198                            list.add(cls);
199                    }
200                    return list;
201            }
202    
203            public static List<Error> 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<Error> 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<Error> 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 Error create(java.lang.String uri, OntModel ontModel) {
247                    return (Error) ontModel.createOntResource(Error.class, eu.lod2.nlp2rdf.schema.tools.Vocabulary.Error, uri);
248            }
249    
250            public static Error create(OntModel ontModel) {
251                    return create(null, ontModel);
252            }
253    
254            public static Error create(java.lang.String uri) {
255                    return create(uri, eu.lod2.nlp2rdf.schema.tools.Factory.getDefaultModel());
256            }
257    
258            public static Error 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 fatal
272             * with uri http://nlp2rdf.lod2.eu/schema/error/fatal
273             */
274            public boolean existsFatal() {
275                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal);
276            }
277    
278            public boolean hasFatal(java.lang.Boolean booleanValue) {
279                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal);
280            }
281    
282            public java.lang.Boolean getFatal() {
283                    RDFNode n = getPropertyValue(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal);
284                    if (n instanceof Literal) {
285                            Literal l = (Literal) n;
286                            return (java.lang.Boolean) (nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.getBoolean(l));
287                    } else {
288                            log.warn("Could not convert fatal of " + getURI() + " (" + n + ") to type Boolean");
289                            return null;
290                    }
291            }
292    
293            public void setFatal(java.lang.Boolean booleanValue) {
294                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal);
295                    nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.createTypedLiteral((OntModel) getModel(), booleanValue, "http://www.w3.org/2001/XMLSchema#boolean");
296                    Literal literal = nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.createTypedLiteral((OntModel) getModel(), booleanValue, "http://www.w3.org/2001/XMLSchema#boolean");
297                    setPropertyValue(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal, literal);
298            }
299    
300            public void removeFatal() {
301                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.fatal);
302            }
303    
304            /**
305             * Domain property source
306             * with uri http://nlp2rdf.lod2.eu/schema/error/source
307             */
308            public boolean existsSource() {
309                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.source);
310            }
311    
312            public boolean hasSource(java.lang.String stringValue) {
313                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.source);
314            }
315    
316            public int countSource() {
317                    int count = 0;
318                    Iterator<java.lang.String> it = iterateSource();
319                    while (it.hasNext()) {
320                            it.next();
321                            count++;
322                    }
323                    return count;
324            }
325    
326            public Iterator<java.lang.String> iterateSource() {
327                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.source);
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> listSource() {
332                    List<java.lang.String> list = new ArrayList<java.lang.String>();
333                    Iterator<java.lang.String> it = iterateSource();
334                    while (it.hasNext()) {
335                            java.lang.String inst = it.next();
336                            list.add(inst);
337                    }
338                    return list;
339            }
340    
341            public void addSource(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.source, literal);
344            }
345    
346            public void addAllSource(List<java.lang.String> stringList) {
347                    for (java.lang.String o : stringList)
348                            addSource(o);
349            }
350    
351            public void removeSource(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.source, literal);
354            }
355    
356            public void removeAllSource() {
357                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.source);
358    
359            }
360    
361            /**
362             * Domain property message
363             * with uri http://nlp2rdf.lod2.eu/schema/error/hasMessage
364             */
365            public boolean existsMessage() {
366                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message);
367            }
368    
369            public boolean hasMessage(java.lang.String stringValue) {
370                    return hasProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message);
371            }
372    
373            public int countMessage() {
374                    int count = 0;
375                    Iterator<java.lang.String> it = iterateMessage();
376                    while (it.hasNext()) {
377                            it.next();
378                            count++;
379                    }
380                    return count;
381            }
382    
383            public Iterator<java.lang.String> iterateMessage() {
384                    ExtendedIterator<Statement> it = listProperties(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message);
385                    return it.mapWith(nl.tudelft.tbm.eeni.owl2java.model.xsd.XsdUtils.objectAsStringMapper).filterDrop(new NullFilter<java.lang.String>());
386            }
387    
388            public List<java.lang.String> listMessage() {
389                    List<java.lang.String> list = new ArrayList<java.lang.String>();
390                    Iterator<java.lang.String> it = iterateMessage();
391                    while (it.hasNext()) {
392                            java.lang.String inst = it.next();
393                            list.add(inst);
394                    }
395                    return list;
396            }
397    
398            public void addMessage(java.lang.String stringValue) {
399                    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");
400                    setPropertyValue(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message, literal);
401            }
402    
403            public void addAllMessage(List<java.lang.String> stringList) {
404                    for (java.lang.String o : stringList)
405                            addMessage(o);
406            }
407    
408            public void removeMessage(java.lang.String stringValue) {
409                    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");
410                    removeProperty(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message, literal);
411            }
412    
413            public void removeAllMessage() {
414                    removeAll(eu.lod2.nlp2rdf.schema.tools.Vocabulary.message);
415    
416            }
417    
418    }