pla_file/
pla_file.rs

1//! Example: Reading and minimizing a PLA file
2//!
3//! This example demonstrates how to read a PLA file, minimize it using the Cover trait,
4//! and write the result back to a file.
5
6use espresso_logic::{Cover, PLACover, PLAType};
7use std::env;
8use std::io::Write;
9use tempfile::NamedTempFile;
10
11fn main() {
12    println!("=== PLA File Minimization Example ===\n");
13
14    // Create a sample PLA file
15    let pla_content = r#".i 4
16.o 1
17.ilb a b c d
18.ob f
19.p 8
200000 1
210001 1
220010 1
230011 1
240100 1
250101 1
260110 1
270111 1
28.e
29"#;
30
31    println!("Sample PLA content:");
32    println!("{}", pla_content);
33
34    // Write to a temporary file
35    let mut temp_in = NamedTempFile::new().expect("Failed to create temp file");
36    temp_in
37        .write_all(pla_content.as_bytes())
38        .expect("Failed to write temp file");
39    temp_in.flush().expect("Failed to flush temp file");
40
41    // Read the PLA file
42    println!("\nReading PLA file...");
43    let mut cover = match PLACover::from_pla_file(temp_in.path()) {
44        Ok(c) => c,
45        Err(e) => {
46            eprintln!("Error reading PLA: {}", e);
47            return;
48        }
49    };
50
51    println!("PLA loaded successfully!");
52    println!("\nOriginal cover:");
53    println!("  Inputs:  {}", cover.num_inputs());
54    println!("  Outputs: {}", cover.num_outputs());
55    println!("  Cubes:   {}", cover.num_cubes());
56
57    // Minimize using the Cover trait
58    println!("\nMinimizing using Espresso...");
59    if let Err(e) = cover.minimize() {
60        eprintln!("Error minimizing: {}", e);
61        return;
62    }
63
64    println!("\nMinimized cover:");
65    println!("  Inputs:  {}", cover.num_inputs());
66    println!("  Outputs: {}", cover.num_outputs());
67    println!("  Cubes:   {}", cover.num_cubes());
68
69    // Show the minimized PLA
70    match cover.to_pla_string(PLAType::F) {
71        Ok(pla_str) => {
72            println!("\nMinimized PLA:");
73            println!("{}", pla_str);
74        }
75        Err(e) => eprintln!("Error generating PLA: {}", e),
76    }
77
78    // Write to output file if requested
79    if let Some(output_path) = env::args().nth(1) {
80        println!("Writing minimized PLA to: {}", output_path);
81        match cover.to_pla_file(&output_path, PLAType::F) {
82            Ok(_) => println!("Successfully wrote output file!"),
83            Err(e) => eprintln!("Error writing output: {}", e),
84        }
85    } else {
86        println!("\nTo write output to a file, run:");
87        println!("cargo run --example pla_file output.pla");
88    }
89}