Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Init deploy

  • Loading branch information...
commit fb3d94aaf653b79addce227796947d049769d53b 0 parents
Alexey Gaziev authored
BIN  .DS_Store
Binary file not shown
56 classes/element.rb
@@ -0,0 +1,56 @@
+require 'classes/processor.rb'
+
+include Processor
+
+class Element
+ attr_accessor :current_state, :states, :events
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ @states = Hash.new
+ @events = Hash.new
+
+ proc.elements[name] = self unless proc.elements.keys.include? name
+ end
+
+ def states_list
+ s = ''
+ @states.each do |state, v|
+ s += ' ' + state + ":\n"
+ v.events.each do |k,v|
+ s += ' ' + v.name + '(' + v.general_condition
+ s += ', ' + v.second_condition if v.second_condition != 'nil'
+ s += ")\n"
+ end
+ end
+
+ s
+ end
+
+ def events_list
+ s = ''
+ @events.each do |k,v|
+ s += ' ' + v.previous_state.name + ' => ' + v.name +
+ '(' + v.general_condition
+ s += ', ' + v.second_condition if v.second_condition != 'nil'
+ s += ') => ' + v.target_state.name + "\n"
+ end
+
+ s
+ end
+
+ def states_events_list
+ s = ''
+ @events.each_value do |event|
+ s += event.previous_state.name + ' => ' + event.name
+ s += '(' + event.general_condition
+ unless event.second_condition.nil?
+ s += ', ' + event.second_condition
+ end
+ s += ') => ' + event.target_state.name + "\n"
+ end
+
+ s
+ end
+end
21 classes/event.rb
@@ -0,0 +1,21 @@
+class Event
+ attr_reader :name, :element, :previous_state, :target_state
+ attr_accessor :general_condition, :second_condition
+
+ def initialize(name, element, previous_state, target_state, general_condition=nil, second_condition=nil)
+ @name = name
+ @element = element
+ @previous_state = previous_state
+ @target_state = target_state
+ @previous_state.events[@target_state.name + '(' + name + ')'] = self
+ @target_state.events[@previous_state.name + '(' + name + ')'] = self
+ if general_condition.nil?
+ @general_condition = previous_state.name
+ else
+ @general_condition = general_condition
+ end
+ @second_condition = second_condition
+
+ @element.events[@name] = self
+ end
+end
263 classes/processor.rb
@@ -0,0 +1,263 @@
+module Processor
+ attr_accessor :criteries, :elements
+
+ @elements = Hash.new
+ @criteries = Array.new
+
+ def solve
+ st = proc.elements.sort[proc.elements.length - 1][1].states
+ #
+ # print path_to_state proc.elements['x1'].states['/x1']
+ # print path_to_state proc.elements['x2'].states['/x2']
+ # print path_to_state proc.elements['x3'].states['/x3']
+ # print path_to_state proc.elements['x4'].states['/x4a']
+ # print path_to_state proc.elements['x4'].states['/x4o']
+ #
+ # print path_to_state proc.elements['x1'].states['/x1']
+ # print path_to_state proc.elements['x2'].states['/x2']
+ # print path_to_state proc.elements['x3'].states['/x3a']
+ # print path_to_state proc.elements['x3'].states['/x3o']
+ proc.criteries.each do |crit|
+ s = crit.clone
+ crit.scan(/\/x\d{1,2}[oa]?/) do |m|
+ s.gsub!(m, (path_to_state2 proc.elements[m.scan(/x\d{1,2}/)[0]].states[m]))
+ end
+ print s
+ end
+ end
+
+ def path_to_state state
+ m = state.name
+ unless state.is_initial
+ state.previous_states.each_value do |prev|
+ state.events.each_value do |ev|
+ z = prev.name + '('+ev.name+')'
+ if state.events[z] != nil
+ m += ' <= ' + state.events[z].name
+ m += '(' + state.events[z].general_condition
+ unless state.events[z].second_condition == 'nil'
+ m += ', ' + state.events[z].second_condition# path_to_state()
+ end
+ m += ') <= ' + path_to_state(prev) + "\n"
+ end
+ end
+ end
+ end
+ m
+ end
+
+ def path_to_state2 state
+ m = ''
+ unless state.is_initial
+ state.previous_states.each_value do |prev|
+ state.events.each_value do |ev|
+ z = prev.name + '('+ev.name+')'
+ if state.events[z] != nil
+ m += state.events[z].name
+ if state.events[z].previous_state.is_initial
+ m += '(' + state.events[z].general_condition
+ else
+ m += '(' + (path_to_state2 state.events[z].previous_state)
+ end
+ unless state.events[z].second_condition == 'nil'
+ crit = state.events[z].second_condition
+ s = crit.clone
+ p s
+ crit.scan(/\/x\d{1,2}[oa]?/) do |v|
+ s.gsub!(v, (path_to_state2 proc.elements[v.scan(/x\d{1,2}/)[0]].states[v]))
+ end
+ m += ', ' + s# path_to_state()
+ end
+ m += ") + "#path_to_state2(prev) + ") + "
+ end
+ end
+ end
+ end
+ m[0..(m.length - 3)]
+ end
+
+ def read_file path
+ file = File.open path, 'r'
+ file = file.readlines
+ unless file.empty?
+ file.each do |line|
+ case line
+ when /^\/x\d{1,2}/ then
+ state_analyse line
+ when /\/Y/ then
+ proc.criteries << line
+ # p line
+ else next
+ end
+ end
+ sec_cond_analyse
+ end
+ end
+
+ def state_analyse line
+ r = line.match(/x\d{1,2}/)[0]
+ e = nil
+ x = nil
+ x_s = nil
+
+ unless proc.elements.has_key? r
+ eval r + ' = Element.new \'' + r + '\''
+ eval r + 'w = State.new \'' + r + '\', ' + r + ', true'
+ x_s = r + 'w'
+ eval 'x = ' + x_s
+ else
+ eval r + ' = proc.elements[\'' + r + '\']'
+ end
+ eval 'e = ' + r
+
+ if line.match(/^\/x\d{1,2}h/)
+ eval r + 'h = State.new \'/' + r + 'h\', ' + r + ', false, ' +
+ get_prev(line, e)
+ x_s = r + 'h'
+ eval 'x = ' + x_s
+ elsif line.match(/^\/x\d{1,2}[ ]/)
+ eval r + 'b = State.new \'/' + r + '\', ' + r + ', false, ' +
+ get_prev(line, e)
+ x_s = r + 'b'
+ eval 'x = ' + x_s
+ elsif line.match(/^\/x\d{1,2}o/)
+ eval r + 'o = State.new \'/' + r + 'o\', ' + r + ', false, ' +
+ get_prev(line, e)
+ x_s = r + 'o'
+ eval 'x = ' + x_s
+ elsif line.match(/^\/x\d{1,2}a/)
+ eval r + 'a = State.new \'/' + r + 'a\', ' + r + ', false, ' +
+ get_prev(line, e)
+ x_s = r + 'a'
+ eval 'x = ' + x_s
+ end
+ # p 'state = ' + x.name + ' :'
+ # x.previous_states.each_key {|pr| p pr}
+ line.scan(/w\d{1,2}[a-z]?\([^\)]+\)/) do |m|
+ # p r + ' - ' + m
+ m.each do |w|
+ if w.match(/w\d{1,2}\(/)
+ gen_cond = w.match(/\(([^,]+)[,|\)]/)
+ gen_cond = x.previous_states[gen_cond[1]]
+ sec_cond = 'nil'
+ if w.match(/,[ ]?(.+)\)/)
+ sec_cond = w.match(/,[ ]?(.+)\)/)[1]
+ end
+ ev = nil
+ eval w.match(/^(.+)\(/)[1] + ' = Event.new \'' + w.match(/^(.+)\(/)[1] +
+ '\', ' + r + ', gen_cond, ' + x_s + ', nil, \'' + sec_cond + '\''
+ eval 'ev = ' + w.match(/^(.+)\(/)[1]
+ # p ev.name
+ elsif w.match(/w\d{1,2}[he]\(/)
+ gen_cond = w.match(/\(([^,]+)[,|\)]/)
+ gen_cond = x.previous_states[gen_cond[1]]
+ sec_cond = 'nil'
+ if w.match(/,[ ]?(.+)\)/)
+ sec_cond = w.match(/,[ ]?(.+)\)/)[1]
+ end
+ ev = nil
+ eval w.match(/^(.+)\(/)[1] + ' = Event.new \'' + w.match(/^(.+)\(/)[1] +
+ '\', ' + r + ', gen_cond, ' + x_s + ', nil, \'' + sec_cond + '\''
+ eval 'ev = ' + w.match(/^(.+)\(/)[1]
+ # p ev.name
+ elsif w.match(/w\d{1,2}[oa]\(/)
+ gen_cond = w.match(/\(([^,]+)[,|\)]/)
+ gen_cond = x.previous_states[gen_cond[1]]
+ sec_cond = 'nil'
+ if w.match(/,[ ]?(.+)\)/)
+ sec_cond = w.match(/,[ ]?(.+)\)/)[1]
+ end
+ ev = nil
+ eval w.match(/^(.+)\(/)[1] + ' = Event.new \'' + w.match(/^(.+)\(/)[1] +
+ '\', ' + r + ', gen_cond, ' + x_s + ', nil, \'' + sec_cond + '\''
+ eval 'ev = ' + w.match(/^(.+)\(/)[1]
+ # p ev.name
+ end
+ end
+ end
+ end
+
+ def sec_cond_analyse
+ proc.elements.each_value do |el|
+ el.events.each_value do |ev|
+ ps = ev.previous_state
+ unless ps.is_initial
+ ps.events.each do |k,v|
+ ps.queue_push v if (v.target_state == ps) && !(ps.queue.has_value? v)
+ end
+ else
+ ps.queue_push ev if !(ps.queue.has_value? ev)
+ end
+ if ev.second_condition != 'nil'
+ inner_cond_analyse ev.second_condition, ps
+ # p ev.name + ' - ' + ev.general_condition.to_s
+ end
+ # p ev.target_state.name + ' :'
+ # ps.queue.sort.each {|k| p ' ' + k[1].name }
+ end
+ end
+ end
+
+ def ev_analysis ev, state
+ ps = ev.previous_state
+ unless ps.is_initial
+ ps.events.each do |k,v|
+ state.queue_push v if (v.target_state == ps) && !(state.queue.has_value? v)
+ end
+ else
+ state.queue_push ev if !(state.queue.has_value? ev)
+ end
+ if ev.second_condition != 'nil'
+ inner_cond_analyse ev.second_condition, ps
+ end
+ end
+
+ def inner_cond_analyse line, state
+ if line.match /\(.+\)/
+ inner_cond_analyse line.match(/\((.+)\)/)[1], state
+ else
+ line.scan(/[\/]?x\d{1,2}[hoa]?/).each do |st|
+ el = proc.elements[st.match(/(x\d{1,2})/)[1]]
+ ps = el.states[st]
+ ps.events.each do |k,v|
+ ev_analysis v, state
+ state.queue_push v if (v.target_state == ps) && !(state.queue.has_value? v)
+ end
+ end
+ end
+ end
+
+ def get_prev(line, element)
+ cond = Array.new
+ line.scan(/w\d{1,2}[a-z]?\([^\)]+\)/) do |m|
+ # p r + ' - ' + m
+ m.each do |w|
+ c = element.states[w.match(/\(([^,]+)[,|\)]/)[1]].name
+ # c = c + 'w' if c.match(/^x\d{1,2}$/)
+ # c = c[1..(c.length-1)] if c.match(/^\/x\d{1,2}h$/)
+ cond << c unless cond.include? c
+ end
+ end
+ r = '['
+ cond.each {|c| r += '\'' + c + '\','}
+ r = r[0..(r.length-2)] + ']'
+ end
+
+ def all_elements
+ unless proc.elements.nil?
+ proc.elements.each do |k,v|
+ p 'el = ' + k
+ puts " states: \n" + v.states_list
+ puts " events: \n" + v.events_list
+ p '-------------'
+ end
+ end
+ unless proc.criteries.nil?
+ proc.criteries.each { |e| p e }
+ end
+ end
+
+ def proc
+ Processor
+ end
+end
55 classes/state.rb
@@ -0,0 +1,55 @@
+class State
+ attr_reader :name, :element, :is_initial
+ attr_accessor :previous_states, :possibles, :events, :queue
+ @is_initial = false
+
+ def initialize(name, element, is_initial=nil, previous_states=nil, possibles=nil)
+ @name = name
+ @element = element
+ @previous_states = Hash.new
+ @possibles = Hash.new
+ @events = Hash.new
+ @queue = Hash.new
+ element.states[@name] = self
+
+ @is_initial = is_initial unless is_initial.nil?
+
+ unless previous_states.nil?
+ previous_states.each do |pr|
+ @previous_states[pr] = element.states[pr]
+ element.states[pr].possibles[@name] =
+ self unless element.states[pr].possibles.has_key? @name
+ end
+ end
+
+ unless possibles.nil?
+ possibles.each do |p|
+ @possible_states[p.name] = p
+ p.previous_states[@name] = self unless p.previous_states.has_key? @name
+ end
+ end
+ end
+
+ def queue_push obj
+ @queue[(@queue.nil? ? 0 : @queue.length)] = obj
+ nil
+ end
+
+ def previous_list
+ s = ''
+ previous_states.each_key do |ps|
+ s += ps + "\n"
+ end
+
+ s
+ end
+ def possibles_list
+ s = ''
+ possibles.each_key do |pos|
+ s += pos + "\n"
+ end
+
+ s
+ end
+
+end
7 data/2elements.txt
@@ -0,0 +1,7 @@
+/x1 <= w1(x1)
+/x2h <= w2h(x2)
+/x2 <= w2(x2, /x1)
+/x3o <= w3o(x3, /x1) + w3(x3)
+/x3a <= w3a(x3, /x2)
+Y = x3
+/Y = /x2
7 data/3elements.txt
@@ -0,0 +1,7 @@
+/x1 <= w1(x1)
+/x2h <= w2h(x2)
+/x2 <= w2(x2, /x1) + w2e(/x2h, /x1)
+/x3o <= w3o(x3, /x1) + w3(x3)
+/x3a <= w3a(x3, /x2)
+Y = x3
+/Y = /x3o + /x3a
7 data/4elements.txt
@@ -0,0 +1,7 @@
+/x1 <= w1(x1)
+/x2 <= w2(x2)
+/x3 <= w3(x3, /x1 * /x2)
+/x4o <= w4o(x4, x3)
+/x4a <= w4a(x4, /x3)
+
+/Y = /x1 + /x2 + /x3 + /x4o + /x4a
6 data/5elements.txt
@@ -0,0 +1,6 @@
+/x1 <= w1(x1)
+/x2 <= w2(x2)
+/x5 <= w5(x5, /x1 * /x2)
+/x3 <= w3(x3, /x5)
+/x4 <= w4(x4, /x5)
+/Y = /x3 * /x4
10 data/8elements.txt
@@ -0,0 +1,10 @@
+/x1 <= w1(x1)
+/x2 <= w2(x2)
+/x4 <= w4(x4)
+/x5 <= w5(x5)
+/x7 <= w7(x7)
+/x3 <= w3(x3, /x1 * /x2)
+/x6 <= w6(x6, /x4 * /x5)
+/x8 <= w8(x8, /x3 * /x6 + /x6 * /x7 + /x3 * /x7)
+Y = x8
+/Y = /x8
39 script.rb
@@ -0,0 +1,39 @@
+require 'classes/element.rb'
+require 'classes/state.rb'
+require 'classes/event.rb'
+
+read_file 'data/5elements.txt'
+
+# path_to_state proc.elements['x2'].states['/x2']
+
+print "\n\n"
+p '--------------------------------------'
+p '------------all elements--------------'
+p '--------------------------------------'
+print "\n\n"
+all_elements
+solve
+# x1 = Element.new 'x1'
+# x1_w = State.new 'x1', x1, true
+# x1_b = State.new '/x1', x1, false, [x1_w]
+#
+# x2 = Element.new 'x2'
+# x2_w = State.new 'x2', x2, true
+# x2_h = State.new '/x2h', x2, false, [x2_w]
+# x2_b = State.new '/x2', x2, false, [x2_w, x2_h]
+#
+# x3 = Element.new 'x3'
+# x3_w = State.new 'x3', x3, true
+# x3_o = State.new '/x3o', x3, false, [x3_w]
+# x3_a = State.new '/x3a', x3, false, [x3_w]
+#
+# w1 = Event.new 'w1', x1, x1_w, x1_b
+# w2 = Event.new 'w2', x2, x2_w, x2_b, nil, '/x1'
+# v2 = Event.new 'w2h', x2, x2_w, x2_h, nil, 'x1'
+# w2h = Event.new 'w2h', x2, x2_h, x2_b
+# w3o = Event.new 'w3o', x3, x3_w, x3_o, nil, '/x1 * x2'
+# w3a = Event.new 'w3a', x3, x3_w, x3_a, nil, '/x2'
+#
+# puts x3.states_events_list
+# puts
+# puts path_to_state x2_b
47 solves/2elements.txt
@@ -0,0 +1,47 @@
+
+"--------------------------------------"
+"------------all elements--------------"
+"--------------------------------------"
+
+
+"el = x1"
+ states:
+ x1:
+ w1(x1)
+ /x1:
+ w1(x1)
+ events:
+ x1 => w1(x1) => /x1
+"-------------"
+"el = x2"
+ states:
+ /x2h:
+ w2h(x2)
+ x2:
+ w2(x2, /x1)
+ w2h(x2)
+ /x2:
+ w2(x2, /x1)
+ events:
+ x2 => w2h(x2) => /x2h
+ x2 => w2(x2, /x1) => /x2
+"-------------"
+"el = x3"
+ states:
+ /x3a:
+ w3a(x3, /x2)
+ x3:
+ w3a(x3, /x2)
+ w3(x3)
+ w3o(x3, /x1)
+ /x3o:
+ w3(x3)
+ w3o(x3, /x1)
+ events:
+ x3 => w3a(x3, /x2) => /x3a
+ x3 => w3(x3) => /x3o
+ x3 => w3o(x3, /x1) => /x3o
+"-------------"
+"/Y = /x2"
+
+/Y = w2(x2, w1(x1) )
50 solves/3elements.txt
@@ -0,0 +1,50 @@
+
+"--------------------------------------"
+"------------all elements--------------"
+"--------------------------------------"
+
+
+"el = x1"
+ states:
+ x1:
+ w1(x1)
+ /x1:
+ w1(x1)
+ events:
+ x1 => w1(x1) => /x1
+"-------------"
+"el = x2"
+ states:
+ /x2h:
+ w2e(/x2h, /x1)
+ w2h(x2)
+ x2:
+ w2(x2, /x1)
+ w2h(x2)
+ /x2:
+ w2(x2, /x1)
+ w2e(/x2h, /x1)
+ events:
+ x2 => w2h(x2) => /x2h
+ x2 => w2(x2, /x1) => /x2
+ /x2h => w2e(/x2h, /x1) => /x2
+"-------------"
+"el = x3"
+ states:
+ /x3a:
+ w3a(x3, /x2)
+ x3:
+ w3a(x3, /x2)
+ w3(x3)
+ w3o(x3, /x1)
+ /x3o:
+ w3(x3)
+ w3o(x3, /x1)
+ events:
+ x3 => w3a(x3, /x2) => /x3a
+ x3 => w3(x3) => /x3o
+ x3 => w3o(x3, /x1) => /x3o
+"-------------"
+"/Y = /x3o + /x3a"
+
+/Y = w3(x3) + w3o(x3, w1(x1) ) + w3a(x3, w2e(w2h(x2) , w1(x1) ) + w2(x2, w1(x1) ) )
49 solves/4elements.txt
@@ -0,0 +1,49 @@
+
+"--------------------------------------"
+"------------all elements--------------"
+"--------------------------------------"
+
+
+"el = x1"
+ states:
+ x1:
+ w1(x1)
+ /x1:
+ w1(x1)
+ events:
+ x1 => w1(x1) => /x1
+"-------------"
+"el = x2"
+ states:
+ x2:
+ w2(x2)
+ /x2:
+ w2(x2)
+ events:
+ x2 => w2(x2) => /x2
+"-------------"
+"el = x3"
+ states:
+ x3:
+ w3(x3, /x1 * /x2)
+ /x3:
+ w3(x3, /x1 * /x2)
+ events:
+ x3 => w3(x3, /x1 * /x2) => /x3
+"-------------"
+"el = x4"
+ states:
+ /x4a:
+ w4a(x4, /x3)
+ x4:
+ w4a(x4, /x3)
+ w4o(x4, x3)
+ /x4o:
+ w4o(x4, x3)
+ events:
+ x4 => w4a(x4, /x3) => /x4a
+ x4 => w4o(x4, x3) => /x4o
+"-------------"
+"/Y = /x1 + /x2 + /x3 + /x4o + /x4a"
+
+/Y = w1(x1) + w2(x2) + w3(x3, w1(x1) * w2(x2) ) + w4o(x4, x3) + w4a(x4, w3(x3, w1(x1) * w2(x2) ) )
80 solves/8elements.txt
@@ -0,0 +1,80 @@
+"--------------------------------------"
+"------------all elements--------------"
+"--------------------------------------"
+
+
+"el = x8"
+ states:
+ x8:
+ w8(x8, /x3 * /x6 + /x6 * /x7 + /x3 * /x7)
+ /x8:
+ w8(x8, /x3 * /x6 + /x6 * /x7 + /x3 * /x7)
+ events:
+ x8 => w8(x8, /x3 * /x6 + /x6 * /x7 + /x3 * /x7) => /x8
+"-------------"
+"el = x1"
+ states:
+ x1:
+ w1(x1)
+ /x1:
+ w1(x1)
+ events:
+ x1 => w1(x1) => /x1
+"-------------"
+"el = x2"
+ states:
+ x2:
+ w2(x2)
+ /x2:
+ w2(x2)
+ events:
+ x2 => w2(x2) => /x2
+"-------------"
+"el = x3"
+ states:
+ x3:
+ w3(x3, /x1 * /x2)
+ /x3:
+ w3(x3, /x1 * /x2)
+ events:
+ x3 => w3(x3, /x1 * /x2) => /x3
+"-------------"
+"el = x4"
+ states:
+ /x4:
+ w4(x4)
+ x4:
+ w4(x4)
+ events:
+ x4 => w4(x4) => /x4
+"-------------"
+"el = x5"
+ states:
+ /x5:
+ w5(x5)
+ x5:
+ w5(x5)
+ events:
+ x5 => w5(x5) => /x5
+"-------------"
+"el = x6"
+ states:
+ /x6:
+ w6(x6, /x4 * /x5)
+ x6:
+ w6(x6, /x4 * /x5)
+ events:
+ x6 => w6(x6, /x4 * /x5) => /x6
+"-------------"
+"el = x7"
+ states:
+ /x7:
+ w7(x7)
+ x7:
+ w7(x7)
+ events:
+ x7 => w7(x7) => /x7
+"-------------"
+"/Y = /x8"
+
+/Y = w8(x8, w3(x3, w1(x1) * w2(x2) ) * w6(x6, w4(x4) * w5(x5) ) + w6(x6, w4(x4) * w5(x5) ) * w7(x7) + w3(x3, w1(x1) * w2(x2) ) * w7(x7) )
Please sign in to comment.
Something went wrong with that request. Please try again.