sfp 0.3.6 → 0.3.7

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.
@@ -29,6 +29,8 @@
29
29
  # - foreach subclasses, inherits attributes and procedures from superclass
30
30
  # - foreach objects, inherits attributes and procedures from class
31
31
 
32
+ require 'benchmark'
33
+
32
34
  module Sfp
33
35
 
34
36
  class TranslationException < Exception; end
@@ -49,9 +51,10 @@ module Sfp
49
51
  GoalVariable = '-goal-'
50
52
 
51
53
  GlobalConstraintMethod = 1 # 1: proposed method, 2: patrik's, 3: concurrent-actions
54
+ ActivateSimpleGlobalConstraint = false #true
52
55
 
53
56
  attr_accessor :root
54
- attr_reader :variables, :types, :operators, :axioms, :goals
57
+ attr_reader :variables, :types, :operators, :axioms, :goals, :benchmarks
55
58
 
56
59
  def to_sas
57
60
  self.compile_step_1
@@ -60,11 +63,14 @@ module Sfp
60
63
  end
61
64
 
62
65
  def compile_step_2
63
- self.postprocess_simple_global_constraint
66
+ @benchmarks['postprocessing simple global constraint'] = Benchmark.measure do
67
+ self.postprocess_simple_global_constraint
68
+ end
64
69
  end
65
70
 
66
71
  def compile_step_1
67
72
  begin
73
+ @benchmarks = {}
68
74
  @unknown_value = ::Sfp::Unknown.new
69
75
 
70
76
  @arrays = Hash.new
@@ -116,6 +122,7 @@ module Sfp
116
122
  @root['sometime-after'].delete('_parent')
117
123
  end
118
124
 
125
+ @benchmarks['generating variables'] = Benchmark.measure do
119
126
  @root['initial'].accept(Sfp::Visitor::ReferenceModifier.new)
120
127
  @root['goal'].accept(Sfp::Visitor::ReferenceModifier.new) if @root.has_key?('goal')
121
128
  @root['global'].accept(Sfp::Visitor::ReferenceModifier.new) if @root.has_key?('global')
@@ -147,20 +154,31 @@ module Sfp
147
154
 
148
155
  # re-evaluate set variables and types
149
156
  self.evaluate_set_variables_and_types
157
+ end
150
158
 
159
+ @benchmarks['processing goal'] = Benchmark.measure do
151
160
  ### process goal constraint ###
152
161
  process_goal(@root['goal']) if @root.has_key?('goal') and
153
162
  @root['goal'].isconstraint
163
+ end
154
164
 
165
+ @benchmarks['processing global constraint'] = Benchmark.measure do
155
166
  ### process global constrait
156
167
  self.process_global_constraint
168
+ end
157
169
 
170
+ @benchmarks['processing sometime constraint'] = Benchmark.measure do
158
171
  ### normalize sometime formulae ###
159
172
  if @root.has_key?('sometime')
160
173
  raise TranslationException, 'Invalid sometime constraint' if
161
174
  not normalize_formula(@root['sometime'])
162
175
  end
176
+ end
177
+
178
+ # add Sfp::Unknown and Sfp::Undefined value to all non-final variables
179
+ self.add_unknown_undefined_value_to_variables
163
180
 
181
+ @benchmarks['processing procedures'] = Benchmark.measure do
164
182
  ### process all procedures
165
183
  @variables.each_value do |var|
166
184
  if var.is_final
@@ -168,6 +186,7 @@ module Sfp
168
186
  end
169
187
  end
170
188
  self.reset_operators_name
189
+ end
171
190
 
172
191
  ### process sometime modalities ###
173
192
  self.process_sometime if @root.has_key?('sometime')
@@ -177,7 +196,7 @@ module Sfp
177
196
  # detect and merge mutually inclusive operators
178
197
  self.search_and_merge_mutually_inclusive_operators
179
198
 
180
- self.add_unknown_value_to_nonstatic_variables
199
+ #self.add_unknown_value_to_nonstatic_variables
181
200
 
