battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionAXPrompt get user info EmptyAXPrompt get user info

more_horiz

Code:


public class Prompt {
    RegexUtil regexUtil = new RegexUtil();
    public AXKeyValuePair kv = new AXKeyValuePair();
    String prompt = "";
    String regex = "";
    public Prompt() {
        kv.setKey("default");
    }

    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }
    public Boolean process(String in1){
        kv.setValue(regexUtil.extractRegex(regex,in1));
        return kv.getValue().isEmpty(); // is prompt still active?
    }

    public AXKeyValuePair getKv() {
        return kv;
    }

    public void setRegex(String regex) {
        this.regex = regex;
    }
}


Code:


public class AXPrompt {
    /*
    * example use:
    *
    *  // prompt1
        Prompt prompt1 = new Prompt();
        prompt1.kv.setKey("fruit");
        prompt1.setPrompt("do you prefer an apple, banana or grapes?");
        prompt1.setRegex("apple|banana|grapes");
        // prompt 2
        Prompt prompt2 = new Prompt();
        prompt2.kv.setKey("age");
        prompt2.setPrompt("how old are you??");
        prompt2.setRegex("\\d+(\\.\\d+)?");
        Scanner scanner = new Scanner(System.in);
        String in2 = "";
        AXPrompt axPrompt = new AXPrompt();
        axPrompt.addPrompt(prompt1);
        axPrompt.addPrompt(prompt2);
        axPrompt.activate();
        do{
            System.out.println(axPrompt.getPrompt());
            in2 = scanner.nextLine();
            axPrompt.process(in2);
            // extract keyvaluepair:
            AXKeyValuePair temp = axPrompt.getKv();
            // extract data: field, value
            if (!(temp == null)){
                System.out.println(temp.getValue());
            }
        }
        while (axPrompt.getActive());
    * */
    Boolean isActive = false;
    int index = 0;
    ArrayList<Prompt> prompts = new ArrayList<Prompt>();
    AXKeyValuePair kv = null;
    public void addPrompt(Prompt p1){
        prompts.add(p1);
    }
    public String getPrompt(){
        if (prompts.isEmpty()){return "";}
        return prompts.get(index).getPrompt();
    }
    public void process(String in1){
        if (prompts.isEmpty() || !isActive){return;}
        Boolean b1= prompts.get(index).process(in1);
        if (!b1){
            kv = prompts.get(index).getKv();
            index++;
        }
        if(index == prompts.size()){isActive = false;}
    }

    public Boolean getActive() {
        return isActive;
    }

    public AXKeyValuePair getKv() {
        if (kv == null){
            return null;
        }
        AXKeyValuePair temp = new AXKeyValuePair();
        temp.setKey(kv.getKey());
        temp.setValue(kv.getValue());
        kv = null;
        return temp;
    }
    public void activate(){
        // reset
        isActive = true;
        index = 0;
    }
    public void deactivate(){
        // reset
        isActive = false;
        index = 0;
    }
}

use example:


Code:


        // prompt1
        Prompt prompt1 = new Prompt();
        prompt1.kv.setKey("fruit");
        prompt1.setPrompt("do you prefer an apple, banana or grapes?");
        prompt1.setRegex("apple|banana|grapes");
        // prompt 2
        Prompt prompt2 = new Prompt();
        prompt2.kv.setKey("age");
        prompt2.setPrompt("how old are you??");
        prompt2.setRegex("\\d+(\\.\\d+)?");
        Scanner scanner = new Scanner(System.in);
        String in2 = "";
        AXPrompt axPrompt = new AXPrompt();
        axPrompt.addPrompt(prompt1);
        axPrompt.addPrompt(prompt2);
        axPrompt.activate();
        do{
            System.out.println(axPrompt.getPrompt());
            in2 = scanner.nextLine();
            axPrompt.process(in2);
            // extract keyvaluepair:
            AXKeyValuePair temp = axPrompt.getKv();
            // extract data: field, value
            if (!(temp == null)){
                System.out.println(temp.getValue());
            }
        }
        while (axPrompt.getActive());

output and user input example:
do you prefer an apple, banana or grapes?
grapes
grapes
how old are you??
36
36

Last edited by Moti Barski on Fri Nov 10, 2023 8:20 pm; edited 1 time in total

descriptionAXPrompt get user info EmptyRe: AXPrompt get user info

more_horiz
this has several uses:


  1. collect data (user name, email, age, favorite drink)
  2. run small conversations // like in Replika
  3. run visual novel text prompts
  4. combined with a switch case to select the active AXPrompt object can add several paths to a visual novel


alert the Prompt class uses a regex to filter acceptable results

in the above example:

apple|banana|grapes is a regex for any of this 3 options
orange for example will not be acceptable and the prompt will not move forward to the next
prompt(question)

the regex ^(?!\s*$).+ means not empty string.
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply