Skip to content

Commit

Permalink
add async query support
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh0416 committed Dec 12, 2023
1 parent b9e9814 commit ad3e172
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ on:
pull_request:

jobs:
build:
test:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.2.2'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
13 changes: 13 additions & 0 deletions lib/prolog_mqi/prolog_mqi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def query(value)
read
end

def query_async(value, find_all=true)
timeout_str = @timeout.nil? ? '_' : @timeout.to_s
find_all_str = find_all ? 'true' : 'false'
write("run_async(#{value}, #{find_all_str}, #{timeout_str})")
read
end

def query_async_result
timeout_str = @timeout.nil? ? '-1' : @timeout.to_s
write("async_result(#{timeout_str})")
read
end

def read
bytesize = @socket.gets.chomp(".\n").to_i
result = @socket.read(bytesize)
Expand Down
12 changes: 12 additions & 0 deletions test/test_prolog_mqi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def test_friends
end
end

def test_async_query
prolog = PrologMQI::PrologMQI.new
prolog.session do |session|
session.query("consult('#{fixture_prolog('friends')}')")
session.query('assertz(likes(alice, bob))')
session.query('assertz(likes(bob, alice))')

assert session.query_async('likes(alice, X)')
assert_equal([{ 'X' => 'bob' }], session.query_async_result))
end
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def test_character
Expand Down

0 comments on commit ad3e172

Please sign in to comment.