tutorials:rgg-code-structure
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorials:rgg-code-structure [2024/10/11 12:45] – [Replacement rules] tim | tutorials:rgg-code-structure [2025/10/21 12:04] (current) – [XL Blocks - Rules] MH | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| The java blocks are framed by curly brackets '' | The java blocks are framed by curly brackets '' | ||
| - | ===== functions | + | ===== Functions |
| Even so the function declaration follows the syntax of java, the body of the function can be of both code blocks. In difference to java rgg code allows to start directly with the declaration of functions without creating a class before. (Internally the rgg file is handled as one java class). | Even so the function declaration follows the syntax of java, the body of the function can be of both code blocks. In difference to java rgg code allows to start directly with the declaration of functions without creating a class before. (Internally the rgg file is handled as one java class). | ||
| Line 17: | Line 17: | ||
| Additionally there are several functions predefined that can be used to change the behavior of the simulation. At this point we will only talk about the initialization: | Additionally there are several functions predefined that can be used to change the behavior of the simulation. At this point we will only talk about the initialization: | ||
| + | <code java> | ||
| + | protected void init () | ||
| + | [ | ||
| + | {println(" | ||
| + | Axiom ==> A(parameters.length); | ||
| + | ] | ||
| + | </ | ||
| + | The Axiom is the node that is added to the graph in the very beginning of the simulation and is used as an access point. | ||
| ===== Java blocks ===== | ===== Java blocks ===== | ||
| - | The java block allow java syntax up to java 1.6.( this is independent from the java version that is used to run GroIMP). Therefore it is possible to use features such as file reading and writing, mathematical calculations, | + | The java block allow java syntax up to java 1.6. (this is independent from the java version that is used to run GroIMP). Therefore it is possible to use features such as file reading and writing, mathematical calculations, |
| Every new rgg file also imports by default a set of library functions that are used to interact with the graph or the GroIMP platform. This functions can be explored in the function explorer in the software. You can find it on the main menu under ' | Every new rgg file also imports by default a set of library functions that are used to interact with the graph or the GroIMP platform. This functions can be explored in the function explorer in the software. You can find it on the main menu under ' | ||
| Line 45: | Line 53: | ||
| </ | </ | ||
| + | Its syntax is simple and provisional, | ||
| + | |||
| + | The simplest form of a module declaration is | ||
| + | |||
| + | <code java> | ||
| + | module SimpleModule; | ||
| + | </ | ||
| + | |||
| + | which declares a module of name SimpleModule with no fields. These simple modules can be useful for aggregations, | ||
| + | |||
| + | |||
| + | The addition of fields is done as in | ||
| + | |||
| + | <code java> | ||
| + | module ModuleWithFields (int type, String name); | ||
| + | </ | ||
| + | |||
| + | If o is an instance of ModuleWithFields, | ||
| + | |||
| + | A module is a part of the Java class hierarchy. It is possible for a module to be a subclass of another class or module, this is indicated by an extends-clause: | ||
| + | |||
| + | <code java> | ||
| + | module Sub (super.type, | ||
| + | </ | ||
| + | |||
| + | The module Sub is a subclass (submodule) of ModuleWithFields. The fields type and name are inherited from ModuleWithFields, | ||
| + | |||
| + | Sometimes, not all module parameters are to be inherited. In these cases, one has to specify the superclass constructor to be used by explicitly providing its arguments as in | ||
| + | |||
| + | <code java> | ||
| + | module Sub2 (super.name) extends ModuleWithFields (1, name); | ||
| + | </ | ||
| - | It is also possible to have modules that do not extend any other object and have no direct effect on the graph. These nodes can be useful for aggregations, | ||
| ==== Instantiation ==== | ==== Instantiation ==== | ||
| Line 68: | Line 107: | ||
| ==== Queries ==== | ==== Queries ==== | ||
| - | In GroIMP the project graph is considered to hold almost all information on the simulation. Therefore it can also be seen as a knowledge graph. To retrieve this knowledge rgg uses the [[tutorials: | + | In GroIMP the project graph is considered to hold almost all information on the simulation. Therefore it can also be seen as a knowledge graph. To retrieve this knowledge rgg uses the [[tutorials: |
| As shown in the example below in '' | As shown in the example below in '' | ||
| Line 98: | Line 137: | ||
| Since java 1.6 did not include lamda expression, an own implementation was added to rgg. | Since java 1.6 did not include lamda expression, an own implementation was added to rgg. | ||
| The syntax an the explanation can be found [[groimp-platform: | The syntax an the explanation can be found [[groimp-platform: | ||
| - | ===== XL Blocks ===== | ||
| - | While using XL rules mainly | + | |
| + | ===== XL Blocks - Rules ===== | ||
| + | |||
| + | While using XL queries | ||
| + | * Graph rules ('' | ||
| + | * String replacement rules ('' | ||
| + | * Update or Execution rules ('' | ||
| + | |||
| + | All rules have in common that we have a left and a right hand side, both are separated by one of the arrows. | ||
| + | |||
| + | |||
| + | === Graph rules === | ||
| + | |||
| + | Graph rules are indicated by the rule arrow '' | ||
| + | |||
| + | The right hand side is a semicolon-terminated list of graph statements building the replacing graph for the matched graph. While the left hand side represents a fixed graph pattern, the graph statements can build the replacing graph dynamically, | ||
| + | |||
| + | The graph statements use a stack s of nodes and two state variables n – the last created node – and e – the next edge to create – during execution. | ||
| + | |||
| + | After the execution of all graph statements, a final step is executed (if the execution has not been terminated using break): A matching non-context node instance of the left hand side, which has not been the value of an evaluated node expression of the right hand side, is deleted. Similarly, an edge matching a non-context edge pattern of the left hand side is deleted if it is not specified on the right hand side. | ||
| + | |||
| - | ==== Replacement | + | === String |
| - | The concept of an replacement rule is basically | + | The concept of an replacement rule in XL follows the ' |
| The left part can thereby be any [[tutorials: | The left part can thereby be any [[tutorials: | ||
| The right part (the production) is a collection of Nodes ( instances of either turtle commands, 3d objects or modules) which are linked by different edges. In the example below only successor- and branch-edges are used. A node that is separated to its predecessor by only a white space is added as a sucessor to the predecessor. A Node or a set of nodes framed by square brackets in that is added to the predecessor with a branch-edge. | The right part (the production) is a collection of Nodes ( instances of either turtle commands, 3d objects or modules) which are linked by different edges. In the example below only successor- and branch-edges are used. A node that is separated to its predecessor by only a white space is added as a sucessor to the predecessor. A Node or a set of nodes framed by square brackets in that is added to the predecessor with a branch-edge. | ||
| Line 122: | Line 180: | ||
| As shown above it is also possible to forward parameters from the left side to the right (similar to a parametric rewriting rule in formal systems). Moreover it is possible to use '' | As shown above it is also possible to forward parameters from the left side to the right (similar to a parametric rewriting rule in formal systems). Moreover it is possible to use '' | ||
| - | ==== Execution rule ==== | + | === Execution rule === |
| - | The left side of an execution rule is similar to the left side of the production rule. Yet the main difference is that the nodes (or pattern of nodes) found by the query are not replaced. Instead the code on the right side is applied on them. | + | The left side of an execution rule is similar to the left side of the production rule. Yet the main difference is that the nodes (or pattern of nodes) found by the query are not replaced. Instead the (Java) |
| <code java> | <code java> | ||
| Line 142: | Line 200: | ||
| l[angle]=a; | l[angle]=a; | ||
| } | } | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | There are no additional actions executed, so update rules don’t change the topological properties of the | ||
| + | host graph per se. | ||
| + | |||
| + | |||
| + | ==== Working with several Files ==== | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | In GroIMP it is possible to split your code into several files and folders to improve the structure and keep an overview. | ||
| + | <color # | ||
| + | |||
| + | |||
| + | To use function from another file this file needs to be imported similar to a java class or package. | ||
| + | Yet in the case of GroIMP all files are imported by their name without the directories, | ||
| + | <code java> | ||
| + | import parameters.YourModule; | ||
| + | </ | ||
| + | |||
| + | |||
| + | To get this a bit clearer lets assume our parameters.rgg file contains the following code: | ||
| + | <code java> | ||
| + | public static int length = 1; | ||
| + | public static float diameter = 0.1; | ||
| + | module Leaf() ==> leaf3d(1); | ||
| + | module Shoot() extends F(1); | ||
| + | </ | ||
| + | |||
| + | To import one specific object once, you can simply do '' | ||
| + | <code java> | ||
| + | protected void init () | ||
| + | [ | ||
| + | Axiom ==> parameters.Leaf(); | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | If you want to use your module several times you can also import it one in the beginning and then reuse it: | ||
| + | |||
| + | <code java> | ||
| + | import parameters.Leaf; | ||
| + | protected void init () | ||
| + | [ | ||
| + | Axiom ==> Leaf()[RL(180) Leaf()]; | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | Finally you can import all modules with the * operator: | ||
| + | <code java> | ||
| + | import parameters.*; | ||
| + | protected void init () | ||
| + | [ | ||
| + | Axiom ==> Shoot() Leaf()[RL(180) Leaf()]; | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | This can also be done with the static variables using static import: | ||
| + | <code java> | ||
| + | |||
| + | import static parameters.*; | ||
| + | protected void init () | ||
| + | [ | ||
| + | Axiom ==> F(length, | ||
| ] | ] | ||
| </ | </ | ||
tutorials/rgg-code-structure.1728643537.txt.gz · Last modified: 2024/10/11 12:45 by tim
