Class: Upl::Variable
Overview
A variable, either from an existing term_t or a new one
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#term_t ⇒ Object
(also: #to_term_t)
readonly
Returns the value of attribute term_t.
Class Method Summary collapse
- .[](*names) ⇒ Object
- .copy(term_t) ⇒ Object
-
.to_term ⇒ Object
TODO remove bit of a hack to create empty variables for a functor.
Instance Method Summary collapse
- #===(value) ⇒ Object
- #_string ⇒ Object
- #attribute ⇒ Object
- #attributed? ⇒ Boolean
-
#initialize(term_t = nil, name: nil) ⇒ Variable
constructor
A new instance of Variable.
- #pretty_print(pp) ⇒ Object
-
#to_ruby ⇒ Object
create a ruby represetation of the term_t.
- #to_s ⇒ Object
- #unify(value) ⇒ Object
Constructor Details
#initialize(term_t = nil, name: nil) ⇒ Variable
Returns a new instance of Variable.
4 5 6 7 |
# File 'lib/upl/variable.rb', line 4 def initialize term_t = nil, name: nil @term_t = term_t || self.class.to_term @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/upl/variable.rb', line 9 def name @name end |
#term_t ⇒ Object (readonly) Also known as: to_term_t
Returns the value of attribute term_t.
9 10 11 |
# File 'lib/upl/variable.rb', line 9 def term_t @term_t end |
Class Method Details
.[](*names) ⇒ Object
40 41 42 43 |
# File 'lib/upl/variable.rb', line 40 def self.[]( *names ) vars = names.map{|name| new name: name} if vars.size == 1 then vars.first else vars end end |
.copy(term_t) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/upl/variable.rb', line 15 def self.copy term_t inst = new term_t inst.attributed? and inst.attribute inst.to_s inst end |
Instance Method Details
#===(value) ⇒ Object
32 |
# File 'lib/upl/variable.rb', line 32 def === value; unify value end |
#_string ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/upl/variable.rb', line 47 def _string @_string ||= begin rv = Extern::PL_get_chars \ term_t, (str_ref = Fiddle::Pointer[0].ref), # need cvt_variable for normal variables, and cvt_write for clpfd variables Extern::Convert::CVT_VARIABLE | Extern::Convert::CVT_WRITE | Extern::Convert::REP_UTF8 | Extern::Convert::BUF_MALLOC # | Extern::CVT_ALL str_ref.ptr.free = Extern::swipl_free_fn # TODO might need to force utf8 encoding here? # Just use CVT_UTF8 str_ref.ptr.to_s end end |
#attribute ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/upl/variable.rb', line 71 def attribute @attribute ||= begin rv = Extern::PL_get_attr term_t, (val = Extern.PL_new_term_ref) rv == 1 or raise "can't get attribute for variable" Tree.of_term val end end |
#attributed? ⇒ Boolean
63 64 65 66 67 68 69 |
# File 'lib/upl/variable.rb', line 63 def attributed? if instance_variable_defined? :@attributed @attributed else @attributed = (Extern::PL_is_attvar term_t) == 1 end end |
#pretty_print(pp) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/upl/variable.rb', line 79 def pretty_print pp if attributed? attribute.pretty_print pp else if name pp.text name pp.text '=' end pp.text to_s end end |
#to_ruby ⇒ Object
create a ruby represetation of the term_t
13 |
# File 'lib/upl/variable.rb', line 13 def to_ruby; Tree.of_term term_t end |
#to_s ⇒ Object
45 |
# File 'lib/upl/variable.rb', line 45 def to_s; _string end |