Skip to content

Commit

Permalink
refactor code to put queries in their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
markwilkinson committed Aug 26, 2024
1 parent eb01e62 commit 4005d3b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 114 deletions.
124 changes: 124 additions & 0 deletions VP/vp-interface/lib/comon_queries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
class VP
NAMESPACES = "PREFIX ejpold: <http://purl.org/ejp-rd/vocabulary/>
PREFIX ejpnew: <https://w3id.org/ejp-rd/vocabulary#>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dc: <http://purl.org/dc/terms/>
".freeze
# VPCONNECTION = "ejpold:vpConnection ejpnew:vpConnection dcat:theme dcat:themeTaxonomy".freeze
# VPDISCOVERABLE = "ejpold:VPDiscoverable ejpnew:VPDiscoverable".freeze
# VPANNOTATION = "dcat:theme".freeze
VPCONNECTION = "ejpnew:vpConnection".freeze
VPDISCOVERABLE = "ejpnew:VPDiscoverable".freeze
VPANNOTATION = "dcat:theme".freeze

def find_discoverables_query(graph:)
SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact ?servicetype WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact }.
OPTIONAL{?s dc:type ?servicetype }.
}
")
graph.query(vpd)
end

def keyword_search_query(graph:, keyword:)
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact } .
{
VALUES ?searchfields { dc:title dc:description dc:keyword }
?s ?searchfields ?kw
FILTER(CONTAINS(lcase(?kw), '#{keyword}'))
}
}")
# warn "keyword search query #{vpd.to_sparql}"
# warn "graph is #{@graph.size}"
graph.query(vpd)

end

def ontology_search_query(graph:, uri:)
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact } .
{
?s dcat:theme ?theme .
FILTER(CONTAINS(str(?theme), '#{uri}'))
}
}")

graph.query(vpd)
end

def verbose_annotations_query(graph:)
# TODO: This does not respect vpdiscoverable...
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?annot WHERE
{ VALUES ?annotation { dcat:theme dcat:themeTaxonomy }
?s ?annotation ?annot .
}")
graph.query(vpd)
end

def keyword_annotations_query(graph:)
vpd = SPARQL.parse("
#{NAMESPACES}
select DISTINCT ?kw WHERE
{ VALUES ?searchfields { dc:keyword }
?s ?searchfields ?kw .
}")
graph.query(vpd)

end

def collect_data_services_query(graph:)
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?type WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
a dcat:DataService .
{
?s dc:type ?type .
}
}")
graph.query(vpd)
end

123 changes: 9 additions & 114 deletions VP/vp-interface/lib/vp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,11 @@
require_rel "ontologyservers"
require_rel "serviceoutput_processers"
require_rel "discoverable"
require_rel "common_queries"

class VP
attr_accessor :networkgraph, :vpconfig, :fdps, :aboutme

NAMESPACES = "PREFIX ejpold: <http://purl.org/ejp-rd/vocabulary/>
PREFIX ejpnew: <https://w3id.org/ejp-rd/vocabulary#>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dc: <http://purl.org/dc/terms/>
".freeze
# VPCONNECTION = "ejpold:vpConnection ejpnew:vpConnection dcat:theme dcat:themeTaxonomy".freeze
# VPDISCOVERABLE = "ejpold:VPDiscoverable ejpnew:VPDiscoverable".freeze
# VPANNOTATION = "dcat:theme".freeze
VPCONNECTION = "ejpnew:vpConnection".freeze
VPDISCOVERABLE = "ejpnew:VPDiscoverable".freeze
VPANNOTATION = "dcat:theme".freeze

@@thisvp = ""

def initialize(config: vpconfig)
Expand Down Expand Up @@ -86,27 +75,9 @@ def get_resources
end

def find_discoverables
@graph = networkgraph

vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact ?servicetype WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact }.
OPTIONAL{?s dc:type ?servicetype }.

}
")
discoverables = build_from_results(results: @graph.query(vpd))
results = find_discoverables_query(graph: networkgraph)
discoverables = build_from_results(results: results)
warn discoverables
discoverables
end
Expand All @@ -122,62 +93,15 @@ def ontology_search_shell(term:)
end

def keyword_search(keyword: "")
@graph = networkgraph
keyword = keyword.downcase
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact } .
{
VALUES ?searchfields { dc:title dc:description dc:keyword }
?s ?searchfields ?kw
FILTER(CONTAINS(lcase(?kw), '#{keyword}'))
}
}")
warn "keyword search query #{vpd.to_sparql}"
warn "graph is #{@graph.size}"
warn "results of query #{@graph.query(vpd)}"
discoverables = build_from_results(results: @graph.query(vpd))
results = keyword_search_query(graph: networkgraph, keyword: keyword)
discoverables = build_from_results(results: results)
warn discoverables
discoverables
end

def ontology_search(uri: "")
@graph = networkgraph
warn "in ontology search"
warn "parse start"
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?s ?t ?title ?contact WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
dc:title ?title ;
a ?t .
OPTIONAL{?s dcat:contactPoint ?c .
?c <http://www.w3.org/2006/vcard/ns#url> ?contact } .
{
?s dcat:theme ?theme .
FILTER(CONTAINS(str(?theme), '#{uri}'))
}
}")
warn "parse end"
warn "query start"
results = @graph.query(vpd)
warn "query end"

results = ontology_search_query(graph: networkgraph, uri: uri)
discoverables = build_from_results(results: results)
warn discoverables
discoverables
Expand All @@ -186,15 +110,8 @@ def ontology_search(uri: "")
def verbose_annotations
@graph = networkgraph
words = []
results = verbose_annotations_query(graph: networkgraph)

# TODO: This does not respect vpdiscoverable...
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?annot WHERE
{ VALUES ?annotation { dcat:theme dcat:themeTaxonomy }
?s ?annotation ?annot .
}")
results = @graph.query(vpd)
results.each do |res|
uri = res[:annot].to_s
word = ontology_annotations(uri: uri)
Expand All @@ -206,13 +123,7 @@ def verbose_annotations
# end of taxonomy parser

warn "\n\nSWITCH TO KEYWORDS\n\n"
vpd = SPARQL.parse("
#{NAMESPACES}
select DISTINCT ?kw WHERE
{ VALUES ?searchfields { dc:keyword }
?s ?searchfields ?kw .
}")
results = @graph.query(vpd)
results = keyword_annotations_query(graph: networkgraph)
results.each do |res|
next if res[:kw].to_s.empty?

Expand All @@ -227,24 +138,8 @@ def collect_data_services
if File.exist?("./cache/servicetypes.json")
services = thaw_servicetypes
else
graph = networkgraph
warn "in collect data services"
vpd = SPARQL.parse("
#{NAMESPACES}
SELECT DISTINCT ?type WHERE
{
VALUES ?connection { #{VPCONNECTION} }
VALUES ?discoverable { #{VPDISCOVERABLE} }
?s ?connection ?discoverable ;
a dcat:DataService .
{
?s dc:type ?type .
}
}")
results = graph.query(vpd)
results = collect_data_services_query(graph: networkgraph)
prehash = {}
results.each do |r|
type = r[:type].to_s
Expand Down

0 comments on commit 4005d3b

Please sign in to comment.