Class: Factbase::Term

Inherits:
Object
  • Object
show all
Includes:
Aggregates, Aliases, Debug, Defn, Logical, Math, Meta, Ordering, Strings
Defined in:
lib/factbase/term.rb

Overview

Term.

This is an internal class, it is not supposed to be instantiated directly.

It is possible to use for testing directly, for example to make a term with two arguments:

require 'factbase/fact'
require 'factbase/term'
f = Factbase::Fact.new(Mutex.new, { 'foo' => [42, 256, 'Hello, world!'] })
t = Factbase::Term.new(:lt, [:foo, 50])
assert(t.evaluate(f))

The design of this class may look ugly, since it has a large number of methods, each of which corresponds to a different type of a Term. A much better design would definitely involve many classes, one per each type of a term. It’s not done this way because of an experimental nature of the project. Most probably we should keep current design intact, since it works well and is rather simple to extend (by adding new term types). Moreover, it looks like the number of possible term types is rather limited and currently we implement most of them.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Defined Under Namespace

Modules: Aggregates, Aliases, Debug, Defn, Logical, Math, Meta, Ordering, Strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#traced

Methods included from Defn

#defn, #undef

Methods included from Ordering

#prev, #unique

Methods included from Aliases

#as, #join

Methods included from Meta

#absent, #exists, #many, #nil, #one, #size, #type

Methods included from Strings

#concat, #matches, #sprintf

Methods included from Aggregates

#agg, #best, #count, #empty, #first, #max, #min, #nth, #sum

Methods included from Logical

#_only_bool, #always, #and, #and_or_simplify, #and_simplify, #never, #not, #or, #or_simplify, #when

Methods included from Math

#arithmetic, #cmp, #div, #eq, #gt, #lt, #minus, #plus, #times, #to_float, #to_int, #to_str, #zero

Constructor Details

#initialize(operator, operands) ⇒ Term

Ctor.

Parameters:

  • operator (Symbol)

    Operator

  • operands (Array)

    Operands



86
87
88
89
# File 'lib/factbase/term.rb', line 86

def initialize(operator, operands)
  @op = operator
  @operands = operands
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



54
55
56
# File 'lib/factbase/term.rb', line 54

def op
  @op
end

#operandsObject (readonly)

Returns the value of attribute operands.



54
55
56
# File 'lib/factbase/term.rb', line 54

def operands
  @operands
end

Instance Method Details

#evaluate(fact, maps) ⇒ bool

Does it match the fact?

Parameters:

Returns:

  • (bool)

    TRUE if matches



95
96
97
98
99
100
101
# File 'lib/factbase/term.rb', line 95

def evaluate(fact, maps)
  send(@op, fact, maps)
rescue NoMethodError => e
  raise "Probably the term '#{@op}' is not defined at #{self}:\n#{Backtrace.new(e)}"
rescue StandardError => e
  raise "#{e.message} at #{self}:\n#{Backtrace.new(e)}"
end

#simplifyFactbase::Term

Simplify it if possible.

Returns:



105
106
107
108
109
110
111
112
# File 'lib/factbase/term.rb', line 105

def simplify
  m = "#{@op}_simplify"
  if respond_to?(m, true)
    send(m)
  else
    self
  end
end

#to_sString

Turns it into a string.

Returns:

  • (String)

    The string of it



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/factbase/term.rb', line 116

def to_s
  items = []
  items << @op
  items += @operands.map do |o|
    if o.is_a?(String)
      "'#{o.gsub("'", "\\\\'").gsub('"', '\\\\"')}'"
    elsif o.is_a?(Time)
      o.utc.iso8601
    else
      o.to_s
    end
  end
  "(#{items.join(' ')})"
end