Commits

Mikołaj Baranowski committed b2c4f02

Another iteration

  • Participants
  • Parent commits 056af75

Comments (0)

Files changed (6)

+gem 'activesupport', '~> 4.1.1'
+
+class CookeryAction
+  def initialize(name, &procedure)
+    @name = name
+    @procedure = procedure
+  end
+
+  def act(*arguments)
+    instance_exec(*arguments, &@procedure)
+  end
+end
+
+
+Actions = Hash.new # { |h, k| h[k] = Hash.new }
+
+def action(name, type = nil, &procedure)
+  Actions[name] = CookeryAction.new(name, &procedure)
+end

File condition.rb

+def condition
+end

File file-operations.rb

+require_relative 'subject'
+require_relative 'action'
+
+subject(:file, /(.+)/, :file) do |f|
+  path "/tmp/test_data.txt"
+end
+
+action(:read, :file) do |subject, conditions|
+  puts "in action"
+  p subject.get()
+end
+
+action(:write, :file) do |subject, conditions|
+end
+
+action(:count_words) do |subject, conditions|
+end
+
+text = """
+Read file '/tmp/test_data.txt' - with zip compression.
+Count words.
+Print output data.
+"""
+
+# 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
+
+# cond_regexs = Condition_types.map { |type, pronouns| pronouns.map { |pronoun| /#{pronoun} (?:#{articles.join('|')})?#{Regexp.union(*Conditions[type])}/i }}.flatten
+# p cond_regexs
+
+
+text.split("\n").each do |line|
+  puts "- " * 20
+  line.downcase!
+
+  activity, conditions = line.split('-').map(&:strip)
+  puts "activity: #{activity}"
+  puts "conditions: #{conditions}"
+  puts
+
+  if match = regex.match(activity)
+    puts "action: #{match[:action]}"
+    puts "subject: #{match[:subject]}"
+    puts "arguments: #{match[:arguments]}"
+    # puts "condition: #{match[:conditions]}"
+
+    Actions[match[:action].to_sym].act(Subjects[match[:subject].to_sym])
+
+    # 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
+
+    # p cond_matches
+
+    # Actions[match[:action].to_sym][match[:subject].to_sym].call
+  end
+end

File scrambled-eggs.rb

 recipy = """
 Crack eggs into a measuring cup.
+Boil potatos for 30 minutes.
 Add the milk (optional) and salt.
 Beat the mixture until foamy using fork.
 Melt 2 tsp. butter in a frying pan on medium heat
 
 Actions = Hash.new { |h, k| h[k] = Hash.new }
 Condition_types = {:place => [:into, :to],
-                   :condition => [:until]}
+                   :condition => [:until],
+                   :time => [:for]}
 Conditions = Hash.new { |h, k| h[k] = Array.new }
 
-def condition(type, name, &procedure)
-  Conditions[type] << name.to_s.tr('_', ' ')
+def condition(type, regex, &procedure)
+  Conditions[type] << regex
 end
 
 def action(action_name, subject, &procedure)
 end
 action(:mix, "something") do
 end
-
-condition(:place, :measuring_cup) do
+action(:boil, :potatos) do
 end
 
-condition(:condition, :foamy) do
+condition(:place, /measuring cup/i) do
+end
+condition(:condition, /foamy/i) do
+end
+condition(:time, /([0-9]+) minutes/i) do
 end
 
 articles = ["a", "an", "the"].map { |i| i + ' ' }
 regex = /(?<action>#{Actions.keys.join("|")}) (?<subject>\w+)(?<conditions> .+)\./i
 p regex
 
-# condition_regex = Regexp.new(Condition_types.map { |type, pronouns| pronouns.map { |pronoun| "(?:(#{pronoun}) (?:#{articles.join('|')})?(#{Conditions[type]}))" }.join("|") }.join("|"))
-cond_regexs = Condition_types.map { |type, pronouns| pronouns.map { |pronoun| /(#{pronoun}) (?:#{articles.join('|')})?(#{Conditions[type].join("|")})/i }}.flatten
+cond_regexs = Condition_types.map { |type, pronouns| pronouns.map { |pronoun| /#{pronoun} (?:#{articles.join('|')})?#{Regexp.union(*Conditions[type])}/i }}.flatten
 p cond_regexs
 
 recipy.split("\n").each do |line|
+require 'active_support/all'
+
+class CookeryProtocol
+end
+
+class FileProtocol < CookeryProtocol
+  attr_accessor :name, :arguments
+
+  def initialize(name, arguments, &block)
+    @name = /#{name}/
+    @arguments = arguments
+    instance_eval(&block)
+  end
+
+  def path(path)
+    puts "path: #{path}"
+    @path = path
+  end
+
+  def get
+    open(@path).read()
+  end
+end
+
+Subjects = Hash.new
+
+def subject(name, arguments, protocol, &block)
+  protocol_class = "#{protocol.capitalize}Protocol".safe_constantize
+
+  if protocol_class.nil?
+    warn "no such protocol: #{protocol}"
+  elsif protocol_class.superclass == CookeryProtocol
+    Subjects[name] = protocol_class.new(name, arguments, &block)
+  end
+end