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

de.learnlib.algorithm.observationpack.vpa.hypothesis.HypLoc 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.Collections;
import java.util.List;

import de.learnlib.AccessSequenceProvider;
import net.automatalib.alphabet.VPAlphabet;
import net.automatalib.common.smartcollection.ArrayStorage;
import net.automatalib.word.Word;

/**
 * @param 
 *         input symbol type
 */
public class HypLoc implements AccessSequenceProvider {

    private final AbstractHypTrans treeIncoming;
    private final Word aseq;
    private final ArrayStorage> intSuccessors;
    private final ArrayStorage>> returnSuccessors;
    private final int index;
    private boolean accepting;
    private DTNode leaf;

    public HypLoc(VPAlphabet alphabet, int index, boolean accepting, AbstractHypTrans treeIncoming) {
        this.index = index;
        this.accepting = accepting;
        this.intSuccessors = new ArrayStorage<>(alphabet.getNumInternals());
        this.returnSuccessors = new ArrayStorage<>(alphabet.getNumReturns(), ArrayList::new);
        this.treeIncoming = treeIncoming;
        this.aseq = (treeIncoming != null) ? treeIncoming.getAccessSequence() : Word.epsilon();
    }

    public void updateStackAlphabetSize(int newStackAlphaSize) {
        for (int i = 0; i < returnSuccessors.size(); i++) {
            List> transList = returnSuccessors.get(i);
            if (transList == null) {
                transList = new ArrayList<>(Collections.nCopies(newStackAlphaSize, null));
                returnSuccessors.set(i, transList);
            } else if (transList.size() < newStackAlphaSize) {
                transList.addAll(Collections.nCopies(newStackAlphaSize - transList.size(), null));
            }
        }
    }

    public DTNode getLeaf() {
        return leaf;
    }

    public void setLeaf(DTNode leaf) {
        this.leaf = leaf;
    }

    public boolean isRoot() {
        return treeIncoming == null;
    }

    @Override
    public Word getAccessSequence() {
        return aseq;
    }

    public int getIndex() {
        return index;
    }

    public boolean isAccepting() {
        return accepting;
    }

    public void setAccepting(boolean accepting) {
        this.accepting = accepting;
    }

    public HypRetTrans getReturnTransition(int retSymId, int stackSym) {
        final List> succList = returnSuccessors.get(retSymId);
        return succList.get(stackSym);
    }

    public void setReturnTransition(int retSymId, int stackSym, HypRetTrans trans) {
        List> succList = returnSuccessors.get(retSymId);
        if (succList == null) {
            succList = new ArrayList<>(stackSym + 1);
            returnSuccessors.set(retSymId, succList);
        }
        int numSuccs = succList.size();
        if (numSuccs <= stackSym) {
            succList.addAll(Collections.nCopies(stackSym + 1 - numSuccs, null));
        }
        succList.set(stackSym, trans);
    }

    public HypIntTrans getInternalTransition(int intSymId) {
        return intSuccessors.get(intSymId);
    }

    public void setInternalTransition(int intSymId, HypIntTrans succ) {
        intSuccessors.set(intSymId, succ);
    }

    @Override
    public String toString() {
        return Integer.toString(index);
    }
}