Class: SWIPL::PrologFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/swipl/prologframe.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frame_id) ⇒ PrologFrame



4
5
6
# File 'lib/swipl/prologframe.rb', line 4

def initialize( frame_id )
  @frame_id = frame_id
end

Class Method Details

.on(&block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/swipl/prologframe.rb', line 24

def self.on( &block )
  frame = self.open
  begin
    block.call( frame )
  ensure
    frame.close
  end
end

.openObject

Opens a foreign frame



16
17
18
19
20
21
22
# File 'lib/swipl/prologframe.rb', line 16

def self.open
  frame_id = CFFI.PL_open_foreign_frame
  if frame_id == PL_FALSE 
    raise "failed to open frame"
  end
  PrologFrame.new( frame_id )
end

Instance Method Details

#atom_from_string(string) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/swipl/prologframe.rb', line 46

def atom_from_string( string )
  atom_ptr = FFI::MemoryPointer.from_string( string.to_s )
  atom_term = CFFI.PL_new_term_ref
  if CFFI.PL_chars_to_term( atom_ptr, atom_term ) == 0
    raise "failed to create atom from terms"
  end
  Term.new( atom_term )
end

#closeObject



8
9
10
11
12
13
# File 'lib/swipl/prologframe.rb', line 8

def close
  result = CFFI.PL_discard_foreign_frame( @frame_id )
  if result == PL_FALSE
    raise "Failed to close frame"
  end
end

#refs(count) ⇒ Object

allocates teh number of terms and returns an array of those terms

NOTE: SWI requires continous terms from time to time (ie: PL_open_query)



36
37
38
39
40
41
42
43
44
# File 'lib/swipl/prologframe.rb', line 36

def refs( count )
  return [] if count == 0

  base = CFFI.PL_new_term_refs( count )
  #TODO: Verify the result of the query
  (0..(count-1)).map do |index|
    Term.new( base + index )
  end
end