[ 
https://issues.apache.org/jira/browse/JENA-1415?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Christian Bay updated JENA-1415:
--------------------------------
    Description: 
Hey there,
I'm getting a _ConversionException_ when converting a resource to an individual 
by using _as_.

[EDIT] Edited this report because I could reproduce this error in a little 
example.
Jena throws a _ConversionErrorException_ when trying to cast a Resource with 
_as_ to an individual. The resource is received by a query as suggested here 
[https://github.com/Galigator/openllet/blob/integration/examples/src/main/java/openllet/examples/SPARQLDLExample.java]

This is the test code:
{code:java}
package bug;

import openllet.jena.PelletReasonerFactory;
import openllet.query.sparqldl.jena.SparqlDLExecutionFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.util.iterator.ExtendedIterator;

import java.util.ArrayList;

public class SPARQLDLBug
{

        // The ontology loaded as dataset
        private static final String ontology = "ontologies/simple.owl";

        private static final String query = "query.sparql";
        public void run()
        {
                        // First create a Jena ontology model backed by the 
Pellet reasoner
                        // (note, the Pellet reasoner is required)
                        final OntModel m = 
ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);

                        // Then read the _data from the file into the ontology 
model
                        m.read(ontology);

                        // Now read the query file into a query object
                        final Query q = QueryFactory.read(query);

                        // Create a SPARQL-DL query execution for the given 
query and
                        // ontology model
                        final QueryExecution qe = 
SparqlDLExecutionFactory.create(q, m);

                        // We want to execute a SELECT query, do it, and return 
the result set
                        final ResultSet rs = qe.execSelect();

                        ArrayList<String> result = new ArrayList<String>();

                        while(rs.hasNext()){
                                QuerySolution qs = rs.next();
                                // The Bug occurs in the next line
                                Individual in = 
qs.getResource("x").as(Individual.class);
                                ExtendedIterator<OntClass> it = 
in.listOntClasses(true);

                                String className = "";
                                while(it.hasNext()){
                                        className = it.next().toString();
                                }
                                result.add(className);  
                        }

                        qe.close();
        }

        public static void main(final String[] args)
        {
                final SPARQLDLBug app = new SPARQLDLBug();
                app.run();
        }
}
{code}

The error message is:

{code}
Exception in thread "main" org.apache.jena.ontology.ConversionException: Cannot 
convert node 
http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia to 
Individual
        at 
org.apache.jena.ontology.impl.IndividualImpl$1.wrap(IndividualImpl.java:61)
        at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:152)
        at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:31)
        at org.apache.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:62)
        at org.apache.jena.enhanced.EnhNode.as(EnhNode.java:107)
        at bug.SPARQLDLBug.run(SPARQLDLBug.java:58)
        at bug.SPARQLDLBug.main(SPARQLDLBug.java:75)

{code}

This Query is:

{code:xml}
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 PREFIX owl: <http://www.w3.org/2002/07/owl#>
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 PREFIX bio: <http://www8.cs.fau.de/research:cgm/schizophrenia#>
 SELECT ?x
 WHERE { ?x rdf:type bio:AcuteSchizophrenia }
{code}

And the Ontology consist only of a class and one individual.

{code:xml}
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www8.cs.fau.de/research:cgm/schizophrenia#";
     xml:base="http://www8.cs.fau.de/research:cgm/schizophrenia";
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
     xmlns:owl="http://www.w3.org/2002/07/owl#";
     xmlns:xml="http://www.w3.org/XML/1998/namespace";
     xmlns:swrlb="http://www.w3.org/2003/11/swrlb#";
     xmlns:swrl="http://www.w3.org/2003/11/swrl#";
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#";
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#";
     xmlns:dc="http://purl.org/dc/elements/1.1/";>
    <owl:Ontology rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia";>
    </owl:Ontology>

    <!-- http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral -->

    <rdfs:Datatype 
rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral"/>

    <owl:Class 
rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia#AcuteSchizophrenia";>
        <rdfs:label xml:lang="en">Acute schizophrenia</rdfs:label>
    </owl:Class>


        <owl:NamedIndividual 
rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia";>
        <rdf:type 
rdf:resource="http://www8.cs.fau.de/research:cgm/schizophrenia#AcuteSchizophrenia"/>
    </owl:NamedIndividual>

</rdf:RDF>

{code}


  was:
