Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package regex.oracle; | ||
|
||
public enum RgxAcception { | ||
FULLY_ACCEPTED, FULLY_REJECTED, PARTIALLY_ACCEPTED | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} |