ruby-prolog 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffa39b815b60de57f1865b1c5b2abd00345623c2d90a81c4185f5800bfe957ca
4
- data.tar.gz: 2b279f1a9c1e6cfb9320de5c52a6ee484f47e62f1001a58d3bd17c56ff9fca57
3
+ metadata.gz: 4dd70c2b7255275600ce03afa496196c08e1934e8c8a76d4300ea6a29ffa89f5
4
+ data.tar.gz: 2d0aa3ed40c536a4d1f3d39d883640238f1aa569c80de4f16066b08fa7db82d9
5
5
  SHA512:
6
- metadata.gz: 6efaec42888b2749232637261b933e3a07c0a889b4ebc73995b7252e02048438b74b11d9c55a24e2687ecb0581e6d3c6d1efaad43444e3fa4601b5bc9165c432
7
- data.tar.gz: f73024546d7bfd77b39fbd738bd06eacb0b1b3f025309fc55b3a43e332e7b2b8c8ab714db399886a62003a1b261ceab2ed65ee5d7adff9ac3cb267a399a864de
6
+ metadata.gz: ad4daa2df49daf2c182142148c67b54d81828eb953fc54be41f540a47e280afc735dc322d507201afbbdd9dbfb93badc9dc943abb3f019abf8cbf25efba0169b
7
+ data.tar.gz: 06e32aac58dedaaa865be276203f4b3667447ca4e4c3a5ae855d2eaa0c94c14eb47900e59b4445e8f79665e83536edcd1b5d1cfff08af96b8e48c0d3116d1d10
data/README.md CHANGED
@@ -89,6 +89,37 @@ end
89
89
 
90
90
  This will mutate your database. If you want to "fork" your database instead, you can call `db.clone`, which will return a new instance with all stored data. Cloning like this is optimized to copy as little as possible.
91
91
 
92
+ ### `to_prolog`
93
+
94
+ If you're loading rules from a database, you might be generating predicates like this:
95
+
96
+ ```rb
97
+ rules = Ruleset.find_by(org: ...).rules
98
+ db = RubyProlog.new do
99
+ rules['permissions'].map do |role, perm|
100
+ permission[role, perm].fact
101
+ end
102
+ end
103
+ ```
104
+
105
+ However, if something doesn't work, how do find out why?
106
+
107
+ This is where `#to_prolog` comes in handy. Just run it on your instance:
108
+
109
+ ```rb
110
+ puts db.to_prolog
111
+ ```
112
+
113
+ and you'll get something that looks like this:
114
+
115
+ ```text
116
+ permission('admin', 'invite').
117
+ permission('admin', 'ban').
118
+ permission('membe', 'create_post').
119
+ ```
120
+
121
+ Then you can do a quick copy/paste into an environment like [Tau Prolog's sandbox](http://tau-prolog.org/sandbox/) or [SWISH](https://swish.swi-prolog.org) and run some queries.
122
+
92
123
  Examples
93
124
  ----
94
125
 
@@ -14,8 +14,8 @@ module RubyProlog
14
14
  attr_reader :id, :name
15
15
  attr_accessor :db, :clauses
16
16
 
17
- def initialize(db, name)
18
- @id = (@@id_counter += 1)
17
+ def initialize(db, name, explicit_id: nil)
18
+ @id = explicit_id || (@@id_counter += 1)
19
19
  @db = db
20
20
  @name = name
21
21
  @clauses = []
@@ -52,6 +52,7 @@ module RubyProlog
52
52
  goals = rhs.map do |x|
53
53
  case x
54
54
  when TempClause then x.to_goal
55
+ when false then Goal.new(0, 'false', [])
55
56
  else x
56
57
  end
57
58
  end
@@ -259,8 +260,12 @@ module RubyProlog
259
260
  attr_reader :by_name, :by_id
260
261
 
261
262
  def initialize
262
- @by_name = {}
263
- @by_id = {}
263
+ @by_name = {
264
+ 'false' => Predicate.new(self, 'false', explicit_id: 0)
265
+ }
266
+ @by_id = {
267
+ 0 => @by_name['false']
268
+ }
264
269
  @listing_enabled = false
265
270
  @listing = {}
266
271
  end
@@ -1,5 +1,5 @@
1
1
  module RubyProlog
2
2
 
3
- VERSION = '2.1.0'
3
+ VERSION = '2.2.0'
4
4
 
5
5
  end
@@ -90,6 +90,22 @@ describe RubyProlog do
90
90
  _( two.query {_= foo[:X] } ).must_equal [{X: 10}, {X: 30}]
91
91
  end
92
92
 
93
+ it 'supports false' do
94
+ db = RubyProlog.new do
95
+ foo[:_] << [false]
96
+ foo['x'].fact
97
+
98
+ bar[:_] << [:CUT, false]
99
+ bar['x'].fact
100
+
101
+ baz[false].fact
102
+ end
103
+
104
+ _( db.query{ foo['x'] } ).must_equal [{}]
105
+ _( db.query{ bar['x'] } ).must_equal []
106
+ _( db.query{ baz[false] } ).must_equal [{}]
107
+ end
108
+
93
109
  it 'should be able to query simple family trees.' do
94
110
 
95
111
  c = RubyProlog.new do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-prolog
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Preston Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler