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
use std::env;
use std::fmt::write;
use std::path::PathBuf;
pub(crate) mod output;
use crate::artifact::{
Artifact, ArtifactCreator, ArtifactSaver, JsonTrace, TlaConfigFile, TlaFile, TlaFileSuite,
TlaTrace,
};
use crate::Error;
use clap::{AppSettings, Clap, Subcommand};
use serde_json::{json, Value as JsonValue};
use std::path::Path;
#[derive(Clap, Debug)]
#[clap(name = "modelator")]
#[clap(setting = AppSettings::DisableHelpSubcommand)]
pub struct CliOptions {
#[clap(subcommand)]
subcommand: Modules,
}
#[derive(Debug, Subcommand)]
enum Modules {
#[clap(subcommand)]
Tla(TlaMethods),
#[clap(subcommand)]
Apalache(ApalacheMethods),
#[clap(subcommand)]
Tlc(TlcMethods),
}
#[derive(Debug, Subcommand)]
#[clap(setting = AppSettings::DisableHelpSubcommand)]
enum TlaMethods {
GenerateTests {
tla_file: String,
tla_config_file: String,
},
TlaTraceToJsonTrace {
tla_trace_file: String,
},
}
#[derive(Debug, Subcommand)]
#[clap(setting = AppSettings::DisableHelpSubcommand)]
enum ApalacheMethods {
Test {
tla_file: String,
tla_config_file: String,
},
Parse {
tla_file: String,
},
}
#[derive(Debug, Subcommand)]
#[clap(setting = AppSettings::DisableHelpSubcommand)]
enum TlcMethods {
Test {
tla_file: String,
tla_config_file: String,
},
}
impl CliOptions {
pub fn run(self) -> output::CliOutput {
let result = self.subcommand.run();
output::CliOutput::with_result(result)
}
}
impl Modules {
fn run(self) -> Result<JsonValue, Error> {
let runtime = crate::ModelatorRuntime::default();
runtime.setup()?;
match self {
Self::Tla(options) => options.run(),
Self::Apalache(options) => options.run(),
Self::Tlc(options) => options.run(),
}
}
}
impl TlaMethods {
fn run(self) -> Result<JsonValue, Error> {
match self {
Self::GenerateTests {
tla_file,
tla_config_file,
} => Self::generate_tests(tla_file, tla_config_file),
Self::TlaTraceToJsonTrace { tla_trace_file } => {
Self::tla_trace_to_json_trace(tla_trace_file)
}
}
}
fn generate_tests(
tla_file_path: String,
tla_config_file_path: String,
) -> Result<JsonValue, Error> {
let file_suite =
TlaFileSuite::from_tla_and_config_paths(tla_file_path, tla_config_file_path)?;
let tests = crate::model::language::Tla::generate_tests(&file_suite)?;
tracing::debug!("Tla::generate_tests output {:#?}", tests);
let dir = env::current_dir()?;
let written_files = {
let mut ret = Vec::<(PathBuf, PathBuf)>::new();
for tla_test_suite in tests {
let tla_file_full_path = tla_test_suite.tla_file.try_write_to_dir(&dir)?;
let cfg_file_full_path = tla_test_suite.tla_config_file.try_write_to_dir(&dir)?;
ret.push((tla_file_full_path, cfg_file_full_path));
}
ret
};
json_list_generated_tests(written_files)
}
fn tla_trace_to_json_trace(tla_trace_file: String) -> Result<JsonValue, Error> {
let tla_trace = TlaTrace::try_read_from_file(tla_trace_file)?;
let json_trace = crate::model::language::Tla::tla_trace_to_json_trace(tla_trace)?;
tracing::debug!("Tla::tla_trace_to_json_trace output {}", json_trace);
write_json_trace_to_file(json_trace)
}
}
impl ApalacheMethods {
fn run(self) -> Result<JsonValue, Error> {
match self {
Self::Test {
tla_file,
tla_config_file,
} => Self::test(tla_file, tla_config_file),
Self::Parse { tla_file } => Self::parse(tla_file),
}
}
fn test(tla_file_path: String, tla_config_file_path: String) -> Result<JsonValue, Error> {
let runtime = crate::ModelatorRuntime::default();
let input_artifacts =
TlaFileSuite::from_tla_and_config_paths(tla_file_path, tla_config_file_path)?;
let res = {
let mut ret = crate::model::checker::Apalache::test(&input_artifacts, &runtime)?;
ret.0.extends_module_name = Some(input_artifacts.tla_file.module_name().to_string());
ret
};
tracing::debug!("Apalache::test output {}", res.0);
write_tla_trace_to_file(res.0)
}
fn parse(tla_file: String) -> Result<JsonValue, Error> {
let runtime = crate::ModelatorRuntime::default();
let tla_file = TlaFileSuite::from_tla_path(tla_file)?;
let res = crate::model::checker::Apalache::parse(&tla_file, &runtime)?;
tracing::debug!("Apalache::parse output {}", res.0);
write_parsed_tla_file_to_file(res.0)
}
}
impl TlcMethods {
fn run(self) -> Result<JsonValue, Error> {
match self {
Self::Test {
tla_file,
tla_config_file,
} => Self::test(tla_file, tla_config_file),
}
}
fn test(tla_file_path: String, tla_config_file_path: String) -> Result<JsonValue, Error> {
let runtime = crate::ModelatorRuntime::default();
let input_artifacts =
TlaFileSuite::from_tla_and_config_paths(tla_file_path, tla_config_file_path)?;
let tla_trace = {
let mut ret = crate::model::checker::Tlc::test(&input_artifacts, &runtime)?;
ret.0.extends_module_name = Some(input_artifacts.tla_file.module_name().to_string());
ret.0
};
tracing::debug!("Tlc::test output {}", tla_trace);
write_tla_trace_to_file(tla_trace)
}
}
#[allow(clippy::unnecessary_wraps)]
fn json_list_generated_tests(test_files: Vec<(PathBuf, PathBuf)>) -> Result<JsonValue, Error> {
let json_array = test_files
.into_iter()
.map(|(tla, cfg)| {
json!({
"tla_file": tla.into_os_string().into_string().unwrap(),
"tla_config_file": cfg.into_os_string().into_string().unwrap()
})
})
.collect();
Ok(JsonValue::Array(json_array))
}
#[allow(clippy::unnecessary_wraps)]
fn write_parsed_tla_file_to_file(tla_file: TlaFile) -> Result<JsonValue, Error> {
let name = format!("{}Parsed.tla", tla_file.module_name());
let path = Path::new(&name);
tla_file.try_write_to_file(path)?;
Ok(json!({
"tla_file": crate::util::absolute_path(path),
}))
}
fn write_tla_trace_to_file(tla_trace: TlaTrace) -> Result<JsonValue, Error> {
let path = Path::new("trace.tla");
tla_trace.try_write_to_file(path)?;
Ok(json!({
"tla_trace_file": crate::util::absolute_path(&path),
}))
}
fn write_json_trace_to_file(json_trace: JsonTrace) -> Result<JsonValue, Error> {
let path = Path::new("trace.json");
json_trace.try_write_to_file(path)?;
Ok(json!({
"json_trace_file": crate::util::absolute_path(&path),
}))
}