sfp 0.3.18 → 0.3.19

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: ca049ea19fd6ff1ee58312bdd03bdefa3358e342
4
+ data.tar.gz: d6808887e1039d2e75f5360491d9584b5e842cb0
5
+ SHA512:
6
+ metadata.gz: be71d391cc8d77de2fec90208d31d232643eebe216e0677eb7641d0cc0ff5d4c8e8bb5fc435cf5f230d884ca8f5964337c760cb757ffa82381098fb736d2ba2f
7
+ data.tar.gz: b1394aabf249f510ebb843a4228299a4ed1028419e10f86054924a200c3b3507b82a8494e9d14b616664c89666e1745d4777e10d46a341d7a2fb83f3c8eb223c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.18
1
+ 0.3.19
data/lib/sfp/Sfplib.rb CHANGED
@@ -218,7 +218,8 @@ end
218
218
  Hash.send(:define_method, "ref") {
219
219
  return '$' if not self.has_key?('_parent') or self['_parent'] == nil
220
220
  me = (self.has_key?('_self') ? self['_self'] : '')
221
- return self['_parent'].ref + "." + me
221
+ r = self['_parent'].ref
222
+ r << ".#{me}"
222
223
  }
223
224
 
224
225
  # accept method as implementation of Visitor pattern
@@ -144,7 +144,8 @@ module Sfp
144
144
  @root['sometime'].accept(value_collector) if @root.has_key?('sometime')
145
145
 
146
146
  # remove duplicates from type's set of value
147
- @types.each_value { |type| type.uniq! }
147
+ #@types.each_value { |values| values.uniq! }
148
+ @types.keys.each { |type| @types[type] = to_set(@types[type]) }
148
149
 
149
150
  # set domain values for each variable
150
151
  self.set_variable_values
@@ -185,7 +186,7 @@ module Sfp
185
186
  @types[var.type] << var.goal
186
187
  end
187
188
  end
188
- @types.each_value { |type| type.uniq! }
189
+ @types.keys.each { |type| @types[type] = to_set(@types[type]) }
189
190
 
190
191
  # add Sfp::Unknown and Sfp::Undefined value to all non-final variables
191
192
  self.add_unknown_undefined_value_to_variables
@@ -221,6 +222,10 @@ module Sfp
221
222
  end
222
223
  end
223
224
 
225
+ def to_set(array)
226
+ array.inject([]) { |result,item| result << item unless result.include?(item); result }
227
+ end
228
+
224
229
  def variable_name_and_value(var_id, value_index)
225
230
  i = @vars.index { |v| v.id == var_id }
226
231
  var = @vars[i]
@@ -629,67 +634,65 @@ module Sfp
629
634
  # version
630
635
  out = "begin_version\n3\nend_version\n"
631
636
  # metric
632
- out += "begin_metric\n1\nend_metric\n"
637
+ out << "begin_metric\n1\nend_metric\n"
638
+
639
+ benchmarks = {}
640
+
641
+ vars = {}
642
+ benchmarks[:conversion] = Benchmark.measure do
643
+ @variables.each { |k,v| vars[k.to_sym] = v }
644
+ end
645
+ names = vars.keys
646
+
633
647
  # variables
634
- variable_index = @variables.keys
635
- variable_index.sort!
636
- out += "#{variable_index.length}\n"
637
- variable_index.each { |i|
638
- @variables[i].id = variable_index.index(i) # set variable's index
639
- out += @variables[i].to_sas(@root['initial']) + "\n"
640
- }
648
+ out << "#{names.length}\n"
649
+ names.sort!
650
+ names.each_index do |i|
651
+ var = vars[ names[i] ]
652
+ var.id = i
653
+ out << var.to_sas(@root['initial']) + "\n"
654
+ end
655
+
641
656
  # mutex
