Generator.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-7.1.2).
03  * Copyright (c) 2007-2023 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.ext.grammar;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import java.util.List;
25 import java.util.function.Function;
26 
27 import io.jenetics.ext.grammar.Cfg.NonTerminal;
28 import io.jenetics.ext.grammar.Cfg.Symbol;
29 
30 /**
31  * Generator interface for generating <em>sentences</em>/<em>derivation trees</em>
32  * from a given grammar.
33  *
34  @param <T> the terminal token type of the grammar
35  @param <R> the result type of the generator
36  *
37  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
38  @since 7.1
39  @version 7.1
40  */
41 @FunctionalInterface
42 public interface Generator<T, R> {
43 
44     /**
45      * Generates a new sentence from the given grammar. If the generation of the
46      * sentence fails, an empty list is returned.
47      *
48      @param cfg the generating grammar
49      @return a newly created result
50      */
51     R generate(final Cfg<? extends T> cfg);
52 
53     /**
54      * Maps the generated result from type {@code R} to type {@code R1}.
55      *
56      @param f the mapping function
57      @param <R1> the target type
58      @return a new generator with target type {@code R1}
59      @throws NullPointerException if the mapping function is {@code null}
60      */
61     default <R1> Generator<T, R1> map(final Function<? super R, ? extends R1> f) {
62         requireNonNull(f);
63         return cfg -> f.apply(generate(cfg));
64     }
65 
66     /**
67      * Standard algorithm for selecting a list of alternative symbols from the
68      * given {@code rule}.
69      *
70      @param rule the rule to select the alternative from
71      @param cfg the grammar to select the alternative from
72      @param index the symbol selection strategy
73      @param <T> the terminal type
74      @return the selected symbols
75      @throws NullPointerException if one of the arguments is {@code null}
76      */
77     static <T> List<Symbol<T>> select(
78         final NonTerminal<T> rule,
79         final Cfg<T> cfg,
80         final SymbolIndex index
81     ) {
82         return cfg.rule(rule)
83             .map(r -> r.alternatives()
84                 .get(index.next(r, r.alternatives().size()))
85                 .symbols())
86             .orElse(List.of());
87     }
88 
89 }