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

net.automatalib.words.impl.GrowingVPDAlphabet Maven / Gradle / Ivy

Go to download

The core artifact of AutomataLib. This library contains concrete implementations for many of the automaton model interfaces defined in the API artifact, as well as abstract base classes that facilitate implementing new automaton model classes. Note that concrete algorithms (traversal, reachability analysis etc.) are not in the scope of this artifact.

There is a newer version: 0.11.0
Show newest version
/* Copyright (C) 2013-2018 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 *
 * 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 net.automatalib.words.impl;

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

import net.automatalib.words.abstractimpl.AbstractVPDAlphabet;

/**
 * A {@link net.automatalib.words.VPDAlphabet} implementation that allows to add new symbols after its construction.
 * Wraps input symbols in a {@link VPDSym} instance to allow faster mappings from symbols to indexes.
 *
 * @param 
 *         input symbol type
 *
 * @author Malte Isberner
 */
public class GrowingVPDAlphabet extends AbstractVPDAlphabet> {

    private final List> allSyms = new ArrayList<>();
    private final List> callSyms = new ArrayList<>();
    private final List> returnSyms = new ArrayList<>();
    private final List> internalSyms = new ArrayList<>();

    @Override
    public VPDSym getSymbol(final int index) throws IllegalArgumentException {
        return allSyms.get(index);
    }

    @Override
    public int getSymbolIndex(final VPDSym symbol) throws IllegalArgumentException {
        if (!hasValidIndex(symbol, allSyms)) {
            throw new IllegalArgumentException();
        }
        return symbol.getGlobalIndex();
    }

    @Override
    public VPDSym getCallSymbol(final int index) throws IllegalArgumentException {
        return callSyms.get(index);
    }

    @Override
    public int getCallSymbolIndex(final VPDSym symbol) throws IllegalArgumentException {
        if (symbol.getType() != SymbolType.CALL || !hasValidIndex(symbol, callSyms)) {
            throw new IllegalArgumentException();
        }
        return symbol.getLocalIndex();
    }

    @Override
    public Collection> getCallSymbols() {
        return callSyms;
    }

    @Override
    public VPDSym getInternalSymbol(final int index) throws IllegalArgumentException {
        return internalSyms.get(index);
    }

    @Override
    public int getInternalSymbolIndex(final VPDSym symbol) throws IllegalArgumentException {
        if (symbol.getType() != SymbolType.INTERNAL || !hasValidIndex(symbol, internalSyms)) {
            throw new IllegalArgumentException();
        }
        return symbol.getLocalIndex();
    }

    @Override
    public Collection> getInternalSymbols() {
        return internalSyms;
    }

    @Override
    public VPDSym getReturnSymbol(final int index) throws IllegalArgumentException {
        return returnSyms.get(index);
    }

    @Override
    public int getReturnSymbolIndex(final VPDSym symbol) throws IllegalArgumentException {
        if (symbol.getType() != SymbolType.RETURN || !hasValidIndex(symbol, returnSyms)) {
            throw new IllegalArgumentException();
        }
        return symbol.getLocalIndex();
    }

    @Override
    public Collection> getReturnSymbols() {
        return returnSyms;
    }

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

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

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

    @Override
    public SymbolType getSymbolType(final VPDSym symbol) {
        return symbol.getType();
    }

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

    public VPDSym addNewSymbol(final I userObject, final SymbolType type) {
        final List> localList;
        switch (type) {
            case CALL:
                localList = callSyms;
                break;
            case RETURN:
                localList = returnSyms;
                break;
            default:
                localList = internalSyms;
                break;
        }

        final VPDSym vpdSym = new VPDSym<>(userObject, type, localList.size(), allSyms.size());
        allSyms.add(vpdSym);
        localList.add(vpdSym);

        return vpdSym;
    }

    private boolean hasValidIndex(VPDSym symbol, List> localSymbols) {
        final int localIdx = symbol.getLocalIndex();
        final int globalIdx = symbol.getGlobalIndex();

        return localIdx >= 0 && localIdx < localSymbols.size() && globalIdx >= 0 && globalIdx < allSyms.size();

    }

}