182
201
  #self.dump_types
183
202
  #self.dump_vars
@@ -294,6 +313,11 @@ module Sfp
294
313
  #####
295
314
  def postprocess_simple_global_constraint
296
315
  @operators.keys.each do |name|
316
+ # skip global, sometime, and goal verifier operators
317
+ next if name =~ /\-globalop\-/
318
+ next if name =~ /\-sometime\-/
319
+ next if name =~ /\-goal\-/
320
+
297
321
  operator = @operators[name]
298
322
  @operators.delete(name)
299
323
 
@@ -327,23 +351,36 @@ module Sfp
327
351
  end
328
352
  end
329
353
 
330
- # TODO - test with premise and conclusion clauses have > 1 statements
331
- #
332
- # pre |= premise
354
+ # return true if precondition supports premise
333
355
  def pre_support_premise(operator, premise)
334
356
  premise.each { |var,c|
335
357
  next if var[0,1] == '_'
336
- return false if !operator.has_key?(var) or (!operator[var].pre.nil? and c['_value'] != operator[var].pre)
358
+ return false if !operator.has_key?(var) or (!operator[var].pre.nil? and operator[var].pre != c['_value'])
337
359
  }
338
360
  true
339
361
  end
340
362
 
363
+ # return true if postcondition supports premise
364
+ def post_support_premise(operator, premise)
365
+ premise.each { |var,c|
366
+ return true if var[0,1] != '_' and operator.has_key?(var) and
367
+ !operator[var].post.nil? and operator[var].post == c['_value']
368
+ }
369
+ false
370
+ end
371
+
341
372
  def post_threat_conclusion(operator, conclusion)
373
+ conclusion.each { |var,c|
374
+ return true if var[0,1] == '_' and operator.has_key?(var) and
375
+ !operator[var].post.nil? and operator[var].post != c['_value']
376
+ }
377
+ false
378
+ end
379
+
380
+ def pre_threat_conclusion(operator, conclusion)
342
381
  conclusion.each { |var,c|
343
382
  next if var[0,1] == '_'
344
- if operator.has_key?(var) and !operator[var].post.nil? and operator[var].post != c['_value']
345
- return true
346
- end
383
+ return true if !operator.has_key?(var) or (!operator[var].pre.nil? and operator[var].pre != c['_value'])
347
384
  }
348
385
  false
349
386
  end
@@ -351,10 +388,12 @@ module Sfp
351
388
  return [operator] if not @global_simple_implications.is_a?(Hash)
352
389
 
353
390
  @global_simple_implications.each do |id,imply|
354
- # If the operator's precondition support the premise, then
391
+ # If the operator's precondition support the premise or
392
+ # if the operator's postcondition support the premise, then
355
393
  # it should support the conclusion as well.
356
- if pre_support_premise(operator, imply['_premise'])
357
- imply['_conclusion'].each { |var,c|
394
+ if pre_support_premise(operator, imply['_premise']) or
395
+ post_support_premise(operator, imply['_premise'])
396
+ imply['_conclusion'].each do |var,c|
358
397
  next if var[0,1] == '_'
359
398
  if operator.has_key?(var)
360
399
  if operator[var].pre.nil? # enforce the operator to support the conclusion
@@ -366,27 +405,30 @@ module Sfp
366
405
  # enforce the operator to support the conclusion
367
406
  operator[var] = Parameter.new(@variables[var], c['_value'])
368
407
  end
369
- }
408
+ end
370
409
  end
371
410
  end
372
411
 
373
412
  results = []
374
413
  @global_simple_implications.each do |id,imply|