642
- out += "0\n"
643
- # initial state
644
- out += "begin_state\n"
645
- #pre = ( (pre.is_a?(Hash) and pre.isnull) ? 0 : (pre == nil ? -1 : @var.index(pre)) )
646
- variable_index.each { |i|
647
- if @variables[i].init.is_a?(Hash) and @variables[i].init.isnull
648
- out += "0\n"
649
- elsif @variables[i].init.is_a?(::Sfp::Unknown)
650
- out += "#{@variables[i].length-1}\n"
657
+ out << "0\n"
658
+
659
+ # initial & goal state
660
+ out << "begin_state\n"
661
+ goal = ''
662
+ goal_count = 0
663
+ names.each do |name|
664
+ var = vars[name]
665
+ if var.init.is_a?(Hash) and var.init.isnull
666
+ out << "0\n"
667
+ elsif var.init.is_a?(::Sfp::Unknown)
668
+ out << "#{var.length - 1}\n"
651
669
  else
652
- val = @variables[i].index(@variables[i].init).to_s
653
- out += "#{val}\n"
654
- if val.length <= 0
655
- raise TranslationException,
656
- "Unknown init: #{@variables[i].name} = #{@variables[i].init.inspect}"
657
- end
670
+ val = var.index(var.init).to_s
671
+ raise TranslationException, "Unknown init: #{var.name}=#{var.init.inspect}" if val.length <= 0
672
+ out << "#{val}\n"
658
673
  end
659
- }
660
- out += "end_state\n"
661
- # goal
662
- out += "begin_goal\n"
663
- count = 0
664
- goal = ''
665
- variable_index.each { |i|
666
- if @variables[i].goal != nil
667
- goal += variable_index.index(i).to_s + ' ' +
668
- @variables[i].index(@variables[i].goal).to_s + "\n"
669
- count += 1
674
+
675
+ if var.goal != nil
676
+ goal << "#{var.id} #{var.index(var.goal)}\n"
677
+ goal_count += 1
670
678
  end
671
- }
672
- out += "#{count}\n#{goal}end_goal\n"
679
+ end
680
+ out << "end_state\nbegin_goal\n#{goal_count}\n#{goal}end_goal\n"
681
+
673
682
  # operators
674
- #out += "#{@operators.length}\n"
675
683
  ops = ''
676
684
  total = 0
677
- @operators.each_value { |op|
678
- next if op.total_preposts <= 0
679
- # HACK! - an exception may arise if a value in condition or effect is not in variable's domain
680
- begin
681
- ops += op.to_sas(@root['initial'], @variables) + "\n"
682
- total += 1
683
- rescue Exception => exp
684
- #puts "#{exp}\n#{exp.backtrace.join("\n")}"
685
- end
686
- }
687
- out += "#{total}\n"
688
- out += ops
689
- # axioms
690
- out += "0"
685
+ @operators.each_value do |operator|
686
+ next if operator.total_preposts <= 0
687
+ ops << operator.to_sas2(@root['initial'], vars)
688
+ ops << "\n"
689
+ total += 1
690
+ end
691
+ out << "#{total}\n"
692
+ out << ops
691
693
 
692
- return out
694
+ # axioms
695
+ out << "0"
693
696
  end
694
697
 
695
698
  def reset_operators_name
@@ -1810,24 +1813,29 @@ module Sfp
1810
1813
 
1811
1814
  def to_s
1812
1815
  s = @name.to_s + '|' + @type.to_s
1813
- s += '|' + (@init == nil ? '-' : (@init.is_a?(Hash) ? @init.tostring : @init.to_s))
1814
- s += '|' + (@goal == nil ? '-' : (@goal.is_a?(Hash) ? @goal.tostring : @goal.to_s))
1815
- s += '|' + (@is_final ? 'final' : 'notfinal') + "\n"
1816
- s += "\t["
1817
- self.each { |v| s += (v.is_a?(Hash) ? v.tostring : v.to_s) + ',' }
1818
- s = (self.length > 0 ? s.chop : s) + "]"
1819
- return s
1816
+ s << '|'
1817
+ s << (@init == nil ? '-' : (@init.is_a?(Hash) ? @init.tostring : @init.to_s))
1818
+ s << '|'
1819
+ s << (@goal == nil ? '-' : (@goal.is_a?(Hash) ? @goal.tostring : @goal.to_s))
1820
+ s << '|'
1821
+ s << (@is_final ? 'final' : 'notfinal') + "\n"
1822
+ s << "\t["
1823
+ self.each { |v| s << (v.is_a?(Hash) ? v.tostring : v.to_s) + ',' }
1824
+ s = (self.length > 0 ? s.chop : s)
1825
+ s << "]"
1820
1826
  end
