summaryrefslogtreecommitdiff
path: root/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/CacheInconsistencyException.java
blob: 67c2e73c6dbef109d7d9db6daaf0d71ea3a1e9f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package basiclearner;

import net.automatalib.words.Word;

/**
 * Contains the full input for which non-determinism was observed, as well as the full new output
 * and the (possibly shorter) old output with which it disagrees
 * 
 * @author Ramon Janssen
 */
public class CacheInconsistencyException extends RuntimeException {
	private final Word oldOutput, newOutput, input;
	
	public CacheInconsistencyException(Word input, Word oldOutput, Word newOutput) {
		this.input = input;
		this.oldOutput = oldOutput;
		this.newOutput = newOutput;
	}
	
	public CacheInconsistencyException(String message, Word input, Word oldOutput, Word newOutput) {
		super(message);
		this.input = input;
		this.oldOutput = oldOutput;
		this.newOutput = newOutput;
	}
	

	/**
	 * The shortest cached output word which does not correspond with the new output
	 * @return
	 */
	public Word getOldOutput() {
		return this.oldOutput;
	}
	
	/**
	 * The full new output word
	 * @return
	 */
	public Word getNewOutput() {
		return this.newOutput;
	}

	/**
	 * The shortest sublist of the input word which still shows non-determinism
	 * @return
	 */
	public Word getShortestInconsistentInput() {
	    int indexOfInconsistency = 0;
	    while (oldOutput.getSymbol(indexOfInconsistency).equals(newOutput.getSymbol(indexOfInconsistency))) {
	        indexOfInconsistency ++;
	    }
	    return this.input.subWord(0, indexOfInconsistency);
	}
	
	@Override
	public String toString() {
		return "Non-determinism detected\nfull input:\n" + this.input + "\nfull new output:\n" + this.newOutput + "\nold output:\n" + this.oldOutput;
	}
}