0

I wrote the following jena query in Eclipse which produces a URI and description of a medical disorder pertaining to the inner ear.

    public static void main(String[] args) {
        String FOAF = "http://http://xmlns.com/foaf/0.1/";
        String NS = "http://philshields.altervista.org/owl/_";
        String rdf= "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        String owl= "http://www.w3.org/2002/07/owl#" ;
            String xsd= "http://www.w3.org/2001/XMLSchema#";
                String rdfs= "http://www.w3.org/2000/01/rdf-schema#";
                    String dc= "http://purl.org/dc/elements/1.1/";

        Model model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
        //model =  FileManager.get().loadModel("c:/Ontologies/ICD-10 ontologies/2017_10_26_ICD10_AM_Code Order.owl");
        model =  FileManager.get().loadModel("c:/jena/ICD.owl");
        String resultsAsString;
        String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" + 
                "PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" + 
                "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\r\n" + 
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\n" + 
                "PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" + 
                "SELECT  (str(?subject) AS ?string) ?object\r\n" + 
                "   WHERE {?subject dc:title ?object \r\n" + 
                "FILTER regex(?object,\"inner ear\",\"i\")}\r\n" + 
                "" ;
          Query query = QueryFactory.create(queryString) ;
          try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
                ResultSet results = qexec.execSelect() ;

                for ( ; results.hasNext() ; )
                {

                  //QuerySolution soln = results.nextSolution() ;
                  ResultSetFormatter.out(System.out, results, query) ;

                }

          }
                 }

It produces the correct results shown here:

-------------------------------------------------------------------------------------------------------------------------------------
| string                                                                                  | object                                  |
=====================================================================================================================================
| "http://www.semanticweb.org/philshields/ontologies/2017/8/untitled-ontology-341#H83.3"  | "Noise effects on inner ear"            |
| "http://www.semanticweb.org/philshields/ontologies/2017/8/untitled-ontology-341#S01.38" | "Open wound of inner ear"               |
| "http://www.semanticweb.org/philshields/ontologies/2017/8/untitled-ontology-341#H83.8"  | "Other specified diseases of inner ear" |
| "http://www.semanticweb.org/philshields/ontologies/2017/8/untitled-ontology-341#H83.9"  | "Disease of inner ear, unspecified"     |

I am trying to discard all of the URI and keep the fragment value which is the code number of the disorder after the hash.

I tried using SELECT (str(?subject) AS ?string) ?object, but it does not work.

unor
  • 92,415
  • 26
  • 211
  • 360
Phil
  • 31
  • 4
  • 1
    Possible duplicate of [Why does my SPARQL query return the URI of a resource instead of its name?](https://stackoverflow.com/questions/28479394/why-does-my-sparql-query-return-the-uri-of-a-resource-instead-of-its-name) – UninformedUser Oct 31 '17 at 02:47
  • 1
    Why not simply using `STRAFTER()`? – Ivo Velitchkov Oct 31 '17 at 03:14
  • @Phil, probably you could also use [`afn:localname(?subject)`](https://jena.apache.org/documentation/query/library-function.html). – Stanislav Kralin Oct 31 '17 at 05:44
  • Thanks i'll try the above suggestions and I'll get back – Phil Oct 31 '17 at 06:04
  • 3
    Thanks all, I read the duplicate post above and realised I had made a mistake in the construction of the ontology. The ICD code that I am trying to extract is a CLASS. Being a class (and its URI) is ok if you are a machine but I need to have the ICD code available to humans. I will re-construct the ontology and place an extra annotation in the class that is the code. Anyway I learnt a valuablle lesson which is 'write annotations for everything that humans may want to see'. – Phil Nov 01 '17 at 01:24

1 Answers1

1

SPARQL way:

SELECT  (strafter(str(?subject), "#") as ?fragment) WHERE
{
 #... Something...
}

JAVA way:

Resource subject = // Some way to fetch from your query;

String fragment = resource.getlocalName(); // This will return fragment
Pratik
  • 908
  • 2
  • 11
  • 34