1821
1827
 
1822
1828
  # return variable representation in SAS+ format
1823
1829
  def to_sas(root)
1824
1830
  sas = "begin_variable\nvar_#{@id}#{@name}\n#{@layer}\n#{self.length}\n"
1825
1831
  self.each { |v|
1826
- v = root.at?(v) if v.is_a?(String) and v.isref
1832
+ if v.is_a?(String) and v.isref
1833
+ v = root.at?(v)
1834
+ end
1827
1835
  v = '"' + v + '"' if v.is_a?(String)
1828
- sas += (v.is_a?(Hash) ? (v.isnull ? "null\n" : "#{v.ref}\n") : "#{v}\n")
1836
+ sas << (v.is_a?(Hash) ? (v.isnull ? "null\n" : "#{v.ref}\n") : "#{v}\n")
1829
1837
  }
1830
- return sas += "end_variable"
1838
+ sas << "end_variable"
1831
1839
  end
1832
1840
 
1833
1841
  def not(x)
@@ -1956,23 +1964,51 @@ module Sfp
1956
1964
  end
1957
1965
  }
1958
1966
  sas = "begin_operator\n#{@name}"
1959
- @params.each { |k,v| sas += " #{k}=#{v}" if k != '$.this' } if @params != nil
1960
- sas += "\n#{prevail.length}\n"
1967
+ @params.each { |k,v| sas << " #{k}=#{v}" if k != '$.this' } if @params != nil
1968
+ sas << "\n#{prevail.length}\n"
1961
1969
  prevail.each { |p|
1962
1970
  line = p.to_sas(root, variables)
1963
1971
  raise TranslationException if line[line.length-1] == ' '
1964
- sas += "#{line}\n"
1972
+ sas << "#{line}\n"
1965
1973
  }
1966
- sas += "#{prepost.length}\n"
1974
+ sas << "#{prepost.length}\n"
1967
1975
  prepost.each { |p|
1968
1976
  line = p.to_sas(root, variables, false)
1969
1977
  raise TranslationException if line[line.length-1] == ' '
1970
- sas += "#{line}\n"
1978
+ sas << "#{line}\n"
1971
1979
  }
1972
- sas += "#{@cost}\nend_operator"
1980
+ sas << "#{@cost}\nend_operator"
1973
1981
  return sas
1974
1982
  end
1975
1983
 
1984
+ def to_sas2(root, variables)
1985
+ prevails = self.values.select { |p| p.post.nil? }
1986
+ preposts = self.values.select { |p| not p.post.nil? }
1987
+
1988
+ sas = "begin_operator\n#{@name}"
1989
+ @params.each do |key,val|
1990
+ sas << " #{key}=#{val}" if key != '$.this'
1991
+ end if @params.is_a?(Hash)
1992
+
1993
+ sas << "\n#{prevails.length}\n"
1994
+ prevails.each do |p|
1995
+ line = p.to_sas2(root, variables)
1996
+ raise TranslationException if line[-1] == ' '
1997
+ sas << line
1998
+ sas << "\n"
1999
+ end
2000
+
2001
+ sas << "#{preposts.length}\n"
2002
+ preposts.each do |p|
2003
+ line = p.to_sas2(root, variables, false)
2004
+ raise TranslationException if line[-1] == ' '
2005
+ sas << line
2006
+ sas << "\n"
2007
+ end
2008
+
2009
+ sas << "#{@cost}\nend_operator"
2010
+ end
2011
+
1976
2012
  def to_sfw
1977
2013
  if not (@name =~ /.*\$.*/)
1978
2014
  id , name = @name.split('-', 2)
@@ -2025,10 +2061,10 @@ module Sfp
2025
2061
  raise Exception, "Invalid axiom: total preposts <= 0" if preposts.length <= 0
2026
2062
 
2027
2063
  sas = "begin_rule"
2028
- sas += "\n#{prevails.length}"
2029
- prevails.each { |var,param| sas += "\n" + param.to_sas(root, variables) }
2030
- preposts.each { |var,param| sas += "\n" + param.to_sas(root, variables) }
2031
- sas += "\nend_rule"
2064
+ sas << "\n#{prevails.length}"
2065
+ prevails.each { |var,param| sas << "\n#{param.to_sas(root, variables)}" }
2066
+ preposts.each { |var,param| sas << "\n#{param.to_sas(root, variables)}" }
2067
+ sas << "\nend_rule"
2032
2068
  return sas
