File size: 10,645 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use anyhow::Context;
use loda_rust_core::util::BigIntVec;
use loda_rust_core::oeis::{OeisId, OeisIdHashSet};
use crate::postmine::{PathUtil, PostMineError};
use std::error::Error;
use std::ffi::OsStr;
use std::fmt;
use std::path::{Path, PathBuf};
use std::fs;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::collections::HashSet;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum State {
    PendingProcessing,
    Keep,
    Reject,
}

pub struct CandidateProgram {
    state: State,
    path_original: PathBuf,
    path_keep: PathBuf,
    path_reject: PathBuf,
    filename_original: String,
    lodacpp_terms: BigIntVec,
    possible_ids: OeisIdHashSet,
    keep_ids: OeisIdHashSet,
    minimized_program: String,
}

impl CandidateProgram {
    pub fn new(path: &Path) -> Result<Self, Box<dyn Error>> {
        assert!(path.is_absolute());
        assert!(path.is_file());

        let filename_osstr: &OsStr = path.file_name().ok_or(PostMineError::UnableToExtractFilenameFromPath)?;
        let filename_original: String = filename_osstr.to_string_lossy().to_string();

        let instance = Self {
            state: State::PendingProcessing,
            path_original: PathBuf::from(path),
            path_keep: PathUtil::path_keep(path),
            path_reject: PathUtil::path_reject(path),
            filename_original: filename_original,
            lodacpp_terms: vec!(),
            possible_ids: HashSet::new(),
            keep_ids: HashSet::new(),
            minimized_program: String::new(),
        };
        Ok(instance)
    }

    #[allow(dead_code)]
    pub fn filename_original(&self) -> &String {
        &self.filename_original
    }

    pub fn state(&self) -> State {
        self.state
    }

    pub fn update_lodacpp_terms(&mut self, terms: BigIntVec) {
        self.lodacpp_terms = terms;
    }

    pub fn lodacpp_terms(&self) -> &BigIntVec {
        &self.lodacpp_terms
    }

    pub fn possible_id_insert(&mut self, id: OeisId) {
        self.possible_ids.insert(id);
    }

    pub fn is_possible_ids_empty(&self) -> bool {
        self.possible_ids.is_empty()
    }

    pub fn possible_ids(&self) -> &OeisIdHashSet {
        &self.possible_ids
    }

    pub fn possible_id_vec(&self) -> Vec<OeisId> {
        let mut ids: Vec<OeisId> = self.possible_ids.clone().into_iter().collect();
        ids.sort();
        ids
    }

    pub fn keep_id_insert(&mut self, id: OeisId) {
        self.keep_ids.insert(id);
    }

    pub fn is_keep_ids_empty(&self) -> bool {
        self.keep_ids.is_empty()
    }

    pub fn keep_id_vec(&self) -> Vec<OeisId> {
        let mut ids: Vec<OeisId> = self.keep_ids.clone().into_iter().collect();
        ids.sort();
        ids
    }

    pub fn keep_program_ids_as_string(&self) -> String {
        let ids: Vec<OeisId> = self.keep_id_vec();
        let strings: Vec<String> = ids.iter().map(|id| id.a_number()).collect();
        strings.join(",")
    }

    pub fn assign_minimized_program(&mut self, minimized_program: String) {
        self.minimized_program = minimized_program;
    }

    pub fn minimized_program(&self) -> &String {
        &self.minimized_program
    }

    pub fn path_original(&self) -> &Path {
        &self.path_original
    }

    #[allow(dead_code)]
    pub fn path_reject(&self) -> &Path {
        &self.path_reject
    }

    #[allow(dead_code)]
    pub fn path_keep(&self) -> &Path {
        &self.path_keep
    }

    /// Prefix error messages with `;`, so that when they can be inserted into a LODA program
    /// and the LODA program is still a valid program.
    fn prefix_with_semicolon<I: AsRef<str>>(input: I) -> String {
        // let v: Vec<&str> = input.as_ref().split("\n").collect();
        // v.join("\n; ")
        input.as_ref().replace("\n", "\n; ")
    }

    pub fn perform_reject<I: AsRef<str>>(&mut self, reason_reject: I) -> anyhow::Result<()> {
        if self.state != State::PendingProcessing {
            return Err(anyhow::anyhow!("perform_reject of candidate program with already resolved state"));
        }
        fs::rename(&self.path_original, &self.path_reject)
            .with_context(|| format!("perform_reject: Unable to rename file from: {:?} to: {:?}", &self.path_original, &self.path_reject))?;
        let mut file = OpenOptions::new()
            .write(true)
            .append(true)
            .open(&self.path_reject)
            .with_context(|| format!("perform_reject: Unable to open in append-mode. path: {:?}", &self.path_reject))?;
        let reason: String = Self::prefix_with_semicolon(reason_reject.as_ref());
        writeln!(file, "\n; reject-reason: {}", reason)
            .with_context(|| format!("perform_reject: Unable to append to rejection-reason to file: {:?}", &self.path_reject))?;
        self.state = State::Reject;
        Ok(())
    }

