Class: PgVerify::Cli::BaseCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/pg-verify/cli/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Make Thor exit with non-0 in case of errors

Returns:

  • (Boolean)


300
# File 'lib/pg-verify/cli/cli.rb', line 300

def self.exit_on_failure?(); true end

Instance Method Details

#dccaObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pg-verify/cli/cli.rb', line 169

def dcca()
    models = CliUtils.load_models(options)

    models.each { |model|
        Shell::LoadingPrompt.while_loading("Checking for deadlocks") {
            NuSMV::Runner.new().run_check!(model); "ok"
        }

        dcca = Model::DCCA.new(model, NuSMV::Runner.new)
        result = Shell::LoadingPrompt.while_loading("Calculating DCCA for #{model.name.to_s.c_string}") {
            dcca.perform()
        }
        result.each { |hazard, crit_sets|
            s = crit_sets.length == 1 ? "" : "s"
            message = "Hazard #{hazard.text.to_s.c_string} (#{hazard.expression.to_s.c_blue}) "
            message += "has #{crit_sets.length.to_s.c_num} minimal critical fault set#{s}:" if crit_sets.length > 0
            message += "has no minimal critical fault sets, meaning it is safe!".c_success if crit_sets.length == 0
            puts message
            crit_sets.each { |set|
                puts "\t{ #{set.map(&:to_s).map(&:c_blue).join(', ')} }"
            }
        }
    }
end

#doctorObject



292
293
294
# File 'lib/pg-verify/cli/cli.rb', line 292

def doctor()
    Doctor.check()
end

#initObject



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
# File 'lib/pg-verify/cli/cli.rb', line 253

def init()

    target = options[:directory].blank? ? Dir.pwd() : File.expand_path(options[:directory])
    if Dir.exist?(target) && Dir.entries(target).size > 2
        puts "The target directory #{target.c_file} isn't empty!"
        exit 1
    end

    # Create target dir
    FileUtils.mkdir_p(target)

    # Copy files to target
    template_dir = File.join(PgVerify.root, "data", "project-template")
    files = Dir.glob(File.join(template_dir, '**', '*'), File::FNM_DOTMATCH).select { |f| File.file?(f) }
    files.each { |f| 
        target_file = File.join(target, f.sub(template_dir, ""))
        target_file = target_file.gsub(".resource", "")
        FileUtils.mkdir_p(File.dirname(target_file))
        FileUtils.cp(f, target_file) unless File.basename(f) == ".keep"
    }
    # Copy the actual default config into the project as that
    # will contain all keys and should be commented
    FileUtils.cp(File.join(PgVerify.root, "data", "config", "pg-verify.yml"), File.join(target, ".pg-verify.yml"))
    prelude_pg_file = File.join(PgVerify.root, "integration_tests", "ruby_dsl", "016_pressure_tank.rb")
    FileUtils.cp(prelude_pg_file, File.join(target, Settings.ruby_dsl.default_script_name))

    # Initialize git project
    Dir.chdir(target) { 
        Core::CMDRunner.run_cmd("git init")
        Core::CMDRunner.run_cmd("git add .")
        Core::CMDRunner.run_cmd("git commit -m \"Initial Commit\"")
    }

    puts "Successfully initialized project at #{target.c_file}!"
    puts "You can read the #{'README.md'.c_file} to get started."
    puts "Run #{'pg-verify doctor'.c_blue} and follow the instructions to set up your environment!"
end

#simulateObject



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
# File 'lib/pg-verify/cli/cli.rb', line 207

def simulate()
    render_options = {
        render_labels: !options[:"hide-labels"],
        render_precons: !options[:"hide-precons"],
        render_guards: !options[:"hide-guards"],
        render_actions: !options[:"hide-actions"]
    }

    models = CliUtils.load_models(options)
    runner = NuSMV::Runner.new

    models.each { |model|
        Shell::LoadingPrompt.while_loading("Checking for deadlocks") {
            NuSMV::Runner.new().run_check!(model); "ok"
        }

        trace = Shell::LoadingPrompt.while_loading("Simulating model #{model.name.to_s.c_string}") {
            runner.run_simulation(model, options[:steps], random: options[:random])
        }

        # Print the trace
        puts trace.to_s

        next unless options[:png]

        # Prepare output dir
        out_dir = File.expand_path("simulate-" + model.name.to_s.gsub(/\W+/, '_').downcase, Settings.outdir)
        FileUtils.mkdir_p(out_dir)

        # Generate images
        Shell::LoadingPrompt.while_loading("Rendering states") { |printer|
            trace.states.each_with_index { |variable_state, index|
                printer.printl("Step #{index + 1}/#{trace.states.length}")
                puml = Transform::PumlTransformation.new(render_options).transform_graph(model, variable_state: variable_state)
                png = PlantumlBuilder::Formats::PNG.new(puml).load
                out_path = File.expand_path("step-#{index}.png", out_dir)
                File.binwrite(out_path, png)
            }
        }
        puts "Wrote #{trace.states.length.to_s.c_num} files to #{out_dir.c_file}"
    }

end

#testObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pg-verify/cli/cli.rb', line 140

def test()
    models = CliUtils.load_models(options)

    models.each { |model|
        Shell::LoadingPrompt.while_loading("Checking for deadlocks") {
            NuSMV::Runner.new().run_check!(model); "ok"
        }

        results = Shell::LoadingPrompt.while_loading("Running specifications") {
            NuSMV::Runner.new().run_specs(model)
        }

        results.each { |result|
            stat_string = result.success ? "PASSED".c_success : "FAILED".c_error
            puts "[ #{stat_string} ] #{result.spec.text}"
            puts "           #{result.spec.expression.to_s.c_blue}"
            unless result.success
                puts "           Here is a counter example:".c_red
                trace_s = result.trace.to_s.indented(str: "           >>  ".c_red)
                puts trace_s + "\n"
            end
        }
    }
end

#versionObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/pg-verify/cli/cli.rb', line 125

def version()
    banner = File.read(File.join(PgVerify.root, "data", "banner.txt"))
    banner = banner.gsub("0.1.0", PgVerify::VERSION)
    colors = [ "red", "orange", "yellow", "orange", "red" ]
    banner = banner.split("\n").each_with_index.map { |l, i| l.send(:"c_#{colors[i]}") }.join("\n")
    puts banner
    puts 
    puts "You are running #{'pg-verify'.c_string} version #{PgVerify::VERSION.to_s.c_num}"
    puts "Installation root: #{PgVerify.root.to_s.c_file}"
end