0
public class Main {

public static void main(String args[]){



    //read an ontology

    String filename = "IOTOntology.owl";
    Model model = ModelFactory.createDefaultModel();
    OntModel model1 = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

    //over

    // Waits for the device ID
    System.out.print("Enter name of man: ");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String devID = null;
     try {
         devID = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read device ID!");
         System.exit(1);
      } 


     try {
            File file=new File(filename);
            FileInputStream reader=new FileInputStream(file);
            System.out.println("The absolute path of the file is:"+ file.getAbsolutePath());

            model.read(reader, "RDF/XML");


            // Create a SPARQL query from the given string.
            String queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
            "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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>" +
            "select ?name "+
            "where { "+
            " ?User a rdf:name"+
            "} \n ";

            Query query = QueryFactory.create(queryString);

            try ( // Execute the query and obtain results
                QueryExecution qe = QueryExecutionFactory.create(query, model)) {
                ResultSet results =  qe.execSelect();

                // Output query results
                ResultSetFormatter.out(System.out, results, query);
                qe.close();

            }
            model.close();
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }      
    }
}

I have some problem because my table is empty, it writes in command line only this:

--------
| name |
========
--------

and this is the link to my ontology: https://github.com/isidoramalkata/IOTOntology.git

Filburt
  • 17,626
  • 12
  • 64
  • 115
Isidora Bojovic
  • 101
  • 2
  • 5

1 Answers1

1

I think you need to read some more about RDF and SPARQL first...

Your query is

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>
select ?name where { 
  ?User a rdf:name
}

That doesn't make any sense:

  • You select a variable ?name that doesn't occur in the query
  • you're using a as predicate which is a shortcut for rdf:type property, which in fact assigns resources to classes, thus, you would aak for resources that are of a type rdf:name
  • which moreover doesn't exist in your ontology

Have a look at the triple pattern:

?User a rdf:name

Your data is

<owl:NamedIndividual rdf:about="file://SHOnt.owl#UserIsidora">
        <rdf:type rdf:resource="file://SHOnt.owl#User"/>
        <SHOnt:phoneNumber rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0605222024</SHOnt:phoneNumber>
        <SHOnt:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Isidora</SHOnt:name>
        <SHOnt:email rdf:datatype="http://www.w3.org/2001/XMLSchema#string">isibojovic@gmail.com</SHOnt:email>
</owl:NamedIndividual>

There is no triple in your data that matches this pattern. RDF triple means

subject predicate object

i.e.

@prefix shont: <file://SHOnt.owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
shont:UserIsidora shont:name "Isidora"^^xsd:string .

This is Turtle syntax which is what SPARQL is based on, thus, it's always good to have a look at your data in this syntax.

SPARQL query:

prefix shont: <file://SHOnt.owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?name where { 
      ?User shont:name ?name
}

As I said, you really should read an RDF tutorial first.

UninformedUser
  • 8,397
  • 1
  • 14
  • 23