-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathInterpreterExample2.cs
160 lines (120 loc) · 4.22 KB
/
InterpreterExample2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//-------------------------------------------------------------------------------------
// InterpreterExample2.cs
//-------------------------------------------------------------------------------------
using System;
using UnityEngine;
using System.Collections;
using System.Globalization;
using System.Reflection;
public class InterpreterExample2 : MonoBehaviour
{
void Start ( )
{
string question1 = "2 Gallons to pints";
AskQuestion(question1);
string question2 = "4 Gallons to tablespoons";
AskQuestion(question2);
}
protected void AskQuestion(string question)
{
ConversionContext context = new ConversionContext(question);
string fromConversion = context.fromConversion; // in this example fromConversion is always the second word
string toConversion = context.toConversion;
double quantity = context.quantity;
// Trying to get a matching class for the word "fromConversion"
try
{
// Getting the type, we also have to define the namespace (in this case InterpreterPattern as defined above)
// and fromConversion should hold the class name (in this case Gallons)
Type type = Type.GetType("InterpreterPattern." + fromConversion);
object instance = Activator.CreateInstance(type);
Expression expression = instance as Expression;
// Get the matching method: e.g. (toConversion = pints)
MethodInfo method = type.GetMethod(toConversion);
string result = (string)method.Invoke(instance, new object[] { quantity });
Debug.Log("Output: " + quantity.ToString() + " " + fromConversion + " are " + result + " " + toConversion);
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
}
// Context object that does try to make sense of an input string:
public class ConversionContext
{
public string conversionQues { get; protected set; }
public string fromConversion { get; protected set; }
public string toConversion { get; protected set; }
public double quantity { get; protected set; }
protected string[] partsOfQues;
// here happens the sensemaking
public ConversionContext(string input)
{
Debug.Log("Input: " + input);
this.conversionQues = input;
this.partsOfQues = input.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
if (partsOfQues.Length >= 4)
{
fromConversion = GetCapitalized(partsOfQues[1]);
// 1 gallon to pints
toConversion = GetLowerCase(partsOfQues[3]);
// get quantitiy:
double quant;
double.TryParse(partsOfQues[0], out quant);
this.quantity = quant;
}
}
// Some helper methods:
protected string GetCapitalized(string word)
{
word = word.ToLower();
word = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);
// make sure a 's' is appended
if (word.EndsWith("s") == false)
{
word += "s";
}
return word;
}
protected string GetLowerCase(string word)
{
return word.ToLower();
}
}
// Definition of all the things the concrete expression
// shall be able to convert into
public abstract class Expression
{
public abstract string gallons(double quantity);
public abstract string quarts(double quantity);
public abstract string pints(double quantity);
public abstract string cups(double quantity);
public abstract string tablespoons(double quantity);
}
// concrete class
public class Gallons : Expression
{
#region implemented abstract members of Expression
public override string gallons(double quantity)
{
return quantity.ToString();
}
public override string quarts(double quantity)
{
return (quantity * 4).ToString();
}
public override string pints(double quantity)
{
return (quantity * 8).ToString();
}
public override string cups(double quantity)
{
return (quantity * 16).ToString();
}
public override string tablespoons(double quantity)
{
return (quantity * 256).ToString();
}
#endregion
}