see also: [[xl_programming_language:queries|Graph queries in XL]]
In the following a set queries is listed and the results of them applied on the graph in the image. This graph is created by the turtle query: ''A[F A][R F A] F A ;''
{{:tutorials:graph_query_example.png?400|}}
|Query | Result | Description |
|**Basic** |||
|A or a:A | {a1,a2,a3,a4} | addresses all instances of module A |
|a:A,a.len==1 or A(1) | all A with len==1 | unary predicates |
|**Node based path pattern** |||
|(* A F *) A | {a3} | direct combination of nodes |
|(* F *) A | {a2,a3,a4} | other example |
|**Edge based path pattern** |||
|**ANY_EDGE** |||
| %%(* A --> *) F %% | {f1,f2} | next node |
| %%A (*<-- F*) %% | {a2,a3,a4} | prev node |
| %%(* F -- *) A %% | {a1,a2,a3,a4} | undirected |
|**SUCCESSOR_EDGE** |||
| %%(* A > *) F %% | {f2} | next node |
| %%A (* < F *) %% | {a2,a3,a4} | prev node |
| %%(* F --- *) A %% | {a1,a2,a3,a4} | undirected |
|**BRANCH_EDGE** |||
| %%(*A +>*) F %% | {f1} | next node |
| %%A (* <+ F *) %% | {} | prev node |
| %%(*F -+- *) A %% | {a1} | undirected |
|**Single Match, Late Match and Optional Patterns** |||
| %%A (: --> F)%% | {f1} | find first pattern |
| %%A (& --> F)%% | {f1} | find last pattern |
| %%A (? +> F)%% | {Null} | changes nothing to NULL |
|**Transitive closures** |||
| %%(* A > > *) A %% | {a3} | Path described by single edges |
| %%(* A +> > *) A %% | {a2} | other example |
| %%(* A (-->)* *) A %% | {a1,a2,a3,a4} |(0-to-n edges possible) |
| %%(* A (-->)+ *) A %% | {a2,a3,a4} | (1-to-n edges possible) |
| %%(* A (-->)? *) F %% | {f1,f2} | (0-to-1 edges possible) |
| %%(* A (-->){2} *)F %% | {f2} | 2 edges |
| %%(* A (-->){1,2} *) A %% | {a2,a3} | min 1 max 2 edges |
|**Combined** |||
| %%f:F, A +> f, A < f %% | {f1} | patterns combined by comma |
====== Additional information ======
===== Field based conditions=====
{{:groimp-platform:parametric_example_graph_queries.png?direct&400 |}}
|Query | Result | Description |
| A(2,2.3)| a1 | all fields are defined |
| A(2,) | a1,a3,a4| only the first field is defined and considered |
| A(2,x),(x>2) | a1,a3 | the first field is set, the second is a query variable used in a condition |
| A(,x),(x>2) | a1,a2,a3 | the first field undefined, the second is a query variable used in a condition |
| A(x,y),(x>y) | a4 | both fields are query variables used in the same condition |
The condition part behind the comma can be replaced by any boolean condition, including predefined functions that return a boolean value.
The query variables defined here can also be used in the production of the rewriting rule or in followup expressions of a lambda expression.
===== Queries as query conditions =====
As said above a query condition can be any boolean condition, this includes also queries and lambda expressions, as long as the return a boolean value for example the [[groimp-platform:xl-builtin-methods|Analytical Operator]] empty(), here used to find the last F of each branch and remove it using a [[:groimp-platform:xl-rules |SPO rule]].
[f:F,(empty((*f (-->)+ F*))) ==>>;]
Or a query as a part of a boolean condition, for example used with the count operator to get the order of an internode in a tree model:
(*f:F,(count((*f ((<)*<+)+F*))==0)*) //all F of order 0
(*f:F,(count((*f ((<)*<+)+F*))==1)*) //all F of order 1
The inner query of this is explained below.
===== Transitive closures =====
As shown above transitive closure can be used to define a range of edges between two nodes, e.g. ''%% A (>)* B %%'' for all B with A successor of an A. This can be combined with other edges: ''%% A (>)* +> B %%'' to show
| query | description |
| ''%% A (>)+ B %%'' | All B's that have a A above connected only by successor edges |
| ''%% A (>)* +> B %%'' | All B's that have a A above connected only by successor edges and one branch-edge directly before the B|
| ''%% A ((>)* +>)+ B %%'' | All B's where the edge pattern of as many successors and one branch, comes at least once between the B and an A|
| ''%% A (>)* +> (>)* B %%'' | As many sucessors followed by a branch followed by as many successors |
| ''%% A (-->)* C (-->)* B %%''| All B' that have an ancestor of the type A and an ancestor of the type C with C between the A and the B |
Using transitive closure it is possible to find the same node several times!!! for instance in a graph
%%Axiom ==> A A A B;%%
the pattern (*A (>)* B*) would return B three times.