2033
2069
  end
2034
2070
  end
@@ -2073,6 +2109,29 @@ module Sfp
2073
2109
  return "0 #{@var.id} #{pre} #{post}"
2074
2110
  end
2075
2111
 
2112
+ def to_sas2(root, variables, is_prevail=true)
2113
+ ### resolve reference
2114
+ pre = ((@pre.is_a?(String) and @pre.isref) ? variables[@pre.to_sym].init : @pre)
2115
+ ### calculate index
2116
+ if pre.is_a?(Hash) and pre.isnull
2117
+ pre = 0
2118
+ elsif pre.nil?
2119
+ pre = -1
2120
+ else
2121
+ pre = @var.index(pre)
2122
+ end
2123
+ return "#{@var.id} #{pre}" if is_prevail
2124
+
2125
+ ### resolve reference
2126
+ post = ((@post.is_a?(String) and @post.isref) ? variables[@post.to_sym].init : @post)
2127
+ ### calculate index
2128
+ if post.is_a?(Hash) and post.isnull
2129
+ "0 #{@var.id} #{pre} 0"
2130
+ else
2131
+ "0 #{@var.id} #{pre} #{@var.index(post)}"
2132
+ end
2133
+ end
2134
+
2076
2135
  def to_s
2077
2136
  return @var.name + ',' +
2078
2137
  (@pre == nil ? '-' : (@pre.is_a?(Hash) ? @pre.tostring : @pre.to_s)) + ',' +
data/lib/sfp/visitors.rb CHANGED
@@ -92,13 +92,17 @@ module Sfp
92
92
  if value.length >= 8 and value[0,8] == '$.parent'
93
93
  _, _, rest = value.split('.', 3)
94
94
  if parent.has_key?('_parent')
95
- parent[name] = parent['_parent'].ref + (rest == nil ? '' : '.' + rest)
95
+ val = parent['_parent'].ref
96
+ val << ".#{rest}" if not rest.nil?
97
+ parent[name] = val
96
98
  else
97
99
  raise Exception
98
100
  end
99
101
  elsif value.length >= 6 and value[0,6] == '$.this'
100
102
  _, _, rest = value.split('.', 3)
101
- parent[name] = parent.ref + (rest == nil ? '' : '.' + rest)
103
+ val = parent.ref
104
+ val << ".#{rest}" if not rest.nil?
105
+ parent[name] = val
102
106
  end
103
107
  end
104
108
  true
metadata CHANGED
@@ -1,38 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sfp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.18
5
- prerelease:
4
+ version: 0.3.19
6
5
  platform: ruby
7
6
  authors:
8
7
  - Herry
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-14 00:00:00.000000000 Z
11
+ date: 2013-11-20 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: antlr3
16
- requirement: &16030420 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.10.0
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *16030420
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.10.0
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rake
27
- requirement: &16029240 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *16029240
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  description: A Ruby API and script for SFP language parser
37
42
  email: herry13@gmail.com
38
43
  executables:
@@ -67,26 +72,25 @@ files:
67
72
  homepage: https://github.com/herry13/sfp-ruby
68
73
  licenses:
69
74
  - BSD
75
+ metadata: {}
70
76
  post_install_message:
71
77
  rdoc_options: []
72
78
  require_paths:
73
79
  - lib
74
80
  required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
81
  requirements:
77
- - - ! '>='
82
+ - - '>='
78
83
  - !ruby/object:Gem::Version
79
84
  version: '0'
80
85
  required_rubygems_version: !ruby/object:Gem::Requirement
81
- none: false
82
86
  requirements:
83
- - - ! '>='
87
+ - - '>='
84
88
  - !ruby/object:Gem::Version
85
89
  version: '0'
86
90
  requirements: []
87
91
  rubyforge_project: sfp
88
- rubygems_version: 1.8.11
92
+ rubygems_version: 2.1.10
89
93
  signing_key:
90
- specification_version: 3
94
+ specification_version: 4
91
95
  summary: SFP Parser
92
96
  test_files: []