/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 */
package io.jenetics.ext.grammar;

import static java.util.Objects.requireNonNull;

import static io.jenetics.ext.grammar.SentenceGenerator.Expansion.LEFT_TO_RIGHT;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import io.jenetics.ext.grammar.Cfg.NonTerminal;
import io.jenetics.ext.grammar.Cfg.Symbol;
import io.jenetics.ext.grammar.Cfg.Terminal;

/**
 * Standard implementation of a sentence generator. The generator can generate
 * sentences by expanding the grammar in a {@link Expansion#LEFT_FIRST} or
 * {@link Expansion#LEFT_TO_RIGHT} order.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since !__version__!
 * @version !__version__!
 */
public final class SentenceGenerator<T>
	implements Generator<T, List<Terminal<T>>>
{

	/**
	 * Defines the expansion strategy used when generating the sentences.
	 */
	public enum Expansion {

		/**
		 * The symbol replacement always starting from the leftmost nonterminal
		 * as described in
		 * <a href="https://www.brinckerhoff.org/tmp/grammatica_evolution_ieee_tec_2001.pdf">
		 * Grammatical Evolution</a>.
		 */
		LEFT_FIRST,

		/**
		 * The symbol replacement is performed from left to right and is repeated
		 * until all non-terminal symbol has been expanded.
		 */
		LEFT_TO_RIGHT;
	}

	private final SymbolIndex _index;
	private final Expansion _expansion;
	private final int _limit;

	/**
	 * Create a new sentence generator from the given parameters.
	 *
	 * @param index the symbol index function used for generating the sentences
	 * @param expansion the sentence generation strategy to use for generating
	 *        the sentences
	 * @param limit the maximal allowed sentence length. If the generated
	 *        sentence exceeds this length, the generation is interrupted and
	 *        an empty sentence (empty list) is returned.
	 */
	public SentenceGenerator(
		final SymbolIndex index,
		final Expansion expansion,
		final int limit
	) {
		_index = requireNonNull(index);
		_expansion = requireNonNull(expansion);
		_limit = limit;
	}

	/**
	 * Create a new sentence generator from the given parameters.
	 *
	 * @param index the symbol index function used for generating the sentences
	 * @param limit the maximal allowed sentence length. If the generated
	 *        sentence exceeds this length, the generation is interrupted and
	 *        an empty sentence (empty list) is returned.
	 */
	public SentenceGenerator(
		final SymbolIndex index,
		final int limit
	) {
		this(index, LEFT_TO_RIGHT, limit);
	}

	/**
	 * Generates a new sentence from the given grammar, <em>cfg</em>.
	 * <p>
	 * The following code snippet shows how to create a random sentence from a
	 * given grammar:
	 * <pre>{@code
	 * final Cfg<String> cfg = Bnf.parse("""
	 *     <expr> ::= ( <expr> <op> <expr> ) | <num> | <var> |  <fun> ( <arg>, <arg> )
	 *     <fun>  ::= FUN1 | FUN2
	 *     <arg>  ::= <expr> | <var> | <num>
	 *     <op>   ::= + | - | * | /
	 *     <var>  ::= x | y
	 *     <num>  ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
	 *     """
	 * );
	 *
	 * final RandomGenerator random = RandomGenerator.of("L64X256MixRandom");
	 * final List<Terminal<String>> sentence = Sentence.generate(
	 *     cfg, random::nextInt, 1_000
	 * );
	 * final String string = sentence.stream()
	 *     .map(Symbol::value)
	 *     .collect(Collectors.joining());
	 *
	 * System.out.println(string);
	 * }</pre>
	 * <em>Some sample output:</em>
	 * <pre>{@code
	 * > ((x-FUN1(5,5))+8)
	 * > (FUN2(y,5)-FUN2(0,x))
	 * > x
	 * > FUN2(x,x)
	 * > 5
	 * > FUN2(y,FUN2((FUN1(5,FUN1(y,2))*9),y))
	 * > ((FUN1(x,5)*9)*(x/(y*FUN2(x,y))))
	 * > (9-(y*(x+x)))
	 * > }</pre>
	 *
	 * @param cfg the generating grammar
	 * @return a newly created terminal list (sentence), or an empty list if
	 *         the length of the sentence exceed the defined sentence limit
	 */
	@Override
	public List<Terminal<T>> generate(final Cfg<? extends T> cfg) {
		final var sentence = new LinkedList<Symbol<T>>();
		generate(Cfg.upcast(cfg), sentence);

		return sentence.stream()
			.map(t -> (Terminal<T>)t)
			.toList();
	}

	private void generate(final Cfg<T> cfg, final List<Symbol<T>> symbols) {
		symbols.add(cfg.start());

		boolean proceed;
		do {
			proceed = false;

			final ListIterator<Symbol<T>> sit = symbols.listIterator();
			while (sit.hasNext() &&
				(_expansion == Expansion.LEFT_TO_RIGHT || !proceed))
			{
				if (sit.next() instanceof NonTerminal<T> nt) {
					sit.remove();
					expand(cfg, nt).forEach(sit::add);
					proceed = true;
				}
			}

			if (symbols.size() > _limit) {
				symbols.clear();
				proceed = false;
			}
		} while (proceed);
	}

	private List<Symbol<T>> expand(final Cfg<T> cfg, final NonTerminal<T> symbol) {
		return cfg.rule(symbol)
			.map(rule -> rule.alternatives()
				.get(_index.next(rule, rule.alternatives().size()))
				.symbols())
			.orElse(List.of());
	}

}

@@ -819,17 +819,16 @@
 onNull;%0A
-%0A
 import s
@@ -890,23 +890,20 @@
 on.LEFT_
-TO_RIGH
+FIRS
 T;%0A%0Aimpo
@@ -3271,23 +3271,20 @@
 x, LEFT_
-TO_RIGH
+FIRS
 T, limit

Fix default generation method.