Skip to content

Commit 5ed98af

Browse files
authored
Merge pull request #253 from Simaris/246/construct_queries
Support for Construct Query
2 parents 0a44eb8 + 4d1ef8b commit 5ed98af

File tree

10 files changed

+177
-20
lines changed

10 files changed

+177
-20
lines changed

lib/jekyll-rdf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
require 'jekyll/drops/rdf_literal'
4444
require 'jekyll/drops/rdf_resource'
4545
require 'jekyll/drops/rdf_resource_class'
46+
require 'jekyll/drops/rdf_graph'
4647
require 'jekyll/exceptions/NoPrefixMapped'
4748
require 'jekyll/exceptions/NoPrefixesDefined'
4849
require 'jekyll/exceptions/UnMarkedUri'

lib/jekyll/drops/rdf_graph.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
##
2+
# MIT License
3+
#
4+
# Copyright (c) 2016 Elias Saalmann, Christian Frommert, Simon Jakobi,
5+
# Arne Jonas Präger, Maxi Bornmann, Georg Hackel, Eric Füg
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
26+
module Jekyll #:nodoc:
27+
module JekyllRdf #:nodoc:
28+
module Drops #:nodoc:
29+
class RdfGraph < Liquid::Drop
30+
attr_reader :graph
31+
32+
def initialize(graph)
33+
@graph = graph
34+
end
35+
36+
def to_s
37+
to_nquads
38+
end
39+
40+
def to_nquads
41+
result = @graph.statements.map{|state|
42+
state.to_nquads
43+
}.join("")
44+
return result
45+
end
46+
47+
def to_ntriples
48+
result = @graph.statements.map{|state|
49+
state.to_ntriples
50+
}.join("")
51+
return result
52+
end
53+
end
54+
end
55+
end
56+
end

lib/jekyll/filters/rdf_sparql_query.rb

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,14 @@ module Filter
3838
# * +query+ - the SPARQL query
3939
#
4040
def sparql_query(resource = nil, query)
41-
query = query.clone #sometimes liquid wont reinit static strings in for loops
42-
if(rdf_substitude_nil?(resource))
43-
query.gsub!('?resourceUri', "<#{Jekyll::JekyllRdf::Helper::RdfHelper::page.data['rdf'].term}>")
44-
elsif(resource.class <= Array)
45-
resource.each_with_index do |uri, index|
46-
return unless valid_resource?(uri)
47-
if(uri.class <= Jekyll::JekyllRdf::Drops::RdfResource)
48-
query.gsub!("?resourceUri_#{index}", uri.term.to_ntriples)
49-
else
50-
query.gsub!("?resourceUri_#{index}", "#{rdf_resolve_prefix(uri.to_s)}")
51-
end
52-
end
53-
else
54-
return unless valid_resource?(resource)
55-
query.gsub!('?resourceUri', to_string_wrap(resource))
56-
end if query.include? '?resourceUri' #the only purpose of the if statement is to substitute ?resourceUri
57-
unless Jekyll::JekyllRdf::Helper::RdfHelper::prefixes["rdf_prefixes"].nil?
58-
query = query.prepend(" ").prepend(Jekyll::JekyllRdf::Helper::RdfHelper::prefixes["rdf_prefixes"])
59-
end
41+
query = prepare_query(resource, query)
42+
return if query.nil?
6043
begin
61-
result = Jekyll::JekyllRdf::Helper::RdfHelper::sparql.query(query).map do |solution|
44+
result = Jekyll::JekyllRdf::Helper::RdfHelper::sparql.query(query)
45+
if (result.class == RDF::Graph)
46+
return Jekyll::JekyllRdf::Drops::RdfGraph.new(result)
47+
end
48+
result.map! do |solution|
6249
hsh = solution.to_h
6350
hsh.update(hsh){ |k,v| Jekyll::JekyllRdf::Drops::RdfTerm.build_term_drop(v, Jekyll::JekyllRdf::Helper::RdfHelper::site, true).add_necessities(Jekyll::JekyllRdf::Helper::RdfHelper::site, Jekyll::JekyllRdf::Helper::RdfHelper::page)}
6451
hsh.collect{|k,v| [k.to_s, v]}.to_h

