You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by Miguel Bento Alves <mb...@gmail.com> on 2014/09/03 00:49:32 UTC

Re: Sparql commands in rules

Hi Andy, 

&n is illegal SPARQL, ok, but that does not means that can be used to refer a outer variable? The symbol “&” can appear in a SPARQL command (excepting in a String)? 

and the symbol “$”?

BTW, I almost finished the development of an engine to evaluate rules that combines rules terms with sparql commands. To finish, I only need to define the special char to make reference to outer variables (for now, and to develop the engine, I'm using a non-valid char).

Miguel




On 27 Aug 2014, at 18:51, Miguel Bento Alves <mb...@gmail.com> wrote:

> Hi Andy, 
> 
>> &n is illegal SPARQL.  Use ?n if you want to use the more robust rewiring of the AST (abstract syntax tree) or algebra.
>> 
> 
> I think that should be defined a special char (or char sequence) to make reference to outer variables, variables from rule terms for instance, in a Sparql command declared in a rule. In this way, it will be clear when we want to make reference to outer variables. If we use the same char as variables (?), inadvertently  can be made reference to a outer variable and the system undesirably replace the variable by a value. 
> 
> Furthermore, and in the best of my knowledge (correct me if I’m wrong), in SPARQL command, a subquery cannot make reference to outer variables in the query pattern. If I use the same symbol of variables to make reference to outer variables, seems that my sparql command has a "special behaviour". In my point of view, a command SPARQL in a rule should have the same behaviour as a subquery in a sparql command, except that we can introduce a way to make reference to outer variables. 
> 
> I hope that I have clearly exposed my idea. Otherwise, I will try to explain better. 
> 
> have you any suggestion for the special char (or char sequence)? 
> 
> 
> Miguel
> 
> 
> 
> On 19 Aug 2014, at 16:15, Andy Seaborne <an...@apache.org> wrote:
> 
>> On 19/08/14 16:03, Miguel Bento Alves wrote:
>>> Andy,
>>> 
>>> "Another approach is to use a named variable, parse the SPARQL query, which identifies variables, then rewrite the parsed structure to replace occurrences of variable 'n' by a specific value.”
>>> 
>>> in a first glance, it's what I expect to do. If I have a variable ?n instantiated, I will lookup for &n and replace.
>> 
>> That's string processing.
>> 
>> &n is illegal SPARQL.  Use ?n if you want to use the more robust rewiring of the AST (abstract syntax tree) or algebra.
>> 
>> 	Andy
>> 
>>> 
>>> I read carefully your suggestions but I need to start to have a well founded opinion. I will start only in next week.
>>> 
>>> Miguel
>>> 
>>> 
>>> On 19 Aug 2014, at 14:36, Andy Seaborne <an...@apache.org> wrote:
>>> 
>>>> On 18/08/14 12:17, Miguel Bento Alves wrote:
>>>>> Dear all,
>>>>> 
>>>>> I will start to develop an engine to evaluate rules that combines SPARQL commands with rule terms. The purpose is to be possible to define rules like:
>>>>> 
>>>>> ex:SportsMan ex:minimumSportsPlayed 1 .
>>>>> ex:IronMan ex:minimumSportsPlayed 3 .
>>>>> 
>>>>> 
>>>>> (?x rdf:type ?t) <-
>>>>> 	?t ex:minimumSportsPlayed ?n .
>>>>> 	(\\\SPARQL 	
>>>>> 		 (Select ?x 	
>>>>> 		 Where {	
>>>>> 			 ?x ex:playSport ?y . 	
>>>>> 		 } 	
>>>>> 		 group by ?x 	
>>>>> 		 having (count(1) >=&n)
>>>>> 	 \\\SPARQL).	
>>>>> 	
>>>>> Note: in a first approach syntax "&n" is to make reference to ?n .
>>>>> 
>>>>> my question is: how we should make reference to external variables in a Sparql command?
>>>>> 
>>>>> To start the discussion, I have two proposals:
>>>>> 	the symbol "&";
>>>>> 	keep the symbol "?", where some variables must be instantiated before the execution of the sparql command. Something like:
>>>>> 	 (?x rdf:type ?t) <-
>>>>> 		?t ex:minimumSportsPlayed ?n .
>>>>> 		(\\\SPARQL 	
>>>>> 			 (Select ?x 	
>>>>> 			 Where {	
>>>>> 				 ?x ex:playSport ?y . 	
>>>>> 			 } 	
>>>>> 			 group by ?x 	
>>>>> 			 having (count(1) >=?n)
>>>>> 		 \\\SPARQL).	
>>>>> 	
>>>>> 	
>>>>> Miguel
>>>>> 
>>>> 
>>>> Using & means you need to process the query string and so need to be careful of & used elsewhere (e.g. in a string, in the '&&' operator).
>>>> 
>>>> Another approach is to use a named variable, parse the SPARQL query, which identifies variables, then rewrite the parsed structure to replace occurrences of variable 'n' by a specific value.
>>>> 
>>>> If "?t ex:minimumSportsPlayed ?n" has multiple matches, it would need to loop on ?n.
>>>> 
>>>> I do have some code that rewrites the abstract syntax tree:
>>>> 
>>>> https://github.com/afs/AFS-Dev/tree/master/src/main/java/element
>>>> 
>>>> which you're free to copy and use.
>>>> 
>>>> Or you can do it on the Algebra -- see com.hp.hpl.jena.sparql.core.Substitute(Op, Binding)
>>>> 
>>>> If you know "?t ex:minimumSportsPlayed ?n" is from the data, you may be able to convert that part and the SPARQL query into a single algebra expression bu joining the two parts, and execute the whole right-hand side as SPARQL.
>>>> 
>>>> 	Andy
>>>> 
>>> 
>> 
> 


Re: Sparql commands in rules

Posted by Miguel Bento Alves <mb...@gmail.com>.
Ok, Thanks (and my apologies for not having remembered to search in specifications). 

Miguel

On 04 Sep 2014, at 12:19, Andy Seaborne <an...@apache.org> wrote:

> On 03/09/14 15:02, Miguel Bento Alves wrote:
>> Hi Andy,
>> 
>> My idea is replace any “&” by a internal valid variable, ensuring that this variable does not exist in command. For instance,
>> …
>> 			 group by ?x 	
>> 			 having (count(1) >=&n)
>> 		 \\\SPARQL).
>> ...
>> 
>> will be replaced by
>> 
>> 			 group by ?x 	
>> 			 having (count(1) >=?OV_A8y7AbAA)
>> 		 \\\SPARQL).
>> 
>> After I parse the command and I extract all variables used. If miss some variable that I replace I assume that was done a bad replacement (in a string, for instance) and this replacement is canceled.
>> 
>> 
>>> An alternative is to replace occurrence of a named variable, ?n
>> I start by this approach but seems very difficult and not clear what is external variables and what is not.
>> 
>> It will be very useful for an answer to this question:
>> Which are the valid chars for a variable name? Is there any place where this is defined?
> 
> http://www.w3.org/TR/sparql11-query/#rVARNAME
> 
>> 
>> Miguel
>> 
>> 
>> On 03 Sep 2014, at 11:38, Andy Seaborne <an...@apache.org> wrote:
>> 
>>> On 02/09/14 23:49, Miguel Bento Alves wrote:
>>>> Hi Andy,
>>>> 
>>>> &n is illegal SPARQL, ok, but that does not means that can be used to
>>>> refer a outer variable? The symbol “&” can appear in a SPARQL command
>>>> (excepting in a String)?
>>> 
>>> If yo udo it that way, then you must replace the &n before passing the string to the SPARQL parser otherwise it won't parse.
>>> 
>>> An alternative is to replace occurrence of a named variable, ?n
>>> 
>>> It depends on the details of "refer to an outer variable" means.
>>> 
>>> If it is that the outer variable can have a number of values then it's either a join of the results of the query and a table of values from the rest of the rule matching or it's a repeated execution with different values of ?n.
>>> 
>>> Straight substitution of ?n for a string value, passing to the parser and executing is close but can end up in different results depending on whether ?n is used nested inside the query, either unprojected from a subquery  (it's a different variable) or in nested OPTIONALs (substitution can violates bottom-up execution semantics).
>>> 
>>> It's easier to let the execution engine worry about this.
>>> 
>>> You can use an initial binding for repeated execution.  That avoids the nested different definition problems.  Or add a VALUES clause to the query.
>>> 
>>>> 
>>>> and the symbol “$”?
>>>> 
>>>> BTW, I almost finished the development of an engine to evaluate rules
>>>> that combines rules terms with sparql commands. To finish, I only
>>>> need to define the special char to make reference to outer variables
>>>> (for now, and to develop the engine, I'm using a non-valid char).
>>>> 
>>>> Miguel
>>>> 
>>> 
>>> 	Andy


Re: Sparql commands in rules

Posted by Andy Seaborne <an...@apache.org>.
On 03/09/14 15:02, Miguel Bento Alves wrote:
> Hi Andy,
>
> My idea is replace any “&” by a internal valid variable, ensuring that this variable does not exist in command. For instance,
> …
> 			 group by ?x 	
> 			 having (count(1) >=&n)
> 		 \\\SPARQL).
> ...
>
> will be replaced by
>
> 			 group by ?x 	
> 			 having (count(1) >=?OV_A8y7AbAA)
> 		 \\\SPARQL).
>
> After I parse the command and I extract all variables used. If miss some variable that I replace I assume that was done a bad replacement (in a string, for instance) and this replacement is canceled.
>
>
>> An alternative is to replace occurrence of a named variable, ?n
> I start by this approach but seems very difficult and not clear what is external variables and what is not.
>
> It will be very useful for an answer to this question:
> Which are the valid chars for a variable name? Is there any place where this is defined?

http://www.w3.org/TR/sparql11-query/#rVARNAME

>
> Miguel
>
>
> On 03 Sep 2014, at 11:38, Andy Seaborne <an...@apache.org> wrote:
>
>> On 02/09/14 23:49, Miguel Bento Alves wrote:
>>> Hi Andy,
>>>
>>> &n is illegal SPARQL, ok, but that does not means that can be used to
>>> refer a outer variable? The symbol “&” can appear in a SPARQL command
>>> (excepting in a String)?
>>
>> If yo udo it that way, then you must replace the &n before passing the string to the SPARQL parser otherwise it won't parse.
>>
>> An alternative is to replace occurrence of a named variable, ?n
>>
>> It depends on the details of "refer to an outer variable" means.
>>
>> If it is that the outer variable can have a number of values then it's either a join of the results of the query and a table of values from the rest of the rule matching or it's a repeated execution with different values of ?n.
>>
>> Straight substitution of ?n for a string value, passing to the parser and executing is close but can end up in different results depending on whether ?n is used nested inside the query, either unprojected from a subquery  (it's a different variable) or in nested OPTIONALs (substitution can violates bottom-up execution semantics).
>>
>> It's easier to let the execution engine worry about this.
>>
>> You can use an initial binding for repeated execution.  That avoids the nested different definition problems.  Or add a VALUES clause to the query.
>>
>>>
>>> and the symbol “$”?
>>>
>>> BTW, I almost finished the development of an engine to evaluate rules
>>> that combines rules terms with sparql commands. To finish, I only
>>> need to define the special char to make reference to outer variables
>>> (for now, and to develop the engine, I'm using a non-valid char).
>>>
>>> Miguel
>>>
>>
>> 	Andy
>


Re: Sparql commands in rules

Posted by Miguel Bento Alves <mb...@gmail.com>.
Hi Andy, 

My idea is replace any “&” by a internal valid variable, ensuring that this variable does not exist in command. For instance, 
…
			 group by ?x 	
			 having (count(1) >=&n)
		 \\\SPARQL).
...

will be replaced by 

			 group by ?x 	
			 having (count(1) >=?OV_A8y7AbAA)
		 \\\SPARQL).

After I parse the command and I extract all variables used. If miss some variable that I replace I assume that was done a bad replacement (in a string, for instance) and this replacement is canceled. 


> An alternative is to replace occurrence of a named variable, ?n
I start by this approach but seems very difficult and not clear what is external variables and what is not. 

It will be very useful for an answer to this question:
Which are the valid chars for a variable name? Is there any place where this is defined?

Miguel


On 03 Sep 2014, at 11:38, Andy Seaborne <an...@apache.org> wrote:

> On 02/09/14 23:49, Miguel Bento Alves wrote:
>> Hi Andy,
>> 
>> &n is illegal SPARQL, ok, but that does not means that can be used to
>> refer a outer variable? The symbol “&” can appear in a SPARQL command
>> (excepting in a String)?
> 
> If yo udo it that way, then you must replace the &n before passing the string to the SPARQL parser otherwise it won't parse.
> 
> An alternative is to replace occurrence of a named variable, ?n
> 
> It depends on the details of "refer to an outer variable" means.
> 
> If it is that the outer variable can have a number of values then it's either a join of the results of the query and a table of values from the rest of the rule matching or it's a repeated execution with different values of ?n.
> 
> Straight substitution of ?n for a string value, passing to the parser and executing is close but can end up in different results depending on whether ?n is used nested inside the query, either unprojected from a subquery  (it's a different variable) or in nested OPTIONALs (substitution can violates bottom-up execution semantics).
> 
> It's easier to let the execution engine worry about this.
> 
> You can use an initial binding for repeated execution.  That avoids the nested different definition problems.  Or add a VALUES clause to the query.
> 
>> 
>> and the symbol “$”?
>> 
>> BTW, I almost finished the development of an engine to evaluate rules
>> that combines rules terms with sparql commands. To finish, I only
>> need to define the special char to make reference to outer variables
>> (for now, and to develop the engine, I'm using a non-valid char).
>> 
>> Miguel
>> 
> 
> 	Andy


Re: Sparql commands in rules

Posted by Andy Seaborne <an...@apache.org>.
On 02/09/14 23:49, Miguel Bento Alves wrote:
> Hi Andy,
>
> &n is illegal SPARQL, ok, but that does not means that can be used to
> refer a outer variable? The symbol “&” can appear in a SPARQL command
> (excepting in a String)?

If yo udo it that way, then you must replace the &n before passing the 
string to the SPARQL parser otherwise it won't parse.

An alternative is to replace occurrence of a named variable, ?n

It depends on the details of "refer to an outer variable" means.

If it is that the outer variable can have a number of values then it's 
either a join of the results of the query and a table of values from the 
rest of the rule matching or it's a repeated execution with different 
values of ?n.

Straight substitution of ?n for a string value, passing to the parser 
and executing is close but can end up in different results depending on 
whether ?n is used nested inside the query, either unprojected from a 
subquery  (it's a different variable) or in nested OPTIONALs 
(substitution can violates bottom-up execution semantics).

It's easier to let the execution engine worry about this.

You can use an initial binding for repeated execution.  That avoids the 
nested different definition problems.  Or add a VALUES clause to the query.

>
> and the symbol “$”?
>
> BTW, I almost finished the development of an engine to evaluate rules
> that combines rules terms with sparql commands. To finish, I only
> need to define the special char to make reference to outer variables
> (for now, and to develop the engine, I'm using a non-valid char).
>
> Miguel
>

	Andy