Hey there,
I'm getting a _ConversionException_ when converting a resource to an individual 
by using _as_. In another bugreport 
[https://issues.apache.org/jira/browse/JENA-1380] this exception occured 
because of lacking OWL2 support. Is it something similiar here? Below is the 
relevant part of the OWL File: 
{code:xml}
    <!-- http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia 
-->

    <owl:NamedIndividual 
rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia";>
        <rdf:type 
rdf:resource="http://www8.cs.fau.de/research:cgm/schizophrenia#AcuteSchizophrenia"/>
    </owl:NamedIndividual>

{code}
The code snippet sets up a query on a OntModel which uses the Openllet 
reasoner. 
{code:java}
                private ArrayList<String> queryForRecommended(Query query){;
                OntDocumentManager mgr = new OntDocumentManager();

                //imports
                mgr.addAltEntry(Misc.STR_TIME,Misc.PATH_TIME);
                mgr.addAltEntry(Misc.STR_SCHIZO,Misc.PATH_SCHIZO);
                
                // Create OntModel
                OntModelSpec s = new 
OntModelSpec(PelletReasonerFactory.THE_SPEC);
                s.setDocumentManager(mgr);

                OntModel ontModel = ModelFactory.createOntologyModel(s);
                try{
                        ontModel.read(patient.getFile().toURI().toString());
                }catch(Exception e){
                        e.printStackTrace();
                        return null;
                }

                QueryExecution qe = QueryExecutionFactory.create(query, 
ontModel);

                ResultSet rs = qe.execSelect();

                ArrayList<String> result = new ArrayList<String>();

                while(rs.hasNext()){
                        QuerySolution qs = rs.next();
                        // ------- ERROR ---------
                        Individual in = 
qs.getResource("diag").as(Individual.class); 
                   
                        ExtendedIterator it = in.listOntClasses(true);

                        String className = "";
                        while(it.hasNext()){
                                className = it.next().toString();
                        }
                        result.add(className);
                }
                qe.close();
                return result;
        }
{code}
This is the error message I'm getting. The Jena Api mention the following: 'In 
order to be recognised as an individual, rather than a generic resource, at 
least one rdf:type statement' .
But a rdf:type statement does exist. I'm a bit clueless.
{code}
Exception in thread "main" org.apache.jena.ontology.ConversionException: Cannot 
convert node 
http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia to 
Individual
        at 
org.apache.jena.ontology.impl.IndividualImpl$1.wrap(IndividualImpl.java:61)
        at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:152)
        at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:31)
        at org.apache.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:62)
        at org.apache.jena.enhanced.EnhNode.as(EnhNode.java:107)
        at gui.OWLHandler.queryForRecommended(OWLHandler.java:655)
        [...]
{code}
It is worth mentioning that I'm refactoring this code and it worked quite well 
with the Jena version 2.1.0 and the Pellet Reasoner for Owlapiv3.


