Module: Predicate::Factory
Instance Method Summary collapse
-
#and(left, right = nil) ⇒ Object
Builds a AND predicate using two sub predicates.
-
#comp(op, h) ⇒ Object
:nodoc:.
-
#contradiction ⇒ Object
Factors a Predicate that captures False.
-
#empty(operand) ⇒ Object
Factors an EMPTY predicate that responds true when its operand is something empty.
-
#from_hash(h, op = :eq) ⇒ Object
Builds a AND predicate between all key/value pairs of the provided Hash, using the comparison operator specified.
-
#has_size(left, right) ⇒ Object
Factors a SIZE predicate that responds true when its operand has a size meeting the right constraint (typically a Range literal).
-
#identifier(name) ⇒ Object
Factors a Predicate for a free variable whose name is provided.
-
#in(left, right) ⇒ Object
(also: #among)
Factors a IN predicate between a variable and either a list of values of another variable.
-
#literal(literal) ⇒ Object
Factors a Literal node for some ruby value.
-
#match(left, right, options) ⇒ Object
Factors a MATCH predicate between a variable and a literal or another variable.
-
#native(arg) ⇒ Object
Factors a predicate for a ruby Proc that returns truth-value for a single argument.
-
#not(operand) ⇒ Object
Negates an existing predicate.
-
#opaque(arg) ⇒ Object
Converts ‘arg` to an opaque predicate, whose semantics depends on the actual usage of the library.
-
#or(left, right = nil) ⇒ Object
Builds a OR predicate using two sub predicates.
-
#placeholder ⇒ Object
Builds and returns a placeholder that can be used everywhere a literal can be used.
-
#qualified_identifier(qualifier, name) ⇒ Object
Factors a Predicate for a qualified free variable.
-
#tautology ⇒ Object
Factors a Predicate that captures True.
-
#var(formaldef, semantics = :dig) ⇒ Object
Factors a var node, using a given extractor semantics.
-
#vars(*args) ⇒ Object
Factors a couple of variables at once.
Instance Method Details
#and(left, right = nil) ⇒ Object
Builds a AND predicate using two sub predicates.
Please favor ‘Predicate#&` instead.
63 64 65 |
# File 'lib/predicate/factory.rb', line 63 def and(left, right = nil) _factor_predicate([:and, sexpr(left), sexpr(right)]) end |
#comp(op, h) ⇒ Object
:nodoc:
84 85 86 |
# File 'lib/predicate/factory.rb', line 84 def comp(op, h) from_hash(h, op) end |
#contradiction ⇒ Object
Factors a Predicate that captures False
12 13 14 |
# File 'lib/predicate/factory.rb', line 12 def contradiction _factor_predicate([:contradiction, false]) end |
#empty(operand) ⇒ Object
Factors an EMPTY predicate that responds true when its operand is something empty.
Default evaluation uses ruby ‘empty?` method.
144 145 146 |
# File 'lib/predicate/factory.rb', line 144 def empty(operand) _factor_predicate([:empty, sexpr(operand)]) end |
#from_hash(h, op = :eq) ⇒ Object
Builds a AND predicate between all key/value pairs of the provided Hash, using the comparison operator specified.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/predicate/factory.rb', line 180 def from_hash(h, op = :eq) if h.empty? tautology else terms = h.to_a.map{|(k,v)| if v.is_a?(Array) [:in, sexpr(k), sexpr(v)] else [op, sexpr(k), sexpr(v)] end } terms = terms.size == 1 ? terms.first : terms.unshift(:and) _factor_predicate terms end end |
#has_size(left, right) ⇒ Object
Factors a SIZE predicate that responds true when its operand has a size meeting the right constraint (typically a Range literal)
151 152 153 |
# File 'lib/predicate/factory.rb', line 151 def has_size(left, right) _factor_predicate([:has_size, sexpr(left), sexpr(right)]) end |
#identifier(name) ⇒ Object
Factors a Predicate for a free variable whose name is provided. If the variable is a Boolean variable, this is a valid Predicate, otherwise it must be used in a higher-level expression.
41 42 43 |
# File 'lib/predicate/factory.rb', line 41 def identifier(name) _factor_predicate([:identifier, name]) end |
#in(left, right) ⇒ Object Also known as: among
Factors a IN predicate between a variable and either a list of values of another variable.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/predicate/factory.rb', line 100 def in(left, right) case right when Range return contradiction if right.size == 0 rl = gte(left, right.begin) rr = right.exclude_end? ? lt(left, right.end) : lte(left, right.end) self.and(rl, rr) else left, right = sexpr(left), sexpr(right) if right.literal? && right.empty_value? contradiction else _factor_predicate([:in, left, right]) end end end |
#literal(literal) ⇒ Object
Factors a Literal node for some ruby value.
19 20 21 |
# File 'lib/predicate/factory.rb', line 19 def literal(literal) _factor_predicate([:literal, literal]) end |
#match(left, right, options) ⇒ Object
Factors a MATCH predicate between a variable and a literal or another variable.
Matching options can be passes and are specific to the actual usage of the library.
134 135 136 137 138 |
# File 'lib/predicate/factory.rb', line 134 def match(left, right, ) s = [:match, sexpr(left), sexpr(right)] s << unless .nil? _factor_predicate(s) end |
#native(arg) ⇒ Object
Factors a predicate for a ruby Proc that returns truth-value for a single argument.
165 166 167 |
# File 'lib/predicate/factory.rb', line 165 def native(arg) _factor_predicate([:native, arg]) end |
#not(operand) ⇒ Object
Negates an existing predicate.
Please favor ‘Predicate#!` instead.
77 78 79 |
# File 'lib/predicate/factory.rb', line 77 def not(operand) _factor_predicate([:not, sexpr(operand)]) end |
#opaque(arg) ⇒ Object
Converts ‘arg` to an opaque predicate, whose semantics depends on the actual usage of the library.
171 172 173 |
# File 'lib/predicate/factory.rb', line 171 def opaque(arg) _factor_predicate([:opaque, arg]) end |
#or(left, right = nil) ⇒ Object
Builds a OR predicate using two sub predicates.
Please favor ‘Predicate#|` instead.
70 71 72 |
# File 'lib/predicate/factory.rb', line 70 def or(left, right = nil) _factor_predicate([:or, sexpr(left), sexpr(right)]) end |
#placeholder ⇒ Object
Builds and returns a placeholder that can be used everywhere a literal can be used. Placeholders can be bound later, using ‘Predicate#bind`.
54 55 56 |
# File 'lib/predicate/factory.rb', line 54 def placeholder Placeholder.new end |
#qualified_identifier(qualifier, name) ⇒ Object
Factors a Predicate for a qualified free variable. Same remark as in ‘identifier`.
47 48 49 |
# File 'lib/predicate/factory.rb', line 47 def qualified_identifier(qualifier, name) _factor_predicate([:qualified_identifier, qualifier, name]) end |
#tautology ⇒ Object
Factors a Predicate that captures True
7 8 9 |
# File 'lib/predicate/factory.rb', line 7 def tautology _factor_predicate([:tautology, true]) end |
#var(formaldef, semantics = :dig) ⇒ Object
Factors a var node, using a given extractor semantics
26 27 28 |
# File 'lib/predicate/factory.rb', line 26 def var(formaldef, semantics = :dig) _factor_predicate([:var, formaldef, semantics]) end |
#vars(*args) ⇒ Object
Factors a couple of variables at once. The semantics can be passed as a Symbol as last argument and defaults to :dig
32 33 34 35 |
# File 'lib/predicate/factory.rb', line 32 def vars(*args) args << :dig unless args.last.is_a?(Symbol) args[0...-1].map{|v| var(v, args.last) } end |