375
- imply['_premise'].each do |var,c|
376
- next if var[0,1] == '_'
377
- @variables[var].not(c['_value']).each { |x|
378
- op = operator.clone
379
- if op.has_key?(var)
380
- if op[var].pre != x
381
- op[var].pre == x
414
+ if pre_threat_conclusion(operator, imply['_conclusion']) or
415
+ post_threat_conclusion(operator, imply['_conclusion'])
416
+ imply['_premise'].each do |var,c|
417
+ next if var[0,1] == '_'
418
+ @variables[var].not(c['_value']).each do |x|
419
+ op = operator.clone
420
+ if op.has_key?(var)
421
+ if op[var].pre != x
422
+ op[var].pre == x
423
+ results << op
424
+ end
425
+ else
426
+ op[var] = Parameter.new(@variables[var], x)
382
427
  results << op
383
428
  end
384
- else
385
- op[var] = Parameter.new(@variables[var], x)
386
- results << op
387
429
  end
388
- }
389
- end if post_threat_conclusion(operator, imply['_conclusion'])
430
+ end
431
+ end
390
432
  end
391
433
  return [operator] if results.length <= 0
392
434
  results
@@ -415,7 +457,10 @@ module Sfp
415
457
  end
416
458
 
417
459
  global = @root['global']
418
- simples = global.select { |k,v| k[0,1] != '_' and simple_formulae(k, v) }
460
+ simples = global.select { |k,v| k[0,1] != '_' and
461
+ (!k.isref or @variables.has_key?(k)) and
462
+ simple_formulae(k, v)
463
+ }
419
464
 
420
465
  @global_simple_conjunctions = {}
421
466
  @global_simple_implications = {}
@@ -447,7 +492,7 @@ module Sfp
447
492
 
448
493
  ### normalize global constraint formula ###
449
494
  if @root.has_key?('global') and @root['global'].isconstraint
450
- preprocess_simple_global_constraint
495
+ preprocess_simple_global_constraint if ActivateSimpleGlobalConstraint
451
496
 
452
497
  keys = @root['global'].keys.select { |k| k[0,1] != '_' }
453
498
  @total_complex_global_constraints = keys.length
@@ -896,6 +941,15 @@ module Sfp
896
941
  }
897
942
  end
898
943
 
944
+ def add_unknown_undefined_value_to_variables
945
+ @variables.each_value { |variable|
946
+ #next if variable.is_final
947
+ variable << @unknown_value
948
+ variable << Sfp::Undefined.create(variable.type)
949
+ variable.uniq!
950
+ }
951
+ end
952
+
899
953
  def apply_global_constraint_method_1(operator)
900
954
  return true if not @root.has_key?('global') or not @root['global'].isconstraint
901
955
  operator[@global_var.name] = Parameter.new(@global_var, true, false)
@@ -1056,7 +1110,7 @@ module Sfp
1056
1110
  # TODO
1057
1111
  #puts 'nested right: ' + left + ' = ' + right['_value']
1058
1112
 
1059
- raise TranslationException, 'not implemented: normalized_nested_right'
1113
+ raise TranslationException, "not implemented: normalized_nested_right => #{right}"
1060
1114
  end
1061
1115
 
1062
1116
  def normalize_nested_right_only(left, right, formula)
data/sfp.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'sfp'
3
- s.version = '0.3.6'
4
- s.date = '2013-08-05'
3
+ s.version = '0.3.7'
4
+ s.date = '2013-08-13'
5
5
  s.summary = 'SFP Parser'
6
6
  s.description = 'A Ruby API and script for SFP language parser'
7
7
  s.authors = ['Herry']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sfp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-05 00:00:00.000000000 Z
12
+ date: 2013-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &14689520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 1.7.5
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 1.7.5
24
+ version_requirements: *14689520
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: antlr3
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &14689040 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ~>
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: 1.9.0
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 1.9.0
35
+ version_requirements: *14689040
46
36
  description: A Ruby API and script for SFP language parser
47
37
  email: herry13@gmail.com
48
38
  executables:
@@ -89,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
79
  version: '0'
90
80
  requirements: []
91
81
  rubyforge_project: sfp
92
- rubygems_version: 1.8.23
82
+ rubygems_version: 1.8.11
93
83
  signing_key:
94
84
  specification_version: 3
95
85
  summary: SFP Parser