Module: When::Events::DataSet::SparqlRepository

Defined in:
lib/when_exe/events.rb

Overview

外部 SPARQL サーバーを利用する場合に動作を置き換える

Instance Method Summary collapse

Instance Method Details

#event(uri, graph = nil) ⇒ Hash<String(GraphURI)=>RDF:Repository>

指定の URI を主語とする Statement からなる RDF:Repository を生成する

Parameters:

  • uri (String)

    主語の URI

  • graph (String) (defaults to: nil)

    検索対象のグラフ

Returns:

  • (Hash<String(GraphURI)=>RDF:Repository>)

    生成した Repository の Hash



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/when_exe/events.rb', line 915

def event(uri, graph=nil)
  rep     = Hash.new {|hash,key| hash[key] = ::RDF::Repository.new}
  subject = @resource[uri]

  # 問い合わせ文字列を準備する
  query_string  = "SELECT DISTINCT ?predicate ?object \n"
  query_string << "FROM <#{graph}> \n" if /./ =~ graph
  query_string << "WHERE { \n <#{uri}> ?predicate ?object . \n} \n"
# return query_string

  # 問い合わせを実行する
  client = ::SPARQL::Client.new(@endpoint)
  client.query(query_string).each do |solution|
    statement = ::RDF::Statement.new(subject, solution[:predicate], solution[:object])
    rep[''].insert(statement)
    rep[graph].insert(statement) if graph
  end
  rep
end

#intersection_events(options) ⇒ Array<When::Events::Event>

指定の条件を満たす Event の Array を返す

Parameters:

  • options (Hash)

    以下の通り

Options Hash (options):

  • 'date' (String)

    日付範囲

  • 'contributor' (String)

    情報提供者

  • 'graph' (String)

    グラフ

  • 'group' (String)

    グループ

  • 'location' (String)

    空間位置

  • 'keyword' (String)

    キーワード

  • 'limit' (String or Integer)

    最大取得数

  • 'offset' (String or Integer)

    取得開始レコード

  • 'count' (Boolean)

    イベント数のみの確認か?

Returns:



951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/when_exe/events.rb', line 951

def intersection_events(options)

  # 問い合わせ文字列を準備する
  rdf_keys   = Hash.new {|hash,key| hash[key]=[]}
  role_keys  = @role.keys - [LABEL, REFERENCE, WHAT_DAY]
  role_keys.each do |role_key|
    @role[role_key][:target].scan(/<(.+?)>/) {rdf_keys[$1] << role_key}
  end
  keywords   = (/./ =~ options['keyword']) ? options['keyword'].split('*') : []
  loc_edges  = @order.key?(WEST) || @order.key?(SOUTH) || @order.key?(BOTTOM)
  triples    = []
  predicates = {}
  rdf_keys.each_pair do |rdf_key, role_key|
    if role_key.include?(SUBJECT)
      predicates['?s'] = rdf_key
    elsif role_key.include?(HAS_PART)
      keywords.each do |keyword|
        triples << "?s <#{rdf_key}> \"#{keyword}\" . "
      end
    elsif role_key.include?(CONTRIBUTOR) && /\A[^!]/ =~ options['contributor']
      triples << "?s <#{rdf_key}> \"#{options['contributor']}\" . "
    elsif role_key.include?(GROUP) && /./ =~ options['group']
      triples << "?s <#{rdf_key}> \"#{options['group']}\" . "
    elsif role_key.include?(SPATIAL) && /./ =~ options['location'] && !loc_edges
      triples << "?s <#{rdf_key}> \"#{options['location']}\" . "
    else
      object = "?o#{predicates.size}"
      predicates[object] = rdf_key
      triples << "?s <#{rdf_key}> #{object} . "
    end
  end
  select  = options['count'] ?
              "SELECT DISTINCT (COUNT(*) AS ?count) " :
              "SELECT DISTINCT #{predicates.keys.join(' ')} "
  filters = []
  filters.concat(range_filter(When.date_or_era(options['date']), predicates, rdf_keys, 'valid', options['date'])) if /./ =~ options['date']
  filters.concat(regex_filter(options['contributor'], predicates, rdf_keys, 'contributor'))
  if /./ =~ options['location'] && loc_edges
    location = When.where?(options['location'])
    filters.concat(range_filter(location.long, predicates, rdf_keys, 'longitude')) if @order.key?(WEST)
    filters.concat(range_filter(location.lat,  predicates, rdf_keys, 'latitude' )) if @order.key?(SOUTH)
    filters.concat(range_filter(location.alt,  predicates, rdf_keys, 'altitude' )) if @order.key?(BOTTOM)
  end
  query_string  = "#{select}\n"
  query_string << "FROM <#{options['graph']}> \n" if @role.key?(GRAPH) && /./ =~ options['graph']
  query_string << "WHERE { \n #{triples.map {|t| t+"\n "}.join('')}"
  query_string << " FILTER ( \n  #{filters.map {|f| ' '+f}.join(" && \n  ")}\n  ) \n" unless filters.empty?
  query_string << "} \n"
  query_string << "ORDER BY #{attr2var(@order.key?(START) ? 'start' : 'valid', predicates, rdf_keys)} \n" unless options['count']
  query_string << "LIMIT  #{options['limit']} \n"  if options['limit' ].to_i > 1
  query_string << "OFFSET #{options['offset']} \n" if options['offset'].to_i > 1
  return query_string if options['debug']

  # 問い合わせを実行する
  client = ::SPARQL::Client.new(@endpoint)
  if options['count']
    # イベント数のみ確認する
    client.query(query_string).each do |solution|
      return solution[:count].to_i
    end
    return 0

  else
    # イベントの内容を取り出す
    client.query(query_string).map {|solution|
      event = Hash[*predicates.keys.map {|predicate|
                object = solution[predicate[1..-1].to_sym]
                [predicates[predicate],
                 case object
                 when ::RDF::Literal::Integer ; object.to_i
                 when ::RDF::Literal::Numeric ; object.to_f
                 else object.to_s
                 end]
              }.flatten]
      rdf_keys.each_pair do |rdf_key, role_key|
        next if event.key?(rdf_key)
        if role_key.include?(HAS_PART)
          event[rdf_key] = keywords
        elsif role_key.include?(CONTRIBUTOR)
          event[rdf_key] = options['contributor']
        elsif role_key.include?(GRAPH)
          event[rdf_key] = (/./ =~ options['graph'] ? options['graph'] : @default_graph)
        elsif role_key.include?(SPATIAL)
          event[rdf_key] = options['location']
        end
      end
      uri = event[predicates['?s']]
      id  = uri[@default_graph.length..-1] if @default_graph && uri.index(@default_graph)
      Event.new(self, id || uri, event)
    }
  end
end