forked from JohnPool/CSharpProlog
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathExternalUsage.cs
More file actions
409 lines (335 loc) · 12.6 KB
/
Copy pathExternalUsage.cs
File metadata and controls
409 lines (335 loc) · 12.6 KB
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
/*-----------------------------------------------------------------------------------------
C#Prolog -- Copyright (C) 2007-2015 John Pool -- j.pool@ision.nl
This library is free software; you can redistribute it and/or modify it under the terms of
the GNU Lesser General Public License as published by the Free Software Foundation; either
version 3.0 of the License, or any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl-3.0.html), or
enter 'license' at the command prompt.
-------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
#if mswindows
using System.Windows.Forms;
#endif
namespace Prolog
{
#region SolutionSet
public class SolutionSet
{
string query;
public string Query { get { return query; } internal set { query = value; } }
bool success;
public bool Success { get { return success; } internal set { success = value; } }
string errorMsg;
public string ErrMsg { get { return errorMsg; } internal set { errorMsg = value; } }
public bool HasError { get { return errorMsg != null; } }
List<Solution> solutionSet;
public int Count { get { return solutionSet.Count; } }
Solution currVarSet;
public SolutionSet()
{
solutionSet = new List<Solution>();
success = false;
errorMsg = null;
}
internal void CreateVarSet()
{
solutionSet.Add(currVarSet = new Solution());
}
internal void AddToVarSet(string name, string type, string value)
{
currVarSet.Add(name, type, value);
success = true;
}
public IEnumerable<Solution> NextSolution
{
get
{
foreach (Solution s in solutionSet)
yield return s;
}
}
public Solution this[int i]
{
get
{
return solutionSet[i];
}
}
public override string ToString()
{
if (errorMsg != null)
return errorMsg;
if (success)
{
if (solutionSet.Count == 0)
return "yes";
else
{
StringBuilder sb = new StringBuilder();
int i = 0;
foreach (Solution s in solutionSet)
sb.AppendLine("Solution {0}\r\n{1}", ++i, s.ToString());
return sb.ToString();
}
}
else
return "no";
}
}
#endregion SolutionSet
#region Solution
public class Solution // a solution is a set of variables
{
List<Variable> variables;
int Count { get { return variables.Count; } }
public Solution()
{
variables = new List<Variable>();
}
internal void Add(string name, string type, string value)
{
variables.Add(new Variable(name, type, value));
}
public IEnumerable<Variable> NextVariable
{
get
{
foreach (Variable v in variables)
yield return v;
}
}
public Variable this[int i]
{
get
{
return variables[i];
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (Variable v in variables)
sb.AppendLine(v.ToString());
return sb.ToString();
}
}
#endregion Solution
#region Variable
public class Variable
{
string name;
public string Name { get { return name; } }
string type;
public string Type { get { return type; } }
string value;
public string Value { get { return value; } }
public Variable(string name, string type, string value)
{
this.name = name;
this.type = type;
this.value = value;
}
public override string ToString()
{
return string.Format("{0} ({1}) = {2}", name, type, value);
}
}
#endregion Variable
public partial class PrologEngine
{
#if mswindows
#region Batch Processing
public bool ProcessArgs(string[] args, bool windowsMode)
{
if (args.Length == 0) return false;
// Process command line arguments. First is a query, optional second
// arg is number of backtrack attempts (default is 0, 'infinite' = *)
// If the first argument contains spaces, it must be enclosed in double quotes.
// Example: pld "['path.pl'], solve( p(2,2), L)" 4
string command = args[0].Dequoted().Trim();
Query = command + (command.EndsWith(".") ? null : "."); // append a dot if necessary
int solutionCount = 0; // i.e. find all solutions (backtrack until failure; value corresponds to '*')
string msg = null;
if (args.Length > 2)
msg = string.Format("Superfluous argument '{0}'", args[2]);
else if (args.Length == 2 && !(int.TryParse(args[1], out solutionCount) || args[1] == "*"))
msg = string.Format("Illegal value '{0}' for maximum number of solutions", args[1]);
else
solutionCount = 1;
if (msg == null)
{
int i = 0;
bool found = false; // true if at least one solution found
foreach (PrologEngine.ISolution s in SolutionIterator)
{
if (Error || (!found && !s.Solved)) // only an immediate 'no' give rise to an error
{
msg = s.ToString();
break;
}
if (++i == solutionCount) break;
found = true;
}
}
if (msg != null)
{
if (windowsMode)
MessageBox.Show(msg);
else
Console.WriteLine(msg);
Environment.ExitCode = 1; // sets DOS ERRORLEVEL to 1
}
return true;
}
#endregion Batch Processing
#endif
#region GetAllSolutionsXml
// Store solutions in an xml structure
#if !NETSTANDARD
public string GetAllSolutionsXml(string sourceFileName, string destinFileName, string query)
{
return GetAllSolutionsXml(sourceFileName, destinFileName, query, 0);
}
public string GetAllSolutionsXml(string sourceFileName, string destinFileName, string query, int maxSolutionCount)
{
XmlTextWriter xtw = null;
StreamWriter sw = null;
if (destinFileName == null)
xtw = new XmlTextWriter(new MemoryStream(), System.Text.Encoding.GetEncoding(1252));
else
xtw = new XmlTextWriter(destinFileName, null);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
try
{
if (sourceFileName != null)
Reset();
if (sourceFileName != null)
Consult(sourceFileName);
Query = query + (query.EndsWith(".") ? null : "."); // append a dot if necessary
int i = 0;
bool found = false; // true if at least one solution found
bool firstSol = true;
foreach (PrologEngine.ISolution s in SolutionIterator)
{
if (Error)
{
xtw.WriteStartElement("error");
xtw.WriteCData(s.ToString());
xtw.WriteEndElement();
break;
}
else if (!found && !s.Solved)
{
xtw.WriteStartElement("solutions");
xtw.WriteAttributeString("success", "false");
xtw.WriteStartElement("query");
xtw.WriteString(query);
xtw.WriteEndElement();
break;
}
if (firstSol)
{
firstSol = false;
xtw.WriteStartElement("solutions");
xtw.WriteAttributeString("success", "true");
xtw.WriteStartElement("query");
xtw.WriteString(query);
xtw.WriteEndElement();
}
bool firstVar = true;
foreach (PrologEngine.IVarValue varValue in s.VarValuesIterator)
{
if (varValue.DataType == "none") break;
if (firstVar)
{
firstVar = false;
xtw.WriteStartElement("solution");
}
xtw.WriteStartElement("variable");
xtw.WriteAttributeString("name", varValue.Name);
xtw.WriteAttributeString("type", varValue.DataType);
xtw.WriteString(varValue.Value.ToString());
xtw.WriteEndElement(); // variable
}
if (!firstVar) xtw.WriteEndElement(); // solution
if (++i == maxSolutionCount) break;
found = true;
}
if (!Error) xtw.WriteEndElement(); // solutions
xtw.WriteEndDocument();
xtw.Flush();
if (destinFileName == null)
{
if (xtw.BaseStream == null) return null;
return new ASCIIEncoding().GetString(((MemoryStream)xtw.BaseStream).ToArray());
}
else
return null;
}
finally
{
if (xtw != null) xtw.Close();
if (sw != null) sw.Close();
}
}
#endif
#endregion GetAllSolutionsXml
#region GetAllSolutions
// Store solutions in an GetAllSolutions class
public SolutionSet GetAllSolutions(string sourceFileName, string query)
{
return GetAllSolutions(sourceFileName, query, 0);
}
public SolutionSet GetAllSolutions(string sourceFileName, string query, int maxSolutionCount)
{
SolutionSet solutions = new SolutionSet();
try
{
if (sourceFileName != null) Reset();
if (sourceFileName != null) Consult(sourceFileName);
Query = solutions.Query = query + (query.EndsWith(".") ? null : "."); // append a dot if necessary
int i = 0;
bool found = false;
bool varFound = false;
foreach (PrologEngine.ISolution s in SolutionIterator)
{
if (Error)
{
solutions.ErrMsg = s.ToString();
break;
}
else if (!found && !s.Solved)
break;
solutions.Success = true;
bool firstVar = true;
foreach (PrologEngine.IVarValue varValue in s.VarValuesIterator)
{
if (varValue.DataType == "none") break;
if (firstVar)
{
firstVar = false;
solutions.CreateVarSet();
}
solutions.AddToVarSet(varValue.Name, varValue.DataType, varValue.Value.ToString());
varFound = true;
}
if (++i == maxSolutionCount || !varFound) break;
found = true;
}
}
catch (Exception e)
{
solutions.ErrMsg = e.Message;
}
return solutions;
}
#endregion GetAllSolutions
}
}