Class: Oso::Polar::Host
- Inherits:
-
Object
- Object
- Oso::Polar::Host
- Defined in:
- lib/oso/polar/host.rb
Overview
Translate between Polar and the host language (Ruby).
Constant Summary collapse
- DEFAULT_COMBINE_QUERY =
proc { raise 'implement combine_query to use data filtering' }
- DEFAULT_BUILD_QUERY =
proc { raise 'implement build_query to use data filtering' }
- DEFAULT_EXEC_QUERY =
proc { raise 'implement exec_query to use data filtering' }
- OPS =
{ 'Lt' => :<, 'Gt' => :>, 'Eq' => :==, 'Geq' => :>=, 'Leq' => :<=, 'Neq' => :!= }.freeze
Instance Attribute Summary collapse
-
#accept_expression ⇒ Object
writeonly
Sets the attribute accept_expression.
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#build_query ⇒ Object
Returns the value of attribute build_query.
-
#combine_query ⇒ Object
Returns the value of attribute combine_query.
-
#exec_query ⇒ Object
Returns the value of attribute exec_query.
- #types ⇒ Hash<String, UserType> readonly
Instance Method Summary collapse
-
#cache_class(cls, name:, fields:, build_query:, combine_query:, exec_query:) ⇒ String
Store a Ruby class in the #types cache.
-
#cache_instance(instance, id: nil) ⇒ Integer
Cache a Ruby instance in the #instances cache, fetching a new id if one isn't provided.
- #enrich_message(msg) ⇒ Object
-
#get_class(name) ⇒ Class
Fetch a Ruby class from the #types cache.
-
#get_instance(id) ⇒ Object
Fetch a Ruby instance from the #instances cache.
-
#initialize(ffi_polar) ⇒ Host
constructor
A new instance of Host.
- #initialize_copy(other) ⇒ Object
-
#instance?(id) ⇒ Boolean
Check if an instance exists in the #instances cache.
-
#isa?(instance, class_tag:) ⇒ Boolean
Check if instance is an instance of class.
-
#make_instance(cls_name, args:, kwargs:, id:) ⇒ Integer
Construct and cache a Ruby instance.
-
#operator(operation, args) ⇒ Boolean
Compare two values.
-
#register_mros ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#serialize_types ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #subclass?(left_tag:, right_tag:) ⇒ Boolean
-
#subspecializer?(instance_id, left_tag:, right_tag:) ⇒ Boolean
Check if the left class is more specific than the right class with respect to the given instance.
-
#to_polar(value) ⇒ Hash<String, Object>
Turn a Ruby value into a Polar term that's ready to be sent across the FFI boundary.
-
#to_ruby(data) ⇒ Object
Turn a Polar term passed across the FFI boundary into a Ruby value.
Constructor Details
#initialize(ffi_polar) ⇒ Host
Returns a new instance of Host.
78 79 80 81 82 83 84 85 86 |
# File 'lib/oso/polar/host.rb', line 78 def initialize(ffi_polar) @ffi_polar = ffi_polar @types = {} @instances = {} @accept_expression = false @combine_query = DEFAULT_COMBINE_QUERY @build_query = DEFAULT_BUILD_QUERY @exec_query = DEFAULT_EXEC_QUERY end |
Instance Attribute Details
#accept_expression=(value) ⇒ Object
Sets the attribute accept_expression
71 72 73 |
# File 'lib/oso/polar/host.rb', line 71 def accept_expression=(value) @accept_expression = value end |
#adapter ⇒ Object
Returns the value of attribute adapter.
72 73 74 |
# File 'lib/oso/polar/host.rb', line 72 def adapter @adapter end |
#build_query ⇒ Object
Returns the value of attribute build_query.
72 73 74 |
# File 'lib/oso/polar/host.rb', line 72 def build_query @build_query end |
#combine_query ⇒ Object
Returns the value of attribute combine_query.
72 73 74 |
# File 'lib/oso/polar/host.rb', line 72 def combine_query @combine_query end |
#exec_query ⇒ Object
Returns the value of attribute exec_query.
72 73 74 |
# File 'lib/oso/polar/host.rb', line 72 def exec_query @exec_query end |
#types ⇒ Hash<String, UserType> (readonly)
58 59 60 |
# File 'lib/oso/polar/host.rb', line 58 def types @types end |
Instance Method Details
#cache_class(cls, name:, fields:, build_query:, combine_query:, exec_query:) ⇒ String
Store a Ruby class in the #types cache.
under a previously-registered name.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/oso/polar/host.rb', line 113 def cache_class(cls, name:, fields:, build_query:, combine_query:, exec_query:) # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength raise DuplicateClassAliasError.new name: name, old: get_class(name), new: cls if types.key? name types[name] = types[cls] = UserType.new( name: name, klass: PolarClass.new(cls), id: cache_instance(cls), fields: fields || {}, combine_query: combine_query || self.combine_query, exec_query: exec_query || self.exec_query, build_query: build_query || self.build_query ) name end |
#cache_instance(instance, id: nil) ⇒ Integer
Cache a Ruby instance in the #instances cache, fetching a new id if one isn't provided.
171 172 173 174 175 176 177 |
# File 'lib/oso/polar/host.rb', line 171 def cache_instance(instance, id: nil) id = ffi_polar.new_id if id.nil? # Save the instance as a PolarClass if it is a non-anonymous class instance = PolarClass.new(instance) if instance.is_a?(Class) instances[id] = instance id end |
#enrich_message(msg) ⇒ Object
412 413 414 415 416 |
# File 'lib/oso/polar/host.rb', line 412 def (msg) msg.gsub(/\^\{id: ([0-9]+)\}/) do get_instance(Regexp.last_match[1].to_i).to_s end end |
#get_class(name) ⇒ Class
Fetch a Ruby class from the #types cache.
99 100 101 102 103 104 |
# File 'lib/oso/polar/host.rb', line 99 def get_class(name) typ = types[name] raise UnregisteredClassError, name if typ.nil? typ.klass.get end |
#get_instance(id) ⇒ Object
Fetch a Ruby instance from the #instances cache.
156 157 158 159 160 161 162 163 |
# File 'lib/oso/polar/host.rb', line 156 def get_instance(id) raise UnregisteredInstanceError, id unless instance? id instance = instances[id] return instance.get if instance.is_a? PolarClass instance end |
#initialize_copy(other) ⇒ Object
88 89 90 91 92 |
# File 'lib/oso/polar/host.rb', line 88 def initialize_copy(other) @ffi_polar = other.ffi_polar @types = other.types.dup @instances = other.instances.dup end |
#instance?(id) ⇒ Boolean
Check if an instance exists in the #instances cache.
142 143 144 145 146 147 148 149 |
# File 'lib/oso/polar/host.rb', line 142 def instance?(id) case id when Integer instances.key? id else instances.value? id end end |
#isa?(instance, class_tag:) ⇒ Boolean
Check if instance is an instance of class.
248 249 250 251 252 |
# File 'lib/oso/polar/host.rb', line 248 def isa?(instance, class_tag:) instance = to_ruby(instance) cls = get_class(class_tag) instance.is_a? cls end |
#make_instance(cls_name, args:, kwargs:, id:) ⇒ Integer
Construct and cache a Ruby instance.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/oso/polar/host.rb', line 187 def make_instance(cls_name, args:, kwargs:, id:) instance = if kwargs.empty? # This check is for Ruby < 2.7. get_class(cls_name).__send__(:new, *args) else get_class(cls_name).__send__(:new, *args, **kwargs) end cache_instance(instance, id: id) rescue StandardError => e raise PolarRuntimeError, "Error constructing instance of #{cls_name}: #{e}" end |
#operator(operation, args) ⇒ Boolean
Compare two values
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/oso/polar/host.rb', line 213 def operator(operation, args) left, right = args op = OPS[operation] raise PolarRuntimeError, "Unsupported external operation '#{left.class} #{operation} #{right.class}'" if op.nil? begin left.__send__ op, right rescue StandardError raise PolarRuntimeError, "External operation '#{left.class} #{operation} #{right.class}' failed." end end |
#register_mros ⇒ Object
rubocop:disable Metrics/AbcSize
128 129 130 131 132 133 134 135 136 |
# File 'lib/oso/polar/host.rb', line 128 def register_mros # rubocop:disable Metrics/AbcSize types.values.uniq.each do |typ| mro = [] typ.klass.get.ancestors.each do |a| mro.push(types[a].id) if types.key?(a) end ffi_polar.register_mro(typ.name, mro) end end |
#serialize_types ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/oso/polar/host.rb', line 254 def serialize_types # rubocop:disable Metrics/AbcSize, Metrics/MethodLength polar_types = {} types.values.uniq.each do |typ| tag = typ.name fields = typ.fields field_types = {} fields.each do |k, v| field_types[k] = if v.is_a? ::Oso::Polar::DataFiltering::Relation { 'Relation' => { 'kind' => v.kind, 'other_class_tag' => v.other_type, 'my_field' => v.my_field, 'other_field' => v.other_field } } else { 'Base' => { 'class_tag' => types[v].name } } end end polar_types[tag] = field_types end polar_types end |
#subclass?(left_tag:, right_tag:) ⇒ Boolean
239 240 241 |
# File 'lib/oso/polar/host.rb', line 239 def subclass?(left_tag:, right_tag:) get_class(left_tag) <= get_class(right_tag) end |
#subspecializer?(instance_id, left_tag:, right_tag:) ⇒ Boolean
Check if the left class is more specific than the right class with respect to the given instance.
232 233 234 235 236 237 |
# File 'lib/oso/polar/host.rb', line 232 def subspecializer?(instance_id, left_tag:, right_tag:) mro = get_instance(instance_id).class.ancestors left_index = mro.index(get_class(left_tag)) right_index = mro.index(get_class(right_tag)) left_index && right_index && left_index < right_index end |
#to_polar(value) ⇒ Hash<String, Object>
Turn a Ruby value into a Polar term that's ready to be sent across the FFI boundary.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/oso/polar/host.rb', line 285 def to_polar(value) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity value = case true # rubocop:disable Lint/LiteralAsCondition when value.instance_of?(TrueClass) || value.instance_of?(FalseClass) { 'Boolean' => value } when value.instance_of?(Integer) { 'Number' => { 'Integer' => value } } when value.instance_of?(Float) if value == Float::INFINITY value = 'Infinity' elsif value == -Float::INFINITY value = '-Infinity' elsif value.nan? value = 'NaN' end { 'Number' => { 'Float' => value } } when value.instance_of?(String) { 'String' => value } when value.instance_of?(Array) { 'List' => value.map { |el| to_polar(el) } } when value.instance_of?(Hash) { 'Dictionary' => { 'fields' => value.transform_values { |v| to_polar(v) } } } when value.instance_of?(Predicate) { 'Call' => { 'name' => value.name, 'args' => value.args.map { |el| to_polar(el) } } } when value.instance_of?(Variable) # This is supported so that we can query for unbound variables { 'Variable' => value.name } when value.instance_of?(Expression) { 'Expression' => { 'operator' => value.operator, 'args' => value.args.map { |el| to_polar(el) } } } when value.instance_of?(Pattern) dict = to_polar(value.fields)['value'] if value.tag.nil? { 'Pattern' => dict } else { 'Pattern' => { 'Instance' => { 'tag' => value.tag, 'fields' => dict['Dictionary'] } } } end else instance_id = nil class_id = nil class_repr = nil # id=class_id, # pass `class_id` & `class_repr` for registered types if value.is_a?(Class) && types.key?(value) instance_id = class_id = types[value].id elsif types.key?(value.class) class_id = types[value.class].id class_repr = types[value.class].name end { 'ExternalInstance' => { 'instance_id' => cache_instance(value, id: instance_id), 'repr' => nil, 'class_repr' => class_repr, 'class_id' => class_id } } end { 'value' => value } end |
#to_ruby(data) ⇒ Object
Turn a Polar term passed across the FFI boundary into a Ruby value.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/oso/polar/host.rb', line 354 def to_ruby(data) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity tag, value = data['value'].first case tag when 'String', 'Boolean' value when 'Number' num = value.values.first case value.keys.first when 'Float' case num when 'Infinity' Float::INFINITY when '-Infinity' -Float::INFINITY when 'NaN' Float::NAN else unless value['Float'].is_a? Float # rubocop:disable Metrics/BlockNesting raise PolarRuntimeError, "Expected a floating point number, got \"#{value['Float']}\"" end num end else num end when 'List' value.map { |el| to_ruby(el) } when 'Dictionary' value['fields'].transform_values { |v| to_ruby(v) } when 'ExternalInstance' get_instance(value['instance_id']) when 'Call' Predicate.new(value['name'], args: value['args'].map { |a| to_ruby(a) }) when 'Variable' Variable.new(value) when 'Expression' raise UnexpectedPolarTypeError, tag unless accept_expression args = value['args'].map { |a| to_ruby(a) } Expression.new(value['operator'], args) when 'Pattern' case value.keys.first when 'Instance' tag = value.values.first['tag'] fields = value.values.first['fields']['fields'].transform_values { |v| to_ruby(v) } Pattern.new(tag, fields) when 'Dictionary' fields = value.values.first['fields'].transform_values { |v| to_ruby(v) } Pattern.new(nil, fields) else raise UnexpectedPolarTypeError, "#{value.keys.first} variant of Pattern" end else raise UnexpectedPolarTypeError, tag end end |