talia_core 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +1 -1
- data/config/talia_core.yml +2 -0
- data/config/talia_core.yml.example +3 -1
- data/generators/talia_base/templates/app/controllers/ontologies_controller.rb +2 -2
- data/generators/talia_base/templates/app/controllers/sources_controller.rb +1 -1
- data/lib/swicky/api_result.rb +57 -0
- data/lib/swicky/json_encoder.rb +175 -0
- data/lib/swicky/notebook.rb +128 -0
- data/lib/talia_core/active_source.rb +1 -1
- data/lib/talia_core/active_source_parts/xml/generic_reader.rb +2 -7
- data/lib/talia_core/active_source_parts/xml/rdf_builder.rb +9 -56
- data/lib/talia_core/active_source_parts/xml/source_builder.rb +5 -1
- data/lib/talia_core/initializer.rb +2 -2
- data/lib/talia_core/rdf_import.rb +4 -4
- data/lib/talia_core/rdf_resource.rb +15 -15
- data/lib/talia_util/rake_tasks.rb +1 -1
- data/lib/talia_util/rdf_update.rb +4 -4
- data/lib/talia_util/uri_helper.rb +20 -0
- data/lib/talia_util/util.rb +4 -4
- data/lib/talia_util/xml/base_builder.rb +47 -0
- data/lib/talia_util/xml/rdf_builder.rb +172 -0
- data/test/swicky/json_encoder_test.rb +116 -0
- data/test/swicky/notebook_test.rb +107 -0
- data/test/talia_core/rdf_resource_test.rb +1 -1
- data/test/talia_util/rdf_builder_test.rb +87 -0
- metadata +13 -6
- data/lib/talia_core/active_source_parts/xml/base_builder.rb +0 -47
- data/lib/talia_util/some_sigla.xml +0 -1960
@@ -7,7 +7,7 @@ module TaliaCore
|
|
7
7
|
# of the pitfalls of the original class.
|
8
8
|
class RdfResource
|
9
9
|
|
10
|
-
include
|
10
|
+
include ActiveRDF::ResourceLike
|
11
11
|
|
12
12
|
class << self
|
13
13
|
|
@@ -34,18 +34,18 @@ module TaliaCore
|
|
34
34
|
|
35
35
|
# Direct writing of a predicate, with having to fetch a list first
|
36
36
|
def direct_write_predicate(predicate, value)
|
37
|
-
FederationManager.add(self, predicate, value)
|
37
|
+
ActiveRDF::FederationManager.add(self, predicate, value)
|
38
38
|
end
|
39
39
|
|
40
40
|
# Clears all rdf for this resource. FIXME: Not context-aware.
|
41
41
|
def clear_rdf
|
42
|
-
FederationManager.delete_all(self)
|
42
|
+
ActiveRDF::FederationManager.delete_all(self)
|
43
43
|
end
|
44
44
|
|
45
45
|
# Removes the given predicate (restrict to the triple with the
|
46
46
|
# given value if a value is given).
|
47
47
|
def remove(predicate, value = nil)
|
48
|
-
FederationManager.delete(self, predicate, value)
|
48
|
+
ActiveRDF::FederationManager.delete(self, predicate, value)
|
49
49
|
end
|
50
50
|
|
51
51
|
# Returns the value(s) of the given predicates as a PropertyList filled
|
@@ -53,7 +53,7 @@ module TaliaCore
|
|
53
53
|
def [](predicate)
|
54
54
|
predicate = N::URI.new(predicate) unless(predicate.kind_of?(N::URI))
|
55
55
|
|
56
|
-
property_list = Query.new(object_class).distinct(:o).where(self, predicate, :o).execute
|
56
|
+
property_list = ActiveRDF::Query.new(object_class).distinct(:o).where(self, predicate, :o).execute
|
57
57
|
|
58
58
|
PropertyList.new(predicate, property_list, self, source_exists?)
|
59
59
|
end
|
@@ -77,7 +77,7 @@ module TaliaCore
|
|
77
77
|
|
78
78
|
def [](property)
|
79
79
|
property = N::URI.new(property) unless(property.kind_of?(N::URI))
|
80
|
-
Query.new(@obj_class).distinct(:s).where(:s, property, @obj_uri).execute
|
80
|
+
ActiveRDF::Query.new(@obj_class).distinct(:s).where(:s, property, @obj_uri).execute
|
81
81
|
end
|
82
82
|
private(:type)
|
83
83
|
end
|
@@ -93,13 +93,13 @@ module TaliaCore
|
|
93
93
|
|
94
94
|
# Returns the predicates that are directly defined for this resource
|
95
95
|
def direct_predicates
|
96
|
-
Query.new(N::Predicate).distinct(:p).where(self, :p, :o).execute
|
96
|
+
ActiveRDF::Query.new(N::Predicate).distinct(:p).where(self, :p, :o).execute
|
97
97
|
end
|
98
98
|
|
99
99
|
# Returns the "inverse" predicates for the resource. these are the predicates
|
100
100
|
# for which this resource exists as an object
|
101
101
|
def inverse_predicates
|
102
|
-
qry = Query.new.distinct.select(:p)
|
102
|
+
qry = ActiveRDF::Query.new.distinct.select(:p)
|
103
103
|
qry.where(:s, :p, N::URI.new(uri.to_s))
|
104
104
|
qry.execute.collect{ |res| N::Predicate.new(res.uri) }
|
105
105
|
end
|
@@ -108,8 +108,8 @@ module TaliaCore
|
|
108
108
|
# been optimized so that if only one RDF backend is present it won't do
|
109
109
|
# any copying around.
|
110
110
|
def save
|
111
|
-
if((ConnectionPool.read_adapters.size == 1) &&
|
112
|
-
(ConnectionPool.write_adapter == ConnectionPool.read_adapters.first))
|
111
|
+
if((ActiveRDF::ConnectionPool.read_adapters.size == 1) &&
|
112
|
+
(ActiveRDF::ConnectionPool.write_adapter == ActiveRDF::ConnectionPool.read_adapters.first))
|
113
113
|
save_default_types # Only write the "default" types to the store
|
114
114
|
else
|
115
115
|
full_save # Do the full save operation
|
@@ -118,7 +118,7 @@ module TaliaCore
|
|
118
118
|
|
119
119
|
# Returns the types of this resource as N::SourceClass objects
|
120
120
|
def types
|
121
|
-
types = Query.new(N::SourceClass).distinct(:t).where(self,N::RDF::type,:t).execute
|
121
|
+
types = ActiveRDF::Query.new(N::SourceClass).distinct(:t).where(self,N::RDF::type,:t).execute
|
122
122
|
# Add the "default" types if necessary
|
123
123
|
self.class.default_types.each do |def_type|
|
124
124
|
types << def_type unless(types.include?(def_type))
|
@@ -133,7 +133,7 @@ module TaliaCore
|
|
133
133
|
# Saves the the "default" types of this resource to the writing adapter
|
134
134
|
def save_default_types
|
135
135
|
self.class.default_types.each do |t|
|
136
|
-
FederationManager.add(self, N::RDF::type, t)
|
136
|
+
ActiveRDF::FederationManager.add(self, N::RDF::type, t)
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
@@ -141,11 +141,11 @@ module TaliaCore
|
|
141
141
|
# the writing adapter. This operation can be very slow.
|
142
142
|
def full_save
|
143
143
|
types.each do |t|
|
144
|
-
FederationManager.add(self, N::RDF::type, t)
|
144
|
+
ActiveRDF::FederationManager.add(self, N::RDF::type, t)
|
145
145
|
end
|
146
146
|
|
147
|
-
Query.new(N::URI).distinct(:p,:o).where(self, :p, :o).execute do |p, o|
|
148
|
-
FederationManager.add(self, p, o)
|
147
|
+
ActiveRDF::Query.new(N::URI).distinct(:p,:o).where(self, :p, :o).execute do |p, o|
|
148
|
+
ActiveRDF::FederationManager.add(self, p, o)
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
@@ -36,15 +36,15 @@ module TaliaUtil
|
|
36
36
|
# number of blank nodes
|
37
37
|
def rdfs_from_owl
|
38
38
|
# Remove previous auto rdfs triples
|
39
|
-
FederationManager.clear(N::TALIA.auto_rdfs.context)
|
39
|
+
ActiveRDF::FederationManager.clear(N::TALIA.auto_rdfs.context)
|
40
40
|
|
41
41
|
# This gets all OWL classes in the store
|
42
|
-
all_qry = Query.new(N::URI).distinct.select(:class)
|
42
|
+
all_qry = ActiveRDF::Query.new(N::URI).distinct.select(:class)
|
43
43
|
all_qry.where(:class, N::RDF::type, N::OWL.Class)
|
44
44
|
all_owl = all_qry.execute
|
45
45
|
|
46
46
|
# This gets all OWL classes that already have an RDF class attached
|
47
|
-
qry_rdfs = Query.new(N::URI).distinct.select(:class)
|
47
|
+
qry_rdfs = ActiveRDF::Query.new(N::URI).distinct.select(:class)
|
48
48
|
qry_rdfs.where(:class, N::RDF::type, N::OWL.Class)
|
49
49
|
qry_rdfs.where(:class, N::RDF::type, N::RDFS.Class)
|
50
50
|
classes_with_rdfs = qry_rdfs.execute
|
@@ -76,7 +76,7 @@ module TaliaUtil
|
|
76
76
|
class_hash.each do |klass, status|
|
77
77
|
if(status == :has_rdfs_class)
|
78
78
|
modified = modified + 1
|
79
|
-
FederationManager.add(N::URI.new(klass), N::RDF.type, N::RDFS.Class, N::TALIA.auto_rdfs_context)
|
79
|
+
ActiveRDF::FederationManager.add(N::URI.new(klass), N::RDF.type, N::RDFS.Class, N::TALIA.auto_rdfs_context)
|
80
80
|
end
|
81
81
|
yield(class_hash.size) if(block_given?)
|
82
82
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TaliaUtil
|
2
|
+
|
3
|
+
# Some helper methods to deal with URI and IRI strings
|
4
|
+
module UriHelper
|
5
|
+
|
6
|
+
# Removes all characters that are illegal in IRIs, so that the
|
7
|
+
# URIs can be imported
|
8
|
+
def irify(uri)
|
9
|
+
N::URI.new(uri.to_s.gsub( /[<>"{}|\\^`\s]/, '+')).to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
# Sanitize an URI to be passed into SPARQL queries
|
13
|
+
def sanitize_sparql(uri_or_string)
|
14
|
+
uri_or_string = uri_or_string.to_s.gsub( /[<>"{}|\\^`\s]/, '') # Remove forbidden chars that we know of
|
15
|
+
URI.escape(uri_or_string) # Escape everything else
|
16
|
+
uri_or_string.gsub('%23', '#') # Revert the hash character, we need that intact
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/talia_util/util.rb
CHANGED
@@ -25,7 +25,7 @@ module TaliaUtil
|
|
25
25
|
# Set up the ontologies from the given folder
|
26
26
|
def setup_ontologies
|
27
27
|
# Clear the ontologies from RDF, if possible
|
28
|
-
adapter = ConnectionPool.write_adapter
|
28
|
+
adapter = ActiveRDF::ConnectionPool.write_adapter
|
29
29
|
if(adapter.supports_context?)
|
30
30
|
TaliaCore::RdfImport.clear_file_contexts
|
31
31
|
else
|
@@ -108,7 +108,7 @@ module TaliaUtil
|
|
108
108
|
|
109
109
|
# Flush the RDF store
|
110
110
|
def flush_rdf
|
111
|
-
ConnectionPool.write_adapter.clear
|
111
|
+
ActiveRDF::ConnectionPool.write_adapter.clear
|
112
112
|
end
|
113
113
|
|
114
114
|
# Remove the data directories
|
@@ -145,7 +145,7 @@ module TaliaUtil
|
|
145
145
|
else
|
146
146
|
rec.property_value
|
147
147
|
end
|
148
|
-
FederationManager.add(subject, predicate, object)
|
148
|
+
ActiveRDF::FederationManager.add(subject, predicate, object)
|
149
149
|
yield if(block_given?)
|
150
150
|
end
|
151
151
|
|
@@ -155,7 +155,7 @@ module TaliaUtil
|
|
155
155
|
# are defined outside the core.
|
156
156
|
TaliaCore::ActiveSource.find(:all, :select => 'uri, type AS runtime_type').each do |src|
|
157
157
|
type = (src.runtime_type || 'ActiveSource')
|
158
|
-
FederationManager.add(src, N::RDF.type, N::TALIA + type)
|
158
|
+
ActiveRDF::FederationManager.add(src, N::RDF.type, N::TALIA + type)
|
159
159
|
yield if(block_given?)
|
160
160
|
end
|
161
161
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module TaliaUtil
|
2
|
+
module Xml
|
3
|
+
|
4
|
+
# Base class for builders that create XML representations. This uses a Builder::XmlMarkup object
|
5
|
+
# in the background which does the actual XML writing.
|
6
|
+
#
|
7
|
+
# All builders will be used through the #open method, which can be passed either a Builder::XmlMarkup
|
8
|
+
# object, or the options to create one.
|
9
|
+
#
|
10
|
+
# Subclasses must provide a build_structure method that creates the outer structure of the XML
|
11
|
+
class BaseBuilder
|
12
|
+
|
13
|
+
# Creates a new builder. The options are equivalent for the options of the
|
14
|
+
# underlying Xml builder. The builder itself will be passed to the block that
|
15
|
+
# is called by this method.
|
16
|
+
# If you pass a :builder option instead, it will use the given builder instead
|
17
|
+
# of creating a new one
|
18
|
+
def self.open(options)
|
19
|
+
my_builder = self.new(options)
|
20
|
+
my_builder.send(:build_structure) do
|
21
|
+
yield(my_builder)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Builds to a string, using a default builder. This returns the string and otherwise
|
26
|
+
# works like #open
|
27
|
+
def self.make_xml_string
|
28
|
+
xml = ''
|
29
|
+
open(:target => xml, :indent => 2) do |builder|
|
30
|
+
yield(builder)
|
31
|
+
end
|
32
|
+
|
33
|
+
xml
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Create a new builder
|
39
|
+
def initialize(options)
|
40
|
+
@builder = options[:builder]
|
41
|
+
@builder ||= Builder::XmlMarkup.new(options)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
module TaliaUtil
|
2
|
+
module Xml
|
3
|
+
# Class for creating xml-rdf data
|
4
|
+
class RdfBuilder < BaseBuilder
|
5
|
+
|
6
|
+
# Writes a simple "flat" triple. If the object is a string, it will be
|
7
|
+
# treated as a "value" while an object (ActiveSource or N::URI) will be treated
|
8
|
+
# as a "link".
|
9
|
+
#
|
10
|
+
# Throws an exception if the predicate cannot be turned into a namespaced
|
11
|
+
# representation
|
12
|
+
def write_triple(subject, predicate, object)
|
13
|
+
subject = subject.respond_to?(:uri) ? subject.uri.to_s : subject
|
14
|
+
predicate = predicate.respond_to?(:uri) ? predicate : N::URI.new(predicate)
|
15
|
+
@builder.rdf :Description, "rdf:about" => subject do
|
16
|
+
write_predicate(predicate, [ object ])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Writes all the given triples.
|
21
|
+
def write_triples(triples)
|
22
|
+
triples.each do |triple|
|
23
|
+
write_triple(*triple)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
def self.open_for_triples(triples, options = nil)
|
29
|
+
my_builder = self.new(options)
|
30
|
+
|
31
|
+
triple_hash = my_builder.send(:prepare_triples, triples)
|
32
|
+
|
33
|
+
my_builder.send(:build_structure) do
|
34
|
+
my_builder.send(:write_for_triples, triple_hash)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def self.xml_string_for_triples(triples)
|
40
|
+
xml = ''
|
41
|
+
open_for_triples(triples, :target => xml, :indent => 2)
|
42
|
+
xml
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
|
48
|
+
def prepare_triples(triples)
|
49
|
+
triple_hash = {}
|
50
|
+
triples.each do |triple|
|
51
|
+
subject = triple.shift
|
52
|
+
subject = subject.to_s
|
53
|
+
predicate = triple.first.to_uri
|
54
|
+
namespaced_predicate = predicate.to_name_s
|
55
|
+
if(predicate == namespaced_predicate)
|
56
|
+
# We have an unknown namespace
|
57
|
+
namespaced_predicate = make_predicate_namespace(predicate)
|
58
|
+
end
|
59
|
+
triple_hash[subject] ||= {}
|
60
|
+
triple_hash[subject][N::URI.new(namespaced_predicate)] ||= []
|
61
|
+
triple_hash[subject][N::URI.new(namespaced_predicate)] << triple.last
|
62
|
+
end
|
63
|
+
triple_hash
|
64
|
+
end
|
65
|
+
|
66
|
+
# Write for the open_for_triples
|
67
|
+
def write_for_triples(triple_hash)
|
68
|
+
triple_hash.each do |subject, values|
|
69
|
+
@builder.rdf :Description, 'rdf:about' => subject.to_uri.to_name_s do # Element describing this resource
|
70
|
+
values.each do |predicate, objects|
|
71
|
+
write_predicate(predicate, objects, false)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def make_predicate_namespace(predicate)
|
78
|
+
pred_uri = URI.parse(predicate.to_s)
|
79
|
+
path_parts = split_uri!(pred_uri)
|
80
|
+
raise(ArgumentError, "Illegal predicate URL #{predicate}") if(path_parts[0].blank? || path_parts[1].blank?)
|
81
|
+
namespace = make_namespace(pred_uri)
|
82
|
+
"#{namespace}:#{path_parts[1]}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def split_uri!(uri)
|
86
|
+
if(uri.fragment)
|
87
|
+
fragment = uri.fragment
|
88
|
+
uri.fragment = ''
|
89
|
+
[uri.path, fragment]
|
90
|
+
else
|
91
|
+
path_parts = /\A(.*[\/#])([^\/#]+)\Z/.match(uri.path)
|
92
|
+
uri.path = path_parts[1]
|
93
|
+
[ path_parts[1], path_parts[2] ]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def make_namespace(namespace_uri)
|
98
|
+
candidate = /([^\.]+)(\.[^\.]*)?\Z/.match(namespace_uri.host)[1]
|
99
|
+
raise(ArgumentError, "Illegal namespace #{namespace_uri.to_s}") if(candidate.blank?)
|
100
|
+
first_candidate = candidate.downcase
|
101
|
+
candidate = first_candidate
|
102
|
+
counter = 1
|
103
|
+
additional_namespaces[candidate.to_sym] ||= namespace_uri.to_s
|
104
|
+
while(additional_namespaces[candidate.to_sym] != namespace_uri.to_s)
|
105
|
+
counter += 1
|
106
|
+
candidate = "#{first_candidate}#{counter}"
|
107
|
+
additional_namespaces[candidate.to_sym] ||= namespace_uri.to_s
|
108
|
+
end
|
109
|
+
candidate
|
110
|
+
end
|
111
|
+
|
112
|
+
# Build the structure for the XML file and pass on to
|
113
|
+
# the given block
|
114
|
+
def build_structure
|
115
|
+
@builder.rdf :RDF, namespaces do
|
116
|
+
yield
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def additional_namespaces
|
121
|
+
@additional_namespaces ||= {}
|
122
|
+
end
|
123
|
+
|
124
|
+
def namespaces
|
125
|
+
namespaces = self.class.namespaces
|
126
|
+
additional_namespaces.each { |key, value| namespaces["xmlns:#{key.to_s}"] = value.to_s }
|
127
|
+
namespaces
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.namespaces
|
131
|
+
@namespaces ||= begin
|
132
|
+
namespaces = {}
|
133
|
+
N::Namespace.shortcuts.each { |key, value| namespaces["xmlns:#{key.to_s}"] = value.to_s }
|
134
|
+
namespaces
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Build an rdf/xml string for one predicate, with the given values
|
139
|
+
def write_predicate(predicate, values, check_predicate = true)
|
140
|
+
values.each { |val| write_single_predicate(predicate, val, check_predicate) }
|
141
|
+
end # end method
|
142
|
+
|
143
|
+
def write_single_predicate(predicate, value, check_predicate = true)
|
144
|
+
is_property = value.respond_to?(:uri)
|
145
|
+
value_properties = is_property ? { 'value' => value } : extract_values(value.to_s)
|
146
|
+
value = value_properties.delete('value')
|
147
|
+
predicate_name = predicate.to_name_s
|
148
|
+
raise(ArgumentError, "Cannot turn predicate #{predicate} into namespace name") if(check_predicate && (predicate == predicate_name))
|
149
|
+
@builder.tag!(predicate.to_name_s, value_properties) do
|
150
|
+
if(is_property)
|
151
|
+
@builder.rdf :Description, 'rdf:about' => value.uri.to_s
|
152
|
+
else
|
153
|
+
@builder.text!(value)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Splits up the value, extracting encoded language codes and RDF data types. The
|
159
|
+
# result will be returned as a hash, with the "true" value being "value"
|
160
|
+
def extract_values(value)
|
161
|
+
prop_string = TaliaCore::PropertyString.parse(value)
|
162
|
+
result = {}
|
163
|
+
result['value'] = prop_string
|
164
|
+
result['rdf:datatype'] = prop_string.type if(prop_string.type)
|
165
|
+
result['xml:lang'] = prop_string.lang if(prop_string.lang)
|
166
|
+
|
167
|
+
result
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
require 'rexml/document'
|
3
|
+
|
4
|
+
module Swicky
|
5
|
+
|
6
|
+
class JsonEncoderTest < ActiveSupport::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@encoder = JsonEncoder.new(test_triples)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_make_predicate_local
|
13
|
+
local = @encoder.send(:make_predicate_local, N::TALIA.foobar)
|
14
|
+
assert_equal('foobar', local)
|
15
|
+
assert_equal({ 'foobar' => { 'uri' => N::TALIA.foobar.to_s, 'valueType' => 'item' } }, @encoder.instance_variable_get(:@properties_hash))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_make_predicate_local_multi
|
19
|
+
@encoder.send(:make_predicate_local, N::TALIA.foobar)
|
20
|
+
@encoder.send(:make_predicate_local, N::LOCAL.foobar)
|
21
|
+
@encoder.send(:make_predicate_local, N::TALIA.foobar)
|
22
|
+
local = @encoder.send(:make_predicate_local, N::RDF.foobar)
|
23
|
+
assert_equal('foobar3', local)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_make_type_local
|
27
|
+
local = @encoder.send(:make_type_local, N::TALIA.Foobar)
|
28
|
+
assert_equal('Foobar', local)
|
29
|
+
assert_equal({ 'Foobar' => { 'uri' => N::TALIA.Foobar.to_s } }, @encoder.instance_variable_get(:@types_hash))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_build_item
|
33
|
+
item = @encoder.send(:build_item, N::LOCAL.Foo, {
|
34
|
+
N::RDF.type => [ N::TALIA.Foobar ],
|
35
|
+
N::TALIA.hasIt => ['blarg', 'bar'],
|
36
|
+
N::TALIA.strangeThing => ['what'],
|
37
|
+
'label' => 'bar'
|
38
|
+
})
|
39
|
+
assert_equal([{
|
40
|
+
'uri' => N::LOCAL.Foo.to_s,
|
41
|
+
'type' => [ 'Foobar' ],
|
42
|
+
'label' => 'bar',
|
43
|
+
'hasIt' => ['blarg', 'bar'],
|
44
|
+
'strangeThing' => 'what'
|
45
|
+
}], item)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_to_json
|
49
|
+
assert_equal(expected_result.to_json, @encoder.to_json)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def expected_result
|
55
|
+
{
|
56
|
+
"items" => [
|
57
|
+
{
|
58
|
+
"uri"=>N::LOCAL.foobar.to_s,
|
59
|
+
"type"=>["MyType"],
|
60
|
+
"label"=>"foobar",
|
61
|
+
"first"=>["worksit", "worksit2"]
|
62
|
+
},
|
63
|
+
{
|
64
|
+
"uri"=>N::TALIA.whatever.to_s,
|
65
|
+
"type"=>["Resource"],
|
66
|
+
"label"=>"whatever",
|
67
|
+
"predicate"=>"The cool thing",
|
68
|
+
"first2"=>"The cool thing"
|
69
|
+
},
|
70
|
+
{
|
71
|
+
"uri"=>"http://www.barbaa.com/fun",
|
72
|
+
"type"=>["Resource"],
|
73
|
+
"label"=>"The cool thing",
|
74
|
+
"predicate"=>"whatever"
|
75
|
+
}
|
76
|
+
],
|
77
|
+
"types" => {
|
78
|
+
"MyType"=>
|
79
|
+
{
|
80
|
+
"uri"=>N::TALIA.MyType.to_s
|
81
|
+
},
|
82
|
+
"Resource"=> {
|
83
|
+
"uri"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#Resource"
|
84
|
+
}
|
85
|
+
},
|
86
|
+
"properties"=> {
|
87
|
+
"first"=> {
|
88
|
+
"uri"=>"http://www.foobar.com/bar/moo/first",
|
89
|
+
"valueType"=>"item"
|
90
|
+
},
|
91
|
+
"predicate"=>{
|
92
|
+
"uri"=>N::TALIA.predicate.to_s,
|
93
|
+
"valueType"=>"item"
|
94
|
+
},
|
95
|
+
"first2"=>{
|
96
|
+
"uri"=>"http://www.foobar.com/bar/moo#first", "valueType"=>"item"
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
def test_triples
|
104
|
+
[
|
105
|
+
[ N::LOCAL.foobar, 'http://www.foobar.com/bar/moo/first', "worksit"],
|
106
|
+
[ N::LOCAL.foobar, 'http://www.foobar.com/bar/moo/first', "worksit2"],
|
107
|
+
[ N::LOCAL.foobar, N::RDF.type, N::TALIA.MyType ],
|
108
|
+
[ N::TALIA.whatever, N::TALIA.predicate, N::URI.new('http://www.barbaa.com/fun')],
|
109
|
+
[ N::TALIA.whatever, 'http://www.foobar.com/bar/moo#first', N::URI.new('http://www.barbaa.com/fun')],
|
110
|
+
[ "http://www.barbaa.com/fun".to_uri, N::RDFS.label, "The cool thing"],
|
111
|
+
[ "http://www.barbaa.com/fun".to_uri, N::TALIA.predicate, N::TALIA.whatever]
|
112
|
+
]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
2
|
+
require 'rexml/document'
|
3
|
+
|
4
|
+
module Swicky
|
5
|
+
|
6
|
+
# Test the ActiveSource
|
7
|
+
class NotebookTest < ActiveSupport::TestCase
|
8
|
+
|
9
|
+
|
10
|
+
def setup
|
11
|
+
setup_once(:flush) { TaliaCore::TestHelper::flush_store }
|
12
|
+
@notebook = Notebook.new('dan', 'booky')
|
13
|
+
@testpointer = "http://dbin.org/swickynotes/demo/HanselAndGretel.htm#xpointer(start-point(string-range(//DIV[@id='http://dbin.org/swickynotes/demo/HG_1']/P[1]/SPAN[1]/text()[1],'',0))/range-to(string-range(//DIV[@id='http://dbin.org/swickynotes/demo/HG_1']/P[1]/SPAN[1]/text()[1],'',266)))"
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
@notebook.delete
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_url
|
21
|
+
assert_equal(@notebook.url, N::LOCAL + 'users/dan/swicky_notebooks/booky')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_user_url
|
25
|
+
assert_equal(@notebook.user_url, N::LOCAL + 'users/dan')
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_load
|
29
|
+
assert_notebook_empty
|
30
|
+
load_notebook
|
31
|
+
assert_notebook_full
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_delete
|
35
|
+
load_notebook
|
36
|
+
@notebook.delete
|
37
|
+
assert_notebook_empty
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_data
|
41
|
+
load_notebook
|
42
|
+
assert_equal(@notebook.data.size, 236)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_xml_data
|
46
|
+
load_notebook
|
47
|
+
xml_data = @notebook.xml_data
|
48
|
+
assert_kind_of(String, xml_data)
|
49
|
+
assert_not_equal('', xml_data)
|
50
|
+
# Check xml validity
|
51
|
+
assert_nothing_raised { REXML::Document.new(xml_data) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_find_all_empty
|
55
|
+
assert_equal([], Notebook.find_all)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_find_all_existing
|
59
|
+
load_notebook
|
60
|
+
assert_equal([@notebook.url], Notebook.find_all)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_find_all_user
|
64
|
+
load_notebook
|
65
|
+
assert_equal([@notebook.url], Notebook.find_all('dan'))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_find_all_user_nonexistent
|
69
|
+
load_notebook
|
70
|
+
assert_equal([], Notebook.find_all('mic'))
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_get_coordinates
|
74
|
+
load_notebook
|
75
|
+
coords = Notebook.coordinates_for("http://dbin.org/swickynotes/demo/HanselAndGretel.htm")
|
76
|
+
assert_equal([ @testpointer ], coords)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_annotations_for_url
|
80
|
+
load_notebook
|
81
|
+
assert_equal(180, Notebook.annotations_for_url("http://discovery-project.eu/ontologies/philoSpace/SourceFragment#ec9796a5349b290a7610763dcbc47af2").size)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_annotations_for_xpointer
|
85
|
+
load_notebook
|
86
|
+
assert_equal(180, Notebook.annotations_for_xpointer(@testpointer).size)
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def assert_notebook_empty
|
92
|
+
assert_equal(0, ActiveRDF::Query.new(N::URI).select(:s, :p, :o).distinct.where(:s, :p, :o, @notebook.url).execute.size)
|
93
|
+
assert_equal(0, ActiveRDF::Query.new(N::URI).select(:user).where(:user, N::TALIA.hasSwickyNotebook, :notebook).where(:notebook, N::RDF.type, N::TALIA.SwickyNotebook).execute.size)
|
94
|
+
end
|
95
|
+
|
96
|
+
def assert_notebook_full
|
97
|
+
assert_equal(236, ActiveRDF::Query.new(N::URI).select(:s, :p, :o).distinct.where(:s, :p, :o, @notebook.url).execute.size)
|
98
|
+
assert_equal(1, ActiveRDF::Query.new(N::URI).select(:user).where(:user, N::TALIA.hasSwickyNotebook, :notebook).where(:notebook, N::RDF.type, N::TALIA.SwickyNotebook).execute.size)
|
99
|
+
end
|
100
|
+
|
101
|
+
def load_notebook
|
102
|
+
@notebook.load(TaliaCore::TestHelper.fixture_file("notebook.rdf"))
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
@@ -84,7 +84,7 @@ module TaliaCore
|
|
84
84
|
res.save
|
85
85
|
|
86
86
|
# Check if the defaul type was written
|
87
|
-
rdfs_prop = Query.new(N::SourceClass).distinct(:t).where(res,N::RDF::type,:t).execute
|
87
|
+
rdfs_prop = ActiveRDF::Query.new(N::SourceClass).distinct(:t).where(res,N::RDF::type,:t).execute
|
88
88
|
assert_equal(2, rdfs_prop.size)
|
89
89
|
assert(rdfs_prop.include?(N::RDFS.Resource))
|
90
90
|
end
|