spread2rdf 0.0.1pre.1 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +8 -8
  2. data/Gemfile.ocra +6 -0
  3. data/VERSION +1 -1
  4. data/bin/spread2rdf +1 -1
  5. data/lib/spread2rdf/attributes.rb +1 -0
  6. data/lib/spread2rdf/cli.rb +128 -34
  7. data/lib/spread2rdf/coord.rb +49 -0
  8. data/lib/spread2rdf/mapping/cell.rb +105 -0
  9. data/lib/spread2rdf/mapping/column.rb +63 -0
  10. data/lib/spread2rdf/mapping/column_block.rb +23 -0
  11. data/lib/spread2rdf/mapping/default_cell_mappings.rb +26 -0
  12. data/lib/spread2rdf/mapping/element.rb +64 -0
  13. data/lib/spread2rdf/mapping/resource.rb +95 -0
  14. data/lib/spread2rdf/mapping/sheet.rb +80 -0
  15. data/lib/spread2rdf/mapping/spreadsheet.rb +56 -0
  16. data/lib/spread2rdf/mapping/statement.rb +22 -0
  17. data/lib/spread2rdf/mapping/worksheet.rb +12 -0
  18. data/lib/spread2rdf/namespace.rb +6 -2
  19. data/lib/spread2rdf/roo_helper.rb +45 -0
  20. data/lib/spread2rdf/schema/column.rb +37 -0
  21. data/lib/spread2rdf/{spreadsheet/sub_sheet.rb → schema/column_block.rb} +3 -2
  22. data/lib/spread2rdf/{spreadsheet → schema}/element.rb +12 -16
  23. data/lib/spread2rdf/schema/schema.rb +19 -0
  24. data/lib/spread2rdf/schema/sheet.rb +87 -0
  25. data/lib/spread2rdf/schema/sheet_dsl.rb +54 -0
  26. data/lib/spread2rdf/schema/spreadsheet.rb +49 -0
  27. data/lib/spread2rdf/schema/spreadsheet_dsl.rb +42 -0
  28. data/lib/spread2rdf/schema/statement_mapping_schema.rb +26 -0
  29. data/lib/spread2rdf/schema/worksheet.rb +47 -0
  30. data/lib/spread2rdf.rb +34 -2
  31. data/ontologies/unit-v1.1.ttl +8330 -0
  32. data/spread2rdf.gemspec +4 -2
  33. metadata +50 -26
  34. data/lib/spread2rdf/helper.rb +0 -14
  35. data/lib/spread2rdf/spreadsheet/column.rb +0 -48
  36. data/lib/spread2rdf/spreadsheet/column_mapping_context.rb +0 -156
  37. data/lib/spread2rdf/spreadsheet/coord.rb +0 -51
  38. data/lib/spread2rdf/spreadsheet/mapping_context.rb +0 -67
  39. data/lib/spread2rdf/spreadsheet/mapping_dsl.rb +0 -23
  40. data/lib/spread2rdf/spreadsheet/sheet.rb +0 -128
  41. data/lib/spread2rdf/spreadsheet/sheet_dsl.rb +0 -34
  42. data/lib/spread2rdf/spreadsheet/sheet_mapping_context.rb +0 -90
  43. data/lib/spread2rdf/spreadsheet/sub_sheet_mapping_context.rb +0 -55
  44. data/lib/spread2rdf/spreadsheet/worksheet.rb +0 -49
  45. data/lib/spread2rdf/spreadsheet.rb +0 -92
