prolog_mqi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8abe4438970b7b52d37ea9a90606f117ec3da035979338ed5b3dae270480eee
4
+ data.tar.gz: 59a42726e7e3caf68180cee92bba94c33a76935d36ffca278dc7e3dc690e159d
5
+ SHA512:
6
+ metadata.gz: 84e2d5b237865b9cf9fe3d9f5a8fab0db15d3b30e2a6eb2d6bf3cf301be9c042bcc20b848e3585ecc2c0751b1a1b86f97bc7997709193552a1bc7002393844a7
7
+ data.tar.gz: 41c934900602039472f413fc0ee31b6beeaf0b3a51466af41173ebbb51cba0ee57cf9bc31c6c644345f7f5702a551c314906f2fce09caeea69c477401036cfe1
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Metrics/MethodLength:
13
+ Max: 20
14
+
15
+ Layout/LineLength:
16
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in Prologmqi.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'rake', '~> 13.0'
10
+
11
+ gem 'rubocop'
12
+ gem 'rubocop-minitest'
13
+ gem 'rubocop-rake'
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prolog_mqi (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.6.3)
11
+ language_server-protocol (3.17.0.3)
12
+ minitest (5.18.1)
13
+ parallel (1.23.0)
14
+ parser (3.2.2.3)
15
+ ast (~> 2.4.1)
16
+ racc
17
+ racc (1.7.1)
18
+ rainbow (3.1.1)
19
+ rake (13.0.6)
20
+ regexp_parser (2.8.1)
21
+ rexml (3.2.5)
22
+ rubocop (1.53.1)
23
+ json (~> 2.3)
24
+ language_server-protocol (>= 3.17.0)
25
+ parallel (~> 1.10)
26
+ parser (>= 3.2.2.3)
27
+ rainbow (>= 2.2.2, < 4.0)
28
+ regexp_parser (>= 1.8, < 3.0)
29
+ rexml (>= 3.2.5, < 4.0)
30
+ rubocop-ast (>= 1.28.0, < 2.0)
31
+ ruby-progressbar (~> 1.7)
32
+ unicode-display_width (>= 2.4.0, < 3.0)
33
+ rubocop-ast (1.29.0)
34
+ parser (>= 3.2.1.0)
35
+ rubocop-minitest (0.31.0)
36
+ rubocop (>= 1.39, < 2.0)
37
+ rubocop-rake (0.6.0)
38
+ rubocop (~> 1.0)
39
+ ruby-progressbar (1.13.0)
40
+ unicode-display_width (2.4.2)
41
+
42
+ PLATFORMS
43
+ x86_64-linux
44
+
45
+ DEPENDENCIES
46
+ minitest (~> 5.0)
47
+ prolog_mqi!
48
+ rake (~> 13.0)
49
+ rubocop
50
+ rubocop-minitest
51
+ rubocop-rake
52
+
53
+ BUNDLED WITH
54
+ 2.4.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Yet Another AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # prologmqi.rb
2
+ Yet Another Prolog Interface for Ruby
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/test_*.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrologMQI
4
+ class Error < StandardError; end
5
+ class PrologError < Error; end
6
+ class LaunchError < Error; end
7
+ class PrologQueryTimeoutError < PrologError; end
8
+ class PrologConnectionFailedError < PrologError; end
9
+ class PrologNoQueryError < PrologError; end
10
+ class PrologQueryCancelledError < PrologError; end
11
+ class PrologResultNotAvailableError < PrologError; end
12
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrologMQI
4
+ class Parser
5
+ def initialize(plain)
6
+ @term = Term.new(JSON.parse(plain))
7
+ end
8
+
9
+ def parse
10
+ handle_error
11
+ resolve_content
12
+ end
13
+
14
+ # rubocop:disable Metrics/AbcSize
15
+ # rubocop:disable Metrics/CyclomaticComplexity
16
+ def handle_error
17
+ return unless @term.name == 'exception'
18
+ raise PrologError, @term unless result.key?('args') && @term.args.length >= 1
19
+
20
+ case @term.args[0]
21
+ when 'no_more_results'
22
+ nil
23
+ when 'connection_failed'
24
+ raise PrologConnectionFailedError, @term
25
+ when 'time_limit_exceeded'
26
+ raise PrologQueryTimeoutError, @term
27
+ when 'no_query'
28
+ raise PrologNoQueryError, @term
29
+ when 'cancel_goal'
30
+ raise PrologQueryCancelledError, @term
31
+ when 'result_not_available'
32
+ raise PrologResultNotAvailableError, @term
33
+ else
34
+ raise PrologError, @term
35
+ end
36
+ end
37
+ # rubocop:enable Metrics/AbcSize
38
+ # rubocop:enable Metrics/CyclomaticComplexity
39
+
40
+ def resolve_content
41
+ return false if @term.name == 'false'
42
+
43
+ if @term.name == 'true'
44
+ answers = []
45
+ @term.args[0].each do |answer|
46
+ next answers << true if answer.empty?
47
+
48
+ answers << answer.to_h { |a| Term.new(a).args }
49
+ end
50
+ return true if answers == [true]
51
+
52
+ return answers
53
+ end
54
+ @term
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrologMQI
4
+ class PrologMQI
5
+ def initialize(timeout: 5)
6
+ @timeout = timeout
7
+
8
+ @process = nil
9
+ @unix_domain_socket = nil
10
+ @running = false
11
+ end
12
+
13
+ def running?
14
+ @running
15
+ end
16
+
17
+ def session
18
+ start unless running?
19
+ yield self
20
+ ensure
21
+ stop if running?
22
+ end
23
+
24
+ def start
25
+ # Start prolog process
26
+
27
+ @stdin, @stdout, @stderr, @process =
28
+ Open3.popen3('swipl', '--quiet', '-g', 'mqi_start', '-t', 'halt', '--', '--write_connection_values=true',
29
+ '--create_unix_domain_socket=true')
30
+
31
+ unix_domain_socket = @stdout.gets.chomp
32
+ password = @stdout.gets.chomp
33
+
34
+ # Create unix socket
35
+ Timeout.timeout(@timeout) do
36
+ @socket = UNIXSocket.new(unix_domain_socket)
37
+ rescue Errno::ECONNREFUSED
38
+ retry
39
+ end
40
+
41
+ # Authenticate
42
+ write(password)
43
+ read
44
+ end
45
+
46
+ def query(value)
47
+ timeout_str = @timeout.nil? ? '_' : @timeout.to_s
48
+ write("run(#{value}, #{timeout_str})")
49
+ read
50
+ end
51
+
52
+ def read
53
+ bytesize = @socket.gets.chomp(".\n").to_i
54
+ result = @socket.read(bytesize)
55
+ Parser.new(result).parse
56
+ end
57
+
58
+ def write(value)
59
+ message = "#{value}.\n"
60
+ # FIXME: in PrologMQI major version 0,
61
+ # the message length is count of Unicode code points, not bytes.
62
+ bytesize = message.bytesize
63
+ @socket.write("#{bytesize}.\n")
64
+ @socket.write(message)
65
+ end
66
+
67
+ def stop(kill: false)
68
+ @socket.close
69
+ @stdin.close
70
+ @stdout.close
71
+ @stderr.close
72
+ @process.kill if kill
73
+ @process.close
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrologMQI
4
+ class Term
5
+ def initialize(term)
6
+ @term = term
7
+ end
8
+
9
+ def functor?
10
+ @term.is_a?(Hash) && @term.key?('functor') && @term.key?('args')
11
+ end
12
+
13
+ def array?
14
+ @term.is_a?(Array)
15
+ end
16
+
17
+ def variable?
18
+ @term.is_a?(String) && (@term[0].upcase == @term[0] || @term.start_with?('_'))
19
+ end
20
+
21
+ def atom?
22
+ @term.is_a?(String) && !variable?
23
+ end
24
+
25
+ def name
26
+ return @term if atom? || variable?
27
+
28
+ @term['functor']
29
+ end
30
+
31
+ def args
32
+ @term['args']
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrologMQI
4
+ VERSION = '0.1.0'
5
+ end
data/lib/prolog_mqi.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'open3'
5
+ require 'socket'
6
+ require 'timeout'
7
+
8
+ require_relative 'prolog_mqi/errors'
9
+ require_relative 'prolog_mqi/parser'
10
+ require_relative 'prolog_mqi/prolog_mqi'
11
+ require_relative 'prolog_mqi/term'
12
+ require_relative 'prolog_mqi/version'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/prolog_mqi/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'prolog_mqi'
7
+ spec.version = PrologMQI::VERSION
8
+ spec.authors = ['Delton Ding']
9
+ spec.email = ['shenghao.ding@yetanother.ai']
10
+
11
+ spec.summary = 'Write a short summary, because RubyGems requires one.'
12
+ spec.description = 'Write a longer description or delete this line.'
13
+ spec.homepage = 'https://github.com/yet-another-ai/prologmqi.rb'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.0.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/yet-another-ai/prologmqi.rb'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ spec.metadata['rubygems_mfa_required'] = 'true'
37
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prolog_mqi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Delton Ding
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write a longer description or delete this line.
14
+ email:
15
+ - shenghao.ding@yetanother.ai
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/prolog_mqi.rb
29
+ - lib/prolog_mqi/errors.rb
30
+ - lib/prolog_mqi/parser.rb
31
+ - lib/prolog_mqi/prolog_mqi.rb
32
+ - lib/prolog_mqi/term.rb
33
+ - lib/prolog_mqi/version.rb
34
+ - prolog_mqi.gemspec
35
+ homepage: https://github.com/yet-another-ai/prologmqi.rb
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/yet-another-ai/prologmqi.rb
40
+ source_code_uri: https://github.com/yet-another-ai/prologmqi.rb
41
+ rubygems_mfa_required: 'true'
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.4.10
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Write a short summary, because RubyGems requires one.
61
+ test_files: []