Changes between Version 1 and Version 2 of SemanticQuery/Variables


Ignore:
Timestamp:
Apr 17, 2015, 2:58:31 PM (10 years ago)
Author:
manualwiki
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SemanticQuery/Variables

    v1 v2  
    22 
    33= Using variables in semantic queries = 
    4 While writing semantic queries sometimes we need to refer back to a property of an entity at some earlier point. To make it clear suppose we want to write such query. A query always has the starting point {{{mods}}}. Moving on we select functions with {{{funs}}}. Here we need to filter all the functions based on its module. Here come the variables in. 
     4While writing semantic queries sometimes we need to refer back to a property of an entity at some earlier point. To make it clear suppose we want to write such query: we would like to know about functions with the same name as the module it resides in, like {{{mymod:mymod/1}}}. Lets start with all the functions of all modules: 
     5{{{ 
     6mods.funs 
     7}}} 
     8 
     9This query has starting point {{{mods}}}. Moving on we select functions with {{{funs}}}. At this point we need to filter all the functions based on its module. Here come the variables in: 
    510 
    611The problem can easily solved by the following query: 
     
    914}}} 
    1015 
    11 Here for each module the variable {{{A}}} binds the module's name, that is, the value of {{{A}}} changes. 
     16Here for each module the variable {{{A}}} binds the module's name. Unlike in Erlang, the value of {{{A}}} changes. 
    1217 
    1318In the following sections we show how to make variable bindings and present some examples of using variables in semantic queries. 
    1419 
    1520== Binding == 
    16 Variables can only be created using properties in filters. It is ''valid'' to write 
     21Variables may only be defined using properties in filters or selectors. 
     22=== Properties === 
     23It is ''valid'' to write 
    1724{{{ 
    1825mods[name=A] 
     
    2532but it is ''semantic error'' to write 
    2633{{{ 
    27 mods[A=not_a_property] 
     34mods[A=this_is_not_a_property_of_modules] 
    2835}}} 
    2936or 
     
    3239}}} 
    3340 
    34 As you may noticed, the binding operator {{{ = }}} is commutative. Also, one may interchangeably use {{{ = }}} and {{{ == }}} to bind and compare. The first {{{ = }}} or {{{ == }}} binds, the others compare.  
     41As you may noticed, the binding operator {{{ = }}} is commutative. Also, one may interchangeably use {{{ = }}} and {{{ == }}} to bind and compare. In case of variables the first {{{ = }}} or {{{ == }}} binds, the others test equality.  
    3542 
    36 The general form of binding is the following: 
     43The general form of binding to a property is the following: 
    3744{{{ 
    3845[property=Variable] 
     
    4552Variables may bind to values of any type. They can hold atoms, integers, strings etc. 
    4653 
     54=== Selectors === 
     55 
     56It is also possible to bind a variable to a selector. In this case the variable holds a database node. 
     57{{{ 
     58mods->M 
     59}}} 
     60 
     61Variable {{{M}}} can function as a selector in the rest of a query. Its semantics is to set the state of current entities to the bound entity. 
     62{{{ 
     63mods->M.funs[name=foo].M.path 
     64}}} 
     65is equivalent to 
     66{{{ 
     67mods[.funs[name=foo]].path 
     68}}} 
     69 
     70Variables bound this way can be used to filter entities. It comes handy when we would like to know all the recursive functions: 
     71{{{ 
     72mods.funs->A.calls?A 
     73}}} 
     74 
     75Of course one cannot mix variables bound to properties and selectors. The following query will make the type checker complain: 
     76{{{ 
     77mods[name=M].funs?M 
     78}}} 
     79 
    4780== Occurences == 
    4881 
    4982=== Filters === 
    50 Variables can only occur in filters. In its simplest form a semantic query is sequence of selectors and filters. When a variable binding takes place the variable can be used in any following filter. In other words, the ''scope'' of a variable is right from its binding. 
     83Variables may occur in filters. In its simplest form a semantic query is sequence of selectors and filters. When a variable binding takes place the variable can be used in any following filter. In other words, the ''scope'' of a variable is right from its binding. 
    5184{{{ 
    5285mods.funs[name=A].calls[name==A] 
     
    75108}}} 
    76109yields type error, because {{{vars.name}}} has type of string. 
     110 
     111 
     112=== At the end of a query === 
     113 
     114Ending a query with a variable make RefactorErl show the set of values of that variable. 
     115{{{ 
     116mods[name=M].M 
     117}}} 
     118Results in 
     119{{{ 
     120Mymod1 
     121  M = Mymod1 
     122Mymod2 
     123  M = Mymod2 
     124}}} 
    77125 
    78126