This is my query and code. I have to find just one tripe: Subject
highscore by a student
Like subject English highscore is 60 by Smith.
When I run query inside Protege, it gives me the result
Smith 60 and it is fine. This result (Smith 60), I cant get inside Jena
code and get exception.
Code is below.
String queryString2=
"prefix std:<http://www.semanticweb.org/ontologies#>"+
"SELECT ?student ?highScore " +
" WHERE {" + " {"+ "select (max(?score) as ?highScore)"
+ " { ?student std:Englishscore ?score" +
" }" +
" } " +
"?student std:Englishsscore ?highScore" +
"}";
System.out.println(queryString2);
Query query2 = QueryFactory.create(queryString2);
QueryExecution qexec = QueryExecutionFactory.create(query2, model) ;
ResultSet results2 = qexec.execSelect() ;
Literal r= results2.next().get("highScore").asLiteral();
JOptionPane.showMessageDialog(null,r.toString());
On Mon, Jan 9, 2017 at 10:23 PM, Andy Seaborne <[email protected]> wrote:
> Once again you do not provide a complete, minimal example.
> And this is different code to what you showed earlier.
>
>
> std:Englishscore
> std:Englishsscore <-- if this is wrong
>
> there is no match and next() is NoSuchElementException
>
> Andy
>
> On 09/01/17 17:51, javed khan wrote:
>
>> I am sorry Andy I did not understand what you mentioned.
>>
>> The problem still there, though I have made several changes: and the
>> problem points here
>>
>> Literal r= results2.next().get("highScore").asLiteral();
>> or
>> RDFNode r2= results2.next().get("student").asResource();
>>
>> The error message is
>>
>> *Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException:
>> QueryIteratorCloseable*
>> * at
>> com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.nex
>> tBinding(QueryIteratorBase.java:9*
>>
>> 3
>>
>> On Mon, Jan 9, 2017 at 6:53 PM, Andy Seaborne <[email protected]> wrote:
>>
>> That is not the error in the query I pointed out. It makes a difference
>>> in the query.
>>>
>>>
>>> On 09/01/17 15:45, javed khan wrote:
>>>
>>> I used std:Englishscore in my query and is also in my ontology. If I
>>>> wrote
>>>> somewhere EnglishScore, it will be a typo error.
>>>>
>>>> On Mon, Jan 9, 2017 at 6:41 PM, Andy Seaborne <[email protected]> wrote:
>>>>
>>>>
>>>>
>>>>> On 09/01/17 15:05, javed khan wrote:
>>>>>
>>>>> Hello Lorenz, I think the problem is in the query itself inside Jena
>>>>>
>>>>>> code(probably the syntax of the query). Because a very simple query
>>>>>> like *?student
>>>>>> std:EnglishScore ?score*
>>>>>> works.
>>>>>> The querystring is:
>>>>>>
>>>>>> prefix std:<http://www.semanticweb.org/ontologies#>PREFIX rdfs:<
>>>>>> http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf:<
>>>>>> http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>>>>>> SELECT * WHERE { {select (max(?score) as ?highScore) { ?student
>>>>>> std:Englishscore ?score } } ?student std:Englishsscore ?highScore}
>>>>>>
>>>>>>
>>>>>> std:Englishscore vs std:Englishsscore
>>>>>
>>>>> spelt differently.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> On Mon, Jan 9, 2017 at 5:40 PM, Lorenz Buehmann <
>>>>>> [email protected]> wrote:
>>>>>>
>>>>>> Inline images are not allowed here, thus we can't see anything. And a
>>>>>>
>>>>>> screenshot of Protege is not really helpful.
>>>>>>>
>>>>>>> Prefixes might be wrong. We neither know the final query, nor your
>>>>>>> code.
>>>>>>>
>>>>>>> Please show a sample of the data in best case in Turtle format. And
>>>>>>> the
>>>>>>> final query - please not again as Java concatenated String but as the
>>>>>>> output from System.out.println(query2)
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On 09.01.2017 15:06, javed khan wrote:
>>>>>>>
>>>>>>> Inline image 1
>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> It shows the result in Protege: Mac is student name and 6 is
>>>>>>>> highscore
>>>>>>>> of English category scored by him.
>>>>>>>>
>>>>>>>> On Mon, Jan 9, 2017 at 4:34 PM, Andy Seaborne <[email protected]
>>>>>>>> <mailto:[email protected]>> wrote:
>>>>>>>>
>>>>>>>> Maybe there are no results.
>>>>>>>>
>>>>>>>> This is not a complete, minimal example. Unseen details probably
>>>>>>>> matter.
>>>>>>>>
>>>>>>>> Andy
>>>>>>>>
>>>>>>>>
>>>>>>>> On 09/01/17 13:28, javed khan wrote:
>>>>>>>>
>>>>>>>> Hello Lorenz, yes I need student and highscore.
>>>>>>>>
>>>>>>>> I just tried this , no exception but it does not display the
>>>>>>>> message in
>>>>>>>> JOptionPane.showMessageDialog()
>>>>>>>>
>>>>>>>> Query query2 = QueryFactory.create(queryString2);
>>>>>>>> QueryExecution qexec = QueryExecutionFactory.create(query2,
>>>>>>>> model) ;
>>>>>>>> ResultSet results2 = qexec.execSelect() ;
>>>>>>>>
>>>>>>>> RDFNode r;
>>>>>>>> while(results2.hasNext()){
>>>>>>>>
>>>>>>>> QuerySolution sol= results2.nextSolution();
>>>>>>>> r=sol.get("student");
>>>>>>>> JOptionPane.showMessageDialog(null,"high score is"
>>>>>>>> + r
>>>>>>>> );
>>>>>>>> }
>>>>>>>>
>>>>>>>> On Mon, Jan 9, 2017 at 1:42 PM, Lorenz Buehmann <
>>>>>>>> [email protected]
>>>>>>>> <mailto:[email protected]>> wrote:
>>>>>>>>
>>>>>>>> @ As per the previous discussion, I guess he also wants
>>>>>>>> to
>>>>>>>> have the
>>>>>>>> student - and of course the highest score,
>>>>>>>>
>>>>>>>> As Chris said, you have to distinguish between literals
>>>>>>>> and URIs/blank
>>>>>>>> nodes.
>>>>>>>>
>>>>>>>> Which finally leads me to the suggestion to read an RDF
>>>>>>>> tutorial +
>>>>>>>> SPARQL tutorial first. Most questions are really basic.
>>>>>>>>
>>>>>>>>
>>>>>>>> On 09.01.2017 09:38, Chris Dollin wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On 08/01/17 18:32, javed khan wrote:
>>>>>>>>
>>>>>>>> This query gives me exception:
>>>>>>>>
>>>>>>>> Exception in thread "AWT-EventQueue-0"
>>>>>>>> com.hp.hpl.jena.rdf.model.Lite
>>>>>>>> ralRequiredException:
>>>>>>>> http://www.semanticweb.org/t/ontologies#Smith
>>>>>>>> <http://www.semanticweb.org/t/ontologies#Smith>
>>>>>>>>
>>>>>>>> "SELECT * " +
>>>>>>>>
>>>>>>>> " WHERE {" + " {"+ "select
>>>>>>>> (max(?score) as
>>>>>>>> ?highScore)"
>>>>>>>> + " { ?student std:Englishscore ?score" +
>>>>>>>> " }" +
>>>>>>>> " } " +
>>>>>>>> "?student std:Englishscore ?highScore"
>>>>>>>> +
>>>>>>>>
>>>>>>>> "}";
>>>>>>>>
>>>>>>>>
>>>>>>>> The variable ?subject will be the subject of a
>>>>>>>> triple.
>>>>>>>> Subjects may be blank nodes or resources with URIs.
>>>>>>>>
>>>>>>>> Query query2 =
>>>>>>>> QueryFactory.create(queryString2);
>>>>>>>> QueryExecution qexec =
>>>>>>>> QueryExecutionFactory.create(query2, model) ;
>>>>>>>> ResultSet results2 = qexec.execSelect() ;
>>>>>>>>
>>>>>>>> Literal
>>>>>>>> l2=results2.next().get("student").asLiteral();
>>>>>>>>
>>>>>>>>
>>>>>>>> get("student") will return the value of "student",
>>>>>>>> which will
>>>>>>>> be a resource or blank node, but NOT a literal.
>>>>>>>> asLiteral
>>>>>>>> expects a Literal.
>>>>>>>>
>>>>>>>> BOOM.
>>>>>>>>
>>>>>>>> My pre-caffeine guess is that you want "highscore"
>>>>>>>> not
>>>>>>>> "student".
>>>>>>>> As others have said, offer complete minimal
>>>>>>>> examples.
>>>>>>>>
>>>>>>>> Chris
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>
>>