Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
as reported by IntelliJ's analysis
  • Loading branch information
mtf90 committed Dec 22, 2024
1 parent c46cefd commit 7c7aa9d
Show file tree
Hide file tree
Showing 23 changed files with 58 additions and 184 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

* The `de.learnlib.tooling:learnlib-annotation-processor` artifact has been dropped. The functionality has been moved to a [standalone project](https://github.com/LearnLib/build-tools).
* The `de.learnlib:learnlib-rpni-edsm` and `de.learnlib:learnlib-rpni-mdl` artifacts have been dropped. The code has been merged with the `de.learnlib:learnlib-rpni` artifact.
* `MQUtil` has been stripped of unused methods. Especially the `query` method can be simulated by the respective oracles themselves.
* `PropertyOracle`s can no longer set a property. This value is now immutable and must be provided during instantiation. Previously, the internal state wasn't updated accordingly if a property was overridden.
* `SymbolQueryOracle`s (and related code such as the respective caches, counters, etc.) have been removed without replacement. Equivalent functionality on the basis of the new `AdaptiveMembershipOracle`s is available instead.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testAutomaton() {
automaton.addState();
}

Assert.assertEquals(states, automaton.size());
Assert.assertEquals(automaton.size(), states);
automaton.getStates().forEach(x -> Assert.assertTrue(x.getIncomingTransitions().isEmpty()));

final StateIDs<ADTState<Character, Integer>> stateIds = automaton.stateIDs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ private Word<I> toWord(Word<I> prefix) {

@Override
public STNodeImpl<I> prepend(I a) {
STNodeImpl<I> n = children.get(a);
if (n == null) {
n = new STNodeImpl<>(this, a);
children.put(a, n);
}
return n;
return children.computeIfAbsent(a, a1 -> new STNodeImpl<>(this, a1));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import de.learnlib.query.DefaultQuery;
import de.learnlib.tooling.annotation.builder.GenerateBuilder;
import de.learnlib.util.MQUtil;
import de.learnlib.util.nfa.NFALearnerWrapper;
import net.automatalib.alphabet.Alphabet;
import net.automatalib.automaton.fsa.impl.CompactDFA;
import net.automatalib.automaton.fsa.impl.CompactNFA;
import net.automatalib.util.automaton.fsa.NFAs;
import net.automatalib.word.Word;

/**
Expand Down Expand Up @@ -74,46 +73,9 @@ public void startLearning() {
* hypothesis.
*
* @return a DFA learner view of this learner
*
* @see #getDeterminizedHypothesis()
*/
public DFALearner<I> asDFALearner() {
return new DFALearner<I>() {

@Override
public String toString() {
return NLStarLearner.this.toString();
}

@Override
public void startLearning() {
NLStarLearner.this.startLearning();
}

@Override
public boolean refineHypothesis(DefaultQuery<I, Boolean> ceQuery) {
return NLStarLearner.this.refineHypothesis(ceQuery);
}

@Override
public CompactDFA<I> getHypothesisModel() {
return NLStarLearner.this.getDeterminizedHypothesis();
}

};
}

/**
* Retrieves a deterministic version of the hypothesis. The DFA is obtained through
* {@link NFAs#determinize(net.automatalib.automaton.fsa.NFA)}.
*
* @return a deterministic version of the hypothesis
*/
public CompactDFA<I> getDeterminizedHypothesis() {
if (hypothesis == null) {
throw new IllegalStateException();
}
return NFAs.determinize(hypothesis);
return new NFALearnerWrapper<>(this.alphabet, this);
}

private void completeConsistentTable(List<List<Row<I>>> initialUnclosed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class OPLearnerDFA<I> extends AbstractOPLearner<DFA<?, I>, I, Boolean, Bo
* @param epsilonRoot
* whether to ensure the root of the discrimination tree is always labeled using the empty word.
*/
@GenerateBuilder(defaults = AbstractOPLearner.BuilderDefaults.class)
@GenerateBuilder(defaults = BuilderDefaults.class)
public OPLearnerDFA(Alphabet<I> alphabet,
MembershipOracle<I, Boolean> oracle,
LocalSuffixFinder<? super I, ? super Boolean> suffixFinder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ protected boolean refineHypothesisSingle(DefaultQuery<I, Boolean> ceQuery) {

do {
splitState(outIncons);
closeTransitions();
while (finalizeAny()) {
do {
closeTransitions();
}
} while (finalizeAny());

outIncons = findOutputInconsistency();
} while (outIncons != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ protected boolean refineHypothesisSingle(DefaultQuery<I, D> ceQuery) {

do {
splitState(outIncons);
closeTransitions();
while (finalizeAny()) {
do {
closeTransitions();
}
} while (finalizeAny());

outIncons = findOutputInconsistency();
} while (outIncons != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void testValue() {

final RedBlueMerge<BlueFringePTAState<Boolean, Void>, Boolean, Void> merge1 = pta.tryMerge(qEpsilon, qB);
Assert.assertNotNull(merge1);
Assert.assertEquals(2L, EDSMUtil.score(merge1.toMergedAutomaton(), positiveSamples, negativeSamples));
Assert.assertEquals(EDSMUtil.score(merge1.toMergedAutomaton(), positiveSamples, negativeSamples), 2L);

final RedBlueMerge<BlueFringePTAState<Boolean, Void>, Boolean, Void> merge2 = pta.tryMerge(qA, qB);
Assert.assertNotNull(merge2);
// book is wrong, should be 2
Assert.assertEquals(2L, EDSMUtil.score(merge2.toMergedAutomaton(), positiveSamples, negativeSamples));
Assert.assertEquals(EDSMUtil.score(merge2.toMergedAutomaton(), positiveSamples, negativeSamples), 2L);
}

@SuppressWarnings("unchecked")
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/de/learnlib/sul/ObservableSUL.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default ObservableSUL<S, I, O> fork() {
* {@code Object o = getState(); int hc = o.hashCode(); [step(...)|pre()|post()]; assert o.hashCode() == hc;}
*
* Furthermore, if states can be retrieved, but each state is not a deep copy, then this SUL <b>must</b> be
* forkable, i.e. if !{@link #deepCopies()} then {@link #canFork()} must hold.
* forkable, i.e. if {@code !deepCopies()} then {@code canFork()} must hold.
*
* @return whether each state is a deep copy.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ public <E extends Enum<E>> E getEnumValue(LearnLibProperty property, Class<E> en
return getTypedValue(property, p -> Enum.valueOf(enumClazz, p.toUpperCase(Locale.ROOT)));
}

public boolean getBool(LearnLibProperty property, boolean defaultValue) {
return WrapperUtil.booleanValue(getBoolean(property), defaultValue);
}

public @Nullable Boolean getBoolean(LearnLibProperty property) {
return getTypedValue(property, Boolean::parseBoolean);
}

public int getInt(LearnLibProperty property, int defaultValue) {
return WrapperUtil.intValue(getInteger(property), defaultValue);
}
Expand All @@ -89,7 +81,7 @@ public int getInt(LearnLibProperty property, int defaultValue) {
try {
return valueExtractor.apply(prop);
} catch (IllegalArgumentException ex) {
LOG.warn(Category.CONFIG, "Could not parse LearnLib property '" + property + "'.", ex);
LOG.warn(Category.CONFIG, String.format("Could not parse LearnLib property '%s'.", property), ex);
return null;
}
}
Expand Down
4 changes: 0 additions & 4 deletions commons/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ limitations under the License.
<groupId>net.automatalib</groupId>
<artifactId>automata-api</artifactId>
</dependency>
<dependency>
<groupId>net.automatalib</groupId>
<artifactId>automata-commons-util</artifactId>
</dependency>
<dependency>
<groupId>net.automatalib</groupId>
<artifactId>automata-core</artifactId>
Expand Down
46 changes: 0 additions & 46 deletions commons/util/src/main/java/de/learnlib/util/MQUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,17 @@
*/
package de.learnlib.util;

import java.util.Collection;
import java.util.Objects;

import de.learnlib.oracle.MembershipOracle;
import de.learnlib.oracle.OmegaQueryAnswerer;
import de.learnlib.oracle.QueryAnswerer;
import de.learnlib.query.DefaultQuery;
import de.learnlib.query.OmegaQuery;
import de.learnlib.query.Query;
import net.automatalib.automaton.concept.SuffixOutput;
import net.automatalib.common.util.Pair;
import net.automatalib.word.Word;
import org.checkerframework.checker.nullness.qual.Nullable;

public final class MQUtil {

private MQUtil() {
// prevent instantiation
}

public static <I, D> DefaultQuery<I, D> normalize(MembershipOracle<I, D> oracle, DefaultQuery<I, D> query) {
if (query.isNormalized()) {
return query;
}
return query(oracle, Word.epsilon(), query.getInput());
}

public static <I, D> DefaultQuery<I, D> query(MembershipOracle<I, D> oracle, Word<I> prefix, Word<I> suffix) {
DefaultQuery<I, D> qry = new DefaultQuery<>(prefix, suffix);
oracle.processQuery(qry);
return qry;
}

public static <I, D> DefaultQuery<I, D> query(MembershipOracle<I, D> oracle, Word<I> queryWord) {
return query(oracle, Word.epsilon(), queryWord);
}

public static <I, D> void answerQueries(QueryAnswerer<I, D> answerer, Collection<? extends Query<I, D>> queries) {
for (Query<I, D> query : queries) {
Word<I> prefix = query.getPrefix();
Word<I> suffix = query.getSuffix();
D answer = answerer.answerQuery(prefix, suffix);
query.answer(answer);
}
}

public static <S, I, D> void answerOmegaQueries(OmegaQueryAnswerer<S, I, D> answerer,
Collection<? extends OmegaQuery<I, D>> queries) {
for (OmegaQuery<I, D> query : queries) {
final Word<I> prefix = query.getPrefix();
final Word<I> loop = query.getLoop();
final int repeat = query.getRepeat();
Pair<@Nullable D, Integer> answer = answerer.answerQuery(prefix, loop, repeat);
query.answer(answer.getFirst(), answer.getSecond());
}
}

public static <I, D> boolean isCounterexample(DefaultQuery<I, D> query, SuffixOutput<I, D> hyp) {
D qryOut = query.getOutput();
D hypOut = hyp.computeSuffixOutput(query.getPrefix(), query.getSuffix());
Expand Down
1 change: 0 additions & 1 deletion commons/util/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
requires de.learnlib.api;
requires de.learnlib.filter.statistic;
requires net.automatalib.api;
requires net.automatalib.common.util;
requires net.automatalib.core;
requires net.automatalib.util;
requires org.slf4j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.util.function.Function;

import net.automatalib.word.Word;
import org.checkerframework.checker.initialization.qual.UnknownInitialization;

public abstract class AbstractObservationTableWriter<I, D> implements ObservationTableWriter<I, D> {

protected Function<? super Word<? extends I>, ? extends String> wordToString;
protected Function<? super D, ? extends String> outputToString;
private final Function<? super Word<? extends I>, ? extends String> wordToString;
private final Function<? super D, ? extends String> outputToString;

public AbstractObservationTableWriter() {
this(Objects::toString, Objects::toString);
Expand All @@ -43,15 +42,6 @@ public AbstractObservationTableWriter(Function<? super Word<? extends I>, ? exte
return Objects::toString;
}

public void setWordToString(@UnknownInitialization(AbstractObservationTableWriter.class) AbstractObservationTableWriter<I, D> this,
Function<? super Word<? extends I>, ? extends String> wordToString) {
this.wordToString = safeToStringFunction(wordToString);
}

public void setOutputToString(Function<? super D, ? extends String> outputToString) {
this.outputToString = safeToStringFunction(outputToString);
}

protected String wordToString(Word<? extends I> word) {
return wordToString.apply(word);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,35 @@ public ObservationTableASCIIWriter(boolean rowSeparators) {

@Override
public void write(ObservationTable<? extends I, ? extends D> table, Appendable out) throws IOException {
writeInternal(table, super.wordToString, super.outputToString, out);
writeInternal(table, out);
}

/**
* Utility method to bind wildcard generics.
*
* @see #write(ObservationTable, Appendable)
*/
private <I, D> void writeInternal(ObservationTable<I, D> table,
Function<? super Word<? extends I>, ? extends String> wordToString,
Function<? super D, ? extends String> outputToString,
Appendable out) throws IOException {
List<Word<I>> suffixes = table.getSuffixes();
private <I2 extends I, D2 extends D> void writeInternal(ObservationTable<I2, D2> table, Appendable out)
throws IOException {
List<Word<I2>> suffixes = table.getSuffixes();
int numSuffixes = suffixes.size();

int[] colWidth = new int[numSuffixes + 1];

int i = 1;
for (Word<I> suffix : suffixes) {
colWidth[i++] = wordToString.apply(suffix).length();
for (Word<I2> suffix : suffixes) {
colWidth[i++] = wordToString(suffix).length();
}

for (Row<I> row : table.getAllRows()) {
int thisWidth = wordToString.apply(row.getLabel()).length();
for (Row<I2> row : table.getAllRows()) {
int thisWidth = wordToString(row.getLabel()).length();
if (thisWidth > colWidth[0]) {
colWidth[0] = thisWidth;
}

i = 1;
for (D value : table.rowContents(row)) {
thisWidth = outputToString.apply(value).length();
thisWidth = outputToString(value).length();
if (thisWidth > colWidth[i]) {
colWidth[i] = thisWidth;
}
Expand All @@ -88,40 +86,40 @@ private <I, D> void writeInternal(ObservationTable<I, D> table,
// Header
content[0] = "";
i = 1;
for (Word<I> suffix : suffixes) {
content[i++] = wordToString.apply(suffix);
for (Word<I2> suffix : suffixes) {
content[i++] = wordToString(suffix);
}
appendContentRow(out, content, colWidth);
appendSeparatorRow(out, '=', colWidth);

boolean first = true;
for (Row<I> spRow : table.getShortPrefixRows()) {
for (Row<I2> spRow : table.getShortPrefixRows()) {
if (first) {
first = false;
} else if (rowSeparators) {
appendSeparatorRow(out, '-', colWidth);
}
content[0] = wordToString.apply(spRow.getLabel());
content[0] = wordToString(spRow.getLabel());
i = 1;
for (D value : table.rowContents(spRow)) {
content[i++] = outputToString.apply(value);
content[i++] = outputToString(value);
}
appendContentRow(out, content, colWidth);
}

appendSeparatorRow(out, '=', colWidth);

first = true;
for (Row<I> lpRow : table.getLongPrefixRows()) {
for (Row<I2> lpRow : table.getLongPrefixRows()) {
if (first) {
first = false;
} else if (rowSeparators) {
appendSeparatorRow(out, '-', colWidth);
}
content[0] = wordToString.apply(lpRow.getLabel());
content[0] = wordToString(lpRow.getLabel());
i = 1;
for (D value : table.rowContents(lpRow)) {
content[i++] = outputToString.apply(value);
content[i++] = outputToString(value);
}
appendContentRow(out, content, colWidth);
}
Expand Down
Loading

0 comments on commit 7c7aa9d

Please sign in to comment.