Skip to content

Commit

Permalink
oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
parcaini committed May 29, 2019
1 parent 8042911 commit f938067
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
26 changes: 26 additions & 0 deletions mutrex/src/regex/oracle/AutomaticOracle.java
@@ -0,0 +1,26 @@
package regex.oracle;

import dk.brics.automaton.Automaton;
import dk.brics.automaton.RegExp;
import dk.brics.automaton.RunAutomaton;

/** it represents an oracle given by a regex */
public class AutomaticOracle implements RegexOracle {
private Automaton automaton;
private RunAutomaton automatonRunner;

public AutomaticOracle(String regex) {
this(new RegExp(regex));
}

public AutomaticOracle(RegExp regExp) {
assert regExp != null;
automaton = regExp.toAutomaton();
automatonRunner = new RunAutomaton(automaton);
}

@Override
public boolean accept(String s) {
return automatonRunner.run(s);
}
}
69 changes: 69 additions & 0 deletions mutrex/src/regex/oracle/CachedOracle.java
@@ -0,0 +1,69 @@
package regex.oracle;

import java.util.HashSet;
import java.util.Set;

/**
* it is an oracle that keeps memory of the string it has been asked
*
* @author garganti
*
*/
public class CachedOracle implements RegexOracle {
private Set<String> acceptedByOracle;
private Set<String> rejectedByOracle;
private RegexOracle oracle;

// the real oracle to ask in case there is new string to evaluate
public CachedOracle(RegexOracle oracle) {
acceptedByOracle = new HashSet<String>();
rejectedByOracle = new HashSet<String>();
this.oracle = oracle;
}

/**
* it keeps track also of the string previously evaluated to reduce the number
* of evaluations
*
* @param s
* @return
*/
@Override
public final boolean accept(String s) {
// if already evaluated
if (acceptedByOracle.contains(s)) {
return true;
} else if (rejectedByOracle.contains(s)) {
return false;
} else {
// not evaluated yet
boolean isAccepted = oracle.accept(s);
if (isAccepted) {
acceptedByOracle.add(s);
} else {
rejectedByOracle.add(s);
}
return isAccepted;
}
}

public Set<String> getAcceptedByOracle() {
return acceptedByOracle;
}

public Set<String> getRejectedByOracle() {
return rejectedByOracle;
}

public int getNumEvaluatedStrings() {
return acceptedByOracle.size() + rejectedByOracle.size();
}

public int getNumAcceptedStrings() {
return acceptedByOracle.size();
}

public RegexOracle getOracle() {
return oracle;
}
}
11 changes: 11 additions & 0 deletions mutrex/src/regex/oracle/RegexOracle.java
@@ -0,0 +1,11 @@
package regex.oracle;

// given an example, it is able to accept or reject
// the cached version is in another class
// this one has no memory
public interface RegexOracle {

/** is string s accepted by the oracle */
public abstract boolean accept(String s);

}
5 changes: 5 additions & 0 deletions mutrex/src/regex/oracle/RgxAcception.java
@@ -0,0 +1,5 @@
package regex.oracle;

public enum RgxAcception {
FULLY_ACCEPTED, FULLY_REJECTED, PARTIALLY_ACCEPTED
}
26 changes: 26 additions & 0 deletions mutrex/src/regex/oracle/UserOracle.java
@@ -0,0 +1,26 @@
package regex.oracle;

import java.util.Scanner;

public class UserOracle implements RegexOracle {

@Override
public boolean accept(String s) {
return askUser(s, "accept");
}

private boolean askUser(String s, String acceptReject) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Do you " + acceptReject + " \"" + s + "\"?" + " Y for yes, N for no");
String line = in.nextLine();
if (line.equalsIgnoreCase("Y")) {
return true;
}
if (line.equalsIgnoreCase("N")) {
return false;
}
System.out.println("I did not understand.");
}
}
}

0 comments on commit f938067

Please sign in to comment.