-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
209 lines (189 loc) · 7.47 KB
/
MainForm.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Mommosoft.ExpertSystem;
namespace JakaGra
{
public partial class MainForm : Form
{
Localization l = new Localization();
private Mommosoft.ExpertSystem.Environment clips = new Mommosoft.ExpertSystem.Environment();
public MainForm()
{
ConsoleTraceListener tl = new ConsoleTraceListener();
InitializeComponent();
clips.AddRouter(new DebugRouter());
clips.Load("bazaaut.clp");
clips.Reset();
clips.Run(1);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
NextUIState();
}
private void OnClickButton(object sender, EventArgs e)
{
if ((string)nextButton.Tag == "Next")
{
if (choicesPanel.Controls.Count != 0)
{
if (isNumeric)
{
var nud = choicesPanel.Controls.OfType<NumericUpDown>().First();
var xd = clips.AssertString("(" + relationAsserted + " " + nud.Value + ")");
Console.WriteLine("(" + relationAsserted + " " + nud.Value + ") ");
}
else if (isCheckbox)
{
var checkedButtons = choicesPanel.Controls.OfType<CheckBox>()
.Where(r => r.Checked);
foreach (var box in checkedButtons)
{
var xd = clips.AssertString("(" + relationAsserted + " " + box.Tag + ")");
Console.WriteLine("(" + relationAsserted + " " + box.Tag + ") ");
}
}
else if (isYesNo)
{
var checkedButton = choicesPanel.Controls.OfType<RadioButton>()
.First(r => r.Checked);
if ((string) checkedButton.Tag == "yes")
{
var xd = clips.AssertString("(" + relationAsserted + ")");
Console.WriteLine("(" + relationAsserted + ")");
}
}
else
{
var checkedButton = choicesPanel.Controls.OfType<RadioButton>()
.First(r => r.Checked);
var xd = clips.AssertString("(" + relationAsserted + " " + checkedButton.Tag + ")");
Console.WriteLine("(" + relationAsserted + " " + checkedButton.Tag + ") ");
}
}
else
{
clips.AssertString("(start)");
}
clips.Run(1);
NextUIState();
}
else if ((string)nextButton.Tag == "Restart")
{
clips.Reset();
clips.Run(1);
NextUIState();
}
}
private string lastRelationAsserted = "";
private string relationAsserted;
bool isNumeric = false;
private bool isCheckbox = false;
private bool isYesNo = false;
private void NextUIState()
{
choicesPanel.Controls.Clear();
String evalStr = "(find-all-facts ((?f UI-state)) TRUE)";
using (var result = (MultifieldValue)clips.Eval(evalStr))
{
var fv = (FactAddressValue)result[0];
relationAsserted = fv.GetFactSlot("relation-asserted").ToString();
if (relationAsserted == lastRelationAsserted)
{
clips.Run(1);
NextUIState();
return;
}
lastRelationAsserted = relationAsserted;
isNumeric = false;
isYesNo = false;
isCheckbox = false;
switch (fv.GetFactSlot("state").ToString())
{
case "numeric":
nextButton.Text = l["Next"];
nextButton.Tag = "Next";
isNumeric = true;
break;
case "checkbox":
nextButton.Text = l["Next"];
nextButton.Tag = "Next";
isCheckbox = true;
break;
case "yesno":
nextButton.Text = l["Next"];
nextButton.Tag = "Next";
isYesNo = true;
break;
case "final":
nextButton.Text = l["Restart"];
nextButton.Tag = "Restart";
Console.WriteLine("***ZACZYNAMY OD NOWA***");
break;
case "initial":
nextButton.Text = l["Next"];
nextButton.Tag = "Next";
break;
default:
nextButton.Text = l["Next"];
nextButton.Tag = "Next";
break;
}
choicesPanel.Controls.Clear();
if (isNumeric)
{
var nud = new NumericUpDown();
nud.Increment = 1;
nud.DecimalPlaces = 0;
nud.Maximum = 500;
choicesPanel.Controls.Add(nud);
}
else if (isCheckbox)
{
var va = (MultifieldValue)fv.GetFactSlot("valid-answers");
foreach (var item in va)
{
var rb = new CheckBox();
rb.Text = l[item.ToString()];
rb.Tag = item.ToString();
rb.AutoSize = true;
choicesPanel.Controls.Add(rb);
}
}
else if (isYesNo)
{
var rbYes = new RadioButton();
rbYes.Text = l["yes"];
rbYes.Tag = "yes";
rbYes.Checked = fv.GetFactSlot("response").ToString() == "yes";
var rbNo = new RadioButton();
rbNo.Text = l["no"];
rbNo.Tag = "no";
rbNo.Checked = !rbYes.Checked;
choicesPanel.Controls.Add(rbYes);
choicesPanel.Controls.Add(rbNo);
}
else
{
var va = (MultifieldValue)fv.GetFactSlot("valid-answers");
var selected = fv.GetFactSlot("response").ToString();
foreach (var item in va)
{
var rb = new RadioButton();
rb.Text = l[item.ToString()];
rb.Checked = item.ToString() == selected;
rb.Tag = item.ToString();
rb.AutoSize = true;
choicesPanel.Controls.Add(rb);
}
}
messageLabel.Text = l[fv.GetFactSlot("display").ToString()];
}
}
private void messageLabel_Click(object sender, EventArgs e)
{
}
}
}