@@ -1,90 +0,0 @@
1
- module Spread2RDF
2
- class Spreadsheet
3
- class Sheet
4
- class MappingContext < Spreadsheet::MappingContext
5
-
6
- self.attributes = {
7
- row_range: nil
8
- }
9
-
10
- alias sheet element
11
-
12
- def initialize(sheet, parent_context = nil, attr = {})
13
- super
14
- @objects = ( sheet.columns - [ sheet.subject_column ] ).map do |column|
15
- column.map(row_range, self).compact.presence
16
- end.compact
17
- return if @objects.empty?
18
- subject_description
19
- statements_to_object
20
- worksheet.graph << self.graph
21
- end
22
-
23
- ########################################################################
24
- # subject mapping
25
-
26
- # TODO: every new context instance (for the same cell) returns a different bnode, it must be stored ...
27
- def subject
28
- @subject ||= case subject_mapping_mode
29
- when :bnode then RDF::Node.new
30
- when :from_column then subject_resource_from_column
31
- else raise 'unknown subject mapping type'
32
- end
33
- end
34
- alias subject_resource subject
35
-
36
- def subject_resource_type
37
- sheet.subject.try(:fetch, :type, nil) or
38
- (sheet.subject.try(:fetch, :sub_class_of, nil) && RDF::RDFS.Class) or
39
- nil
40
- end
41
-
42
- def subject_namespace
43
- subject_namespace_name =
44
- sheet.subject.try(:fetch, :uri, nil).try(:fetch, :namespace, nil)
45
- Namespace.resolve_to_namespace(subject_namespace_name)
46
- end
47
-
48
- private
49
-
50
- def subject_mapping_mode
51
- case
52
- when ( sheet.subject.try(:fetch, :uri, nil) || sheet.subject ) == :bnode
53
- :bnode
54
- else
55
- :from_column
56
- end
57
- end
58
-
59
- def subject_name_suffix
60
- cells = row_range.map do |row|
61
- cell_value(row: row, column: sheet.subject_column.coord).presence
62
- end.compact
63
- raise "no subject found for #{sheet} in #{row_range}" if cells.empty?
64
- raise "multiple subjects found for #{sheet} in #{row_range}: #{cells.inspect}" if cells.count > 1
65
- cells.first
66
- end
67
-
68
- def subject_resource_from_column
69
- namespace = subject_namespace
70
- subject_suffix = Helper.resource_name(subject_name_suffix)
71
- #puts "subject resource for #{sheet} in #{range}: " + RDF::URI.new("#{namespace}#{subject_suffix}" )
72
- RDF::URI.new("#{namespace}#{subject_suffix}")
73
- end
74
-
75
- def subject_description
76
- type = subject_resource_type
77
- statement(subject, RDF.type, type) unless type.nil?
78
- if type == RDF::RDFS.Class &&
79
- super_class = sheet.subject.try(:fetch, :sub_class_of, nil)
80
- statement(subject, RDF::RDFS.subClassOf, super_class)
81
- end
82
- end
83
-
84
- def statements_to_object
85
- end
86
-
87
- end
88
- end
89
- end
90
- end
@@ -1,55 +0,0 @@
1
- module Spread2RDF
2
- class Spreadsheet
3
- class SubSheet
4
- class MappingContext < Sheet::MappingContext
5
-
6
- self.attributes = {
7
- }
8
-
9
- alias sub_sheet element
10
- alias column_block element
11
-
12
- private
13
-
14
- ##########################################################################
15
- # Statement mapping
16
- # TODO: Duplication Column::MappingContext ! Share it ?
17
-
18
- def statement_mapping_mode
19
- case
20
- when column_block.statement == :none then :ignore
21
- when column_block.statement == :none then :ignore
22
- when column_block.predicate.nil? then :ignore
23
- when restriction_mode then :restriction
24
- else :default
25
- end
26
- end
27
-
28
- def restriction_mode
29
- restriction_mode = column_block.statement
30
- case restriction_mode
31
- when :restriction then RDF::OWL.hasValue
32
- when Hash then restriction_mode[:restriction]
33
- else nil
34
- end
35
- end
36
-
37
- def statements_to_object
38
- case statement_mapping_mode
39
- when :default
40
- statement(parent_context.subject, column_block.predicate, subject)
41
- when :restriction
42
- restriction_class = RDF::Node.new
43
- statements(
44
- [ parent_context.subject, RDF::RDFS.subClassOf, restriction_class ],
45
- [ restriction_class, RDF.type, RDF::OWL.Restriction ],
46
- [ restriction_class, RDF::OWL.onProperty, column_block.predicate ],
47
- [ restriction_class, restriction_mode, subject ]
48
- )
49
- end
50
- end
51
-
52
- end
53
- end
54
- end
55
- end
@@ -1,49 +0,0 @@
1
- module Spread2RDF
2
- class Spreadsheet
3
- class Worksheet < Sheet
4
-
5
- self.attributes = {
6
- }
7
-
8
- attr_reader :cell_mapping
9
- attr_reader :graph
10
-
11
- def initialize(parent, options={}, &block)
12
- super
13
- @cell_mapping = {}
14
- @graph = RDF::Repository.new
15
- end
16
-
17
- def init
18
- index_columns!
19
- end
20
-
21
- =begin
22
- def cell_mapping_by_name(name)
23
-
24
- end
25
-
26
- def cell_mapping_by_coord(coord)
27
-
28
- end
29
- =end
30
-
31
- def index_columns!
32
- column_index = start_coord.column_as_number
33
- each_column do |column|
34
- column.instance_variable_set :@coord,
35
- Roo::Base.number_to_letter(column_index)
36
- column_index += 1
37
- end
38
- end
39
-
40
- def row_range
41
- range = roo { (Coord[start].row .. spreadsheet.roo.last_row) }
42
- range.begin <= range.end ? range : nil
43
- end
44
-
45
- private
46
-
47
- end
48
- end
49
- end
@@ -1,92 +0,0 @@
1
- require 'spread2rdf/spreadsheet/coord'
2
- require 'spread2rdf/spreadsheet/element'
3
- require 'spread2rdf/spreadsheet/mapping_context'
4
- require 'spread2rdf/spreadsheet/sheet'
5
- require 'spread2rdf/spreadsheet/sub_sheet'
6
- require 'spread2rdf/spreadsheet/worksheet'
7
- require 'spread2rdf/spreadsheet/sheet_mapping_context'
8
- require 'spread2rdf/spreadsheet/sub_sheet_mapping_context'
9
- require 'spread2rdf/spreadsheet/column'
10
- require 'spread2rdf/spreadsheet/column_mapping_context'
11
-
12
- require 'spread2rdf/spreadsheet/sheet_dsl'
13
- require 'spread2rdf/spreadsheet/mapping_dsl'
14
-
15
- module Spread2RDF
16
- class Spreadsheet
17
-
18
- attr_reader :name
19
- attr_reader :worksheet
20
- attr_reader :worksheet_mapping
21
- attr_reader :input_file
22
- attr_reader :roo
23
-
24
- def initialize(name, &block)
25
- @name = name
26
- @worksheet = {}
27
- @worksheet_mapping = {}
28
- @schema_spec = block
29
- end
30
-
31
- def spreadsheet
32
- self
33
- end
34
-
35
- def worksheets
36
- @worksheet.values
37
- end
38
-
39
- def templates
40
- @template.values
41
- end
42
-
43
- def read(filename)
44
- @input_file = filename
45
- load_roo
46
- load_schema
47
- load_resources
48
- self
49
- end
50
-
51
- def graph
52
- graph = RDF::Repository.new
53
- worksheets.each { |worksheet| graph << worksheet.graph }
54
- graph
55
- end
56
- alias to_rdf graph
57
-
58
- private
59
-
60
- # TODO: make this work with other spreadsheets than Excel
61
- def load_roo
62
- options = {}
63
- options[:packed], options[:file_warning] = :zip, :ignore if
64
- File.extname(@input_file).downcase == '.xlsm'
65
- @roo = Roo::Excelx.new(@input_file, options)
66
- end
67
-
68
- def load_schema
69
- Spreadsheet::MappingDSL.new(self).instance_exec(&@schema_spec)
70
- worksheets.each { |worksheet| worksheet.init }
71
- end
72
-
73
- def load_resources
74
- worksheets.each do |worksheet|
75
- next if worksheet.column.empty?
76
- worksheet.map
77
- end
78
- end
79
-
80
- class << self
81
- def definition(*args, &block)
82
- definitions << new(*args, &block)
83
- end
84
- private :new
85
-
86
- def definitions
87
- @@definitions ||= []
88
- end
89
- end
90
-
91
- end
92
- end