-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.c
130 lines (120 loc) · 3.54 KB
/
solver.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttshivhu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/17 10:28:44 by ttshivhu #+# #+# */
/* Updated: 2017/10/18 09:17:07 by ttshivhu ### ########.fr */
/* */
/* ************************************************************************** */
#include "expert.h"
static void init_list(t_expert **list, char *facts, char *queries)
{
while (*facts)
{
if (add_item(list, *facts, 0, 1) && g_view)
{
sleep(1);
printf("It is given as a fact that \x1b[34m%c\x1b[0m is true\n", *facts);
}
facts++;
}
while (*queries)
{
add_item(list, *queries, 0, 0);
queries++;
}
}
static void add_alpha_to_stack(t_expert **list, char **rules)
{
int i;
int j;
i = -1;
while (rules[++i])
{
j = 0;
while (rules[i][j])
{
if (rules[i][j] <= 'Z' && rules[i][j] >= 'A')
add_item(list, rules[i][j], 0, 0);
j++;
}
}
}
static int check_error_in_line(char *line)
{
int i;
i = 0;
while (line[i])
{
if (line[i] <= 'Z' && line[i] >= 'A')
i++;
else if (line[i] == '(' || line[i] == ')')
i++;
else if (line[i] == '+' || line[i] == '|' || line[i] == '^')
i++;
else if (ft_strncmp(line + i, "=>", 2) == 0)
i+=2;
else if (ft_strncmp(line + i, "<=>", 3) == 0)
i+=3;
else if (line[i] == '!')
i++;
else
return (-1);
}
return (1);
}
static int check_error_n_solve(t_expert **list, char **rules, char **error)
{
int i;
i = -1;
while (rules[++i])
{
if (check_error_in_line(rules[i]) == -1)
{
*error = ft_strdup(rules[i]);
return (-1);
}
}
add_alpha_to_stack(list, rules);
return (1);
}
void solver(char **rules, char *facts, char *queries)
{
t_expert *list;
char *error;
static int times = 0;
if (g_view)
printf("EVALUATING: \x1b[31m%s\x1b[0m\n", queries);
while (1)
{
list = NULL;
if (times > 0)
facts = readline("\x1b[34mEnter new facts or \x1b[31m\"exit\"\x1b[34m to quit:\x1b[0m ");
if (ft_strcmp("exit", facts) == 0)
break ;
if (!rule_validate(facts))
{
printf("\x1b[31mYou have invalid characters in your facts\x1b[0m\n");
break ;
}
init_list(&list, facts, queries);
if (check_error_n_solve(&list, rules, &error) == -1)
{
printf("Error in line: %s\n", error);
free(error);
return ;
}
algo(&list, rules);
if (g_view)
print_false(list, queries);
print_results(list, queries);
free_list(list);
free(facts);
times++;
if (!g_view)
break ;
}
}