From 9f658987899491b5ba503a88a2177ec68cc4bf63 Mon Sep 17 00:00:00 2001 From: Markus Frohme Date: Mon, 2 Sep 2024 14:59:51 +0200 Subject: [PATCH] adt: cleanup some stream usages --- .../algorithm/adt/ads/DefensiveADS.java | 14 +++--- .../algorithm/adt/learner/ADTLearner.java | 47 ++++++++++++------- .../learnlib/algorithm/adt/util/ADTUtil.java | 8 +++- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/ads/DefensiveADS.java b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/ads/DefensiveADS.java index 31bb315866..03310d55ec 100644 --- a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/ads/DefensiveADS.java +++ b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/ads/DefensiveADS.java @@ -20,10 +20,10 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; import java.util.Queue; import java.util.Set; -import java.util.stream.Collectors; import de.learnlib.algorithm.adt.adt.ADTLeafNode; import de.learnlib.algorithm.adt.adt.ADTNode; @@ -33,6 +33,7 @@ import net.automatalib.automaton.concept.StateIDs; import net.automatalib.automaton.transducer.MealyMachine; import net.automatalib.common.smartcollection.ReflexiveMapView; +import net.automatalib.common.util.HashUtil; import net.automatalib.common.util.Pair; import net.automatalib.util.automaton.ads.ADSUtil; import net.automatalib.word.Word; @@ -144,11 +145,12 @@ private Optional> compute(Map mapping) { @SuppressWarnings("nullness") // false positive https://github.com/typetools/checker-framework/issues/399 final @NonNull Word prefix = splittingWordCandidates.poll(); - final Map currentToInitialMapping = mapping.keySet() - .stream() - .collect(Collectors.toMap(x -> automaton.getSuccessor(x, - prefix), - mapping::get)); + final Map currentToInitialMapping = new HashMap<>(HashUtil.capacity(mapping.size())); + + for (Entry e : mapping.entrySet()) { + currentToInitialMapping.put(automaton.getSuccessor(e.getKey(), prefix), e.getValue()); + } + final BitSet currentSetAsBitSet = new BitSet(); for (S s : currentToInitialMapping.keySet()) { currentSetAsBitSet.set(stateIds.getStateId(s)); diff --git a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/learner/ADTLearner.java b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/learner/ADTLearner.java index 178378c723..0e1ed7d1c2 100644 --- a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/learner/ADTLearner.java +++ b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/learner/ADTLearner.java @@ -19,6 +19,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -60,6 +62,7 @@ import net.automatalib.alphabet.Alphabet; import net.automatalib.alphabet.SupportsGrowingAlphabet; import net.automatalib.automaton.transducer.MealyMachine; +import net.automatalib.common.util.HashUtil; import net.automatalib.common.util.Pair; import net.automatalib.word.Word; import org.slf4j.Logger; @@ -258,29 +261,37 @@ public boolean refineHypothesisInternal(DefaultQuery> ceQuery) { newNode.setHypothesisState(newState); final ADTNode, I, O> temporarySplitter = ADTUtil.getStartOfADS(nodeToSplit); - final List> newTransitions = alphabet.stream() - .map(i -> this.hypothesis.createOpenTransition(newState, - i, - this.adt.getRoot())) - .collect(Collectors.toList()); + final List> newTransitions = new ArrayList<>(alphabet.size()); + + for (I i : alphabet) { + newTransitions.add(this.hypothesis.createOpenTransition(newState, i, this.adt.getRoot())); + } + final List> transitionsToRefine = nodeToSplit.getHypothesisState() .getIncomingTransitions() .stream() .filter(x -> !x.isSpanningTreeEdge()) .collect(Collectors.toList()); - transitionsToRefine.forEach(x -> { + for (ADTTransition x : transitionsToRefine) { x.setTarget(null); x.setSiftNode(temporarySplitter); - }); + } final ADTNode, I, O> finalizedSplitter = this.evaluateAdtExtension(temporarySplitter); - transitionsToRefine.stream().filter(ADTTransition::needsSifting).forEach(x -> { - x.setSiftNode(finalizedSplitter); - this.openTransitions.add(x); - }); - newTransitions.stream().filter(ADTTransition::needsSifting).forEach(this.openTransitions::add); + for (ADTTransition t : transitionsToRefine) { + if (t.needsSifting()) { + t.setSiftNode(finalizedSplitter); + this.openTransitions.add(t); + } + } + + for (ADTTransition t : newTransitions) { + if (t.needsSifting()) { + this.openTransitions.add(t); + } + } this.closeTransitions(); return true; @@ -597,10 +608,15 @@ private boolean validateADS(ADTNode, I, O> oldADS, } final Set, I, O>> newFinalNodes = ADTUtil.collectLeaves(newADS); + final Map, Pair, Word>> traces = + new HashMap<>(HashUtil.capacity(newFinalNodes.size())); + + for (ADTNode, I, O> n : newFinalNodes) { + traces.put(n.getHypothesisState(), ADTUtil.buildTraceForNode(n)); + } final Set> oldFinalStates = ADTUtil.collectHypothesisStates(oldADS); - final Set> newFinalStates = - newFinalNodes.stream().map(ADTNode::getHypothesisState).collect(Collectors.toSet()); + final Set> newFinalStates = new HashSet<>(traces.keySet()); newFinalStates.addAll(cutout); if (!oldFinalStates.equals(newFinalStates)) { @@ -608,9 +624,6 @@ private boolean validateADS(ADTNode, I, O> oldADS, } final Word parentInputTrace = ADTUtil.buildTraceForNode(oldADS).getFirst(); - final Map, Pair, Word>> traces = newFinalNodes.stream() - .collect(Collectors.toMap(ADTNode::getHypothesisState, - ADTUtil::buildTraceForNode)); for (Map.Entry, Pair, Word>> entry : traces.entrySet()) { diff --git a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/util/ADTUtil.java b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/util/ADTUtil.java index 384434c68e..eed15cfb26 100644 --- a/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/util/ADTUtil.java +++ b/algorithms/active/adt/src/main/java/de/learnlib/algorithm/adt/util/ADTUtil.java @@ -232,7 +232,13 @@ private static int computeEffectiveResetsInternal(ADTNode ads final int nextCosts = ADTUtil.isResetNode(ads) ? accumulatedResets + 1 : accumulatedResets; - return ads.getChildren().values().stream().mapToInt(x -> computeEffectiveResetsInternal(x, nextCosts)).sum(); + int resets = 0; + + for (ADTNode value : ads.getChildren().values()) { + resets += computeEffectiveResetsInternal(value, nextCosts); + } + + return resets; } public static Pair, ADTNode> buildADSFromTrace(MealyMachine automaton,