Skip to content

Commit

Permalink
Implementing max number of attempts for S2R
Browse files Browse the repository at this point in the history
  • Loading branch information
ojcchar committed Jul 31, 2021
1 parent 89b7466 commit 88bf2a1
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import sealab.burt.server.statecheckers.participant.ParticipantIdStateChecker;
import sealab.burt.server.statecheckers.s2r.S2RDescriptionStateChecker;
import sealab.burt.server.statecheckers.s2r.S2RPredictionStateChecker;
import sealab.burt.server.statecheckers.s2r.StepInputStateChecker;
import sealab.burt.server.statecheckers.s2r.S2RInputStateChecker;
import sealab.burt.server.statecheckers.yesno.AffirmativeAnswerStateChecker;
import sealab.burt.server.statecheckers.yesno.NegativeAnswerStateChecker;
import seers.textanalyzer.TextProcessor;
Expand Down Expand Up @@ -64,7 +64,7 @@ class ConversationController {
// put(S2R_PREDICTED_SELECTED, new NStateChecker(CONFIRM_PREDICTED_SELECTED_S2R_SCREENS));
put(S2R_PREDICTED_SELECTED, new S2RPredictionStateChecker());
put(S2R_MISSING_SELECTED, new DefaultActionStateChecker(CONFIRM_SELECTED_MISSING_S2R));
put(S2R_INPUT, new StepInputStateChecker());
put(S2R_INPUT, new S2RInputStateChecker());

put(S2R_AMBIGUOUS_SELECTED, new S2RDescriptionStateChecker());
// put(S2R_AMBIGUOUS_SELECTED, new NStateChecker(CONFIRM_SELECTED_AMBIGUOUS_S2R));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static String getScreenshotPathForGraphState(GraphState graphState,
return getScreenshotPath(state, stateScreenshotPath, graphState.getDataSource());
}

