|
| 1 | +/* Copyright (C) 2013-2022 TU Dortmund |
| 2 | + * This file is part of LearnLib, http://www.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.algorithms.discriminationtree; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import com.google.common.io.CharStreams; |
| 24 | +import de.learnlib.acex.analyzers.AcexAnalyzers; |
| 25 | +import de.learnlib.algorithms.discriminationtree.vpda.DTLearnerVPDA; |
| 26 | +import de.learnlib.oracle.equivalence.vpda.SimulatorEQOracle; |
| 27 | +import de.learnlib.oracle.membership.SimulatorOracle; |
| 28 | +import de.learnlib.util.Experiment; |
| 29 | +import net.automatalib.automata.vpda.DefaultOneSEVPA; |
| 30 | +import net.automatalib.automata.vpda.OneSEVPA; |
| 31 | +import net.automatalib.commons.util.IOUtil; |
| 32 | +import net.automatalib.serialization.dot.GraphDOT; |
| 33 | +import net.automatalib.util.automata.random.RandomAutomata; |
| 34 | +import net.automatalib.words.impl.Alphabets; |
| 35 | +import net.automatalib.words.impl.DefaultVPDAlphabet; |
| 36 | +import org.testng.Assert; |
| 37 | +import org.testng.annotations.Test; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author frohme |
| 41 | + */ |
| 42 | +public class DTVisualizationTest { |
| 43 | + |
| 44 | + private final DTLearnerVPDA<Character> learner; |
| 45 | + |
| 46 | + public DTVisualizationTest() { |
| 47 | + final DefaultVPDAlphabet<Character> alphabet = new DefaultVPDAlphabet<>(Alphabets.characters('a', 'c'), |
| 48 | + Alphabets.characters('1', '3'), |
| 49 | + Alphabets.characters('x', 'z')); |
| 50 | + final DefaultOneSEVPA<Character> vpa = |
| 51 | + RandomAutomata.randomOneSEVPA(new Random(42), 10, alphabet, 0.5, 0.5, true); |
| 52 | + |
| 53 | + final SimulatorOracle<Character, Boolean> mqo = new SimulatorOracle<>(vpa); |
| 54 | + final SimulatorEQOracle<Character> eqo = new SimulatorEQOracle<>(vpa); |
| 55 | + this.learner = new DTLearnerVPDA<>(alphabet, mqo, AcexAnalyzers.BINARY_SEARCH_FWD); |
| 56 | + |
| 57 | + final Experiment<OneSEVPA<?, Character>> exp = new Experiment<>(learner, eqo, alphabet); |
| 58 | + exp.run(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testVisualizeHyp() throws IOException { |
| 63 | + final String expectedHyp = resourceAsString("/hyp.dot"); |
| 64 | + |
| 65 | + final StringWriter actualHyp = new StringWriter(); |
| 66 | + GraphDOT.write(this.learner.getHypothesisModel(), actualHyp); |
| 67 | + |
| 68 | + Assert.assertEquals(actualHyp.toString(), expectedHyp); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testVisualizeDT() throws IOException { |
| 73 | + final String expectedDT = resourceAsString("/dt.dot"); |
| 74 | + |
| 75 | + final StringWriter actualDT = new StringWriter(); |
| 76 | + GraphDOT.write(this.learner.getDiscriminationTree(), actualDT); |
| 77 | + |
| 78 | + Assert.assertEquals(actualDT.toString(), expectedDT); |
| 79 | + } |
| 80 | + |
| 81 | + private String resourceAsString(String resourceName) throws IOException { |
| 82 | + try (InputStream is = getClass().getResourceAsStream(resourceName)) { |
| 83 | + assert is != null; |
| 84 | + return CharStreams.toString(IOUtil.asBufferedUTF8Reader(is)); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments