-
Notifications
You must be signed in to change notification settings - Fork 3
/
alma.c
156 lines (142 loc) · 4.15 KB
/
alma.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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include "alma.h"
#include "alma_command.h"
#include "alma_print.h"
#define LINELEN 1000
extern char logs_on;
extern char python_mode;
// Initialize global variable (declared in alma_formula header) to count up variable IDs
int main(int argc, char **argv) {
int run = 0;
int verbose = 0;
int file_count = 0;
char **files = NULL;
char *agent = NULL;
int max_depth = 1000;
logs_on = 1;
python_mode = 0;
int c;
while ((c = getopt(argc, argv, "rxvf:a:n:")) != -1)
switch (c) {
case 'r':
run = 1;
break;
case 'v':
verbose = 1;
break;
case 'f':
file_count++;
files = realloc(files, sizeof(*files) * file_count);
files[file_count-1] = optarg;
break;
case 'x':
logs_on = (char) 0;
break;
case 'a':
agent = optarg;
break;
case 'n':
max_depth = atoi(optarg);
break;
case '?':
if (optopt == 'f')
printf("Option -%c requires an ALMA file argument.\n", optopt);
else if (optopt == 'a')
printf("Option -%c requires an agent name argument.\n", optopt);
else if (optopt == 'n')
printf("Option -%c requires an integer max nesting dapth.\n", optopt);
else if (isprint (optopt))
printf("Unknown option `-%c'.\n", optopt);
else
printf("Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
return 0;
}
if (files == NULL) {
printf("Please run with at least one ALMA file argument using the \"-f\" option.\n");
return 0;
}
alma *reasoner = malloc(sizeof(*reasoner));
alma_init(reasoner, files, file_count, agent, NULL, NULL, verbose, NULL, logs_on, max_depth);
free(files);
alma_print(reasoner, NULL);
if (run) {
while (!reasoner->idling) {
alma_step(reasoner, NULL);
alma_print(reasoner, NULL);
}
alma_halt(reasoner);
}
else {
char line[LINELEN];
int counter = 0;
kb_logger logger;
logger.log = reasoner->almalog;
logger.buf = NULL;
while (1) {
tee_alt("alma: ", &logger, counter);
fflush(stdout);
if (fgets(line, LINELEN, stdin) != NULL) {
int len = strlen(line);
line[len-1] = '\0';
char *pos;
if (strcmp(line, "step") == 0) {
alma_step(reasoner, NULL);
}
else if (strcmp(line, "print") == 0) {
alma_print(reasoner, NULL);
fflush(stdout);
}
else if (strcmp(line, "halt") == 0) {
alma_halt(reasoner);
break;
}
else if ((pos = strstr(line, "add ")) != NULL && pos == line) {
char *assertion = malloc(len - 4);
strncpy(assertion, line+4, len-4);
alma_assert(reasoner, assertion, NULL);
free(assertion);
}
else if ((pos = strstr(line, "del ")) != NULL && pos == line) {
char *assertion = malloc(len - 4);
strncpy(assertion, line+4, len-4);
alma_remove(reasoner, assertion, NULL);
free(assertion);
}
else if ((pos = strstr(line, "update ")) != NULL && pos == line) {
char *assertion = malloc(len - 7);
strncpy(assertion, line+7, len-7);
alma_update(reasoner, assertion, NULL);
free(assertion);
}
else if ((pos = strstr(line, "obs ")) != NULL && pos == line) {
char *assertion = malloc(len - 4);
strncpy(assertion, line+4, len-4);
alma_observe(reasoner, assertion, NULL);
free(assertion);
}
else if ((pos = strstr(line, "bs ")) != NULL && pos == line) {
char *assertion = malloc(len - 3);
strncpy(assertion, line+3, len-3);
alma_backsearch(reasoner, assertion, NULL);
free(assertion);
}
else {
tee_alt("-a: Command '%s' not recognized\n", &logger, line);
}
} else {
tee_alt("ALMA no more input\n", &logger);
fflush(stdout);
}
counter++;
}
}
return 0;
}