private static String getScreenshotPath(ConversationState state,
public static String getScreenshotPath(ConversationState state,
String inputScreenshotPath,
GraphDataSource dataSource) {
Path screenshotPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public SpecifyInputS2RAction(Intent nextExpectedIntent) {

@Override
public List<ChatBotMessage> execute(ConversationState state) {

// UserResponse msg = (UserResponse) state.get(S2R_MATCHED_MSG);
// String highQualityStepMessage = msg.getMessages().get(0).getMessage();

return createChatBotMessages("It seems that no specific input or value was provided in the step.",
"Can you please provide an input (enclosed in quotes, e.g., \"5\")?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private List<AppStep> removeLastReportSteps(ConversationState state, List<AppSte
.subList(Math.max(stepElements.size() - 5, 0), stepElements.size())
.stream()
.map(el -> (AppStep) el.getOriginalElement())
.filter(Objects::nonNull)
.collect(Collectors.toList());

cleanedInferredSteps2 = cleanedInferredSteps.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<ChatBotMessage> execute(ConversationState state){
StringBuilder message = new StringBuilder("Oops, it seems ");
message.append(getFeedbackMessage(feedback));
return createChatBotMessages(message.toString(),
" Can you please rephrase the step more accurately or provide a different one?");
"Can you please rephrase the step more accurately or provide a different one?");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import sealab.burt.server.msgparsing.Intent;
import sealab.burt.server.output.BugReportElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,13 +72,16 @@ public List<ChatBotMessage> execute(ConversationState state) {
List<BugReportElement> bugReportElements = (List<BugReportElement>) state.get(REPORT_S2R);
AppStep lastStep = (AppStep) bugReportElements.get(bugReportElements.size() - 1).getOriginalElement();

if (DeviceUtils.isCloseApp(lastStep.getAction()))
return getNextStepMessage();
List<AppStep> stateLoops = new ArrayList<>();
if (lastStep != null) { //the last step can be null because of the max # of attempts functionality
if (DeviceUtils.isCloseApp(lastStep.getAction()))
return getNextStepMessage();

List<AppStep> stateLoops = predictor.getStateLoops(currentState, lastStep, nonSelectedSteps);
stateLoops = predictor.getStateLoops(currentState, lastStep, nonSelectedSteps);

if (stateLoops.isEmpty()) {
return getLastStepMessage(state);
if (stateLoops.isEmpty()) {
return getLastStepMessage(state);
}
}

pathsWithLoops = Collections.singletonList(stateLoops);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,113 @@

public @Slf4j
class AttemptManager {

public enum AttemptType {
//OB
OB_MATCHED, OB_NO_MATCH, OB_NOT_PARSED, OB_SCREENS,
//EB
EB_NO_MATCH, EB_NOT_PARSED,
//S2R
S2R_NO_MATCH, S2R_NOT_PARSED, S2R_MATCHED, S2R_INPUT, S2R_AMBIGUOUS
}

public static final Integer MAX_ATTEMPTS_OB = 3;
public static final Integer MAX_ATTEMPTS_EB = 3;
public static final Integer MAX_ATTEMPTS_S2R = 2;
public static final Integer MAX_ATTEMPTS_3 = 3;
public static final Integer MAX_ATTEMPTS_2 = 2;

private final HashMap<AttemptType, MutablePair<Integer, Integer>> attempts = new LinkedHashMap<>() {
{
//OB
put(AttemptType.OB_MATCHED, new MutablePair<>(MAX_ATTEMPTS_OB, -1));
put(AttemptType.OB_NO_MATCH, new MutablePair<>(MAX_ATTEMPTS_OB, -1));
put(AttemptType.OB_SCREENS, new MutablePair<>(MAX_ATTEMPTS_OB, -1));
put(AttemptType.OB_NOT_PARSED, new MutablePair<>(MAX_ATTEMPTS_OB, -1));
put(AttemptType.OB_MATCHED, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.OB_NO_MATCH, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.OB_SCREENS, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.OB_NOT_PARSED, new MutablePair<>(MAX_ATTEMPTS_3, -1));

//EB
put(AttemptType.EB_NO_MATCH, new MutablePair<>(MAX_ATTEMPTS_EB, -1));
put(AttemptType.EB_NOT_PARSED, new MutablePair<>(MAX_ATTEMPTS_EB, -1));
put(AttemptType.EB_NO_MATCH, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.EB_NOT_PARSED, new MutablePair<>(MAX_ATTEMPTS_3, -1));

//S2R
put(AttemptType.S2R_NO_MATCH, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.S2R_NOT_PARSED, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.S2R_AMBIGUOUS, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.S2R_MATCHED, new MutablePair<>(MAX_ATTEMPTS_3, -1));
put(AttemptType.S2R_INPUT, new MutablePair<>(MAX_ATTEMPTS_3, -1));

}
};

//---------------------------------------------------

public void initOrIncreaseCurrentAttemptS2RInput() {
initOrIncreaseCurrentAttempt(AttemptType.S2R_INPUT);
}

public boolean checkNextAttemptAndResetS2RInput() {
return checkNextAttemptAndReset(AttemptType.S2R_INPUT);
}

public void resetCurrentAttemptS2RInput() {
resetAttempt(AttemptType.S2R_INPUT);
}

//---------------------------------------------------

public void initOrIncreaseCurrentAttemptS2RMatch() {
initOrIncreaseCurrentAttempt(AttemptType.S2R_MATCHED);
}

public boolean checkNextAttemptAndResetS2RMatch() {
return checkNextAttemptAndReset(AttemptType.S2R_MATCHED);
}

public void resetCurrentAttemptS2RMatch() {
resetAttempt(AttemptType.S2R_MATCHED);
}

//---------------------------------------------------

public void initOrIncreaseCurrentAttemptS2RNotParsed() {
initOrIncreaseCurrentAttempt(AttemptType.S2R_NOT_PARSED);
}

public boolean checkNextAttemptAndResetS2RNotParsed() {
return checkNextAttemptAndReset(AttemptType.S2R_NOT_PARSED);
}

public void resetCurrentAttemptS2RNotParsed() {
resetAttempt(AttemptType.S2R_NOT_PARSED);
}

//---------------------------------------------------

public void initOrIncreaseCurrentAttemptS2RNoMatch() {
initOrIncreaseCurrentAttempt(AttemptType.S2R_NO_MATCH);
}

public boolean checkNextAttemptAndResetS2RNoMatch() {
return checkNextAttemptAndReset(AttemptType.S2R_NO_MATCH);
}

public void resetCurrentAttemptS2RNoMatch() {
resetAttempt(AttemptType.S2R_NO_MATCH);
}

//---------------------------------------------------

public void initOrIncreaseCurrentAttemptS2RAmbiguous() {
initOrIncreaseCurrentAttempt(AttemptType.S2R_AMBIGUOUS);
}

public boolean checkNextAttemptAndResetS2RAmbiguous() {
return checkNextAttemptAndReset(AttemptType.S2R_AMBIGUOUS);
}

public void resetCurrentAttemptS2RAmbiguous() {
resetAttempt(AttemptType.S2R_AMBIGUOUS);
}

//---------------------------------------------------


public void initOrIncreaseCurrentAttemptEbNoMatch() {
initOrIncreaseCurrentAttempt(AttemptType.EB_NO_MATCH);
Expand Down Expand Up @@ -165,7 +245,7 @@ private void initOrIncreaseCurrentAttempt(AttemptType attemptType) {
Integer currentAttempt = getCurrentAttempt(attemptType);
if (currentAttempt != -1) {
setCurrentAttempt(attemptType, currentAttempt + 1);
}else{
} else {
setCurrentAttempt(attemptType, 1);
}
}
Expand All @@ -174,7 +254,7 @@ private void increaseCurrentAttempt(AttemptType attemptType) {
Integer currentAttempt = getCurrentAttempt(attemptType);
if (currentAttempt != -1) {
setCurrentAttempt(attemptType, currentAttempt + 1);
} else{
} else {
log.warn(String.format("The current attempt (%s) was not initiated: %s",
attemptType,
currentAttempt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,75 @@ public void initOrIncreaseCurrentAttemptEbNotParsed() {
public boolean checkNextAttemptAndResetEbNotParsed() {
return attemptManager.checkNextAttemptAndResetEbNotParsed();
}

//------------------------------

public void initOrIncreaseCurrentAttemptS2RNotParsed() {
attemptManager.initOrIncreaseCurrentAttemptS2RNotParsed();
}

public boolean checkNextAttemptAndResetS2RNotParsed() {
return attemptManager.checkNextAttemptAndResetS2RNotParsed();
}

public void resetCurrentAttemptS2RNotParsed() {
attemptManager.resetCurrentAttemptS2RNotParsed();
}

//------------------------------

public void initOrIncreaseCurrentAttemptS2RNoMatch() {
attemptManager.initOrIncreaseCurrentAttemptS2RNoMatch();
}

public boolean checkNextAttemptAndResetS2RNoMatch() {
return attemptManager.checkNextAttemptAndResetS2RNoMatch();
}

public void resetCurrentAttemptS2RNoMatch() {
attemptManager.resetCurrentAttemptS2RNoMatch();
}

//------------------------------

public void initOrIncreaseCurrentAttemptS2RAmbiguous() {
attemptManager.initOrIncreaseCurrentAttemptS2RAmbiguous();
}

public boolean checkNextAttemptAndResetS2RAmbiguous() {
return attemptManager.checkNextAttemptAndResetS2RAmbiguous();
}

public void resetCurrentAttemptS2RAmbiguous() {
attemptManager.resetCurrentAttemptS2RAmbiguous();
}

//------------------------------

public void initOrIncreaseCurrentAttemptS2RMatch() {
attemptManager.initOrIncreaseCurrentAttemptS2RMatch();
}

public boolean checkNextAttemptAndResetS2RMatch() {
return attemptManager.checkNextAttemptAndResetS2RMatch();
}

public void resetCurrentAttemptS2RMatch() {
attemptManager.resetCurrentAttemptS2RMatch();
}

//-------------------------------------

public void initOrIncreaseCurrentAttemptS2RInput() {
attemptManager.initOrIncreaseCurrentAttemptS2RInput();
}

public boolean checkNextAttemptAndResetS2RInput() {
return attemptManager.checkNextAttemptAndResetS2RInput();
}

public void resetCurrentAttemptS2RInput() {
attemptManager.resetCurrentAttemptS2RInput();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sealab.burt.qualitychecker.graph.GraphTransition;
import sealab.burt.qualitychecker.s2rquality.S2RQualityAssessment;
import sealab.burt.server.actions.commons.ScreenshotPathUtils;
import sealab.burt.server.conversation.state.ConversationState;
import sealab.burt.server.output.BugReportElement;

import java.util.ArrayList;
Expand All @@ -28,15 +27,23 @@ public static void addStepAndUpdateGraphState(ConversationState state,
String stringStep,
S2RQualityAssessment assessment) {
List<BugReportElement> stepElements = (List<BugReportElement>) state.get(REPORT_S2R);
AppStep appStep = assessment.getMatchedSteps().get(0);
String screenshotFile = ScreenshotPathUtils.getScreenshotPathForStep(appStep, state);
if (!state.containsKey(REPORT_S2R)) {
stepElements = new ArrayList<>(Collections.singletonList(
new BugReportElement(stringStep, appStep, screenshotFile)
));


AppStep appStep = null;
String screenshotFile;
if (assessment != null) {
appStep = assessment.getMatchedSteps().get(0);
screenshotFile = ScreenshotPathUtils.getScreenshotPathForStep(appStep, state);

} else {
stepElements.add(new BugReportElement(stringStep, appStep, screenshotFile));
screenshotFile = ScreenshotPathUtils.getScreenshotPath(state, null, null);
}

if (stepElements == null) {
stepElements = new ArrayList<>();
}

stepElements.add(new BugReportElement(stringStep, appStep, screenshotFile));
state.put(REPORT_S2R, stepElements);

//---------------------
Expand All @@ -47,6 +54,7 @@ public static void addStepAndUpdateGraphState(ConversationState state,
}

private static void updateStateBasedOnStep(AppStep appStep, S2RChecker s2rChecker) {
if (appStep == null || s2rChecker == null) return;
GraphTransition transition = appStep.getTransition();
if (transition != null)
s2rChecker.updateState(transition.getTargetState());
Expand Down Expand Up @@ -83,7 +91,7 @@ public static void addStepsToState(ConversationState state,

private static List<BugReportElement> getBugReportElementsFromSteps(List<AppStep> selectedSteps,
ConversationState state) {
//we need to return a modifiable list
//we need to return a modifiable list, that's why we create a new ArrayList
return new ArrayList<>(selectedSteps.stream()
.map(step -> {
String screenshotFile = ScreenshotPathUtils.getScreenshotPathForStep(step, state);
Expand Down
Loading

0 comments on commit 88bf2a1

Please sign in to comment.