marcelotto / spread2rdf

DSL-based converter for spreadsheets to RDF
http://rubygems.org/gems/spread2rdf
MIT License
4 stars 2 forks source link

Custom Ruby blocks not executed for subject columns #12

Open petasis opened 6 years ago

petasis commented 6 years ago

Hi all,

I want from a single cell to generate a subject (from combining a namespace and cell contents), and also add a property (like label) with the cell contents. How this can be done?

Adding a custom code block on subject columns, is not respected, the code is not executed. What worked for me, was to define a new worksheet (for the same excel sheet), and abuse the next column like this:

worksheet 'Students',
     name: :Lesson,
     start: :E2,
     subject: {
       uri: { namespace: ELOD },
       type: ELOD.Lesson
     } do
       column :uri
       column :skip do |value|
         suffix = Mapping::Cell::Default.uri_normalization(value_of_column(:uri))
         uri = RDF::URI.new("#{ELOD}#{suffix}")
         statement( uri, RDF::RDFS.label, RDF::Literal.new(value_of_column(:uri), language: :el))
     end
   end

Is there a way to do this?

petasis commented 6 years ago

I have modified in mapping/resource_creation.rb, resource_from_suffix to:

    def resource_from_suffix
        namespace = schema.resource_creation_namespace
        begin
          suffix = Mapping::Cell::Default.uri_normalization(resource_creation_value)
          rescue
            ## We failed in finding a value from a cell. If a column block,
            ## concat all values...
            if !parent.is_a? ColumnBlock then
              raise
            end
            ## Get all values from all columns in the block
            cells = row_range.map do |row|
              schema.columns.map {|x| parent.cell_value(row: row, column: x.coord)}
            end
            raise "multiple subjects found for Resource in #{row_range} of #{sheet}: #{cells.inspect}" if cells.count > 1
            suffix = Mapping::Cell::Default.uri_normalization(cells.first.join('-'))
        end

        #puts "creating resource " + RDF::URI.new("#{namespace}#{suffix}" )
        RDF::URI.new("#{namespace}#{suffix}")
      end

What does this patch does? If you have defined a column_block like this:

  template :lesson_grade_columns do
     column_block :grade_obj,
       subject: { uri: {namespace: ELOD}, type: ELOD.Grade },
       predicate: ELOD.hasGrade do
         column :lesson, predicate: ELOD.forLesson, object: { language: 'el', datatype: XSD.string, uri: {namespace: ELOD} }
         column :grade,  predicate: ELOD.value, object: { datatype: XSD.integer }
       end
   end

where you want a URI to be generated, but you don't specify a column for the URI, all values (and the namespace) are used to define a URI. So, you don't end with a blank node...