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
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CLIPSNET;
namespace RouterFormsExample
{
class RouterTextBox : System.Windows.Forms.TextBox
{
private class RouterThreadBridge
{
public bool charNeeded = false;
public bool closed = false;
public List<Byte> charList = new List<Byte>();
}
private class TextBoxRouter : Router
{
delegate void AddTextCallback(string text);
static int RouterTextBoxNameIndex = 0;
private RouterTextBox m_RouterTextBox;
public TextBoxRouter(
RouterTextBox theTextBox,
int thePriority) : base("RouterTextBox" + RouterTextBoxNameIndex++,thePriority)
{
m_RouterTextBox = theTextBox;
}
public override bool Query(String logicalName)
{
if (logicalName.Equals(CLIPSNET.Router.STDOUT) ||
logicalName.Equals(CLIPSNET.Router.STDIN))
return true;
else
return false;
}
public void AddText(string text)
{
/*=====================================================*/
/* If we're attempting to modify the control from a */
/* different thread from the one that created it, then */
/* we need to use Invoke to handle the modification. */
/*=====================================================*/
if (m_RouterTextBox.InvokeRequired)
{
Form parentForm = m_RouterTextBox.FindForm();
AddTextCallback d = new AddTextCallback(AddText);
parentForm.Invoke(d, new object[] { text });
}
/*===================================*/
/* Otherwise the thread that created */
/* it can process the modification. */
/*===================================*/
else
{ m_RouterTextBox.AppendText(text); }
}
public override void Write(String logicalName, String printString)
{
this.AddText(printString);
}
public override int Read(String logicalName)
{
RouterThreadBridge theBridge = this.m_RouterTextBox.m_ThreadBridge;
lock (theBridge)
{
if (theBridge.closed)
{
this.m_RouterTextBox.attachedEnv.SetHaltExecution(true);
return -1;
}
if (theBridge.charList.Count == 0)
{
theBridge.charNeeded = true;
try
{ Monitor.Wait(theBridge); }
catch (SynchronizationLockException e)
{ Console.WriteLine(e); }
catch (ThreadInterruptedException e)
{ Console.WriteLine(e); }
}
theBridge.charNeeded = false;
if (theBridge.closed)
{
this.m_RouterTextBox.attachedEnv.SetHaltExecution(true);
return -1;
}
Byte theByte = theBridge.charList[0];
theBridge.charList.RemoveAt(0);
return theByte;
}
}
public override int Unread(String logicalName,int theChar)
{
lock (this.m_RouterTextBox.m_ThreadBridge)
{
this.m_RouterTextBox.m_ThreadBridge.charList.Insert(0,(Byte) theChar);
}
return 0;
}
}
private CLIPSNET.Environment attachedEnv;
private TextBoxRouter m_TextBoxRouter;
private RouterThreadBridge m_ThreadBridge;
public RouterTextBox() : base()
{
m_TextBoxRouter = new TextBoxRouter(this,10);
m_ThreadBridge = new RouterThreadBridge();
this.AcceptsReturn = true;
this.ReadOnly = true;
this.Multiline = true;
}
/****************/
/* AttachRouter */
/****************/
public void AttachRouter(
CLIPSNET.Environment theEnv)
{
attachedEnv = theEnv;
theEnv.AddRouter(m_TextBoxRouter);
}
/****************/
/* DetachRouter */
/****************/
public void DetachRouter()
{
this.ReadOnly = true;
}
/*************/
/* OnClosing */
/*************/
public void OnClosing()
{
lock (m_ThreadBridge)
{
m_ThreadBridge.closed = true;
if (m_ThreadBridge.charNeeded)
{
m_ThreadBridge.charNeeded = false;
Monitor.Pulse(m_ThreadBridge);
}
}
}
/*************/
/* OnKeyDown */
/*************/
protected override void OnKeyDown(KeyEventArgs e)
{
lock (m_ThreadBridge)
{
if (m_ThreadBridge.charNeeded)
{
if (((e.KeyCode == Keys.Delete) || (e.KeyCode == Keys.Back)) &&
(attachedEnv.InputBufferCount() == 0))
{ /* Ignore */ }
else
{ this.ReadOnly = false; }
}
}
base.OnKeyDown(e);
}
/***********/
/* OnKeyUp */
/***********/
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
this.ReadOnly = true;
}
/**************/
/* OnKeyPress */
/**************/
protected override void OnKeyPress(
KeyPressEventArgs e)
{
lock (m_ThreadBridge)
{
if (m_ThreadBridge.charNeeded)
{
m_ThreadBridge.charList.AddRange(Encoding.UTF8.GetBytes(e.KeyChar.ToString()));
this.Select(this.TextLength,this.TextLength);
this.ScrollToCaret();
base.OnKeyPress(e);
m_ThreadBridge.charNeeded = false;
Monitor.Pulse(m_ThreadBridge);
}
}
}
}
}