    pub fn perform_keep<I: AsRef<str>>(&mut self, reason_keep: I) -> anyhow::Result<()> {
        if self.state != State::PendingProcessing {
            return Err(anyhow::anyhow!("perform_keep of candidate program with already resolved state"));
        }
        fs::rename(&self.path_original, &self.path_keep)
            .with_context(|| format!("perform_keep: Unable to rename file from: {:?} to: {:?}", &self.path_original, &self.path_keep))?;
        let mut file = OpenOptions::new()
            .write(true)
            .append(true)
            .open(&self.path_keep)
            .with_context(|| format!("perform_keep: Unable to open in append-mode. path: {:?}", &self.path_keep))?;
        let reason: String = Self::prefix_with_semicolon(reason_keep.as_ref());
        writeln!(file, "\n; keep-reason: {}", reason)
            .with_context(|| format!("perform_keep: Unable to append to keep-reason to file: {:?}", &self.path_keep))?;
        self.state = State::Keep;
        Ok(())
    }

    pub fn perform_keep_or_reject_based_result(&mut self) -> Result<(), Box<dyn Error>> {
        if self.is_keep_ids_empty() {
            let oeis_ids: Vec<OeisId> = self.possible_id_vec();
            if oeis_ids.is_empty() {
                self.perform_reject("Doesn't correspond to any known OEIS sequence")
                    .context("perform_keep_or_reject_based_result doesn't correspond to any known OEIS sequence")?;
                return Ok(());
            }
            let message = format!("Worse than the existing programs: {:?}", oeis_ids);
            self.perform_reject(message)
                .context("perform_keep_or_reject_based_result worse than the existing program")?;
            return Ok(());
        }
        let keep_program_ids: String = self.keep_program_ids_as_string();
        let keep_reason: String = format!("Corresponds to: {}", keep_program_ids);
        self.perform_keep(keep_reason)
            .context("perform_keep_or_reject_based_result")?;
        return Ok(())
    }
}

impl fmt::Display for CandidateProgram {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "'{}'", self.filename_original)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::fs;
    use std::error::Error;
    use std::fs::File;

    #[test]
    fn test_10000_filename_original() -> Result<(), Box<dyn Error>> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_10000_filename_original");
        fs::create_dir(&basedir)?;
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"; A123456
mul $0,2
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;

        // Act
        let candidate_program: CandidateProgram = CandidateProgram::new(&input_path)?;

        // Assert
        assert_eq!(candidate_program.filename_original(), "19840101-054915-1251916462.asm");
        Ok(())
    }

    #[test]
    fn test_20000_prefix_with_semicolon() {
        assert_eq!(CandidateProgram::prefix_with_semicolon("x"), "x");
        assert_eq!(CandidateProgram::prefix_with_semicolon("x\ny"), "x\n; y");
        assert_eq!(CandidateProgram::prefix_with_semicolon("x\ny\nz"), "x\n; y\n; z");
    }

    #[test]
    fn test_30000_perform_reject() -> Result<(), Box<dyn Error>> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_20000_perform_reject");
        fs::create_dir(&basedir)?;
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"; A039207
mov $1,1
add $1,$0
div $1,9
mul $1,2
add $0,$1
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;
        let mut candidate_program: CandidateProgram = CandidateProgram::new(&input_path)?;

        // Act
        candidate_program.perform_reject("REJECT-REASON1\nREJECT-REASON2\nREJECT-REASON3")?;

        // Assert
        assert_eq!(candidate_program.path_original().is_file(), false);
        assert_eq!(candidate_program.path_reject().is_file(), true);
        let output_content: String = fs::read_to_string(candidate_program.path_reject())?;
        assert_eq!(output_content.ends_with("\n; reject-reason: REJECT-REASON1\n; REJECT-REASON2\n; REJECT-REASON3\n"), true);
        Ok(())
    }

    #[test]
    fn test_30001_perform_keep() -> Result<(), Box<dyn Error>> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_20001_perform_keep");
        fs::create_dir(&basedir)?;
        let input_path: PathBuf = basedir.join("19840101-054915-1251916462.asm");

        let input_content = 
r#"; A039207
mov $1,1
add $1,$0
div $1,9
mul $1,2
add $0,$1
"#;
        let mut input_file = File::create(&input_path)?;
        input_file.write_all(input_content.as_bytes())?;
        input_file.sync_all()?;
        let mut candidate_program: CandidateProgram = CandidateProgram::new(&input_path)?;

        // Act
        candidate_program.perform_keep("KEEP-REASON1\nKEEP-REASON2\nKEEP-REASON3")?;

        // Assert
        assert_eq!(candidate_program.path_original().is_file(), false);
        assert_eq!(candidate_program.path_keep().is_file(), true);
        let output_content: String = fs::read_to_string(candidate_program.path_keep())?;
        assert_eq!(output_content.ends_with("\n; keep-reason: KEEP-REASON1\n; KEEP-REASON2\n; KEEP-REASON3\n"), true);
        Ok(())
    }
}