Class: Pione::Package::ScenarioScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/package/scenario-scanner.rb

Overview

ScenarioScanner scans scenario location.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ ScenarioScanner

Create a scanner with the location. The location should be local scheme.



16
17
18
19
20
21
# File 'lib/pione/package/scenario-scanner.rb', line 16

def initialize(location)
  unless location.local?
    raise Location::NotLocal.new(location)
  end
  @location = location
end

Class Method Details

.scan(location) ⇒ Object



10
11
12
# File 'lib/pione/package/scenario-scanner.rb', line 10

def scan(location)
  new(location).scan
end

.scenario?(location) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/pione/package/scenario-scanner.rb', line 6

def scenario?(location)
  not(/^\./.match(location.basename)) and (location + "Scenario.pione").exist?
end

Instance Method Details

#find_name(annotations) ⇒ Object

Find "ScenarioName" annotaion.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pione/package/scenario-scanner.rb', line 71

def find_name(annotations)
  location = @location + "Scenario.pione"
  names = annotations.select {|annotation| annotation.annotation_type == "ScenarioName"}
  case names.size
  when 0
    raise InvalidScenario.new(location, "No scenario name exists.")
  when 1
    names.first.value
  else
    name_list = names.map {|name| name.value}.join(", ")
    raise InvalidScenario.new(location, "Duplicated scenario name found." % name_list)
  end
end

#find_param_set(annotations) ⇒ Object

Find "ParamSet" annotaion.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pione/package/scenario-scanner.rb', line 86

def find_param_set(annotations)
  param_sets = annotations.select {|annotation| annotation.annotation_type == "ParamSet"}
  case param_sets.size
  when 0
    return nil
  when 1
    param_sets.first.value
  else
    location = @location + "Scenario.pione"
    raise InvalidScenario.new(location, "Duplicated parameter set found.")
  end
end

#scanObject

Scan the scenario directory and return scenario informations. If the location is not scenario directory, return false.



25
26
27
28
29
30
31
32
33
34
# File 'lib/pione/package/scenario-scanner.rb', line 25

def scan
  unless self.class.scenario?(@location)
    return false
  else
    name, param_set = scan_annotations
    inputs = scan_data_dir("input")
    outputs = scan_data_dir("output")
    ScenarioInfo.new(name: name, textual_param_sets: param_set, inputs: inputs, outputs: outputs)
  end
end

#scan_annotationsObject

Scan annotations from scenario file and return scenario name and parameter set.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pione/package/scenario-scanner.rb', line 38

def scan_annotations
  location = @location + "Scenario.pione"

  # setup fake language environment
  env = Lang::Environment.new.setup_new_package("ScenarioScanner")

  # parse the scenario document
  Document.load(env, location, nil, nil, nil, @location + "Scenario.pione")

  # get name and parameter set from fake package's annotations
  annotations = env.package_get(Lang::PackageExpr.new(package_id: env.current_package_id)).annotations
  name = find_name(annotations)
  param_set = find_param_set(annotations)

  return name, param_set
rescue Parslet::ParseFailed => e
  raise InvalidScenario.new(location, "Parser failed: " + e.message)
end

#scan_data_dir(name) ⇒ Object

Scan data files.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pione/package/scenario-scanner.rb', line 58

def scan_data_dir(name)
  if (@location + name).exist?
    (@location + name).file_entries.each_with_object([]) do |entry, target|
      unless /^\./.match(entry.basename)
        target << File.join(name, entry.basename)
      end
    end
  else
    return []
  end
end