From 5ae41b6812e163379758942c014ca68e148d96bb Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sun, 12 May 2024 15:51:23 -0400 Subject: [PATCH] #9 once --- .rubocop.yml | 2 ++ lib/judges/fb/once.rb | 61 +++++++++++++++++++++++++++++++++++++++++++ lib/judges/pack.rb | 2 ++ test/test_pack.rb | 8 ++++++ 4 files changed, 73 insertions(+) create mode 100644 lib/judges/fb/once.rb diff --git a/.rubocop.yml b/.rubocop.yml index 8f30860..c3f2229 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -49,3 +49,5 @@ Layout/EmptyLineAfterGuardClause: Enabled: false Layout/CaseIndentation: Enabled: false +Naming/MethodParameterName: + MinNameLength: 2 diff --git a/lib/judges/fb/once.rb b/lib/judges/fb/once.rb new file mode 100644 index 0000000..a780aa4 --- /dev/null +++ b/lib/judges/fb/once.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# Copyright (c) 2024 Yegor Bugayenko +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the 'Software'), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# Returns a decorated global factbase, which only touches facts once +def once(fb) + Factbase::Once.new(fb, $judge) +end + +# Runs only once. +# Author:: Yegor Bugayenko (yegor256@gmail.com) +# Copyright:: Copyright (c) 2024 Yegor Bugayenko +# License:: MIT +class Factbase::Once + def initialize(fb, func) + @fb = fb + @func = func + end + + def query(expr) + expr = "(and #{expr} (not (eq seen '#{@func}')))" + After.new(@fb.query(expr), @func) + end + + def insert + @fb.insert + end + + # What happens after a fact is processed. + class After + def initialize(query, func) + @query = query + @func = func + end + + def each + @query.each do |f| + yield f + f.seen = @func + end + end + end +end diff --git a/lib/judges/pack.rb b/lib/judges/pack.rb index 8767506..e3f8d22 100644 --- a/lib/judges/pack.rb +++ b/lib/judges/pack.rb @@ -22,6 +22,7 @@ require 'yaml' require_relative '../judges' +require_relative '../judges/fb/once' # A single pack. # Author:: Yegor Bugayenko (yegor256@gmail.com) @@ -37,6 +38,7 @@ def initialize(dir) # Run it with the given Factbase and environment variables. def run(fbase, env) $fb = fbase + $judge = File.basename(File.dirname(@dir)) env.each do |k, v| # rubocop:disable Security/Eval eval("$#{k} = '#{v}'", binding, __FILE__, __LINE__) # $foo = 42 diff --git a/test/test_pack.rb b/test/test_pack.rb index 9701480..e685386 100644 --- a/test/test_pack.rb +++ b/test/test_pack.rb @@ -53,4 +53,12 @@ def test_run_isolated assert_equal(1, fb2.size) end end + + def test_with_supplemenary_functions + Dir.mktmpdir do |d| + File.write(File.join(d, 'x.rb'), 'once($fb).insert') + pack = Judges::Pack.new(d) + pack.run(Factbase.new, {}) + end + end end