-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpSystem.cs
435 lines (373 loc) · 16.2 KB
/
ExpSystem.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Globalization;
namespace ExpertSystem
{
class ExpSystem
{
//================== Special data types ===============================
public class CRelation
{
CRelation()
{
mRelationName = "";
mRelationType = 0;
}
public CRelation(string name, int type)
{
mRelationName = name;
mRelationType = type;
}
public string getRelationName()
{
return mRelationName;
}
public int getRelationType()
{
return mRelationType;
}
private string mRelationName;
private int mRelationType;
}
//--------------------------------------------------------
public class CFinalRelation
{
public CFinalRelation()
{
mObj1Code = -1;
mObj1Name = "?";
mRelCode = -1;
mRelName = "?";
mObj2Code = -1;
mObj2Name = "?";
}
public CFinalRelation( int obj1Code, string obj1Name, int relationCode, string relationName, int obj2Code, string obj2Name )
{
mObj1Code = obj1Code;
mObj1Name = obj1Name;
mRelCode = relationCode;
mRelName = relationName;
mObj2Code = obj2Code;
mObj2Name = obj2Name;
}
public int mObj1Code;
public string mObj1Name;
public int mRelCode;
public string mRelName;
public int mObj2Code;
public string mObj2Name;
}
//================== C-tor ===============================
public ExpSystem(string path)
{
expertSystemCretion(path);
}
//================== Methods ===============================
public Dictionary<int, string> getObjects()
{
return objects;
}
public Dictionary<int, CRelation> getRelations()
{
return relations;
}
public int[,] getGivenRelationMatrix()
{
return givenRelationMatrix;
}
public int getGivenRelationsListSize()
{
return givenRelations.Count;
}
public int[,,] getRelationMatrix()
{
return relationMatrix;
}
public int getRelationMatrixSize()
{
return maxVal;
}
public List<CFinalRelation> getOutputRelations()
{
return mOutputRelations;
}
public void expertSystemCretion(string path)
{
maxVal = 0;
mDataPath = path;
string[] lines = System.IO.File.ReadAllLines(mDataPath);
string[] splittedData;
int dataTypeMode = 0;
//input data parsing
foreach (string s in lines)
{
mInputDate.Add(s);
}
relations.Add(0, new CRelation("not connected", -1));
foreach (string s in mInputDate)
{
if (s == "#1")
{
dataTypeMode = 1;
}
else if (s == "#2")
{
dataTypeMode = 2;
}
else if (s == "#3")
{
dataTypeMode = 3;
}
if (dataTypeMode == 1)
{
if (s != "" && !s.StartsWith("#"))
{
splittedData = s.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
objects.Add(Convert.ToInt32(splittedData[0]), splittedData[1]);
}
else if (s == "")
{
//Console.WriteLine("----");
}
}
else if (dataTypeMode == 2)
{
if (s != "" && !s.StartsWith("#"))
{
splittedData = s.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
relations.Add(Convert.ToInt32(splittedData[0]), new CRelation(splittedData[1], Convert.ToInt32(splittedData[2])));
}
else if (s == "")
{
//Console.WriteLine("----");
}
}
else if (dataTypeMode == 3)
{
if (s != "" && !s.StartsWith("#") && s != "" )
{
givenRelations.Add(s);
//Console.WriteLine(s);
}
}
}
foreach( var pair in objects )
{
if( Convert.ToInt32(pair.Key) > maxVal )
{
maxVal = Convert.ToInt32(pair.Key) + 1;
}
}
givenRelationMatrix = new int[givenRelations.Count, 3];
//Console.WriteLine("-----------------------------------------------");
//creating and filling matrix of the given relations
for (int i = 0; i < givenRelations.Count(); i++ )
{
//Console.WriteLine(givenRelations[i]);
splittedData = givenRelations[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
givenRelationMatrix[i,0] = Convert.ToInt32(splittedData[0]);
givenRelationMatrix[i,1] = Convert.ToInt32(splittedData[1]);
givenRelationMatrix[i,2] = Convert.ToInt32(splittedData[2]);
//Console.WriteLine(Convert.ToInt32(splittedData[0]).ToString() + "*" + Convert.ToInt32(splittedData[1]).ToString() + "*" + Convert.ToInt32(splittedData[2]).ToString());
}
Console.WriteLine(maxVal);
//creating and initialising relation matrix
relationMatrix = new int[maxVal, maxVal, 2];//1st dim for storing relation number(coded name), 2d dimension for storing relation type (rank)
for ( int i = 0; i < maxVal; i++ )
{
for (int j = 0; j < maxVal; j++)
{
if ( (i == 0 && j == 0 ) )
{
relationMatrix[i, j, 0] = -1;
relationMatrix[i, j, 1] = -1;
}
else if ( i == 0 )
{
relationMatrix[i, j, 0] = j;
relationMatrix[i, j, 1] = j;
}
else if (j == 0)
{
relationMatrix[i, j, 0] = -i;
relationMatrix[i, j, 1] = -i;
}
else
{
relationMatrix[i, j, 0] = -1;
relationMatrix[i, j, 1] = -1;
}
}
}
//filling relation matrix with given relations
for (int i = 0; i < givenRelations.Count(); i++)
{
int row = givenRelationMatrix[i, 0];
int col = givenRelationMatrix[i, 2];
int relationCodedName = givenRelationMatrix[i, 1];
relationMatrix[row, col, 0] = relationCodedName;
relationMatrix[row, col, 1] = relations[relationCodedName].getRelationType();
mOutputRelations.Add(new CFinalRelation(row, objects[row], relationCodedName, relations[relationCodedName].getRelationName(), col, objects[col]));
}
//making new connections
bool newConnectionAdded = true;
while (newConnectionAdded == true)
{
for (int i = 0; i < givenRelations.Count(); i++)
{
//<x A y1>
int x = givenRelationMatrix[i, 0];
int relation_A = relations[givenRelationMatrix[i, 1]].getRelationType();
int relA_code = givenRelationMatrix[i, 1];
int y1 = givenRelationMatrix[i, 2];
for (int j = 0; j < givenRelations.Count(); j++)
{
//<y2 B z>
int y2 = givenRelationMatrix[j, 0];
int relation_B = relations[givenRelationMatrix[j, 1]].getRelationType();
int relB_code = givenRelationMatrix[j, 1];
int z = givenRelationMatrix[j, 2];
if (( (relation_A > relation_B) && relation_B != 0 ) && (y1 == y2 ) )
{
if (!mOutputRelations.Contains(new CFinalRelation(x, objects[x],
relA_code, relations[relA_code].getRelationName(),
z, objects[z])))
{
mOutputRelations.Add(new CFinalRelation(x, objects[x],
relA_code, relations[relA_code].getRelationName(),
z, objects[z]));
relationMatrix[x, z, 0] = relA_code;
relationMatrix[x, z, 1] = relation_A;
}
//x = givenRelationMatrix[i, 0];
//relation_A = relations[givenRelationMatrix[i, 1]].getRelationType();
//relA_code = givenRelationMatrix[i, 1];
y1 = z;
//givenRelationMatrix[i, 0] : relation_A : givenRelationMatrix[j, 2];
newConnectionAdded = true;
}
else if((relation_A == relation_B) && (y1 == y2) )
{
if (!mOutputRelations.Contains(new CFinalRelation(x, objects[x],
relA_code, relations[relA_code].getRelationName(),
z, objects[z])))
{
mOutputRelations.Add(new CFinalRelation(x, objects[x],
relA_code, relations[relA_code].getRelationName(),
z, objects[z]));
relationMatrix[x, z, 0] = relA_code;
relationMatrix[x, z, 1] = relation_A;
}
y1 = z;
//givenRelationMatrix[i, 0] : relation_A : givenRelationMatrix[j, 2];
newConnectionAdded = true;
}
else if (( ( relation_A < relation_B) || (relation_B == 0)) && (y1 == y2) )
{
if(!mOutputRelations.Contains(new CFinalRelation(x, objects[x],
relB_code, relations[relB_code].getRelationName(),
z, objects[z])))
{
mOutputRelations.Add(new CFinalRelation(x, objects[x],
relB_code, relations[relB_code].getRelationName(),
z, objects[z]));
relationMatrix[x, z, 0] = relB_code;
relationMatrix[x, z, 1] = relation_B;
}
relation_A = relations[relB_code].getRelationType();
relA_code = relB_code;
y1 = z;
//givenRelationMatrix[i, 0] : relation_B : givenRelationMatrix[j, 2];
newConnectionAdded = true;
}
}
}
newConnectionAdded = false;
}
}
public void queryAnalyser( string query )
{
string[] qwestion = query.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (qwestion[0] != "?" && qwestion[1] != "?" && qwestion[2] != "?")
{
int i = Convert.ToInt32(qwestion[0]);
int j = Convert.ToInt32(qwestion[2]);
int k = Convert.ToInt32(qwestion[1]);
if (relationMatrix[i, j, 0] == k)
{
Console.WriteLine("Yes");
Console.WriteLine(objects[i] + " " + relations[k].getRelationName() + " " + objects[j]);
}
else
{
Console.WriteLine("No");
Console.WriteLine("There is no such connection!");
}
}
else
{
string reqObj1Param = qwestion[0];
int reqObj1Code = 0;
string reqConnectionParam = qwestion[1];
int reqConnectionCode = 0;
string reqObj2Param = qwestion[2];
int reqObj2Code = 0;
if (reqObj1Param == "?")
{
reqObj1Code = -1;
}
else
{
reqObj1Code = Convert.ToInt32(reqObj1Param);
}
if (reqConnectionParam == "?")
{
reqConnectionCode = -1;
}
else
{
reqConnectionCode = Convert.ToInt32(reqConnectionParam);
}
if (reqObj2Param == "?")
{
reqObj2Code = -1;
}
else
{
reqObj2Code = Convert.ToInt32(reqObj2Param);
}
int count = 0;
foreach (CFinalRelation rel in mOutputRelations )
{
if (( reqObj1Code == -1 || reqObj1Code == rel.mObj1Code) &&
(reqConnectionCode == -1 || reqConnectionCode == rel.mRelCode) &&
( reqObj2Code == -1 || reqObj2Code == rel.mObj2Code) )
{
Console.WriteLine(rel.mObj1Name + " : " + rel.mRelName + " : " + rel.mObj2Name);
count++;
}
}
if ( count == 0)
{
//Console.WriteLine("No connections pass this template");
Console.WriteLine(objects[Convert.ToInt32(reqObj1Param)] + " has no relation with other objects");
}
}
}
//================== Members ===============================
private int maxVal;
public ArrayList mInputDate = new ArrayList();
List<string> givenRelations = new List<string>();
private string mDataPath;//path to data file
private int[,] givenRelationMatrix;
private int[,,] relationMatrix;
private Dictionary<int, string> objects = new Dictionary<int, string>();//< object code >< object name >
private Dictionary<int, CRelation> relations = new Dictionary<int, CRelation>();//< relation code >< pair ( <relation name><relationType> ) >
private List<CFinalRelation> mOutputRelations = new List<CFinalRelation>();//all relations we have and got
}
}