Changes between Version 18 and Version 19 of SemanticQuery


Ignore:
Timestamp:
Sep 17, 2012, 12:48:35 PM (12 years ago)
Author:
manualwiki
Comment:

update (in comment)

Legend:

Unmodified
Added
Removed
Modified
  • SemanticQuery

    v18 v19  
     1{{{#!comment 
    12[[PageOutline]] 
    23 
     
    4142 
    4243=== Entities === 
     44Entities correspond to the semantic units of Erlang. The result of a query written in the language is a set of entities. Each element of a set belongs to the same type. We have the following entity types: file, function, clause, variable, macro, record, record 
     45field expression. Each entity type has a set of selectors and properties defined for them. For details, see SemanticQuery/Components. 
     46 
     47=== Selectors === 
     48Selectors are binary relations between entities. The entities belong to one of the previously mentioned entity types. A selector selects a set of entities that meet given requirements for each entity. 
     49 
     50''Example:'' You can select the functions defined in a given module. In that case the selection is a relation between modules and functions. 
     51{{{ 
     52@mod.functions 
     53}}} 
     54 
     55==== Initial selectors ==== 
     56Initial selectors get the current file and position as their parameters and return a set of entities as result. The entities of 
     57the result belong to the same type, but the type can not always be determined in advance, it depends on the parameters. Almost all of them begin with the character {{{@}}} to indicate that they depend on a position. 
     58 
     59For example, the initial selector {{{@variable}}} will look for a variable at the given position. If no variable can be found the result will be empty. Besides the position based initial selectors there is another initial selector: {{{mods}}}. This selector returns all of the modules that are loaded into the semantic program graph. 
     60 
     61=== Properties === 
     62Properties are functions that give the value of the property for an entity. The main purpose of properties is to filter sets of entities using them, but their values can be queried too. To query the value of a property you have to use the name of the property at the end of a semantic query. 
     63 
     64''Example:'' To query the value of the property {{{exported}}} for the functions of the given module: 
     65{{{ 
     66@module.functions.exported 
     67}}} 
     68 
     69==== Statistics ==== 
     70 
     71For properties with numeric values statistics are also available. Using these for the results of metric queries can give more 
     72information than a simple list of values. 
     73 
     74''Example:'' To query the average length of the functions of the given module: 
     75{{{ 
     76@file.functions.line_of_code:average 
     77}}} 
     78 
     79=== Filters === 
     80A filter is a boolean expression to select subsets of entities.   
     81After applying a filter, the result contains the elements of the original  
     82set where this boolean expression is true. Building filters is possible using atoms,  
     83strings, integers, properties and embedded queries. The use of strings and integers is  
     84unambiguous, but the names of properties are atoms, so it is checked for each atom if they are properties or not. 
     85 
     86Atoms, strings, integers and properties can be used in comparisons.  
     87The language uses {{{/=, ==, >=, =<, <, >}}} and {{{~}}}.  
     88The results of comparisons are the same as in Erlang.  
     89The resulting expressions can be combined by {{{and}}}, {{{or}}}, and {{{not}}} operators, and parentheses can be used, too.  
     90The {{{~}}} operator is a regular expression matching operator, and it can be used anywhere, 
     91 where other binary comparison operators can be used. The same expressions can be used, which can be used in the {{{re}}} module. 
     92 
     93The operator precedence for the filters is as follows: 
     94 
     95||||   '''Operator precedence (decreasing)'''   || 
     96||{{{not}}} ||unary ||  
     97||{{{/=, ==, >=, =<, <, >, =:=, =/=, ~}}} ||left associative ||  
     98||{{{and}}} ||left associative || 
     99||{{{or}}} ||left associative || 
     100 
     101''Example:'' you may be interested in all the exported functions of a given module,  
     102or the functions with {{{0}}} arity, or maybe a combination of these:  
     103the exported functions with {{{0}}} arity. In the example exported and arity are  
     104both properties of functions and by using them it is possible to build a filter to select the required subset of functions. 
     105{{{ 
     106@module.functions[arity==0 and exported] 
     107}}} 
     108 
     109''Example:'' you may be interested in all the module, whose name do not start with {{{test}}}. 
     110{{{ 
     111mods[name ~ "[^test].*" ] 
     112}}} 
     113 
     114 
     115==== Embedded queries ==== 
     116Embedded queries can be used to query information about entities that is  
     117otherwise unavailable, that is it can not be expressed by the help of properties. 
     118 
     119For example, we may need the functions with variables named {{{File}}}.  
     120This information can not be expressed with the help of properties.  
     121Without embedded queries it is only possible to query the variables named {{{File}}}  
     122and query the functions containing these variables after that, with the following query: 
     123{{{ 
     124mods.functions.variables[name=="File"].function_definition 
     125}}} 
     126Embedded queries make it possible to use these kind of queries effectively,  
     127without the need to continue with the query directly. The continuation of the query is in the filter,  
     128used like a property with a boolean value. The value is considered true if  
     129the result of the query is not empty. For the previous example using the following query will give the desired results. 
     130{{{ 
     131@mods.functions[.variables[name=="File"]] 
     132}}} 
     133 
     134=== Iteration === 
     135 
     136Iteration in the language means the repeated application of a query sequence.  
     137The queries are relations and a sequence of queries is a composition of these queries.  
     138Using iteration is possible if the domain and codomain of the query sequence is the same.  
     139The application is repeated exactly {{{int}}} times. 
     140 
     141The result shown in this case is not only the result of the  
     142iteration but the partial results also, in the form of chains. 
     143 
     144''Example:'' 
     145{{{ 
     146@function.{calls}3 
     147}}} 
     148The result is the same set of entities as of {{{@function.calls.calls.calls}}}.  
     149The result shown in the first case will give more information: it gives the call  
     150chains with the maximum length of 3 starting from a given function. 
     151 
     152=== Transitive closure === 
     153 
     154Transitive closure in the language means the closure of a query sequence.  
     155The query sequence here is the same as in iteration, a 
     156binary relation with the same domain and codomain. 
     157 
     158''Example:'' 
     159{{{ 
     160@function.(calls)+ 
     161}}} 
     162The result shown after this semantic query is the list of all possible call  
     163chains starting from a given function. 
     164 
     165}}} 
     166 
     167[[PageOutline]] 
     168 
     169= Related pages = 
     170 
     171This page describes how semantic queries are constructed. 
     172You may be looking for the following related pages. 
     173 
     174* [SemanticQuery/Components Available components]: lists the names of currently available initial selectors, selectors and properties and their possible abbreviations and synonyms. 
     175* [SemanticQuery/Examples Examples]: basic and advanced query examples, and queries for checking coding conventions. 
     176 
     177 
     178= Querying syntactic and semantic information = 
     179 
     180A semantic query language was designed to query syntactic and semantic information about Erlang programs. The language concepts are defined according to the semantic units and relationships of the Erlang language, e.g. functions and function calls, records and their usage, etc.  
     181 
     182The main elements of the language are the '''entities''': module, function, variable, etc. Each entity has a set of selectors and properties. A '''selector''' selects a set of entities that meet the given requirements. A '''property''' describes some properties of an entity type. It is also possible to filter entities based on the properties. A '''filter''' is a boolean expression to select a subset of entities. We can build filters by using properties with boolean values, valid Erlang comparisons, logical operators or embedded queries. For usage examples, see SemanticQuery/Examples. 
     183 
     184== Formal syntax of the language == 
     185 
     186{{{ 
     187semantic_query    ::= initial_selection ['.' query_sequence] 
     188query_sequence    ::= query ['.' query_sequence] 
     189query             ::= selection | iteration | closure | property_query 
     190initial_selection ::= initial_selector ['[' filter ']'] 
     191selection         ::= selector ['[' filter ']'] 
     192iteration         ::= '{' query_sequence '}' int ['[' filter ']'] 
     193closure           ::= '(' query_sequence ')' int ['[' filter ']'] | 
     194                      '(' query_sequence ')+' ['[' filter ']'] 
     195property_query    ::= property ['[' filter ']'] 
     196}}} 
     197 
     198A semantic query is a sequence of queries starting with an initial selector and an optional filter. Queries are 
     199separated with dots.  
     200A query is a 
     201* selection (calculates the relationship with other entities based on selectors) 
     202* iteration (iterates a query n times) 
     203* closure (calculates the transitive closure of a query sequence) 
     204* property query (selects a property of an entity) 
     205 
     206== Language elements == 
     207 
     208=== Entities === 
    43209Entities correspond to the semantic units of Erlang. The result of a query written in the language is a set of entities. Each element of a set belongs to the same type. We have the following entity types: file, function, variable, macro, record, record 
    44210field expression. Each entity type has a set of selectors and properties defined for them. For details, see SemanticQuery/Components.