rulengine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac1223e45604022c8d2c4543211308ab3ab84998
4
+ data.tar.gz: abbf7be2163d7929b9afaab5ead586872e5e7de7
5
+ SHA512:
6
+ metadata.gz: f418ae646a14e4a1f91259cf773c497a23dfa830a57c97602721d6b59b738b44703deef3aebc2eb1608a65f1c13258fc905295845fbd8392c263ff8ef894a553
7
+ data.tar.gz: dc65edc1231b5f717522b094b9510956a43e928b43434cac5c9534b59be774e4478a7ba045a3b4752233fa1b2efa7c1b7784a1409fb189b8ea471064386e2168
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ rulengine*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rulengine.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 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.
@@ -0,0 +1,58 @@
1
+ # Rulengine
2
+
3
+ ## Why
4
+
5
+ MIT license. Work problem
6
+
7
+ "Facts" change every time, rules don't. No need to optimize facts
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rulengine'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rulengine
24
+
25
+ Until it is moved to a generator, you will need to call this to setup the DB:
26
+ ```
27
+ Rulengine::Engine.build_db
28
+ ```
29
+
30
+ ## Usage example
31
+
32
+ ```
33
+ require 'rulengine'
34
+ Rulengine::Rule.connection
35
+
36
+ add_b = Rulengine::Rule.new given: ['a'], action: {'add': ['b']}
37
+ add_b.save!
38
+ remove_b = Rulengine::Rule.new given: ['a'], action: {'remove': ['b']}
39
+ remove_b.save!
40
+
41
+ Rulengine::Rule.find_conflicts
42
+ ```
43
+
44
+ ## Development
45
+
46
+ TODO: Move specs over
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rulengine.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ # require "rspec/core/rake_task"
3
+
4
+ # RSpec::Core::RakeTask.new(:spec)
5
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
6
+
7
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rulengine"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ require "active_record"
2
+
3
+ require "rulengine/version"
4
+
5
+ # Move?
6
+ require "rulengine/rule"
7
+ require "rulengine/state"
8
+
9
+ module Rulengine
10
+ class Engine
11
+
12
+ # TODO
13
+ def self.build_db
14
+ ActiveRecord::Schema.define do
15
+ unless ActiveRecord::Base.connection.tables.include? 'rules'
16
+ create_table :rules do |t|
17
+ t.json :given # Turns this an its negative into a separate 'fact' class
18
+ t.json :unless_given # Same as "and not given"
19
+ t.json :action # switch to foreign key? Model name? (inherited)
20
+
21
+ t.timestamps
22
+ end
23
+ end
24
+
25
+ unless ActiveRecord::Base.connection.tables.include? 'states'
26
+ create_table :states do |t|
27
+ t.json :data
28
+ t.timestamps
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def initialize
35
+ puts "init rulengine"
36
+ build_db
37
+ end
38
+
39
+ def self.a
40
+ puts 'a'
41
+ 'a'
42
+ end
43
+
44
+ end
45
+ end
46
+
@@ -0,0 +1,100 @@
1
+ require "active_record"
2
+ require "set"
3
+
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ adapter: 'postgresql', # or 'postgresql' or 'sqlite3'
7
+ database: 'pfb',
8
+ username: 'pfb',
9
+ password: '',
10
+ host: 'localhost'
11
+ )
12
+
13
+ ActiveRecord::Base.connection
14
+
15
+
16
+ module Rulengine
17
+ class Rule < ActiveRecord::Base
18
+ InvalidRuleSet = Class.new StandardError
19
+
20
+ def self.list_all_combinations
21
+ # Shortcut, can be greatly optimized
22
+ left = Rulengine::Rule.all.pluck(:given, :unless_given).flatten.to_set
23
+ right = Rulengine::Rule.all.pluck(:action).map(&:values).flatten.to_set
24
+ vars = left + right - [nil]
25
+ # For optimization, remove to_a (less readable)
26
+ (1..vars.count).flat_map{ |size| vars.to_a.combination(size).to_a }
27
+
28
+ end
29
+
30
+
31
+ def self.find_conflicts # Don't pass data, generate it!
32
+ list_all_combinations.each do |data|
33
+ puts "Testing combination: #{data}"
34
+ data = apply_all_rules(data)
35
+ apply_all_rules(data)
36
+ end
37
+ # data = apply_all_rules(data)
38
+ # apply_all_rules(data)
39
+ end
40
+
41
+ def self.apply_all_rules(data, run_count=1)
42
+ # Run twice ensures there are no deep circular conflicts
43
+ data = data.to_set
44
+ state_history = Set.new([data])
45
+
46
+ # run_count = (run_twice && 2) || 1
47
+ (1..run_count).each do |_|
48
+ Rulengine::Rule.all.each do |rule|
49
+ # orig_state = self #.deep_copy
50
+ data_was = data
51
+ data = rule.apply_to(data)
52
+ if data_was != data
53
+ if state_history.include? data.to_set
54
+ raise InvalidRuleSet, [rule, state_history, ]# "Breaking input: #{state_history.first}"]
55
+ end
56
+ state_history.add data.to_set
57
+ end
58
+ end
59
+ end
60
+
61
+ # puts state_history
62
+ data
63
+ end
64
+
65
+
66
+ # Not_given refers to something that MUST NOT be given
67
+ def apply_to(data)
68
+ # data = data.data if data.is_a? State
69
+ data = data.to_set unless data.is_a? Set
70
+ if applies?(data)
71
+
72
+ # Refactor action into classes with `perform`
73
+ action.each do |k, v|
74
+ if k.eql? 'add'
75
+ data += (v || [])
76
+ elsif k.eql? 'remove'
77
+ data -= (v || [])
78
+ else
79
+ puts k, v
80
+ raise NotImplementedError
81
+ end
82
+ end
83
+ # self.save!
84
+ end
85
+
86
+ data
87
+ end
88
+
89
+ def applies?(data)
90
+ if unless_given
91
+ return false if (data & unless_given).present?
92
+ end
93
+ if given
94
+ return false unless given.to_set.subset? data
95
+ end
96
+
97
+ true
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,31 @@
1
+ require "active_record"
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ adapter: 'postgresql', # or 'postgresql' or 'sqlite3'
5
+ database: 'pfb',
6
+ username: 'pfb',
7
+ password: '',
8
+ host: 'localhost'
9
+ )
10
+
11
+ # ActiveRecord::Base.connection
12
+
13
+ module Rulengine
14
+ class State < ActiveRecord::Base
15
+ BadInput = Class.new StandardError
16
+
17
+ def apply_rule(rule)
18
+ raise BadInput unless rule.is_a? Rulengine::Rule
19
+ self.data = Rulengine::Rule.first.apply_to(data.to_set)
20
+ end
21
+
22
+ def to_set
23
+ @data_set ||= data.to_set
24
+ end
25
+
26
+ def data=(value)
27
+ super(value)
28
+ @data_set = nil # Clear cached data set
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Rulengine
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rulengine/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rulengine"
8
+ spec.version = Rulengine::VERSION
9
+ spec.authors = ["Pierre-Francoys Brousseau"]
10
+ spec.email = ["pierre@francoys.com"]
11
+
12
+ spec.summary = %q{A Rule Engine that can apply rules tell when rules will conflict.}
13
+ spec.description = %q{Not only can this rule engine tell when rules conflict but it can be expanded in many ways, including as an automatic theorem solver}
14
+ spec.homepage = "https://github.com/pfbrousseau/rulengine" # "http://rulengine.francoys.com"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_dependency "activerecord", "~> 5.0"
38
+ # spec.add_dependency "sqlite3" # pg?
39
+ spec.add_dependency "pg"
40
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rulengine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre-Francoys Brousseau
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-05 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Not only can this rule engine tell when rules conflict but it can be
84
+ expanded in many ways, including as an automatic theorem solver
85
+ email:
86
+ - pierre@francoys.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/rulengine.rb
101
+ - lib/rulengine/rule.rb
102
+ - lib/rulengine/state.rb
103
+ - lib/rulengine/version.rb
104
+ - rulengine.gemspec
105
+ - tasks/rspec.rake
106
+ homepage: https://github.com/pfbrousseau/rulengine
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ allowed_push_host: https://rubygems.org
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.13
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A Rule Engine that can apply rules tell when rules will conflict.
131
+ test_files: []