User Tools

Site Tools


01_user_documentation:07_rgg_xl:02_xl:08_object:02_pattern:01_inrgg:01_declare_inout

Declaring In/Out

Custom patterns requires to declare variable with both annotations @In and @Out. It can be two different variables, or the same one. E.g.

class patternName(@In Node a, @Out Node b) ( ) // different variables for @In and @Out
class patternName(@In @Out Node a) ( ) // same variable for both @In and @Out

The @In variable is mapped to the incoming pattern, the @Out variable to the outgoing pattern. If @In and @Out is the same variable, the variable is mapped to both the incoming and outgoing patterns.

Different In/ Out

In the case when the @In and @Out are different variable, the pattern can only be used as a path pattern. In this case the syntax is -patternName-> (or <-p-, <-p-> for backward and bidirectional). The output of the previous pattern is mapped to the @In and the input of the following pattern is mapped to the @Out variables.

The three cases are:

  1. forward: @In -p-> @Out
  2. backward: @Out <-p- @In
  3. bidirectional: (same as forward i.e.) @In <-p-> @Out

The direction of the pattern only correspond to the @In and @Out mapping, not to the direction of possible edges. For instance, the following custom pattern p matches to the same value as Node < Node :

class p(@In Node a, @Out Node b) ( a < b )
void rule()[
 n1:Node -p-> n2:Node ::>; // this matches to n1 < n2
]

Same In/ Out

In the case where both @In and @Out are the same variable, the pattern can be used either as:

  • a node pattern: Node p Node , or p (node patterns do not requires patterns before/ after)
  • a path pattern where @In and @Out is the same object.
    • forward: @In@Out -p-> Node . Node is optional.
    • backward: Node <-p- @In@Out . Node is optional.
    • bidirectional: @In@Out <-p-> Node . Node is optional.

When used as a node pattern, the custom pattern is integrated to the query like any other node pattern. Thus, it is connected with the pattern before and after with edges pattern (either implicitly or explicitly).

class p(@In @Out Node n) ( n )
void rule()[
 p ::>; // node pattern can be used a standalone
 Node p Node ::>; // Implicitly add edges pattern between Node and P, and between p and Node (the 2nd one)
 Node -p-> Node ::>; // as path pattern
]

In/ Out Type match

When used as path pattern, custom pattern automatically match the incoming and outgoing matches with the Type of the declared variable (respectively the @In and @Out). Thus, class p(@In A a, @Out B b) can only takes as @In nodes of type A. The matching of the type is automatically done by the pattern matcher. If the Type of the pattern that is mapped to the @In (same for the @Out) do not intersect the Type of the @In declared variable, the compiler will throw an error.

module A;
module B extends A;
class p(@In A a, @Out B b) ( a > b )
void rule()[
 A -p-> B ::>; // OK
 B -p-> A ::>; // Not OK. Compile error -> A cannot be cast to B
 B -p-> B ::>; // OK -> B extends A
]
01_user_documentation/07_rgg_xl/02_xl/08_object/02_pattern/01_inrgg/01_declare_inout.txt · Last modified: 2025/09/03 15:56 by gaetan