Module: When::Events::DataSet::SparqlRepository
- Defined in:
- lib/when_exe/events.rb
Overview
外部 SPARQL サーバーを利用する場合に動作を置き換える
Instance Method Summary collapse
-
#event(uri, graph = nil) ⇒ Hash<String(GraphURI)=>RDF:Repository>
指定の URI を主語とする Statement からなる RDF:Repository を生成する.
-
#intersection_events(options) ⇒ Array<When::Events::Event>
指定の条件を満たす Event の Array を返す.
Instance Method Details
#event(uri, graph = nil) ⇒ Hash<String(GraphURI)=>RDF:Repository>
指定の URI を主語とする Statement からなる RDF:Repository を生成する
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 を返す
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() # 問い合わせ文字列を準備する 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 = (/./ =~ ['keyword']) ? ['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[^!]/ =~ ['contributor'] triples << "?s <#{rdf_key}> \"#{['contributor']}\" . " elsif role_key.include?(GROUP) && /./ =~ ['group'] triples << "?s <#{rdf_key}> \"#{['group']}\" . " elsif role_key.include?(SPATIAL) && /./ =~ ['location'] && !loc_edges triples << "?s <#{rdf_key}> \"#{['location']}\" . " else object = "?o#{predicates.size}" predicates[object] = rdf_key triples << "?s <#{rdf_key}> #{object} . " end end select = ['count'] ? "SELECT DISTINCT (COUNT(*) AS ?count) " : "SELECT DISTINCT #{predicates.keys.join(' ')} " filters = [] filters.concat(range_filter(When.date_or_era(['date']), predicates, rdf_keys, 'valid', ['date'])) if /./ =~ ['date'] filters.concat(regex_filter(['contributor'], predicates, rdf_keys, 'contributor')) if /./ =~ ['location'] && loc_edges location = When.where?(['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 <#{['graph']}> \n" if @role.key?(GRAPH) && /./ =~ ['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 ['count'] query_string << "LIMIT #{['limit']} \n" if ['limit' ].to_i > 1 query_string << "OFFSET #{['offset']} \n" if ['offset'].to_i > 1 return query_string if ['debug'] # 問い合わせを実行する client = ::SPARQL::Client.new(@endpoint) if ['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] = ['contributor'] elsif role_key.include?(GRAPH) event[rdf_key] = (/./ =~ ['graph'] ? ['graph'] : @default_graph) elsif role_key.include?(SPATIAL) event[rdf_key] = ['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 |