Class: Fbe::Conclude

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/conclude.rb

Overview

A concluding block.

You may want to use this class when you want to go through a number of facts in the factbase, applying certain algorithm to each of them and possibly creating new facts from them.

For example, you want to make a new good fact for every bad fact found:

require 'fbe/conclude'
conclude do
  on '(exist bad)'
  follow 'when'
  draw on |n, b|
    n.good = 'yes!'
  end
end

This snippet will find all facts that have bad property and then create new facts, letting the block in the #draw deal with them.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Zerocracy

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb:, judge:, global:, options:, loog:, start:) ⇒ Conclude

Ctor.

Parameters:

  • fb (Factbase)

    The factbase

  • judge (String)

    The name of the judge, from the judges tool

  • global (Hash)

    The hash for global caching

  • options (Judges::Options)

    The options coming from the judges tool

  • loog (Loog)

    The logging facility



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fbe/conclude.rb', line 64

def initialize(fb:, judge:, global:, options:, loog:, start:)
  @fb = fb
  @judge = judge
  @loog = loog
  @options = options
  @global = global
  @start = start
  @query = nil
  @follows = []
  @lifetime_aware = true
  @quota_aware = true
  @timeout = 60
end

Instance Method Details

#consider {|Factbase::Fact| ... } ⇒ Integer

Take every fact, allowing the given block to process it.

For example, you want to add when property to every fact:

require 'fbe/conclude'
conclude do
  on '(always)'
  consider on |f|
    f.when = Time.new
  end
end

Yields:

  • (Factbase::Fact)

    The next fact found by the query

Returns:

  • (Integer)

    The count of the facts processed



167
168
169
170
171
172
# File 'lib/fbe/conclude.rb', line 167

def consider(&)
  roll do |_fbt, a|
    yield a
    nil
  end
end

#draw {|Array<Factbase::Fact,Factbase::Fact>| ... } ⇒ Integer

Create new fact from every fact found by the query.

For example, you want to conclude a reward from every win fact:

require 'fbe/conclude'
conclude do
  on '(exist win)'
  follow 'win when'
  draw on |n, w|
    n.reward = 10
  end
end

This snippet will find all facts that have win property and will create new facts for all of them, passing them one by one in to the block of the draw, where n would be the new created fact and the w would be the fact found.

Yields:

  • (Array<Factbase::Fact,Factbase::Fact>)

    New fact and seen fact

Returns:

  • (Integer)

    The count of the facts processed



145
146
147
148
149
150
151
# File 'lib/fbe/conclude.rb', line 145

def draw(&)
  roll do |fbt, a|
    n = fbt.insert
    fill(n, a, &)
    n
  end
end

#follow(props) ⇒ nil

Set the list of properties to copy from the facts found to new facts.

Parameters:

  • props (Array<String>)

    List of property names

Returns:

  • (nil)

    Nothing



121
122
123
# File 'lib/fbe/conclude.rb', line 121

def follow(props)
  @follows = props.strip.split.compact
end

#lifetime_unawarenil

Make this block NOT aware of lifetime limitations.

When the lifetime is over, the loop will NOT gracefully stop.

Returns:

  • (nil)

    Nothing is returned



93
94
95
# File 'lib/fbe/conclude.rb', line 93

def lifetime_unaware
  @lifetime_aware = false
end

#on(query) ⇒ nil

Set the query that should find the facts in the factbase.

Parameters:

  • query (String)

    The query to execute

Returns:

  • (nil)

    Nothing is returned



112
113
114
115
# File 'lib/fbe/conclude.rb', line 112

def on(query)
  raise 'Query is already set' unless @query.nil?
  @query = query
end

#quota_unawarenil

Make this block not aware of GitHub API quota.

When the quota is reached, the loop will NOT gracefully stop to avoid hitting GitHub API rate limits.

Returns:

  • (nil)

    Nothing is returned



84
85
86
# File 'lib/fbe/conclude.rb', line 84

def quota_unaware
  @quota_aware = false
end

#timeout(sec) ⇒ nil

Make sure this block runs for less than allowed amount of seconds.

When the quota is reached, the loop will gracefully stop to avoid. This helps prevent interruptions in long-running operations.

Parameters:

  • sec (Float)

    Seconds

Returns:

  • (nil)

    Nothing is returned



104
105
106
# File 'lib/fbe/conclude.rb', line 104

def timeout(sec)
  @timeout = sec
end