lib/jekyll/helper/rdf_filter_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ def to_string_wrap(input)
5555
return false
5656
end
5757
end
58+
59+
def prepare_query(resource = nil, query)
60+
query = query.clone #sometimes liquid wont reinit static strings in for loops
61+
if(rdf_substitude_nil?(resource))
62+
query.gsub!('?resourceUri', "<#{Jekyll::JekyllRdf::Helper::RdfHelper::page.data['rdf'].term}>")
63+
elsif(resource.class <= Array)
64+
resource.each_with_index do |uri, index|
65+
return nil unless valid_resource?(uri)
66+
if(uri.class <= Jekyll::JekyllRdf::Drops::RdfResource)
67+
query.gsub!("?resourceUri_#{index}", uri.term.to_ntriples)
68+
else
69+
query.gsub!("?resourceUri_#{index}", "#{rdf_resolve_prefix(uri.to_s)}")
70+
end
71+
end
72+
else
73+
return nil unless valid_resource?(resource)
74+
query.gsub!('?resourceUri', to_string_wrap(resource))
75+
end if query.include? '?resourceUri' #the only purpose of the if statement is to substitute ?resourceUri
76+
unless Jekyll::JekyllRdf::Helper::RdfHelper::prefixes["rdf_prefixes"].nil?
77+
query = query.prepend(" ").prepend(Jekyll::JekyllRdf::Helper::RdfHelper::prefixes["rdf_prefixes"])
78+
end
79+
return query
80+
end
5881
end
5982

6083
module PrefixSolver
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
group :jekyll_plugins do
3+
gem 'jekyll-rdf', :path => '../../../'
4+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
baseurl: "/instance" # the subpath of your site, e.g. /blog
2+
url: "http://example.org/" # the base hostname & protocol for your site
3+
# Build settings
4+
markdown: kramdown
5+
plugins:
6+
- jekyll-rdf
7+
jekyll_rdf:
8+
path: "_data/knowledge-base.ttl"
9+
restriction: "SELECT ?resourceUri WHERE {?resourceUri ?p <http://empty>}"
10+
default_template: "default"
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@prefix eg: <http://example.org/instance/> .
2+
3+
eg:resource1 eg:construct eg:goConstruct .
4+
eg:resource2 eg:construct eg:goConstruct .
5+
eg:resource3 eg:construct eg:goConstruct .
6+
7+
eg:resource4 eg:construct eg:noConstruct .
8+
eg:resource5 eg:construct eg:noConstruct .
9+
eg:resource6 eg:construct eg:noConstruct .
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
---
3+
<!DOCTYPE html>
4+
<html>
5+
<head></head>
6+
<body>
7+
<div>
8+
<h4>This is made with jekyll-rdf</h4>
9+
{{content}}
10+
</div>
11+
</body>
12+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
---
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>A demonstration of Construct queries</title>
7+
<meta charset="UTF-8">
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
9+
</head>
10+
<body>
11+
{%- assign cquery = "CONSTRUCT {?x <http://contruction.orb/construct> <http://construction.org/constructed>} where {?x <http://example.org/instance/construct> <http://example.org/instance/goConstruct>}" -%}
12+
{%- assign test = cquery | sparql_query -%}
13+
<div>
14+
{{ test }}
15+
<br/>
16+
{%- assign nquads = test.to_nquads -%}
17+
{{ nquads }}
18+
<br/>
19+
{%- assign ntriples = test.to_ntriples -%}
20+
{{ ntriples }}
21+
</div>
22+
</body>
23+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'test_helper'
2+
3+
class TestSciMath < Test::Unit::TestCase
4+
include RdfTestUtility
5+
context "cases/constructQueries" do
6+
setup do
7+
setup_jekyll File.dirname(__FILE__)
8+
end
9+
10+
should "work with construction queries" do
11+
content = []
12+
file = File.read(File.join(@source, "_site/constructs.html"))
13+
content = file[/\<div\>(.|\s)*\<\/div>/][5..-7].strip.split("<br/>").map do |entry|
14+
entry.strip
15+
end
16+
assert_equal 3, content.length
17+
nquads = content[0].split("\n")
18+
assert (nquads.include? "<http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
19+
assert (nquads.include? "<http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
20+
assert (nquads.include? "<http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
21+
nquads = content[1].split("\n")
22+
assert (nquads.include? "<http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
23+
assert (nquads.include? "<http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
24+
assert (nquads.include? "<http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
25+
ntriples = content[2].split("\n")
26+
assert (ntriples.include? "<http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource1> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
27+
assert (ntriples.include? "<http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource3> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
28+
assert (ntriples.include? "<http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed> ."), "the return graph should contain >>><http://example.org/instance/resource2> <http://contruction.orb/construct> <http://construction.org/constructed><<<"
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)