Unresolved prefixed name: rdfs:subClassOf in SPARQ

2020-02-14 04:20发布

问题:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;
import java.util.regex.*;
import java.io.*;
import com.hp.hpl.jena.sparql.*;
import com.hp.hpl.jena.*;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;

/*
<applet code="Sample" width=275 height=200>
</applet>
*/
//creating applet
public class Sample extends Applet implements ActionListener
{
 TextField t;
 String msg= " ";
 String token[];
int s=60;
 public void init()
 {
  setBackground(Color.gray);
  Label qa=new Label("QA SYSTEM",Label.CENTER);
  Button search=new Button("Search");
  t=new TextField(25);
  //Adding text box,button in to applet
  add(qa);
  add(t);
  add(search);
  t.addActionListener(this);
  search.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
  String str=ae.getActionCommand();
  if(str.equals("Search"))
  {
   msg=t.getText() ;
   Pattern pat=Pattern.compile("[ ?]");
   String strs[]=pat.split(msg);
   for(int i=0;i<strs.length;i++)
   System.out.println("Token"+(i+1)+":"+strs[i]); 
  }

  try
  {
        //opening owl file
        InputStream in = new FileInputStream(new File("D:/ds.OWL"));
        Model model=ModelFactory.createMemModelMaker().createDefaultModel() ;
        model.read(in,null);       
        in.close();
        String queryString ="SELECT ?ds ?o WHERE {?ds  rdfs:subClassOf ?o }";

        com.hp.hpl.jena.query.Query q = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(q, model);
        ResultSet results = qe.execSelect();
        while (results.hasNext())
        {
            System.out.println(results.getRowNumber() );
        }
        ResultSetFormatter.out(System.out, results, q);
        qe.close();

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

   repaint();
 }
 public void paint(Graphics g)
 {
   g.drawString("wait...",6,80); 
 }
}

Error:

com.hp.hpl.jena.query.QueryParseException: Line 1, column 27: Unresolved prefixed name: rdfs:subClassOf

回答1:

String queryString ="SELECT ?ds ?o WHERE {?ds  rdfs:subClassOf ?o }";

Should be

String queryString ="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?ds ?o WHERE {?ds  rdfs:subClassOf ?o }";  


回答2:

As William's succinct answer implies the problem is that you haven't defined what the rdfs prefix represents. Prefixed Names in SPARQL and other related RDF standards are purely a syntactic convenience for writing queries and data in a more compact and readable way. You can assign a prefix to represent any namespace URI you want so you must always explicitly define your prefixes using the mechanism of the format you are using.

In the case of SPARQL this is the PREFIX keyword which is used to define prefixes. These definitions must appear before the main body of your query and you can have as many definitions as you want present.



标签: rdf sparql jena