Changes between Version 1 and Version 2 of SemanticQuery/Variables
- Timestamp:
- Apr 17, 2015, 2:58:31 PM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
SemanticQuery/Variables
v1 v2 2 2 3 3 = 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. 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: 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 {{{ 6 mods.funs 7 }}} 8 9 This 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: 5 10 6 11 The problem can easily solved by the following query: … … 9 14 }}} 10 15 11 Here for each module the variable {{{A}}} binds the module's name , that is, the value of {{{A}}} changes.16 Here for each module the variable {{{A}}} binds the module's name. Unlike in Erlang, the value of {{{A}}} changes. 12 17 13 18 In the following sections we show how to make variable bindings and present some examples of using variables in semantic queries. 14 19 15 20 == Binding == 16 Variables can only be created using properties in filters. It is ''valid'' to write 21 Variables may only be defined using properties in filters or selectors. 22 === Properties === 23 It is ''valid'' to write 17 24 {{{ 18 25 mods[name=A] … … 25 32 but it is ''semantic error'' to write 26 33 {{{ 27 mods[A= not_a_property]34 mods[A=this_is_not_a_property_of_modules] 28 35 }}} 29 36 or … … 32 39 }}} 33 40 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.41 As 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. 35 42 36 The general form of binding is the following:43 The general form of binding to a property is the following: 37 44 {{{ 38 45 [property=Variable] … … 45 52 Variables may bind to values of any type. They can hold atoms, integers, strings etc. 46 53 54 === Selectors === 55 56 It is also possible to bind a variable to a selector. In this case the variable holds a database node. 57 {{{ 58 mods->M 59 }}} 60 61 Variable {{{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 {{{ 63 mods->M.funs[name=foo].M.path 64 }}} 65 is equivalent to 66 {{{ 67 mods[.funs[name=foo]].path 68 }}} 69 70 Variables bound this way can be used to filter entities. It comes handy when we would like to know all the recursive functions: 71 {{{ 72 mods.funs->A.calls?A 73 }}} 74 75 Of course one cannot mix variables bound to properties and selectors. The following query will make the type checker complain: 76 {{{ 77 mods[name=M].funs?M 78 }}} 79 47 80 == Occurences == 48 81 49 82 === 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.83 Variables 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. 51 84 {{{ 52 85 mods.funs[name=A].calls[name==A] … … 75 108 }}} 76 109 yields type error, because {{{vars.name}}} has type of string. 110 111 112 === At the end of a query === 113 114 Ending a query with a variable make RefactorErl show the set of values of that variable. 115 {{{ 116 mods[name=M].M 117 }}} 118 Results in 119 {{{ 120 Mymod1 121 M = Mymod1 122 Mymod2 123 M = Mymod2 124 }}} 77 125 78 126