|
| 1 | +/* Copyright (C) 2013-2026 TU Dortmund University |
| 2 | + * This file is part of LearnLib <https://learnlib.de>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package de.learnlib.algorithm.lambda.lstar; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Map.Entry; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import de.learnlib.AccessSequenceTransformer; |
| 28 | +import de.learnlib.datastructure.observationtable.ObservationTable; |
| 29 | +import de.learnlib.datastructure.observationtable.Row; |
| 30 | +import de.learnlib.datastructure.observationtable.RowImpl; |
| 31 | +import net.automatalib.alphabet.Alphabet; |
| 32 | +import net.automatalib.common.util.HashUtil; |
| 33 | +import net.automatalib.word.Word; |
| 34 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 35 | + |
| 36 | +class OTView<I, D> implements ObservationTable<I, D> { |
| 37 | + |
| 38 | + private final Alphabet<I> alphabet; |
| 39 | + private final List<List<D>> rowContents; |
| 40 | + private final List<Row<I>> shortPrefixes; |
| 41 | + private final List<Row<I>> longPrefixes; |
| 42 | + private final List<Word<I>> suffixes; |
| 43 | + private final AccessSequenceTransformer<I> asTransformer; |
| 44 | + |
| 45 | + OTView(Alphabet<I> alphabet, |
| 46 | + Set<Word<I>> shortPrefixes, |
| 47 | + Map<Word<I>, List<D>> rows, |
| 48 | + List<Word<I>> suffixes, |
| 49 | + AccessSequenceTransformer<I> asTransformer) { |
| 50 | + final int numInputs = alphabet.size(); |
| 51 | + final int numRows = rows.size(); |
| 52 | + final int numSP = shortPrefixes.size(); |
| 53 | + final int numLP = numRows - numSP; |
| 54 | + |
| 55 | + List<List<D>> contents = new ArrayList<>(numRows); |
| 56 | + List<RowImpl<I>> shortPrefs = new ArrayList<>(numSP); |
| 57 | + List<RowImpl<I>> longPrefs = new ArrayList<>(numLP); |
| 58 | + |
| 59 | + final Map<Word<I>, RowImpl<I>> wordRowMap = new HashMap<>(HashUtil.capacity(numRows)); |
| 60 | + int idx = 0; |
| 61 | + for (Entry<Word<I>, List<D>> e : rows.entrySet()) { |
| 62 | + final Word<I> label = e.getKey(); |
| 63 | + final RowImpl<I> r = new RowImpl<>(label, idx++); |
| 64 | + |
| 65 | + contents.add(Collections.unmodifiableList(e.getValue())); |
| 66 | + wordRowMap.put(label, r); |
| 67 | + |
| 68 | + if (shortPrefixes.contains(label)) { |
| 69 | + shortPrefs.add(r); |
| 70 | + r.makeShort(numInputs); |
| 71 | + } else { |
| 72 | + longPrefs.add(r); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (RowImpl<I> sp : shortPrefs) { |
| 77 | + final Word<I> label = sp.getLabel(); |
| 78 | + for (int i = 0; i < numInputs; i++) { |
| 79 | + @SuppressWarnings("nullness") // short prefixes + 1-letter extensions = long prefixes |
| 80 | + final @NonNull RowImpl<I> row = wordRowMap.get(label.append(alphabet.getSymbol(i))); |
| 81 | + sp.setSuccessor(i, row); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + this.alphabet = alphabet; |
| 86 | + this.rowContents = Collections.unmodifiableList(contents); |
| 87 | + this.shortPrefixes = Collections.unmodifiableList(shortPrefs); |
| 88 | + this.longPrefixes = Collections.unmodifiableList(longPrefs); |
| 89 | + this.suffixes = Collections.unmodifiableList(suffixes); |
| 90 | + this.asTransformer = asTransformer; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Alphabet<I> getInputAlphabet() { |
| 95 | + return alphabet; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Collection<Row<I>> getShortPrefixRows() { |
| 100 | + return shortPrefixes; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Collection<Row<I>> getLongPrefixRows() { |
| 105 | + return longPrefixes; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Row<I> getRow(int idx) { |
| 110 | + final int numSP = shortPrefixes.size(); |
| 111 | + if (idx < numSP) { |
| 112 | + return shortPrefixes.get(idx); |
| 113 | + } else { |
| 114 | + return longPrefixes.get(idx - numSP); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public int numberOfDistinctRows() { |
| 120 | + return shortPrefixes.size() + longPrefixes.size(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public List<Word<I>> getSuffixes() { |
| 125 | + return this.suffixes; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public List<D> rowContents(Row<I> row) { |
| 130 | + return rowContents.get(row.getRowId()); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Word<I> transformAccessSequence(Word<I> word) { |
| 135 | + return this.asTransformer.transformAccessSequence(word); |
| 136 | + } |
| 137 | +} |
0 commit comments