Skip to content

Commit

Permalink
#880: Use code snippets for code examples.
Browse files Browse the repository at this point in the history
Signed-off-by: Franz Wilhelmstötter <franz.wilhelmstoetter@gmail.com>
  • Loading branch information
jenetics committed Aug 31, 2023
1 parent 0879c55 commit a7adc7c
Show file tree
Hide file tree
Showing 134 changed files with 745 additions and 745 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/io/jenetics/gradle/Colorizer.java
Expand Up @@ -47,10 +47,10 @@ public final class Colorizer extends SimpleFileVisitor<Path> {

private static final Charset CHARSET = UTF_8;

// Original start tag: <pre>{@code
// Original start tag: {@snippet lang="java":
private static final String START_TAG = "<pre><code>";

// Original end tag: }</pre>
// Original end tag: }
private static final String END_TAG = "</code></pre>";

private File _baseDir;
Expand Down
Expand Up @@ -31,21 +31,21 @@
/**
* Setup for a (μ, λ)-Evolution Strategy. Applying this setup is done in the
* following way.
* <pre>{@code
* {@snippet lang="java":
* final var engine = Engine.builder(problem)
* .setup(new MLEvolutionStrategy<>(μ, λ, p)
* .build();
* }</pre>
* }
*
* And is equivalent to the following builder setup.
* <pre>{@code
* {@snippet lang="java":
* final var engine = Engine.builder(problem)
* .populationSize(λ)
* .survivorsSize(0)
* .offspringSelector(new TruncationSelector<>(μ))
* .alterers(new Mutator<>(p))
* .build();
* }</pre>
* }
*
* @param <G> the gene type
* @param <C> the fitness result type
Expand Down
Expand Up @@ -31,21 +31,21 @@
/**
* Setup for a (μ + λ)-Evolution Strategy. Applying this setup is done in the
* following way.
* <pre>{@code
* {@snippet lang="java":
* final var engine = Engine.builder(problem)
* .setup(new MpLEvolutionStrategy<>(μ, λ, p)
* .build();
* }</pre>
* }
*
* And is equivalent to the following builder setup.
* <pre>{@code
* {@snippet lang="java":
* final var engine = Engine.builder(problem)
* .populationSize(λ)
* .survivorsSize(μ)
* .offspringSelector(new TruncationSelector<>(μ))
* .alterers(new Mutator<>(p))
* .build();
* }</pre>
* }
*
* @param <G> the gene type
* @param <C> the fitness result type
Expand Down
Expand Up @@ -35,10 +35,10 @@ public interface TreeChromosome<A, G extends TreeGene<A, G>>
/**
* Return the root gene of this chromosome. Since the gene type is also a
* {@link io.jenetics.ext.util.Tree}, you are able to assign it to one.
* <pre>{@code
* {@snippet lang="java":
* final Tree<A, ?> t1 = root();
* final Tree<?, ?> t2 = root();
* }</pre>
* }
* This method is also an alias for {@link #gene()}, which returns the
* first gene of the chromosome.
*
Expand Down
4 changes: 2 additions & 2 deletions jenetics.ext/src/main/java/io/jenetics/ext/WeaselMutator.java
Expand Up @@ -44,7 +44,7 @@
* genotype in the population with the given mutation probability.
* </p>
* {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
* <pre>{@code
* {@snippet lang="java":
* final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
* // Set the 'WeaselSelector'.
* .selector(new WeaselSelector<>())
Expand All @@ -53,7 +53,7 @@
* // Set the 'WeaselMutator'.
* .alterers(new WeaselMutator<>(0.05))
* .build();
* }</pre>
* }
*
* @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
* @see WeaselSelector
Expand Down
4 changes: 2 additions & 2 deletions jenetics.ext/src/main/java/io/jenetics/ext/WeaselProgram.java
Expand Up @@ -29,11 +29,11 @@
* <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
* algorithm.
*
* <pre>{@code
* {@snippet lang="java":
* final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
* .setup(new WeaselProgram<>())
* .build();
* }</pre>
* }
*
* @see WeaselSelector
* @see WeaselMutator
Expand Down
Expand Up @@ -42,7 +42,7 @@
* instances of the <i>best</i> {@link Phenotype}.
* </p>
* {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
* <pre>{@code
* {@snippet lang="java":
* final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
* // Set the 'WeaselSelector'.
* .selector(new WeaselSelector<>())
Expand All @@ -51,7 +51,7 @@
* // Set the 'WeaselMutator'.
* .alterers(new WeaselMutator<>(0.05))
* .build();
* }</pre>
* }
*
* @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
* @see WeaselMutator
Expand Down
Expand Up @@ -61,7 +61,7 @@
* Concatenating evolution engines might be useful, if you want to explore your
* search space with random search first and then start the <em>real</em> GA
* search.
* <pre>{@code
* {@snippet lang="java":
* final Problem<double[], DoubleGene, Double> problem = Problem.of(
* v -> Math.sin(v[0])*Math.cos(v[1]),
* Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2)
Expand Down Expand Up @@ -90,7 +90,7 @@
*
* System.out.println(result + ": " +
* problem.fitness().apply(problem.codec().decode(result)));
* }</pre>
* }
*
* An essential part, when concatenating evolution engines, is to make sure your
* engines are creating <em>limited</em> evolution streams. This is what
Expand Down
Expand Up @@ -57,7 +57,7 @@
* The {@code CyclicEngine} allows to do a broad search-fine search-cycle
* as long as you want.
*
* <pre>{@code
* {@snippet lang="java":
* final Problem<double[], DoubleGene, Double> problem = Problem.of(
* v -> Math.sin(v[0])*Math.cos(v[1]),
* Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2)
Expand Down Expand Up @@ -87,7 +87,7 @@
*
* System.out.println(result + ": " +
* problem.fitness().apply(problem.codec().decode(result)));
* }</pre>
* }
*
* When using a {@code CyclicEnginePool}, you have to limit the final evolution
* stream, additionally to the defined limits on the used partial engines.
Expand Down
8 changes: 4 additions & 4 deletions jenetics.ext/src/main/java/io/jenetics/ext/grammar/Bnf.java
Expand Up @@ -32,15 +32,15 @@
* grammars in
* <a href="https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form">BNF</a>
* format.
* <pre>{@code
* {@snippet lang="java":
* final Cfg<String> grammar = Bnf.parse("""
* <expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')'
* <op> ::= + | - | * | /
* <var> ::= x | y
* <num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
* """
* );
* }</pre>
* }
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @since 7.1
Expand Down Expand Up @@ -68,12 +68,12 @@ static boolean isIdChar(final char c) {
* Parses the given BNF {@code grammar} string to a {@link Cfg} object. The
* following example shows the grammar of a simple arithmetic expression.
*
* <pre>{@code
* {@snippet lang="java":
* <expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')'
* <op> ::= + | - | * | /
* <var> ::= x | y
* <num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
* }</pre>
* }
*
* @param grammar the BNF {@code grammar} string
* @return the parsed {@code BNF} object
Expand Down
Expand Up @@ -43,7 +43,7 @@
/**
* Parser for BNF grammars.
*
* <pre>{@code
* {@snippet lang="java":
* rulelist: rule_* EOF;
* rule: lhs ASSIGN rhs;
* lhs: id;
Expand All @@ -54,7 +54,7 @@
* text: STRING | QUOTED_STRING;
* id: LT ruleid GT;
* ruleid: ID;
* }</pre>
* }
*
* The BNF object is build from the following classes.
* <ul>
Expand Down
Expand Up @@ -38,15 +38,15 @@
/**
* Tokenizer for BNF grammars.
*
* <pre>{@code
* {@snippet lang="java":
* ASSIGN: '::=';
* BAR: '|';
* GT: '>';
* LT: '<';
* ID: ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'-')+;
* STRING: ( '%s' | '%i' )? '"' ( ~ '"' )* '"';
* WS: [ \r\n\t] -> skip;
* }</pre>
* }
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @since 7.1
Expand Down
8 changes: 4 additions & 4 deletions jenetics.ext/src/main/java/io/jenetics/ext/grammar/Cfg.java
Expand Up @@ -68,18 +68,18 @@
* </ul>
*
* You can easily create a <em>Cfg</em> object from a given BNF grammar.
* <pre>{@code
* {@snippet lang="java":
* final Cfg<String> grammar = Bnf.parse("""
* <expr> ::= <num> | <var> | '(' <expr> <op> <expr> ')'
* <op> ::= + | - | * | /
* <var> ::= x | y
* <num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
* """
* );
* }</pre>
* }
*
* It is also possible to create the grammar above programmatically.
* <pre>{@code
* {@snippet lang="java":
* final Cfg<String> grammar = Cfg.of(
* R("expr",
* E(NT("num")),
Expand All @@ -94,7 +94,7 @@
* E(T("8")), E(T("9"))
* )
* );
* }</pre>
* }
*
* @see Bnf#parse(String)
*
Expand Down
12 changes: 6 additions & 6 deletions jenetics.ext/src/main/java/io/jenetics/ext/grammar/Codons.java
Expand Up @@ -38,7 +38,7 @@
* indexes are needed, the values are read from the beginning again. You have
* the possibility to create a {@code Codons} object from different chromosome
* types.
* <pre>{@code
* {@snippet lang="java":
* // Create 'classic' codons from a bit-chromosome, where
* // the genes are split into 8-bit junks and converted
* // into unsigned int values.
Expand All @@ -47,7 +47,7 @@
* // Creating a codons object from an integer chromosome.
* final var ich = IntegerChromosome.of(IntRange.of(0, 256), 1_000);
* final var codons = Codons.ofIntegerGenes(ich);
* }</pre>
* }
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @since 7.1
Expand Down Expand Up @@ -85,9 +85,9 @@ public int next(final Rule<?> rule, final int bound) {
* Creates a new, classical <em>codons</em> object from the given bit-genes.
* The genes is split into 8-bit chunks and converted into an unsigned
* {@code int[]} array.
* <pre>{@code
* {@snippet lang="java":
* final Codons codons = Codons.ofBitGenes(BitChromosome.of(10_000));
* }</pre>
* }
*
* @param genes the genes used for creating the codon object
* @return a new <em>codons</em> object
Expand Down Expand Up @@ -123,10 +123,10 @@ static byte[] toByteArray(final BaseSeq<BitGene> genes) {
/**
* Creates a new <em>codons</em> object from the given int-genes.
*
* <pre>{@code
* {@snippet lang="java":
* final var chromosome = IntegerChromosome.of(IntRange.of(0, 256), 1_000);
* final var codons = Codons.ofIntegerGenes(chromosome);
* }</pre>
* }
*
* @param genes the genes used for creating the codon object
* @return a new <em>codons</em> object
Expand Down
Expand Up @@ -31,7 +31,7 @@
/**
* Standard implementation of a derivation-tree generator. The following code
* snippet lets you generate a derivation tree from a given grammar.
* <pre>{@code
* {@snippet lang="java":
* final Cfg<String> cfg = Bnf.parse("""
* <expr> ::= ( <expr> <op> <expr> ) | <num> | <var> | <fun> ( <arg>, <arg> )
* <fun> ::= FUN1 | FUN2
Expand All @@ -48,7 +48,7 @@
* 1_000
* );
* final Tree<Symbol<String>, ?> tree = generator.generate(cfg);
* }</pre>
* }
*
* @see SentenceGenerator
*
Expand Down

0 comments on commit a7adc7c

Please sign in to comment.