cz.cuni.mff.ms.brodecva.botnicek.ide.compile.library.Recursion.java Source code

Java tutorial

Introduction

Here is the source code for cz.cuni.mff.ms.brodecva.botnicek.ide.compile.library.Recursion.java

Source

/**
 * Copyright Vclav Brodec 2014.
 * 
 * This file is part of Botn?ek.
 * 
 * Botn?ek is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Botn?ek is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Botn?ek.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.cuni.mff.ms.brodecva.botnicek.ide.compile.library;

import java.util.List;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.category.Template;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.template.implementations.Sr;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.template.implementations.Text;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.template.implementations.Topicstar;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.toplevel.Category;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.elements.toplevel.Topic;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.types.NormalWord;
import cz.cuni.mff.ms.brodecva.botnicek.ide.aiml.types.Patterns;
import cz.cuni.mff.ms.brodecva.botnicek.ide.translate.Stack;
import cz.cuni.mff.ms.brodecva.botnicek.library.platform.AIML;
import cz.cuni.mff.ms.brodecva.botnicek.library.platform.AIMLIndex;
import cz.cuni.mff.ms.brodecva.botnicek.library.platform.Index;

/**
 * Knihovna s tmaty pro vyhodnocen spchu zanoen do podst a prchodu
 * celm systmem.
 * 
 * @author Vclav Brodec
 * @version 1.0
 */
public final class Recursion {
    /**
     * Vrt tmata zajiujc: klid zsobnku po spnm prchodu v zanoen
     * st, nvrat do pvodn a pechod na dal stavy v zsobnku v ppad
     * nespchu.
     * 
     * @param pullState
     *            stav pro klid stav, kter jsme rozbalili, ale kvli spchu
     *            je nen teba prochzet (umisuje se po oputn st clov
     *            stavem)
     * @param pullStopState
     *            zarka klidu (umisuje se po vejit do st vstupnm uzlem)
     * @param successState
     *            stav indikujc spn projit podst, v ppad jinak
     *            przdnho zsobnku pak celho systmu
     * @param failState
     *            stav indikujc nespn ukon?en vpo?tu
     * @param returnState
     *            stav uvozujc nvratov stav do st o rove ve pi
     *            zanoen
     * @return knihovn tmata
     */
    public static List<Topic> getLibrary(final NormalWord pullState, final NormalWord pullStopState,
            final NormalWord successState, final NormalWord failState, final NormalWord returnState) {
        Preconditions.checkNotNull(pullState);
        Preconditions.checkNotNull(pullStopState);
        Preconditions.checkNotNull(failState);
        Preconditions.checkNotNull(returnState);

        final String star = AIML.STAR_WILDCARD.getValue();
        final String space = AIML.WORD_DELIMITER.getValue();
        final String pull = pullState.getText();
        final String pullStop = pullStopState.getText();
        final String reTurn = returnState.getText();
        final String success = successState.getText();
        final String fail = failState.getText();
        final Index two = new AIMLIndex(2);

        final ImmutableList.Builder<Topic> topics = ImmutableList.builder();

        // @formatter:off
        // PULL * PULLSTOP * -> PULL PULLSTOP *
        topics.add(Topic.create(Patterns.create(join(pull, star, pullStop, star)),
                Category.createUniversal(
                        Template.create(Stack.set(Text.create(join(pull, pullStop) + space), Topicstar.create(two)),
                                Sr.create()))));

        // PULL PULLSTOP RETURN * -> SUCCESS *
        topics.add(Topic.create(Patterns.create(join(pull, pullStop, reTurn, star)),
                Category.createUniversal(Template.create(Stack.popAndPushWords(successState), Sr.create()))));

        // PULL PULLSTOP * -> *
        topics.add(Topic.create(Patterns.create(join(pull, pullStop, star)),
                Category.createUniversal(Template.create(Stack.pop(), Sr.create()))));

        // PULL PULLSTOP -> SUCCESS
        topics.add(Topic.create(Patterns.create(join(pull, pullStop)),
                Category.createUniversal(Template.create(Stack.set(Text.create(success)), Sr.create()))));

        // PULLSTOP RETURN * * -> 2nd*
        topics.add(Topic.create(Patterns.create(join(pullStop, reTurn, star, star)),
                Category.createUniversal(Template.create(Stack.set(Topicstar.create(two)), Sr.create()))));

        // PULLSTOP * -> *
        topics.add(Topic.create(Patterns.create(join(pullStop, star)),
                Category.createUniversal(Template.create(Stack.pop(), Sr.create()))));

        // PULLSTOP -> FAIL
        topics.add(Topic.create(Patterns.create(pullStop),
                Category.createUniversal(Template.create(Stack.set(Text.create(fail)), Sr.create()))));

        // SUCCESS
        topics.add(Topic.create(Patterns.create(success), Category.createUniversal(Template.create())));
        // @formatter:on

        return topics.build();
    }

    private static String join(final String... parts) {
        return Joiner.on(AIML.WORD_DELIMITER.getValue()).join(parts);
    }

    private Recursion() {
    }
}