All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.learnlib.algorithm.observationpack.vpa.hypothesis.OneSEVPAHypothesis Maven / Gradle / Ivy

Go to download

This artifact provides the implementation of the VPA adaption of the Observation-Pack learning algorithm as discussed in the PhD thesis "Foundations of Active Automata Learning: An Algorithmic Perspective" (https://dx.doi.org/10.17877/DE290R-16359) by Malte Isberner.

The newest version!
/* Copyright (C) 2013-2023 TU Dortmund
 * This file is part of LearnLib, http://www.learnlib.de/.
 *
 * 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.
 */
package de.learnlib.algorithm.observationpack.vpa.hypothesis;

import java.util.ArrayList;
import java.util.List;

import net.automatalib.alphabet.VPAlphabet;
import net.automatalib.automaton.vpa.AbstractSEVPA;
import net.automatalib.automaton.vpa.OneSEVPA;
import net.automatalib.automaton.vpa.StackContents;
import net.automatalib.automaton.vpa.State;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * @param 
 *         input symbol type
 */
public class OneSEVPAHypothesis extends AbstractSEVPA, I> implements OneSEVPA, I> {

    private final List> locations = new ArrayList<>();

    private HypLoc initLoc;

    public OneSEVPAHypothesis(VPAlphabet alphabet) {
        super(alphabet);
    }

    public @Nullable AbstractHypTrans getInternalTransition(State> state, I sym) {
        switch (alphabet.getSymbolType(sym)) {
            case INTERNAL:
                return state.getLocation().getInternalTransition(alphabet.getInternalSymbolIndex(sym));
            case RETURN:
                StackContents stackContents = state.getStackContents();
                assert stackContents != null;
                return state.getLocation()
                            .getReturnTransition(alphabet.getReturnSymbolIndex(sym), stackContents.peek());
            default:
                return null;
        }
    }

    public AbstractHypTrans getInternalTransition(HypLoc loc, I intSym) {
        return loc.getInternalTransition(alphabet.getInternalSymbolIndex(intSym));
    }

    public AbstractHypTrans getReturnTransition(HypLoc loc, I retSym, int stackSym) {
        return loc.getReturnTransition(alphabet.getReturnSymbolIndex(retSym), stackSym);
    }

    public AbstractHypTrans getReturnTransition(HypLoc loc, I retSym, HypLoc stackLoc, I callSym) {
        int stackSym = encodeStackSym(stackLoc, callSym);
        return loc.getReturnTransition(alphabet.getReturnSymbolIndex(retSym), stackSym);
    }

    public HypLoc createLocation(boolean accepting, AbstractHypTrans treeIncoming) {
        HypLoc loc = new HypLoc<>(alphabet, locations.size(), accepting, treeIncoming);
        locations.add(loc);
        return loc;
    }

    public HypLoc initialize() {
        HypLoc loc = createLocation(false, null);
        this.initLoc = loc;

        return loc;
    }

    @Override
    public HypLoc getInternalSuccessor(HypLoc loc, I intSym) {
        return loc.getInternalTransition(alphabet.getInternalSymbolIndex(intSym)).getTargetLocation();
    }

    @Override
    public HypLoc getLocation(int id) {
        return locations.get(id);
    }

    @Override
    public int getLocationId(HypLoc loc) {
        return loc.getIndex();
    }

    @Override
    public List> getLocations() {
        return locations;
    }

    @Override
    public HypLoc getReturnSuccessor(HypLoc loc, I retSym, int stackSym) {
        return loc.getReturnTransition(alphabet.getReturnSymbolIndex(retSym), stackSym).getTargetLocation();
    }

    @Override
    public boolean isAcceptingLocation(HypLoc loc) {
        return loc.isAccepting();
    }

    @Override
    public HypLoc getInitialLocation() {
        return initLoc;
    }

    @Override
    public int size() {
        return locations.size();
    }

}