biopsy 0.1.0.alpha → 0.1.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/lib/biopsy/domain.rb DELETED
@@ -1,156 +0,0 @@
1
- # todo: ensure testing accounts for situation where there are multiple
2
- # input or output files defined in the spec
3
- module Biopsy
4
-
5
- class DomainLoadError < Exception
6
- end
7
-
8
- class Domain
9
-
10
- attr_reader :name
11
- attr_reader :input_filetypes
12
- attr_reader :output_filetypes
13
- attr_reader :objectives
14
- attr_reader :keep_intermediates
15
- attr_reader :gzip_intermediates
16
-
17
- require 'yaml'
18
- require 'pp'
19
-
20
- # Return a new Domain object containing the specification of the
21
- # currently active domain.
22
- def initialize domain=nil
23
- @name = domain.nil? ? self.get_current_domain : domain
24
-
25
- @keep_intermediates = false
26
- @gzip_intermediates = false
27
- self.load_by_name @name
28
- end
29
-
30
- # Return the name of the currently active domain.
31
- def get_current_domain
32
- Settings.instance.domain
33
- rescue
34
- raise "You must specify the domain to use in the biopsy settings file or at the command line."
35
- end
36
-
37
- # Return the path to the YAML definition file for domain with +:name+.
38
- # All +:domain_dirs+ in Settings are searched and the first matching
39
- # file is returned.
40
- def locate_definition name
41
- Settings.instance.locate_config :domain_dir, name
42
- end
43
-
44
- # Check and apply the settings in +:config+ (Hash).
45
- def apply_config config
46
- [:input_filetypes, :output_filetypes, :objectives].each do |key|
47
- raise DomainLoadError.new("Domain definition is missing the required key #{key}") unless config.has_key? key
48
- self.instance_variable_set('@' + key.to_s, config[key])
49
- end
50
- end
51
-
52
- # Load and apply the domain definition with +:name+
53
- def load_by_name name
54
- path = self.locate_definition name
55
- raise DomainLoadError.new("Domain definition file does not exist for #{name}") if path.nil?
56
- config = YAML::load_file(path)
57
- raise DomainLoadError.new("Domain definition file #{path} is not valid YAML") if config.nil?
58
- self.apply_config config.deep_symbolize
59
- end
60
-
61
- # Validate a Target, returning true if the target meets
62
- # the specification of this Domain, and false otherwise.
63
- # +:target+, the Target object to validate.
64
- def target_valid? target
65
- l = []
66
- @input_filetypes.each do |input|
67
- l << [target[:input_files], input]
68
- end
69
- @output_filetypes.each do |output|
70
- l << [target[:output_files], output]
71
- end
72
- errors = []
73
- l.each do |pair|
74
- testcase, definition = pair
75
- errors += self.validate_target_filetypes(testcase, definition)
76
- end
77
- errors
78
- end
79
-
80
- # Returns an empty array if +:testcase+ conforms to definition,
81
- # otherwise returns an array of strings describing the
82
- # errors found.
83
- def validate_target_filetypes testcase, definition
84
- errors = []
85
- # check extensions
86
- testcase.each_pair do |key, f|
87
- ext = File.extname(f)
88
- unless definition[:allowed_extensions].include? ext
89
- errors << %Q{input file #{f} doesn't match any of the filetypes
90
- allowed for this domain}
91
- end
92
- end
93
- # check number of files
94
- in_count = testcase.size
95
- if definition.has_key? :n
96
- unless in_count == definition[:n]
97
- errors << %Q{the number of input files (#{in_count}) doesn't
98
- match the domain specification (#{definition[:n]})}
99
- end
100
- end
101
- if definition.has_key? :min
102
- unless in_count >= definition[:min]
103
- errors << %Q{the number of input files (#{in_count}) is lower
104
- than the minimum for this domain (#{definition[:n]})}
105
- end
106
- end
107
- if definition.has_key? :max
108
- unless in_count >= definition[:max]
109
- errors << %Q{the number of input files (#{in_count}) is greater
110
- than the maximum for this domain (#{definition[:n]})}
111
- end
112
- end
113
- errors
114
- end
115
-
116
- # Write out a template Domain definition to +:filename+
117
- def write_template filename
118
- data = {
119
- :input_filetypes => [
120
- {
121
- :min => 1,
122
- :max => 2,
123
- :allowed_extensions => [
124
- 'txt',
125
- 'csv',
126
- 'tsv'
127
- ]
128
- },
129
- {
130
- :n => 2,
131
- :allowed_extensions => [
132
- 'png'
133
- ]
134
- }
135
- ],
136
- :output_filetypes => [
137
- {
138
- :n => 1,
139
- :allowed_extensions => [
140
- 'pdf',
141
- 'xls'
142
- ]
143
- }
144
- ],
145
- :objectives => [
146
- 'objective1', 'objective2'
147
- ]
148
- }
149
- ::File.open(filename, 'w') do |f|
150
- f.puts data.to_yaml
151
- end
152
- end
153
-
154
- end # end of class Domain
155
-
156
- end # end of module Biopsy
data/test/test_domain.rb DELETED
@@ -1,61 +0,0 @@
1
- require 'helper'
2
-
3
- class TestDomain < Test::Unit::TestCase
4
-
5
- require 'fileutils'
6
-
7
- context "Domain" do
8
-
9
- setup do
10
- @h = Helper.new
11
- @h.setup_tmp_dir
12
-
13
- # we need a domain
14
- @h.setup_domain
15
- domain_name = @h.create_valid_domain
16
- @domain = Biopsy::Domain.new domain_name
17
- end
18
-
19
- teardown do
20
- @h.cleanup
21
- end
22
-
23
- should "be able to find the current domain" do
24
- assert_equal 'test_domain', @domain.get_current_domain
25
- end
26
-
27
- should "be able to find a definition" do
28
- assert_equal @h.domain_path, @domain.locate_definition('test_domain')
29
- end
30
-
31
- should "fail to find a non-existent definition" do
32
- assert_equal nil, @domain.locate_definition('fake_filename')
33
- end
34
-
35
- should "reject any invalid config" do
36
- # generate all trivial invalid configs
37
- @h.domain_data.keys.each do |key|
38
- d = @h.domain_data.clone
39
- d.delete key
40
- filepath = File.join(@h.domain_dir, 'broken_thing.yml')
41
- File.open(filepath, 'w') do |f|
42
- f.puts d.to_yaml
43
- end
44
-
45
- assert_raise Biopsy::DomainLoadError do
46
- @domain.load_by_name 'broken_thing'
47
- end
48
-
49
- File.delete filepath if File.exists? filepath
50
- end
51
- end
52
-
53
- should "write a template that can be loaded as a valid definition" do
54
- @domain.write_template File.join(@h.domain_dir, 'template.yml')
55
- @domain.load_by_name 'template'
56
- assert_equal ['objective1', 'objective2'], @domain.objectives
57
- end
58
-
59
- end # Domain context
60
-
61
- end # TestDomain