Class: Judges::Update

Inherits:
Object show all
Defined in:
lib/judges/commands/update.rb

Overview

The update command.

This class is instantiated by the bin/judge command line interface. You are not supposed to instantiate it yourself.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ Update

Returns a new instance of Update.



42
43
44
# File 'lib/judges/commands/update.rb', line 42

def initialize(loog)
  @loog = loog
end

Instance Method Details

#run(opts, args) ⇒ Object

Run it (it is supposed to be called by the bin/judges script.

Parameters:

  • opts (Hash)

    Command line options (start with ‘–’)

  • args (Array)

    List of command line arguments



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
# File 'lib/judges/commands/update.rb', line 49

def run(opts, args)
  raise 'Exactly two arguments required' unless args.size == 2
  dir = args[0]
  raise "The directory is absent: #{dir.to_rel}" unless File.exist?(dir)
  start = Time.now
  impex = Judges::Impex.new(@loog, args[1])
  fb = impex.import(strict: false)
  fb = Factbase::Looged.new(fb, @loog) if opts['log']
  options = Judges::Options.new(opts['option'])
  if options.empty?
    @loog.debug('No options provided by the --option flag')
  else
    @loog.debug("The following options provided:\n\t#{options.to_s.gsub("\n", "\n\t")}")
  end
  judges = Judges::Judges.new(dir, opts['lib'], @loog)
  c = 0
  churn = Judges::Churn.new(0, 0)
  elapsed(@loog, level: Logger::INFO) do
    loop do
      c += 1
      if c > 1
        @loog.info("\nStarting cycle ##{c}#{opts['max-cycles'] ? " (out of #{opts['max-cycles']})" : ''}...")
      end
      delta = cycle(opts, judges, fb, options)
      churn += delta
      impex.export(fb)
      if delta.zero?
        @loog.info("The update cycle ##{c} has made no changes to the factbase, let's stop")
        break
      end
      if !opts['max-cycles'].nil? && c >= opts['max-cycles']
        @loog.info("Too many cycles already, as set by --max-cycles=#{opts['max-cycles']}, breaking")
        break
      end
      @loog.info("The cycle #{c} modified #{delta} fact(s)")
    end
    throw :"Update finished in #{c} cycle(s), modified #{churn} fact(s)"
  end
  return unless opts['summary']
  fb.query('(eq what "judges-summary")').delete!
  f = fb.insert
  f.what = 'judges-summary'
  f.when = Time.now
  f.version = Judges::VERSION
  f.seconds = Time.now - start
  f.cycles = c
  f.added = churn.added.size
  f.removed = churn.removed.size
  churn.errors.each { |e| f.error = e }
  impex.export(fb)
end