-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (124 loc) · 4.61 KB
/
Program.cs
File metadata and controls
144 lines (124 loc) · 4.61 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
using BackwardsForwardsChaining.Abstractions;
using BackwardsForwardsChaining.Algorithms;
using BackwardsForwardsChaining.Models;
using BackwardsForwardsChaining.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BackwardsForwardsChaining
{
public class Program
{
public static void Main(string[] args)
{
ILoggerService logger = null;
IAlgorithm algorithm = null;
//Read file
Console.WriteLine("Please provide a file name:");
var fileName = Console.ReadLine();
if (String.IsNullOrEmpty(fileName))
{
Console.WriteLine("Please provide a file name.");
return;
}
if (!File.Exists(fileName))
{
Console.WriteLine($"File {fileName} is not found.");
Console.ReadKey();
return;
}
var (rules, facts, goal) = ReadFromFile(fileName);
//Choose output method
Console.WriteLine("Please choose output method by typing:");
Console.WriteLine("1. Console");
Console.WriteLine("2. File");
var outputSelection = Convert.ToChar(Console.ReadLine());
if(outputSelection != '1' && outputSelection != '2')
{
Console.WriteLine($"{outputSelection} is not a valid output method. Please try again.");
Console.ReadKey();
return;
}
switch (outputSelection)
{
case '1':
logger = new ConsoleLoggerService();
break;
case '2':
Console.WriteLine("Please provide output file name:");
var outputFileName = Console.ReadLine();
if (String.IsNullOrEmpty(outputFileName))
{
Console.WriteLine("Please provide a file name.");
return;
}
logger = new FileLoggerService(outputFileName);
break;
}
//Choose algorithm
Console.WriteLine("Please choose the algorithm:");
Console.WriteLine("1. Forwards chaining");
Console.WriteLine("2. Backwards chaining");
var algorithmSelection = Convert.ToChar(Console.ReadLine());
if (algorithmSelection != '1' && algorithmSelection != '2')
{
Console.WriteLine($"{algorithmSelection} is not a algorithm. Please try again.");
Console.ReadKey();
return;
}
switch (algorithmSelection)
{
case '1':
algorithm = new ForwardsChainingAlgorithm(rules, facts, goal, logger);
break;
case '2':
algorithm = new BackwardsChainingAlgorithm(rules, facts, goal, logger);
break;
}
//Execute
algorithm.Execute();
algorithm.Dispose();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
/// <summary>
/// Reads input from file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static (IEnumerable<Rule>, IEnumerable<char>, char) ReadFromFile(string fileName)
{
string line;
var rules = new List<Rule>();
var goal = '0';
var facts = new List<char>();
using (StreamReader file = new StreamReader(fileName))
{
//Skip 3 lines
file.ReadLine();
file.ReadLine();
file.ReadLine();
//Read rules
while ((line = file.ReadLine()) != "")
rules.Add(new Rule()
{
LeftSide = line.Substring(1, line.Length - (line.Length - (line.IndexOf("//") - 1))).Trim().Split(' ').Select(x => x[0]),
RightSide = line[0]
});
//Read facts
file.ReadLine();
line = file.ReadLine();
facts = line.Split(' ').Select(x => x[0]).ToList();
//Read goal
file.ReadLine();
file.ReadLine();
line = file.ReadLine();
goal = line[0];
}
return (rules, facts, goal);
}
}
}