> ConversionException for individuals
> -----------------------------------
>
>                 Key: JENA-1415
>                 URL: https://issues.apache.org/jira/browse/JENA-1415
>             Project: Apache Jena
>          Issue Type: Bug
>          Components: ARQ, Jena
>    Affects Versions: Jena 3.4.0
>         Environment: Linux, Maven, Openllet Reasoner
>            Reporter: Christian Bay
>            Priority: Minor
>
> Hey there,
> I'm getting a _ConversionException_ when converting a resource to an 
> individual by using _as_.
> [EDIT] Edited this report because I could reproduce this error in a little 
> example.
> Jena throws a _ConversionErrorException_ when trying to cast a Resource with 
> _as_ to an individual. The resource is received by a query as suggested here 
> [https://github.com/Galigator/openllet/blob/integration/examples/src/main/java/openllet/examples/SPARQLDLExample.java]
> This is the test code:
> {code:java}
> package bug;
> import openllet.jena.PelletReasonerFactory;
> import openllet.query.sparqldl.jena.SparqlDLExecutionFactory;
> import org.apache.jena.ontology.OntModel;
> import org.apache.jena.query.Query;
> import org.apache.jena.query.QueryExecution;
> import org.apache.jena.query.QueryFactory;
> import org.apache.jena.query.QuerySolution;
> import org.apache.jena.query.ResultSet;
> import org.apache.jena.query.ResultSetFormatter;
> import org.apache.jena.rdf.model.ModelFactory;
> import org.apache.jena.ontology.Individual;
> import org.apache.jena.ontology.OntClass;
> import org.apache.jena.util.iterator.ExtendedIterator;
> import java.util.ArrayList;
> public class SPARQLDLBug
> {
>       // The ontology loaded as dataset
>       private static final String ontology = "ontologies/simple.owl";
>       private static final String query = "query.sparql";
>       public void run()
>       {
>                       // First create a Jena ontology model backed by the 
> Pellet reasoner
>                       // (note, the Pellet reasoner is required)
>                       final OntModel m = 
> ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
>                       // Then read the _data from the file into the ontology 
> model
>                       m.read(ontology);
>                       // Now read the query file into a query object
>                       final Query q = QueryFactory.read(query);
>                       // Create a SPARQL-DL query execution for the given 
> query and
>                       // ontology model
>                       final QueryExecution qe = 
> SparqlDLExecutionFactory.create(q, m);
>                       // We want to execute a SELECT query, do it, and return 
> the result set
>                       final ResultSet rs = qe.execSelect();
>                       ArrayList<String> result = new ArrayList<String>();
>                       while(rs.hasNext()){
>                               QuerySolution qs = rs.next();
>                                 // The Bug occurs in the next line
>                               Individual in = 
> qs.getResource("x").as(Individual.class);
>                               ExtendedIterator<OntClass> it = 
> in.listOntClasses(true);
>                               String className = "";
>                               while(it.hasNext()){
>                                       className = it.next().toString();
>                               }
>                               result.add(className);  
>                       }
>                       qe.close();
>       }
>       public static void main(final String[] args)
>       {
>               final SPARQLDLBug app = new SPARQLDLBug();
>               app.run();
>       }
> }
> {code}
> The error message is:
> {code}
> Exception in thread "main" org.apache.jena.ontology.ConversionException: 
> Cannot convert node 
> http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia to 
> Individual
>         at 
> org.apache.jena.ontology.impl.IndividualImpl$1.wrap(IndividualImpl.java:61)
>         at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:152)
>         at org.apache.jena.enhanced.EnhNode.convertTo(EnhNode.java:31)
>         at 
> org.apache.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:62)
>         at org.apache.jena.enhanced.EnhNode.as(EnhNode.java:107)
>         at bug.SPARQLDLBug.run(SPARQLDLBug.java:58)
>         at bug.SPARQLDLBug.main(SPARQLDLBug.java:75)
> {code}
> This Query is:
> {code:xml}
>  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>  PREFIX owl: <http://www.w3.org/2002/07/owl#>
>  PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
>  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
>  PREFIX bio: <http://www8.cs.fau.de/research:cgm/schizophrenia#>
>  SELECT ?x
>  WHERE { ?x rdf:type bio:AcuteSchizophrenia }
> {code}
> And the Ontology consist only of a class and one individual.
> {code:xml}
> <?xml version="1.0"?>
> <rdf:RDF xmlns="http://www8.cs.fau.de/research:cgm/schizophrenia#";
>      xml:base="http://www8.cs.fau.de/research:cgm/schizophrenia";
>      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
>      xmlns:owl="http://www.w3.org/2002/07/owl#";
>      xmlns:xml="http://www.w3.org/XML/1998/namespace";
>      xmlns:swrlb="http://www.w3.org/2003/11/swrlb#";
>      xmlns:swrl="http://www.w3.org/2003/11/swrl#";
>      xmlns:xsd="http://www.w3.org/2001/XMLSchema#";
>      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#";
>      xmlns:dc="http://purl.org/dc/elements/1.1/";>
>     <owl:Ontology 
> rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia";>
>     </owl:Ontology>
>     <!-- http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral -->
>     <rdfs:Datatype 
> rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral"/>
>     <owl:Class 
> rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia#AcuteSchizophrenia";>
>         <rdfs:label xml:lang="en">Acute schizophrenia</rdfs:label>
>     </owl:Class>
>         <owl:NamedIndividual 
> rdf:about="http://www8.cs.fau.de/research:cgm/schizophrenia#R_AcuteSchizophrenia";>
>         <rdf:type 
> rdf:resource="http://www8.cs.fau.de/research:cgm/schizophrenia#AcuteSchizophrenia"/>
>     </owl:NamedIndividual>
> </rdf:RDF>
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to