rbprolog 0.0.1

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
+ SHA1:
3
+ metadata.gz: 6af2d1c30e5537ecd0129fb9ff8ec732751091c2
4
+ data.tar.gz: fdb511793cdaa753e458f6c7629209f0c1a9744a
5
+ SHA512:
6
+ metadata.gz: 8e9cb8fa7831f2b504b440b2a31ccc2f4a14c0db2ac1e5380af7079d46b14d09d8cc5cec21db243e19eb57f17fb3666a6a94ec1074fb64f822fd216e346eff43
7
+ data.tar.gz: 61703cdc1663c95c817dd7ebc032712c701b8c12fb1180e1b907ae13a546d9aeedc5d1e0c422078c8f163caa6f4dab47ed07ecba27871d6c3015e7aa1c0364c9
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbprolog.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 RUIJIA LI
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Rbprolog
2
+
3
+ The gem simulates logic processing functionality of prolog. It uses DSL style and only provides limited features now.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rbprolog'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rbprolog
18
+
19
+ ## Usage
20
+
21
+ * Include Rbprolog and describe the facts and rules
22
+
23
+ ```ruby
24
+ class FriendLogic
25
+ include Rbprolog
26
+
27
+ keywords :likes, :friends
28
+
29
+ likes 'p1', 's1'
30
+ likes 'p1', 's2'
31
+ likes 'p2', 's2'
32
+ likes 'p3', 's1'
33
+ likes 'p4', X
34
+
35
+ friends 'p1', W, :if => likes?(W, 's2')
36
+ friends X, Y, :if => [likes?(X, Z), likes?(Y, Z)]
37
+ end
38
+ ```
39
+
40
+ * Instance the class to question
41
+
42
+ ```ruby
43
+ l = FriendLogic.new
44
+ l.likes?('p1', 's1') #=> true
45
+ l.friends?('p1', 'p4') #=> true
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new
@@ -0,0 +1,53 @@
1
+ module Rbprolog
2
+ class Context
3
+ attr_accessor :binds
4
+
5
+ def match?(v1, v2)
6
+ v1 = deduce(v1)
7
+ Var === v1 || Var === v2 || v1 == v2
8
+ end
9
+
10
+ def match!(v1, v2)
11
+ if match?(v1, v2)
12
+ if Var === v1 && !(Var === v2)
13
+ @binds[v1.sym] = v2
14
+ # elsif Var === v2 && !(Var === v1)
15
+ # @binds[v2.sym] = v1
16
+ end
17
+
18
+ true
19
+ else
20
+ false
21
+ end
22
+ end
23
+
24
+ def deduce(v)
25
+ if Var === v
26
+ unless @binds[v.sym]
27
+ @stacks.last << v.sym
28
+ @binds[v.sym] = Var.new(v.sym)
29
+ end
30
+
31
+ @binds[v.sym]
32
+ else
33
+ v
34
+ end
35
+ end
36
+
37
+ def scope(predicate, &block)
38
+ @stacks ||= []
39
+ @stacks.push([])
40
+
41
+ @binds ||= {}
42
+
43
+ mirror = @binds.clone
44
+
45
+ result = yield
46
+
47
+ @stacks.pop.each {|bind| @binds.delete bind}
48
+ @binds.merge!(mirror)
49
+
50
+ result
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,42 @@
1
+ module Rbprolog
2
+ class Deduction
3
+ include Enumerable
4
+ attr_accessor :args, :sym
5
+
6
+ def initialize(logic, sym, *args)
7
+ @logic = logic
8
+ @sym = sym
9
+
10
+ @args = args
11
+ end
12
+
13
+ def each
14
+ each_deduce(Context.new, @logic.rules, "", []) do |hash|
15
+ yield hash
16
+ end
17
+ end
18
+
19
+ def each_deduce(context, rules, tabs, id)
20
+ print "#{tabs}#{id.join('.')} #{@sym}?(#{@args.map(&:to_s).join(', ')})"
21
+
22
+ rules.select {|rule| rule.sym == @sym}.each_with_index do |rule, i|
23
+ context.scope(self) do
24
+ puts " => #{@sym}?(#{@args.map {|arg| context.deduce(arg).to_s}.join(', ')})" if i == 0 #{context.to_s} #{context.binds.inspect}" if i == 0
25
+
26
+ rule.each_deduce(rules, *@args.map {|arg| context.deduce(arg)}, tabs + "\t", id + [i]) do |hash|
27
+ context.scope(self) do
28
+ rule.args.each_with_index do |rule_arg, rule_arg_index|
29
+ deduced_arg = context.deduce(@args[rule_arg_index])
30
+ if Var === deduced_arg
31
+ context.binds[deduced_arg.sym] = Var === rule_arg ? hash[rule_arg.sym] : rule_arg
32
+ end
33
+ end
34
+
35
+ yield context.binds
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,59 @@
1
+ module Rbprolog
2
+
3
+ #when fail at one predicate, the output values need be reset, and remove from the parent rule/s context
4
+ class Rule
5
+ attr_accessor :args, :sym
6
+
7
+ def initialize(sym, *args, predicates)
8
+ @sym = sym
9
+ @args = args
10
+ @predicates = [predicates].flatten
11
+ end
12
+
13
+ def each_deduce(rules, *args, tabs, id)
14
+ print "#{tabs}#{id.join('.')} #{@sym}(#{@args.map(&:to_s).join(', ')}).deduce(#{args.map(&:to_s).join(', ')})"
15
+
16
+ context = Context.new
17
+ context.scope(self) do
18
+ if self.match!(context, args)
19
+ puts " => #{@sym}(#{@args.map {|arg| context.deduce(arg).to_s}.join(', ')})" #{context.to_s} #{context.binds.inspect}"
20
+ deduce_predicates(context, rules, *@predicates, tabs, id) do
21
+ yield context.binds
22
+ end
23
+ else
24
+ print "\n"
25
+ end
26
+ end
27
+ end
28
+
29
+ def deduce_predicates(context, rules, *predicates, tabs, id, &block)
30
+ if predicates.empty?
31
+ yield
32
+ else
33
+ predicate = predicates.shift
34
+ if Deduction === predicate
35
+ predicate.each_deduce(context, rules, tabs + "\t", id + [@predicates.size - predicates.size - 1]) do |hash|
36
+ deduce_predicates(context, rules, *predicates, tabs, id, &block)
37
+ end
38
+ else
39
+ @logic.send(:define_singleton_method, :const_missing) do |sym|
40
+ context.binds[sym]
41
+ end
42
+
43
+ predicate.call && deduce_predicates(context, rules, *predicates, tabs, id, &block)
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ def match?(context, args)
50
+ @args.size == args.size && @args.zip(args).all? {|v1, v2| context.match?(v1, v2)}
51
+ end
52
+
53
+ def match!(context, args)
54
+ match = match?(context, args)
55
+ @args.zip(args).each {|v1, v2| context.match!(v1, v2)} if match
56
+ match
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,17 @@
1
+ module Rbprolog
2
+ class Var
3
+ attr_accessor :sym
4
+
5
+ def initialize(sym)
6
+ @sym = sym
7
+ end
8
+
9
+ def to_s
10
+ @sym.to_s
11
+ end
12
+
13
+ def ==(other)
14
+ true
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Rbprolog
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rbprolog.rb ADDED
@@ -0,0 +1,67 @@
1
+ require_relative "rbprolog/version"
2
+ require_relative 'rbprolog/context'
3
+ require_relative 'rbprolog/rule'
4
+ require_relative 'rbprolog/deduction'
5
+ require_relative 'rbprolog/var'
6
+
7
+ module Rbprolog
8
+ def self.included(mod)
9
+ class << mod
10
+ attr_accessor :rules, :syms
11
+
12
+ include ClassMethods
13
+ end
14
+ end
15
+
16
+ def initialize(&block)
17
+ instance_eval(&block) if block
18
+ end
19
+
20
+ def rules
21
+ self.class.rules + (@rules || [])
22
+ end
23
+
24
+ module ClassMethods
25
+ def keywords(*syms)
26
+ raise if syms.any? {|sym| sym.to_s.end_with? '?'}
27
+
28
+ self.syms ||= []
29
+ self.syms.concat(syms)
30
+ end
31
+
32
+ def const_missing(sym)
33
+ Var.new(sym)
34
+ end
35
+
36
+ def method_missing(sym, *args)
37
+ if self.syms.include? sym
38
+ Hash === args.last ? rule(sym, *args) : rule(sym, *args, :if => [])
39
+ elsif self.syms.include? sym.to_s.chomp('?').to_sym
40
+ Deduction.new(self, sym.to_s.chomp('?').to_sym, *args)
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ def rule(sym, *args, options)
47
+ self.rules ||= []
48
+ self.rules << Rule.new(sym, *args, options[:if])
49
+
50
+ unless method_defined?(sym)
51
+ #FIXME only allow fact for now
52
+ define_method(sym) do |*args|
53
+ @rules ||= []
54
+ @rules << Rule.new(sym, *args, [])
55
+ end
56
+
57
+ define_method("#{sym}!") do |*args|
58
+ Deduction.new(self, sym, *args)
59
+ end
60
+
61
+ define_method("#{sym}?") do |*args|
62
+ self.send("#{sym}!", *args).any? {|hash| true}
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
data/rbprolog.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rbprolog/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbprolog"
8
+ spec.version = Rbprolog::VERSION
9
+ spec.authors = ["ruijia.li"]
10
+ spec.email = ["ruijia.li@gmail.com"]
11
+ spec.description = %q{A ruby implementation to simulate prolog partially}
12
+ spec.summary = %q{A prolog DSL in ruby to do AI logic analysis}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,36 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/rbprolog'
3
+
4
+ class FriendLogic
5
+ include Rbprolog
6
+
7
+ keywords :likes, :friends
8
+
9
+ likes 'p1', 's1'
10
+ likes 'p1', 's2'
11
+ likes 'p2', 's2'
12
+ likes 'p3', 's1'
13
+ likes 'p4', X
14
+
15
+ friends 'p1', W, :if => likes?(W, 's2')
16
+ friends X, Y, :if => [likes?(X, Z), likes?(Y, Z)]
17
+ end
18
+
19
+ class TestRbprolog < Test::Unit::TestCase
20
+ def test_friend_logic
21
+ l = FriendLogic.new do
22
+ likes 'p5', 's1'
23
+ end
24
+
25
+ assert_equal true, l.likes?('p1', 's1')
26
+ assert_equal true, l.likes?('p1', 's2')
27
+ assert_equal true, l.friends?('p1', 'p2')
28
+ assert_equal true, l.friends?('p1', 'p3')
29
+ assert_equal true, l.friends?('p1', 'p4')
30
+ assert_equal false, l.friends?('p1', 'p6')
31
+ assert_equal true, l.likes?('p5', 's1')
32
+
33
+ assert_equal ['s1', 's2'], l.likes!('p1', Rbprolog::Var.new(:X)).map {|hash| hash[:X]}.uniq
34
+ assert_equal ['p1', 'p2', 'p3', 'p4', 'p5'], l.friends!('p1', Rbprolog::Var.new(:W)).map {|hash| hash[:W]}.uniq.sort
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbprolog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ruijia.li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A ruby implementation to simulate prolog partially
42
+ email:
43
+ - ruijia.li@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/rbprolog.rb
54
+ - lib/rbprolog/context.rb
55
+ - lib/rbprolog/deduction.rb
56
+ - lib/rbprolog/rule.rb
57
+ - lib/rbprolog/var.rb
58
+ - lib/rbprolog/version.rb
59
+ - rbprolog.gemspec
60
+ - test/test_rbprolog.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: A prolog DSL in ruby to do AI logic analysis
85
+ test_files:
86
+ - test/test_rbprolog.rb