Commits

Mikołaj Baranowski committed f1dc536

another iteration

Comments (0)

Files changed (5)

-
 class CookeryAction
   def initialize(name, &procedure)
     @name = name
+def channel_put(name, value)
+  puts "adding #{value} to channel #{name}"
+  Channels[name] << value
+end
+
+def channel_get(name)
+  Channels[name].shift
+end
+
+Channels = Hash.new { |h, k| h[k] = Array.new }
-def condition
+Conditions = Hash.new
+
+def condition(name, &block)
+  Conditions[name] = block
 end

file-operations.rb

 require_relative 'subject'
 require_relative 'action'
+require_relative 'channel'
+require_relative 'condition'
+require 'zlib'
+
 
 subject(:file, /(.+)/, :file) do |f|
-  path "/tmp/test_data.txt"
+  path f
 end
 
 action(:read, :file) do |subject, conditions|
   puts "in action"
-  p subject.get()
+  x = subject.get()
+  gz = Zlib::GzipReader.new(StringIO.new(x))
+  channel_put(:foo, gz.read)
+  gz.close
+end
+
+condition(:zip_compression) do |subject|
+  gz = Zlib::GzipReader.new(StringIO.new(subject.get()))
+  result = gz.rezd
+  gz.close
+  result
 end
 
-action(:write, :file) do |subject, conditions|
+action(:print_result) do
+  puts "RESULT: #{channel_get(:bar)}"
 end
 
 action(:count_words) do |subject, conditions|
+  channel_put(:bar, channel_get(:foo).split.length)
 end
 
 text = """
-Read file '/tmp/test_data.txt' - with zip compression.
+Read file /tmp/test_data.gzip - with zip compression.
 Count words.
-Print output data.
+Print result.
 """
 
-# regex = /(?<action>#{Actions.keys.join("|")}) (?<subject>\w+)/i
-regex = /(?<action>#{Actions.keys.join("|")}) (?<subject>#{Regexp.union(*Subjects.keys)}) ?(?<arguments>#{Regexp.union(*Subjects.values.map(&:arguments))})?/i
-p regex
+actions = Actions.keys.map { |a| Regexp.new(a.to_s.sub('_', ' '))}
+
+p regex = /(?<action>#{Regexp.union(*actions)}) ?(?<subject>#{Regexp.union(*Subjects.keys)})? ?(?<arguments>#{Regexp.union(*Subjects.values.map(&:arguments))})?/i
 
-# cond_regexs = Condition_types.map { |type, pronouns| pronouns.map { |pronoun| /#{pronoun} (?:#{articles.join('|')})?#{Regexp.union(*Conditions[type])}/i }}.flatten
-# p cond_regexs
+conditions = Conditions.keys.map { |c| Regexp.new(c.to_s.sub('_', ' ')) }
 
+p cond_regex = /(?:with )?(?<condition>#{Regexp.union(*conditions)})/
 
 text.split("\n").each do |line|
+  if line.empty? or line[-1] != '.'
+    next
+  else
+    line = line[0...-1]
+  end
+
   puts "- " * 20
   line.downcase!
 
     puts "arguments: #{match[:arguments]}"
     # puts "condition: #{match[:conditions]}"
 
-    Actions[match[:action].to_sym].act(Subjects[match[:subject].to_sym])
+    action_name = match[:action].sub(' ', '_').to_sym
+
+    if cond_match = cond_regex.match(conditions)
+      p cond_match
+    end
 
-    # cond_matches = Array.new
-    # cond_regexs.each do |cond_regex|
-    #   if cond_match = cond_regex.match(match[:conditions])
-    #     cond_matches.reject! { |m| cond_match.to_s.include?(m.to_s) }
-    #     cond_matches << cond_match unless cond_matches.any? { |m| m.to_s.include?(cond_match.to_s) }
-    #   end
-    # end
+    if match[:subject]
+      Subjects[match[:subject].to_sym].args(match[:arguments])
+      Actions[match[:action].to_sym].act(Subjects[match[:subject].to_sym])
+    else
+      Actions[action_name].act
+    end
 
-    # p cond_matches
 
-    # Actions[match[:action].to_sym][match[:subject].to_sym].call
   end
 end
   def initialize(name, arguments, &block)
     @name = /#{name}/
     @arguments = arguments
-    instance_eval(&block)
+    @block = block
   end
 
   def path(path)
-    puts "path: #{path}"
     @path = path
   end
 
+  def args(*arguments)
+    instance_exec(*arguments, &@block)
+  end
+
   def get
     open(@path).read()
   end