Class: Upl::Dict

Inherits:
Hash show all
Defined in:
lib/upl/dict.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag: nil, values: nil, default_value: nil, &default_blk) ⇒ Dict

Returns a new instance of Dict.



3
4
5
6
7
# File 'lib/upl/dict.rb', line 3

def initialize( tag: nil, values: nil, default_value: nil, &default_blk )
  @tag = tag
  super default_value, &default_blk
  replace values if values
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



49
50
51
# File 'lib/upl/dict.rb', line 49

def tag
  @tag
end

#valuesObject (readonly)

Returns the value of attribute values.



49
50
51
# File 'lib/upl/dict.rb', line 49

def values
  @values
end

Class Method Details

.dict_tag(dict_term_t) ⇒ Object

fetch the tag for the dict



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/upl/dict.rb', line 10

def self.dict_tag( dict_term_t )
  args = TermVector[dict_term_t, nil]

  # TODO need a better api here as well, and other places
  # eg, need to check that args.size == predicate.arity
  # otherwise segfaults and other weird stuff ensue
  rv = Extern::PL_call_predicate \
    Fiddle::NULL, # module
    0, # flags, see PL_open_query
    (Runtime.predicate 'is_dict', args.size),
    args.terms

  rv == 1 or raise "can't retrieve dict tag"

  # now retrieve the variable's value
  args.last.to_ruby
end

.of_term(dict_term_t) ⇒ Object

copy dict_term_t into a ruby structure



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/upl/dict.rb', line 29

def self.of_term( dict_term_t )
  # Have to do a little hoop-jumping here. There are no c-level calls to
  # access dicts, so we have to break them down with prolog predicates.

  query_term, query_hash = Runtime.term_vars 'get_dict(K,Dict,V)'

  # So set the Dict value to dict_term_t above ...
  query_term[1] = dict_term_t
  # ...and remove it from the output variables
  query_hash.delete :Dict

  # now we have a result set with K,V values
  en = Upl::Runtime.term_vars_query query_term, query_hash

  # map to a hash-y thing
  en.each_with_object Dict.new(tag: dict_tag(dict_term_t)) do |row,values|
    values[row[:K]] = row[:V]
  end
end

Instance Method Details

#==(rhs) ⇒ Object



51
52
53
# File 'lib/upl/dict.rb', line 51

def == rhs
  [tag,to_h] == [rhs.tag,rhs.to_h]
end

#pretty_print(pp) ⇒ Object



55
56
57
58
# File 'lib/upl/dict.rb', line 55

def pretty_print pp
  tag.pretty_print pp
  super
end