Class: When::Events::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/when_exe/events.rb

Overview

時間座標を持つイベント

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, id, row) ⇒ Event

イベントの生成

Parameters:

  • dataset (When::Events::DataSet)

    所属する一言語対応データセット

  • id (Integer)

    通し番号

  • row (Array<String>)

    CSVの一行分

  • row (Hash<String=>Object>)

    RDFの1クエリ分



1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'lib/when_exe/events.rb', line 1818

def initialize(dataset, id, row)
  @dataset = dataset
  @id      = id
  @row     = row
  @role    = {}
  @rdf     = {}
  @csv     = {}
  @order   = {}

  case row
  when Hash
    @rdf = row
  when Array
    row.each_with_index do |item, index|
      @csv[(index+1).to_s] = item
    end

    dataset.rdf.each_pair do |item, definition|
      fields = []
      format = definition[:target].gsub(/\[(\d+)\]/) {
        field  =  row[$1.to_i-1]
        field  =  escape(field, definition[:type])
        fields << field
        '%s'
      }
      @rdf[item] = definition[:operation].call(self, format == '%s' ? fields.first : format % fields)
    end
  end

  @role[ID] = @rdf[ID] = id
  dataset.role.each_pair do |item, definition|
    fields = []
    format = definition[:target].gsub('%','%%').gsub(/\[(\d+)\]|<(.+?)>|\{(.+?)\}/) {|match|
      key  = $1 || $2 || $3
      field = case match
              when /\A\[/ ; row[key.to_i-1]
              when /\A</  ; @rdf[key]
              when /\A\{/ ; @role[key]
              end
      field  =  escape(field, definition[:type])
      fields << field
      '%s'
    }
    pre_operation = DataSet::Operations[item]
    @role[item] = 
      if pre_operation
        definition[:operation].call(self, pre_operation.call(self, format == '%s' ? fields.first : format % fields))
      else
        definition[:operation].call(self, format == '%s' ? fields.first : format % fields)
      end
  end

  @date = @role[VALID]
end

Instance Attribute Details

#csvHash<String(番号)=>Object>

CSV変数

Returns:

  • (Hash<String(番号)=>Object>)


1664
1665
1666
# File 'lib/when_exe/events.rb', line 1664

def csv
  @csv
end

#datasetWhen::Events::DataSet (readonly)

所属する一言語対応データセット



1627
1628
1629
# File 'lib/when_exe/events.rb', line 1627

def dataset
  @dataset
end

#dateWhen::TM::TemporalPosition or When::Events::Range

日付または日付範囲



1687
1688
1689
# File 'lib/when_exe/events.rb', line 1687

def date
  @date
end

#idInteger (readonly)

通し番号

Returns:

  • (Integer)


1634
1635
1636
# File 'lib/when_exe/events.rb', line 1634

def id
  @id
end

#orderHash<String(prefix:name)=>Array(Integer(id))> (readonly)

当該順序キーで何番目のイベントか

Returns:



1672
1673
1674
# File 'lib/when_exe/events.rb', line 1672

def order
  @order
end

#originalWhen::Events::Event

deep copy 時の元イベント

Returns:



1679
1680
1681
# File 'lib/when_exe/events.rb', line 1679

def original
  @original
end

#rdfHash<String(prefix:name)=>Object>

RDF変数

Returns:

  • (Hash<String(prefix:name)=>Object>)


1656
1657
1658
# File 'lib/when_exe/events.rb', line 1656

def rdf
  @rdf
end

#roleHash<String(prefix:name)=>Object>

ロール変数

Returns:

  • (Hash<String(prefix:name)=>Object>)


1648
1649
1650
# File 'lib/when_exe/events.rb', line 1648

def role
  @role
end

#rowArray<String> (readonly)

CSVの一行分

Returns:



1641
1642
1643
# File 'lib/when_exe/events.rb', line 1641

def row
  @row
end

Instance Method Details

#abstract(item = ABSTRACT, method = :role) ⇒ String

Note:

ブロックを渡された場合、そのブロックに{}部分のマークアップを依頼する

指定の情報を{}部分のマークアップ処理など行って整形して返す

Parameters:

  • item (String) (defaults to: ABSTRACT)

    返す情報の名称

  • method (Symbol) (defaults to: :role)

    返す情報が属するグループ(:role, :rdf, :csv)

Returns:



1711
1712
1713
1714
1715
1716
# File 'lib/when_exe/events.rb', line 1711

def abstract(item=ABSTRACT, method=:role)
  send(method)[item].gsub(/(\{+)(.*?)(\}+)/) {
    bra, word, cket = $~[1..3]
    '{'*(bra.length/2) + (block_given? ? yield(word) : word) + '}'*(cket.length/2)
  }
end

#deep_copyWhen::Events::Event

イベントの複製

Returns:



1801
1802
1803
1804
1805
1806
1807
1808
# File 'lib/when_exe/events.rb', line 1801

def deep_copy
  result          = self.dup
  result.csv      = @csv.dup
  result.rdf      = @rdf.dup
  result.role     = @role.dup
  result.original = self
  result
end

#each_wordObject

HAS_PART対象の文字列中の{}で囲まれた語に対して yield で指定された処理を行う



1693
1694
1695
1696
1697
1698
1699
# File 'lib/when_exe/events.rb', line 1693

def each_word
  @role[HAS_PART].scan(/(\{+)(.*?)(\}+)/) do
    bra, word, cket = $~[1..3]
    next unless bra.length.odd? && cket.length.odd?
    yield(word)
  end
end

#groupString

Returns:



1741
1742
1743
# File 'lib/when_exe/events.rb', line 1741

def group
  @role[GROUP]
end

#source(item = SOURCE, method = :role) ⇒ Array<String(IRI), String(説明)>

指定の情報のIRIとその説明を返す

Parameters:

  • item (String) (defaults to: SOURCE)

    返す情報の名称

  • method (Symbol) (defaults to: :role)

    返す情報が属するグループ(:role, :rdf, :csv)

Returns:



1726
1727
1728
1729
1730
1731
1732
1733
1734
# File 'lib/when_exe/events.rb', line 1726

def source(item=SOURCE, method=:role)
  iri = send(method)[item]
  return [nil, iri] unless /:\/\// =~ iri
  @dataset.prefix_description.each do |description|
    index = iri.index(description[0])
    return [iri, description[1]] if index && index == 0
  end
  [iri]
end

#statementsArray<RDF::Statement>

自身を主語とする RDF::Statement の Array を返す

Returns:

  • (Array<RDF::Statement>)


1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
# File 'lib/when_exe/events.rb', line 1750

def statements
  unless @statements
    @statements = []
    raise ArgumentError, 'Role for rdf:subject not defined' unless @role.key?(SUBJECT)
    subject = @dataset.resource[@role[SUBJECT]]
    @role.each_pair do |predicate, object|
      case predicate
      when SUBJECT, ID, GRAPH, WHAT_DAY
        # Do nothing
      when HAS_PART
        if @role[HAS_PART].kind_of?(Array)
          words = @role[HAS_PART]
        else
          words = []
          each_word do |word|
            words << word
          end
          words.uniq!
        end
        words.each do |word|
          @statements << ::RDF::Statement(subject, @dataset.resource[predicate], word)
        end
      when VALID
        if object.kind_of?(Range)
          object = object.original
        elsif object.respond_to?(:to_uri)
          object = object.to_uri
        end
        @statements << ::RDF::Statement(subject, @dataset.resource[predicate], object)
    # when ABSTRACT
    #   @statements << ::RDF::Statement(subject, @dataset.resource[predicate], abstract)
      else
        if [URI, IRI].include?(@dataset.role[predicate][:type])
          object = @dataset.resource[object]
        elsif object.respond_to?(:to_uri)
          object = object.to_uri
        elsif @dataset.role[predicate][:lang] != '' && object.kind_of?(String)
          object = ::RDF::Literal.new(object, {:language=>@dataset.role[predicate][:lang]})
        end
        @statements << ::RDF::Statement(subject, @dataset.resource[predicate], object)
      end
    end
  end
  @statements
end