Class: Upl::Variable

Inherits:
Object show all
Defined in:
lib/upl/variable.rb

Overview

A variable, either from an existing term_t or a new one

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/upl/variable.rb', line 9

def name
  @name
end

#term_tObject (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

.to_termObject

TODO remove bit of a hack to create empty variables for a functor



36
37
38
# File 'lib/upl/variable.rb', line 36

def self.to_term
  Extern.PL_new_term_ref
end

Instance Method Details

#===(value) ⇒ Object



32
# File 'lib/upl/variable.rb', line 32

def === value; unify value end

#_stringObject



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

#attributeObject



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

Returns:

  • (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_rubyObject

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_sObject



45
# File 'lib/upl/variable.rb', line 45

def to_s; _string end

#unify(value) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/upl/variable.rb', line 24

def unify value
  warn "don't pass a term_t in here" if Fiddle::Pointer === value
  case Upl::Extern::PL_unify term_t, value.to_term_t
  when Upl::Extern::TRUE;  true
  when Upl::Extern::FALSE; false
  end
end