rpetri 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c62e0d29cfadb266641e7889d1b189dc97bd16d4
4
+ data.tar.gz: 2b78e82aef1bc8af89ea85f78e99ffafb977ba18
5
+ SHA512:
6
+ metadata.gz: 7c626d34d19ef82f6973623434a9163690b5b4637548569a3f5471bab2a8208aa1af79db68db42648e1c089356a5590643e488bab2f74c2508b4169895bdeb45
7
+ data.tar.gz: adbc1c382b1d67cbde0374dc7bf4360d53e5350b31266874756b938436b87603314ac778323daa8a219fc810dfe9b897dd34c45d8eebe3f8375932f302cf004d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Metrics/MethodLength:
5
+ Max: 20
6
+
7
+ # It's either a spec or configuration
8
+ Metrics/BlockLength:
9
+ Enabled: false
10
+
11
+ # For now
12
+ Documentation:
13
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at korolvs@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/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 rpetri.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rpetri (0.1.0)
5
+ dry-configurable (~> 0.7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.0)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ coderay (1.1.2)
16
+ concurrent-ruby (1.0.5)
17
+ diff-lcs (1.3)
18
+ dry-configurable (0.7.0)
19
+ concurrent-ruby (~> 1.0)
20
+ factory_bot (4.10.0)
21
+ activesupport (>= 3.0.0)
22
+ faker (1.8.7)
23
+ i18n (>= 0.7)
24
+ i18n (1.0.1)
25
+ concurrent-ruby (~> 1.0)
26
+ method_source (0.9.0)
27
+ minitest (5.11.3)
28
+ pry (0.11.3)
29
+ coderay (~> 1.1.0)
30
+ method_source (~> 0.9.0)
31
+ rake (10.5.0)
32
+ rspec (3.7.0)
33
+ rspec-core (~> 3.7.0)
34
+ rspec-expectations (~> 3.7.0)
35
+ rspec-mocks (~> 3.7.0)
36
+ rspec-core (3.7.1)
37
+ rspec-support (~> 3.7.0)
38
+ rspec-expectations (3.7.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.7.0)
41
+ rspec-mocks (3.7.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.7.0)
44
+ rspec-support (3.7.1)
45
+ thread_safe (0.3.6)
46
+ tzinfo (1.2.5)
47
+ thread_safe (~> 0.1)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ bundler (~> 1.16)
54
+ factory_bot (~> 4.10.0)
55
+ faker (~> 1.8.7)
56
+ pry (~> 0.11.3)
57
+ rake (~> 10.0)
58
+ rpetri!
59
+ rspec (~> 3.0)
60
+
61
+ BUNDLED WITH
62
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 korolvs
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,43 @@
1
+ # Rpetri
2
+
3
+ Test your application in some fancy way.
4
+ Description will be here. I promise.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rpetri'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rpetri
21
+
22
+ ## Usage
23
+
24
+ Description will be here. I promise.
25
+ At the moment look at specs.
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/korolvs/rpetri. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Rpetri project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rpetri/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rpetri'
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__)
data/bin/setup ADDED
@@ -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,6 @@
1
+ module RPetri
2
+ class Error < StandardError; end
3
+ class ValidationError < Error; end
4
+ class LoopingError < Error; end
5
+ class TooManyStepsError < Error; end
6
+ end
@@ -0,0 +1,8 @@
1
+ module RPetri
2
+ class AllInArc < Arc
3
+ def tokens_to_take(tokens_at_source)
4
+ tokens_at_source
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,7 @@
1
+ module RPetri
2
+ class GeneratorPlace < Place
3
+ def tokens_to_give(tokens, tokens_taken)
4
+ 0
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module RPetri
2
+ class NetPlace < Place
3
+ def initialize(name, options = {}, &block)
4
+ @net = options[:net]
5
+ super
6
+ end
7
+
8
+ def run(options = {})
9
+ return unless @net
10
+ nested_options = options.dup
11
+ nested_options[:logger_prefix] = nested_options[:logger_prefix] + ' '
12
+ @net.run(nested_options)
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,13 @@
1
+ module RPetri
2
+ class PlaceWithLimit < Place
3
+ def initialize(name = nil, options = {}, &block)
4
+ @limit = options[:limit]
5
+ super
6
+ end
7
+
8
+ def tokens_to_take(tokens, tokens_given)
9
+ left_to_limit = @limit - tokens
10
+ tokens_given > left_to_limit ? left_to_limit : tokens_given
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,104 @@
1
+ module RPetri
2
+ class Net
3
+ module Builder
4
+ def initialize
5
+ initialize_hashes
6
+ end
7
+
8
+ def add_place(place_param = nil, options = {}, &block)
9
+ tokens = options.delete(:tokens) || 0
10
+ place = get_item(Place, place_param, options, &block)
11
+ add_places([place], tokens) if place
12
+ end
13
+
14
+ def add_places(places, tokens = 0)
15
+ places.each do |place|
16
+ @places_hash[place.uuid] = place
17
+ end
18
+ add_tokens_to(places, tokens) if tokens > 0
19
+ end
20
+
21
+ def add_transition(transition_param = nil, options = {}, &block)
22
+ transition = get_item(Transition, transition_param, options, &block)
23
+ add_transitions([transition]) if transition
24
+ end
25
+
26
+ def add_transitions(transitions)
27
+ transitions.each do |transition|
28
+ @transitions_hash[transition.uuid] = transition
29
+ end
30
+ end
31
+
32
+ def add_arc(*args)
33
+ arc = nil
34
+ if args[1]
35
+ options = args[2] || {}
36
+ klass = options.delete(:class)
37
+ source = get_item_from_param(args[0], 'source')
38
+ target = get_item_from_param(args[1], 'target')
39
+ arc = (klass || Arc).new(source, target, options)
40
+ elsif args[0].is_a?(Arc)
41
+ arc = args[0]
42
+ end
43
+ add_arcs([arc]) if arc
44
+ end
45
+
46
+ def add_arcs(arcs)
47
+ arcs.each do |arc|
48
+ @arc_sources_hash[arc.source.uuid] << arc
49
+ @arc_targets_hash[arc.target.uuid] << arc
50
+ end
51
+ end
52
+
53
+ def add_tokens_to(places, tokens = 1)
54
+ places = [places] if places.is_a?(Place)
55
+ places.each do |place|
56
+ @initial_tokens_hash[place.uuid] += tokens
57
+ end
58
+ end
59
+
60
+ protected
61
+
62
+ def initialize_hashes
63
+ @places_hash = {}
64
+ @transitions_hash = {}
65
+ @arc_sources_hash = Hash.new { |h, k| h[k] = [] }
66
+ @arc_targets_hash = Hash.new { |h, k| h[k] = [] }
67
+ @initial_tokens_hash = Hash.new(0)
68
+ end
69
+
70
+ def get_item(base_class, item, options, &block)
71
+ case item
72
+ when base_class
73
+ item
74
+ when String
75
+ create_item(item, base_class, options, &block)
76
+ when NilClass
77
+ create_item(item, base_class, options, &block)
78
+ end
79
+ end
80
+
81
+ def create_item(item, base_class, options, &block)
82
+ klass = options.delete(:class) || base_class
83
+ klass.new(item, options, &block)
84
+ end
85
+
86
+ def get_place_by_name(name)
87
+ @places_hash.values.find { |p| p.name == name }
88
+ end
89
+
90
+ def get_transition_by_name(name)
91
+ @transitions_hash.values.find { |t| t.name == name }
92
+ end
93
+
94
+ def get_item_from_param(param, name)
95
+ if param.is_a? String
96
+ item = get_place_by_name(param) || get_transition_by_name(param)
97
+ raise RPetri::ValidationError, "There is no #{name} with this name" unless item
98
+ return item
99
+ end
100
+ param
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,75 @@
1
+ module RPetri
2
+ class Net
3
+ class DSL
4
+ module ClassMethods
5
+ def build(&block)
6
+ net = new
7
+ if block_given?
8
+ called_from = eval('self', block.binding, __FILE__, __LINE__)
9
+ dsl = Net::DSL.send(:new, net, called_from)
10
+ dsl.instance_eval(&block)
11
+ dsl.finilize
12
+ dsl.net
13
+ else
14
+ net
15
+ end
16
+ end
17
+ end
18
+
19
+ private_class_method :new
20
+ attr_reader :net
21
+
22
+ def initialize(net, called_from)
23
+ @net = net
24
+ @called_from = called_from
25
+ @dsl_places_stack = []
26
+ @dsl_transitions_stack = []
27
+ @dsl_arcs_stack = []
28
+ end
29
+
30
+ def method_missing(method, *args, &block)
31
+ if @called_from.respond_to?(method)
32
+ @called_from.send(method, *args, &block)
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ def respond_to_missing?(method, _include_private = false)
39
+ @called_from.respond_to?(method)
40
+ end
41
+
42
+ def finilize
43
+ @dsl_places_stack.each(&:call)
44
+ @dsl_transitions_stack.each(&:call)
45
+ @dsl_arcs_stack.each(&:call)
46
+ end
47
+
48
+ private
49
+
50
+ def place(*options, &block)
51
+ @dsl_places_stack.push(-> { @net.add_place(*options, &block) })
52
+ end
53
+
54
+ def transition(*options, &block)
55
+ @dsl_transitions_stack.push(-> { @net.add_transition(*options, &block) })
56
+ end
57
+
58
+ def arc(*options, &block)
59
+ @dsl_arcs_stack.push(-> { @net.add_arc(*options, &block) })
60
+ end
61
+
62
+ def places(items, tokens = 0)
63
+ @dsl_places_stack.push(-> { @net.add_places(items, tokens) })
64
+ end
65
+
66
+ def transitions(items)
67
+ @dsl_transitions_stack.push(-> { @net.add_transitions(items) })
68
+ end
69
+
70
+ def arcs(items)
71
+ @dsl_arcs_stack.push(-> { @net.add_arcs(items) })
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,131 @@
1
+ require 'logger'
2
+
3
+ module RPetri
4
+ class Net
5
+ module Runner
6
+ def run(options = {})
7
+ @options = options
8
+ run_set_options
9
+ run_init
10
+ run_main
11
+ end
12
+
13
+ protected
14
+
15
+ def run_set_options
16
+ @options[:logger] ||= self.class.config.logger
17
+ @options[:logger_prefix] ||= self.class.config.logger_prefix
18
+ @options[:seed] ||= Random.new_seed
19
+ @max_steps_count = @options[:max_steps_count] || self.class.config.max_steps_count
20
+ @max_loops_count = @options[:max_loops_count] || self.class.config.max_loops_count
21
+ end
22
+
23
+ def run_init
24
+ @random = Random.new(@options[:seed])
25
+ @tokens_hash = @initial_tokens_hash.dup
26
+ @weights_hash = Hash.new(1)
27
+ @transitions_to_run = @transitions_hash.keys
28
+ @history_hash = Hash.new(0)
29
+ end
30
+
31
+ def run_main
32
+ log("Starting with seed: #{@options[:seed]}")
33
+ current_step = 1
34
+ until @transitions_to_run.empty?
35
+ log("Step: #{current_step}")
36
+ step
37
+ current_step += 1
38
+ if current_step > @max_steps_count
39
+ fatal("Too many steps! Already #{current_step}!")
40
+ raise TooManyStepsError
41
+ end
42
+ end
43
+ log('Done!')
44
+ end
45
+
46
+ def step
47
+ possible_transitions_hash.sort_by { |t| @random.rand * @weights_hash[t] }.reverse_each do |uuid, arcs|
48
+ run_transition(uuid, arcs) && break if transition_is_runnable(arcs[:to])
49
+ end
50
+ check_places
51
+ check_for_looping
52
+ end
53
+
54
+ def possible_transitions_hash
55
+ transitions_hash = Hash.new { |h, k| h[k] = { from: [], to: [] } }
56
+ @tokens_hash.select { |_k, v| v > 0 }.each_key do |key|
57
+ @arc_sources_hash[key].each do |arc|
58
+ transitions_hash[arc.target.uuid][:to] = @arc_targets_hash[arc.target.uuid]
59
+ end
60
+ end
61
+ transitions_hash.each do |transition_uuid, hash|
62
+ hash[:from] = @arc_sources_hash[transition_uuid]
63
+ end
64
+ end
65
+
66
+ def transition_is_runnable(arcs)
67
+ @temp_tokens_hash = @tokens_hash.dup
68
+ arcs.each do |arc|
69
+ tokens = @temp_tokens_hash[arc.source.uuid]
70
+ return false unless arc.runnable?(tokens)
71
+ tokens_arc_want_to_take = arc.tokens_to_take(tokens)
72
+ tokens_to_take = arc.source.tokens_to_give(tokens, tokens_arc_want_to_take)
73
+ @temp_tokens_hash[arc.source.uuid] -= tokens_to_take
74
+ end
75
+ true
76
+ end
77
+
78
+ def run_transition(uuid, arcs)
79
+ transition = @transitions_hash[uuid]
80
+ log("Running: #{transition.name}")
81
+ transition.run(@options)
82
+ @weights_hash[uuid] /= 2
83
+ @transitions_to_run.delete(uuid)
84
+ update_tokens(arcs[:to], arcs[:from])
85
+ end
86
+
87
+ def update_tokens(to, from)
88
+ to.each do |arc|
89
+ tokens = @tokens_hash[arc.source.uuid]
90
+ tokens_arc_want_to_take = arc.tokens_to_take(tokens)
91
+ tokens_to_take = arc.source.tokens_to_give(tokens, tokens_arc_want_to_take)
92
+ @tokens_hash[arc.source.uuid] -= tokens_to_take
93
+ end
94
+ from.each do |arc|
95
+ tokens = @tokens_hash[arc.target.uuid]
96
+ tokens_arc_want_to_give = arc.tokens_to_give(tokens)
97
+ tokens_to_give = arc.target.tokens_to_take(tokens, tokens_arc_want_to_give)
98
+ @tokens_hash[arc.target.uuid] += tokens_to_give
99
+ end
100
+ end
101
+
102
+ def check_places
103
+ places_checked = {}
104
+ @tokens_hash.select { |_k, v| v > 0 }.each_key do |uuid|
105
+ next if places_checked[uuid]
106
+ place = @places_hash[uuid]
107
+ log("Checking: #{place.name}")
108
+ place.run(@options)
109
+ places_checked[uuid] = true
110
+ end
111
+ end
112
+
113
+ def check_for_looping
114
+ state_string = @tokens_hash.select { |_k, v| v > 0 }.reduce(&:to_s)
115
+ @history_hash[state_string] += 1
116
+ if @history_hash[state_string] > @max_loops_count
117
+ fatal("Looping! Same state #{@history_hash[state_string]} times!")
118
+ raise LoopingError
119
+ end
120
+ end
121
+
122
+ def log(something)
123
+ @options[:logger].info('RPetri') { @options[:logger_prefix] + ' ' + something }
124
+ end
125
+
126
+ def fatal(something)
127
+ @options[:logger].fatal('RPetri') { @options[:logger_prefix] + ' ' + something }
128
+ end
129
+ end
130
+ end
131
+ end
data/lib/rpetri/net.rb ADDED
@@ -0,0 +1,13 @@
1
+ module RPetri
2
+ class Net
3
+ extend Dry::Configurable
4
+ extend DSL::ClassMethods
5
+ include Builder
6
+ include Runner
7
+
8
+ setting :max_steps_count, 1_000
9
+ setting :max_loops_count, 50
10
+ setting :logger, Logger.new(STDOUT)
11
+ setting :logger_prefix, ''
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ module RPetri
2
+ class Arc < Object
3
+ attr_reader :source, :target, :options
4
+
5
+ def initialize(source, target, options = {})
6
+ @source = source
7
+ @target = target
8
+ @options = options
9
+ validate!
10
+ super()
11
+ end
12
+
13
+ def runnable?(tokens_at_source)
14
+ tokens_at_source > 0
15
+ end
16
+
17
+ def tokens_to_take(tokens_at_source)
18
+ 1
19
+ end
20
+
21
+ def tokens_to_give(tokens_at_target)
22
+ 1
23
+ end
24
+
25
+ protected
26
+
27
+ def validate!
28
+ validate_item(@source, 'Source')
29
+ validate_item(@target, 'Target')
30
+ source_type = @source.is_a?(Place) ? :place : :target
31
+ target_type = @target.is_a?(Place) ? :place : :target
32
+ raise ValidationError, 'Source and Target should be different type' if source_type == target_type
33
+ end
34
+
35
+ def validate_item(item, name)
36
+ return if item.is_a?(Place) || item.is_a?(Transition)
37
+ raise ValidationError, "#{name} should be Place or Transition"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ module RPetri
2
+ class Node < Object
3
+ attr_reader :name, :options, :block
4
+
5
+ def initialize(name = nil, options = {}, &block)
6
+ @name = name
7
+ @options = options
8
+ @block = block
9
+ super()
10
+ end
11
+
12
+ def run(options = {})
13
+ return unless @block
14
+ @eval_context = options[:context]
15
+ eval_context.instance_eval(&@block)
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if eval_context.respond_to?(method)
20
+ eval_context.send(method, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def respond_to_missing?(method, _include_private = false)
27
+ eval_context.respond_to?(method)
28
+ end
29
+
30
+ protected
31
+
32
+ def eval_context
33
+ @eval_context || described_context
34
+ end
35
+
36
+ def described_context
37
+ @described_context ||= eval('self', @block.binding, __FILE__, __LINE__)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ require 'securerandom'
2
+
3
+ module RPetri
4
+ class Object
5
+ attr_reader :uuid
6
+
7
+ def initialize
8
+ @uuid = SecureRandom.uuid
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module RPetri
2
+ class Place < Node
3
+ def tokens_to_take(tokens, tokens_given)
4
+ tokens_given
5
+ end
6
+
7
+ def tokens_to_give(tokens, tokens_taken)
8
+ tokens_taken
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module RPetri
2
+ class Transition < Node
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Rpetri
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/rpetri.rb ADDED
@@ -0,0 +1,19 @@
1
+ # Vendor
2
+ require 'dry/configurable'
3
+ # Own
4
+ require 'rpetri/errors'
5
+ require 'rpetri/objects/object'
6
+ require 'rpetri/objects/node'
7
+ require 'rpetri/objects/place'
8
+ require 'rpetri/objects/transition'
9
+ require 'rpetri/objects/arc'
10
+ require 'rpetri/net/dsl'
11
+ require 'rpetri/net/builder'
12
+ require 'rpetri/net/runner'
13
+ require 'rpetri/net'
14
+ require 'rpetri/version'
15
+ # Extentions
16
+ require 'rpetri/extentions/all_in_arc'
17
+ require 'rpetri/extentions/net_place'
18
+ require 'rpetri/extentions/generator_place'
19
+ require 'rpetri/extentions/place_with_limit'
data/rpetri.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rpetri/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rpetri'
7
+ spec.version = Rpetri::VERSION
8
+ spec.authors = ['korolvs']
9
+ spec.email = ['korolvs@gmail.com']
10
+
11
+ spec.summary = 'Testing ruby code with Petri nets'
12
+ spec.description = 'Testing ruby code with Petri nets'
13
+ spec.homepage = 'http://korolvs.com'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+
20
+ spec.bindir = 'bin'
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'dry-configurable', '~> 0.7.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'factory_bot', '~> 4.10.0'
27
+ spec.add_development_dependency 'faker', '~> 1.8.7'
28
+ spec.add_development_dependency 'pry', '~> 0.11.3'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rpetri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - korolvs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_bot
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.10.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.10.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: faker
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.11.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.11.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: Testing ruby code with Petri nets
112
+ email:
113
+ - korolvs@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/rpetri.rb
131
+ - lib/rpetri/errors.rb
132
+ - lib/rpetri/extentions/all_in_arc.rb
133
+ - lib/rpetri/extentions/generator_place.rb
134
+ - lib/rpetri/extentions/net_place.rb
135
+ - lib/rpetri/extentions/place_with_limit.rb
136
+ - lib/rpetri/net.rb
137
+ - lib/rpetri/net/builder.rb
138
+ - lib/rpetri/net/dsl.rb
139
+ - lib/rpetri/net/runner.rb
140
+ - lib/rpetri/objects/arc.rb
141
+ - lib/rpetri/objects/node.rb
142
+ - lib/rpetri/objects/object.rb
143
+ - lib/rpetri/objects/place.rb
144
+ - lib/rpetri/objects/transition.rb
145
+ - lib/rpetri/version.rb
146
+ - rpetri.gemspec
147
+ homepage: http://korolvs.com
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.6.10
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Testing ruby code with Petri nets
171
+ test_files: []