Class: Upl::Dict
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.dict_tag(dict_term_t) ⇒ Object
fetch the tag for the dict.
-
.of_term(dict_term_t) ⇒ Object
copy dict_term_t into a ruby structure.
Instance Method Summary collapse
- #==(rhs) ⇒ Object
-
#initialize(tag: nil, values: nil, default_value: nil, &default_blk) ⇒ Dict
constructor
A new instance of Dict.
- #pretty_print(pp) ⇒ Object
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
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
49 50 51 |
# File 'lib/upl/dict.rb', line 49 def tag @tag end |
#values ⇒ Object (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 |