June 2014

Introduction and basics

This article is not fully finished yet, it will be extended in the future.

2 Rewriting

In the simplest form an L-system is a set of rewrite rules and a list of initial symbols (often called an axiom). In every iteration the axiom is rewritten by a set of rewrite rules to another list of symbols, which is used as an axiom in the next iteration. This iterative proces repeats until desired number of iterations is reached.

A symbol in Malsys is represented as a string of characters but it is often jsut a single character. Individual symbols are separated by whitespace characters.

This chapter covers absolute basics of rewrite rule mechanics of L-systems. Following chapter covers how symbols can be interpreted as graphical elements to draw simple fractals.

2.1 Simple rewrite rule

Let's define very simple L-system called MyFirstLsystem that will generate a sequence of symbols S. This can be done by defining some special symbol, in this case symbol A, that will be used to clone new symbols S.

Now, the cloning itself is done by rewrite rule that rewrites the symbol A to two symbols S and A. Notice that symbol A is preserved and new symbol S appeared. Other symbols S in the string remains untouched since there are no rewrite rule for them. By setting number of iterations equal to 5 the code will generate 5 symbols S followed by symbol A.

Input

1
2
3
4
5
6
lsystem MyFirstLsystem {
set symbols axiom = A;
set iterations = 5;
rewrite A to S A;
}
process all with SymbolPrinter;

Output

S S S S S A

It is very simple to print result of every iteration by setting property interpretEveryIteration to true as demonstrated in next example.

Input

1
2
3
4
5
6
7
lsystem MyFirstLsystem {
set symbols axiom = A;
set iterations = 5;
set interpretEveryIteration = true;
rewrite A to B A;
}
process all with SymbolPrinter;

Output

  1. A
  2. B A
  3. B B A
  4. B B B A
  5. B B B B A
  6. B B B B B A

2.2 Rewriting is parallel

It is possible to define any number of rewrite rules in the L-system. Their application is "parallel" as if all symbols were rewritten at the same time. This simply means that symbols that are result from one rule wont be rewriten again in the same iteration.

Input

1
2
3
4
5
6
7
8
lsystem OrderOfRulesMatters1 {
set symbols axiom = X I X I X;
set iterations = 5;
set interpretEveryIteration = true;
rewrite X to I;
rewrite I to X;
}
process all with SymbolPrinter;

Output

  1. X I X I X
  2. I X I X I
  3. X I X I X
  4. I X I X I
  5. X I X I X
  6. I X I X I

2.3 Order of rewrite rules matters

Only one rule can be applied to a particular symbol. A search for rewrite rule goes from top to bottom and ends after the first match. This means that the order of rewrite rules in an L-system definition is significant. Compare following two L-systems, the only difference is order of rewrite rules. Quite obviously the results differ.

Input

1
2
3
4
5
6
7
lsystem OrderOfRulesMatters1 {
set symbols axiom = A;
set iterations = 5;
rewrite A to B A;
rewrite A to C A;
}
process all with SymbolPrinter;

Output

B B B B B A

Input

1
2
3
4
5
6
7
lsystem OrderOfRulesMatters2 {
set symbols axiom = A;
set iterations = 5;
rewrite A to C A;
rewrite A to B A;
}
process all with SymbolPrinter;

Output

C C C C C A

2.4 Only one symbol can be rewritten by rewrite rule at once

It is not possible to create rewrite rule that rewrite groups of symbols at once. For example, it is not possible to rewrite symbols A B to B A using one rewrite rule. Every rewrite rule can rewrite only one symbol to any number symbols (including zero). However, rewriting two (or more) symbols "at once" can be achieved with more context rewrite rules L-systems. This is more advanced technique that is discussed in ??.

2.5 Next steps

This chapter is intentionally very short and coveres the very basics because rewriting bare symbols is not very fun. Following chapter coveres how symbols can be interpreted as basic geometric primitives such as lines to draw some simple fractals — that's fun.