Skip to content

Commit 681414a

Browse files
committed
Interpreter Design Pattern - Artificial Intelligence Chat Bot
added Expression and Context with rules
1 parent e5b6dbf commit 681414a

4 files changed

Lines changed: 102 additions & 18 deletions

File tree

pattern/src/com/premaseem/Client.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@
1010
public class Client {
1111
public static void main (String[] args) {
1212
System.out.println("Alexa welcomes you to Artificial intelligence app designed using Interpreter Design pattern :-) ");
13-
Scanner scanner = new Scanner(System.in);
13+
Scanner scanner1 = new Scanner(System.in);
14+
Scanner scanner2 = new Scanner(System.in);
1415

1516
// User input block
1617
String repeatRunFlag = "yes";
1718
while (!repeatRunFlag.equalsIgnoreCase("no")) {
1819
System.out.println("\n Please ask your question related to voting ...");
19-
String question = scanner.nextLine();
20-
Boolean result =true;
21-
String voteEligible = result ? "Yes ! can vote :-) ":"No ! cannot vote :-)";
20+
String question = scanner1.nextLine();
21+
22+
// get Voting context
23+
Context votingContext = new VotingContext();
24+
25+
// get Rules or grammar around context
26+
Expression ruleExpression = votingContext.getRuleExpression();
27+
28+
// question is interpreted based on context and rules
29+
Boolean result = ruleExpression.interpret(question);
2230

31+
// print Results
32+
String voteEligible = result ? "Yes ! can vote :-) ":"No ! cannot vote :-)";
2333
System.out.println("Alexa answered: " + voteEligible);
34+
35+
// Ask from another questions
2436
System.out.println("Do you have another questions ? yes / no ");
25-
repeatRunFlag = scanner.next();
37+
repeatRunFlag = scanner2.next();
2638
}
2739
}
2840
}
41+
42+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
interface Context {
9+
Expression getRuleExpression ();
10+
}
11+
12+
class VotingContext implements Context {
13+
14+
@Override
15+
public Expression getRuleExpression () {
16+
Expression condition_1 = new TerminalExpression("citizen");
17+
Expression condition_2 = new TerminalExpression("18");
18+
return new AndExpression(condition_1, condition_2);
19+
}
20+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.premaseem;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
interface Expression {
9+
public boolean interpret (String context);
10+
}
11+
12+
/**
13+
* Terminal Expression is used in other expressions for interpretation
14+
*/
15+
class TerminalExpression implements Expression {
16+
17+
private String data;
18+
19+
public TerminalExpression(String data){
20+
this.data = data;
21+
}
22+
23+
@Override
24+
public boolean interpret(String context) {
25+
if(context.contains(data)){
26+
return true;
27+
}
28+
return false;
29+
}
30+
}
31+
32+
class AndExpression implements Expression {
33+
34+
private Expression expr1 = null;
35+
private Expression expr2 = null;
36+
37+
public AndExpression(Expression expr1, Expression expr2) {
38+
this.expr1 = expr1;
39+
this.expr2 = expr2;
40+
}
41+
42+
@Override
43+
public boolean interpret(String context) {
44+
return expr1.interpret(context) && expr2.interpret(context);
45+
}
46+
}
47+
48+
class OrExpression implements Expression {
49+
50+
private Expression expr1 = null;
51+
private Expression expr2 = null;
52+
53+
public OrExpression(Expression expr1, Expression expr2) {
54+
this.expr1 = expr1;
55+
this.expr2 = expr2;
56+
}
57+
58+
@Override
59+
public boolean interpret(String context) {
60+
return expr1.interpret(context) || expr2.interpret(context);
61+
}
62+
}
63+

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)