[Cubicweb] CWEP-0002 - RQL rewriting

Vincent Michel vincent.michel at logilab.fr
Fri Dec 13 17:47:05 CET 2013


Hi,


On 13/12/2013 17:26, Aurélien Campéas wrote:
> On 13/12/2013 17:11, Léa Capgen wrote:
>> Hi Cubicweb users,
>>
>> Here is the first hints about allowing users rewriting RQL.
>>
>> Léa,
>>
>
> Hi Léa,
>
>>
>> Rationale
>> ==========
>>
>>
>> Logilab has been thinking about rules and RQL rewriting for years, but
>> never had the time. Using rules in database queries is not a new topic,
>> that dates back to the 70s (read about Datalog_ for example).
>>
>> .. _Datalog: http://en.wikipedia.org/wiki/Datalog
>>
>> With rules and RQL rewriting, we could have syntactic sugar for almost
>> free and have more flexible schemas that define relations and attributes
>> that we can chose not to materialize at the SQL table level.
>>
>> Here are a few examples of use cases. A more complete document can be
>> found here:
>>
>> http://hg.logilab.org/users/lcapgen/cw_rules/file/44522d5906d4/cw-regles-utilisations.rst 
>>
>>
>>
>> Proposal
>> ========
>>
>> Relation rewriting
>> --------------------------
>>
>> Instead of::
>>
>>    Any A,B WHERE C is Contribution, C contributor A, C manifestation B,
>>                  C role R, R name "illustrator"
>>
>> we would like to write::
>>
>>    Any A,B WHERE A illustrator_of B
>>
>> after adding a rule to the schema as follows:
>>
>> .. sourcecode:: python
>>
>>      class illustrator_of(RelationDefinition):
>>          subject = 'Person'
>>          object = 'Manifestation'
>>          rule  = ('C is Contribution, C contributor S, C 
>> manifestation O,'
>>                   'C role R, R name "illustrator"')
>>
>>
>
> So is illustrator_of actually:
>
> * a pure read-only, not db-backed, kindof "virtual" relation ?
>
> * a computed relation ?
>
> * would the (default) primary views of a Person show it ? hide the
>   contributions ?
>
> * what about the edition forms ?
>
>

For now, illustrator_of is a pure read-only "virtual" relation.
We want to have two different behaviors in CW:

    * pure read-only "virtual" relation, using RQL rewrite and the rule 
defined in the schema.
      This is the mode currently implemented.

    * "materialized" relation, using a hook to automatically flush 
information in the database using the rule
      defined in the schema. The main idea here is that (using the 
previous example), modifying/deleting/adding
      a "contributor" or "manifestation" relation, will automatically 
trigger a hook that will insert the "illustrator_of"
      relation (if it make sense).
      There will be no RQL rewrite, and the rule is only used for hook 
triggering/relation computation.

>>
>> Computed attribute
>> ----------------------------
>>
>> Instead of::
>>
>>    Any SUM(SA) GROUPBY S WHERE P works_for S, P salary SA
>>
>> we would like to write::
>>
>>    Any A WHERE S total_salary A
>>
>> after adding to the schema a rule for the computed attribute as follows:
>>
>> .. sourcecode:: python
>>
>>      class Company(EntityType):
>>          name = String()
>>          total_salary = Int(computed=('Any SUM(SA) GROUPBY S WHERE P
>> works_for S, P salary SA'))
>>
>
>
> I'd ask the exact same questions, plus:
>
> * what happens if it is actually in the backend and one writes:
>   c.cw_set(total_salary=42)  ?

This is a current limitation of the Computed Attribute. Similarly to the 
relation (see "illustrator_of" example), we may want to store the result 
in the database using a specific hook, or only use RQL rewriting 
(current implementation).

>
>
>>
>>
>> Map RDF to Yams
>> -------------------------
>>
>> Given the following XY equivalences:
>>
>> .. sourcecode:: python
>>
>>     xy.add_equivalence('CWUser', 'foaf:Person')
>>     xy.add_equivalence('Person', 'foaf:Person')
>>
>> when writing::
>>
>>      Any S WHERE S is foaf:Person
>>
>> we would like the query to be rewritten as::
>>
>>      Any A WHERE A is IN(Person, User)
>>
>>
>> Implementation
>> ===============
>>
>> A first implementation is available here:
>>
>>    http://hg.logilab.org/review/rql/rev/3a74699faa12
>>
>>    http://hg.logilab.org/review/yams/rev/501931830b3a
>>
>>    http://hg.logilab.org/review/cubicweb/rev/972a53c416a8
>>
>>
>> Discussion
>> ==========
>>
>> Please comment. Known limitations can be found here:
>>
>> http://hg.logilab.org/users/lcapgen/cw_rules/file/44522d5906d4/cw-regles-limitations.rst 
>>
>
>
> Regards,
> Aurélien.

Best,

Vincent

> _______________________________________________
> Cubicweb mailing list
> Cubicweb at lists.cubicweb.org
> http://lists.cubicweb.org/mailman/listinfo/cubicweb




More information about the Cubicweb mailing list