dnf 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: 38d7f35e2dbe89fc5eefae38715e9ffb4fd30c5fb2387afa1af111e7ef3d6ad2
4
+ data.tar.gz: 793c5e967fa47434234bdd786c0e46084f0055d9ea76203d883d017dff542173
5
+ SHA512:
6
+ metadata.gz: 368413587764ad6bfb6a3dae501b815238e2141eaa4aea776f8d781e436fcbc0b7d1f1669e7e7527e15a9eeb603917e88315a4309240102e36e65cfb733efd15
7
+ data.tar.gz: fafd471e6a1b1a6fc385cda79f9f8ae86148573f165ee7eff184bc1f4190533b66a0df0ea97cb3a9ec3ff35f3ce18dd91224a303a05e8bb074eed120d13543b1
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ .bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Marco Colli
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # DNF
2
+
3
+ Convert any boolean expression to disjunctive normal form (DNF).
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'dnf'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ expression = "(!a | !b) & c"
15
+ boolean_expression = Dnf::BooleanExpression.new(expression)
16
+ boolean_expression.to_dnf # => "!a & c | !b & c"
17
+ ```
18
+
19
+ ## License
20
+
21
+ The gem is available as open source under the terms of the MIT License.
data/dnf.gemspec ADDED
@@ -0,0 +1,12 @@
1
+ require_relative 'lib/dnf/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'dnf'
5
+ s.version = Dnf::VERSION
6
+ s.summary = 'Convert any boolean expression to disjunctive normal form (DNF).'
7
+ s.author = 'Marco Colli'
8
+ s.homepage = 'https://github.com/collimarco/dnf-ruby'
9
+ s.license = 'MIT'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.add_development_dependency 'rspec'
12
+ end
@@ -0,0 +1,130 @@
1
+ module Dnf
2
+ class BooleanExpression
3
+ attr_reader :expression
4
+
5
+ def initialize(expression)
6
+ @expression = expression
7
+ end
8
+
9
+ def to_dnf
10
+ expr = parse(expression)
11
+ dnf = convert_to_dnf(expr)
12
+ dnf_to_string(dnf)
13
+ end
14
+
15
+ private
16
+
17
+ def parse(expr)
18
+ tokens = tokenize(expr)
19
+ parse_expression(tokens)
20
+ end
21
+
22
+ def tokenize(expr)
23
+ expr.scan(/\w+|[&|!()]/)
24
+ end
25
+
26
+ def parse_expression(tokens)
27
+ parse_or(tokens)
28
+ end
29
+
30
+ def parse_or(tokens)
31
+ left = parse_and(tokens)
32
+ while tokens.first == '|'
33
+ tokens.shift
34
+ right = parse_and(tokens)
35
+ left = [:or, left, right]
36
+ end
37
+ left
38
+ end
39
+
40
+ def parse_and(tokens)
41
+ left = parse_not(tokens)
42
+ while tokens.first == '&'
43
+ tokens.shift
44
+ right = parse_not(tokens)
45
+ left = [:and, left, right]
46
+ end
47
+ left
48
+ end
49
+
50
+ def parse_not(tokens)
51
+ if tokens.first == '!'
52
+ tokens.shift
53
+ expr = parse_primary(tokens)
54
+ [:not, expr]
55
+ else
56
+ parse_primary(tokens)
57
+ end
58
+ end
59
+
60
+ def parse_primary(tokens)
61
+ if tokens.first == '('
62
+ tokens.shift
63
+ expr = parse_expression(tokens)
64
+ tokens.shift # skip ')'
65
+ expr
66
+ else
67
+ token = tokens.shift
68
+ [:var, token]
69
+ end
70
+ end
71
+
72
+ def convert_to_dnf(expr)
73
+ case expr[0]
74
+ when :var
75
+ expr
76
+ when :not
77
+ convert_not_to_dnf(expr)
78
+ when :and
79
+ left = convert_to_dnf(expr[1])
80
+ right = convert_to_dnf(expr[2])
81
+ distribute_and(left, right)
82
+ when :or
83
+ left = convert_to_dnf(expr[1])
84
+ right = convert_to_dnf(expr[2])
85
+ [:or, left, right]
86
+ end
87
+ end
88
+
89
+ def convert_not_to_dnf(expr)
90
+ sub_expr = expr[1]
91
+ case sub_expr[0]
92
+ when :var
93
+ expr
94
+ when :not
95
+ convert_to_dnf(sub_expr[1])
96
+ when :and
97
+ left = convert_not_to_dnf([:not, sub_expr[1]])
98
+ right = convert_not_to_dnf([:not, sub_expr[2]])
99
+ [:or, left, right]
100
+ when :or
101
+ left = convert_not_to_dnf([:not, sub_expr[1]])
102
+ right = convert_not_to_dnf([:not, sub_expr[2]])
103
+ [:and, left, right]
104
+ end
105
+ end
106
+
107
+ def distribute_and(left, right)
108
+ if left[0] == :or
109
+ [:or, distribute_and(left[1], right), distribute_and(left[2], right)]
110
+ elsif right[0] == :or
111
+ [:or, distribute_and(left, right[1]), distribute_and(left, right[2])]
112
+ else
113
+ [:and, left, right]
114
+ end
115
+ end
116
+
117
+ def dnf_to_string(expr)
118
+ case expr[0]
119
+ when :var
120
+ expr[1]
121
+ when :not
122
+ "!#{dnf_to_string(expr[1])}"
123
+ when :and
124
+ "#{dnf_to_string(expr[1])} & #{dnf_to_string(expr[2])}"
125
+ when :or
126
+ "#{dnf_to_string(expr[1])} | #{dnf_to_string(expr[2])}"
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,3 @@
1
+ module Dnf
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/dnf.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'dnf/version'
2
+ require 'dnf/boolean_expression'
3
+
4
+ module Dnf
5
+
6
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'dnf'
3
+
4
+ RSpec.describe Dnf::BooleanExpression do
5
+ describe '#to_dnf' do
6
+ it 'converts simple variable' do
7
+ expect(Dnf::BooleanExpression.new('a').to_dnf).to eq('a')
8
+ end
9
+
10
+ it 'converts negated variable' do
11
+ expect(Dnf::BooleanExpression.new('!a').to_dnf).to eq('!a')
12
+ end
13
+
14
+ it 'converts conjunction' do
15
+ expect(Dnf::BooleanExpression.new('a & b').to_dnf).to eq('a & b')
16
+ end
17
+
18
+ it 'converts disjunction' do
19
+ expect(Dnf::BooleanExpression.new('a | b').to_dnf).to eq('a | b')
20
+ end
21
+
22
+ it 'converts parenthesis' do
23
+ expect(Dnf::BooleanExpression.new('(a)').to_dnf).to eq('a')
24
+ end
25
+
26
+ it 'converts negated parenthesis' do
27
+ expect(Dnf::BooleanExpression.new('!(a)').to_dnf).to eq('!a')
28
+ end
29
+
30
+ it 'converts negation of disjunction' do
31
+ expect(Dnf::BooleanExpression.new('!(a | b)').to_dnf).to eq('!a & !b')
32
+ end
33
+
34
+ it 'converts negation of conjunction' do
35
+ expect(Dnf::BooleanExpression.new('!(a & b)').to_dnf).to eq('!a | !b')
36
+ end
37
+
38
+ it 'converts complex expression' do
39
+ expect(Dnf::BooleanExpression.new('!(a | b) & c').to_dnf).to eq('!a & !b & c')
40
+ end
41
+
42
+ it 'converts nested expression' do
43
+ expect(Dnf::BooleanExpression.new('a & (b | c)').to_dnf).to eq('a & b | a & c')
44
+ end
45
+
46
+ it 'converts nested expression with negation' do
47
+ expect(Dnf::BooleanExpression.new('!a & (b | c)').to_dnf).to eq('!a & b | !a & c')
48
+ end
49
+
50
+ it 'converts multiple nested expressions' do
51
+ expect(Dnf::BooleanExpression.new('(a | b) & (c | d)').to_dnf).to eq('a & c | a & d | b & c | b & d')
52
+ end
53
+
54
+ it 'converts multiple negations' do
55
+ expect(Dnf::BooleanExpression.new('!(!a)').to_dnf).to eq('a')
56
+ end
57
+
58
+ it 'converts combination of negations and conjunctions' do
59
+ expect(Dnf::BooleanExpression.new('!a & !b').to_dnf).to eq('!a & !b')
60
+ end
61
+
62
+ it 'converts expressions with many variables in parenthesis' do
63
+ expect(Dnf::BooleanExpression.new('(a | b | c) & d').to_dnf).to eq('a & d | b & d | c & d')
64
+ end
65
+
66
+ it 'converts expressions with many variables outside parenthesis' do
67
+ expect(Dnf::BooleanExpression.new('(!a | b) & c & d & !e').to_dnf).to eq('!a & c & d & !e | b & c & d & !e')
68
+ end
69
+
70
+ it 'converts deeply nested expressions' do
71
+ expect(Dnf::BooleanExpression.new('(!(a & b & c) | (!e | d)) & a').to_dnf).to eq('!a & a | !b & a | !c & a | !e & a | d & a')
72
+ end
73
+
74
+ it 'converts expressions with long variable names' do
75
+ expect(Dnf::BooleanExpression.new('user & (cat | dog)').to_dnf).to eq('user & cat | user & dog')
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,98 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = "doc"
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dnf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marco Colli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rspec"
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - dnf.gemspec
39
+ - lib/dnf.rb
40
+ - lib/dnf/boolean_expression.rb
41
+ - lib/dnf/version.rb
42
+ - spec/dnf/boolean_expression_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: https://github.com/collimarco/dnf-ruby
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.0.3.1
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Convert any boolean expression to disjunctive normal form (DNF).
67
+ test_files: []