Skip to content

Commit ca53d40

Browse files
Implement Instances goo model and refactor the InstanceLoader to use the goo api
1 parent c4562cc commit ca53d40

1 file changed

Lines changed: 44 additions & 132 deletions

File tree

Lines changed: 44 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,68 @@
11

22
module LinkedData
33
module Models
4-
class Instance
5-
include LinkedData::Hypermedia::Resource
6-
include LinkedData::HTTPCache::CacheableResource
4+
class Instance < LinkedData::Models::Base
75

8-
attr_reader :id, :properties
9-
attr_accessor :label
10-
serialize_default :id, :label, :properties
6+
model :named_individual, name_with: :id, collection: :submission,
7+
namespace: :owl, :schemaless => :true , rdf_type: lambda { |*x| RDF::OWL[:NamedIndividual]}
118

12-
# HTTP cache settings.
13-
cache_timeout 14400
149

15-
def initialize(id,label,properties)
16-
@id = id
17-
if label.nil?
18-
sep = "/"
19-
if not id.to_s["#"].nil?
20-
sep = "#"
21-
end
22-
label = id.to_s.split(sep).last
23-
end
24-
@label = label
25-
@properties = properties
26-
end
10+
attribute :types, :namespace => :rdf, enforce: [:list], property: :type
11+
attribute :submission, :collection => lambda { |s| s.resource_id }, :namespace => :metadata
2712

28-
def add_property_value(p,o)
29-
ps = p.to_s
30-
if not @properties.include?(ps)
31-
@properties[ps] = []
32-
end
33-
@properties[ps] << o
34-
end
13+
serialize_never :submission, :id
14+
serialize_methods :properties
3515

36-
def self.type_uri
37-
LinkedData.settings.id_url_prefix+"metadata/Instance"
16+
cache_timeout 14400
17+
18+
def properties
19+
self.unmapped
3820
end
21+
3922
end
4023
end
4124
module InstanceLoader
4225
def self.count_instances_by_class(submission_id,class_id)
43-
query = <<-eos
44-
PREFIX owl: <http://www.w3.org/2002/07/owl#>
45-
SELECT (count(DISTINCT ?s) as ?c) WHERE
46-
{
47-
GRAPH <#{submission_id.to_s}> {
48-
?s a owl:NamedIndividual .
49-
?s a <#{class_id.to_s}> .
50-
}
51-
}
52-
eos
53-
epr = Goo.sparql_query_client(:main)
54-
graphs = [submission_id]
55-
resultset = epr.query(query, graphs: graphs)
56-
resultset.each do |r|
57-
return r[:c].object
58-
end
59-
return 0
26+
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
27+
s = LinkedData::Models::OntologySubmission.find(submission_id).first
28+
self.instances_by_class_where_query(s,class_id).count
6029
end
6130

62-
def self.get_instances_by_class(submission_id,class_id)
63-
query = <<-eos
64-
PREFIX owl: <http://www.w3.org/2002/07/owl#>
65-
SELECT ?s ?label WHERE
66-
{
67-
GRAPH <#{submission_id.to_s}> {
68-
?s a owl:NamedIndividual .
69-
?s a <#{class_id.to_s}> .
70-
}
71-
}
72-
eos
73-
epr = Goo.sparql_query_client(:main)
74-
graphs = [submission_id]
75-
resultset = epr.query(query, graphs: graphs)
76-
instances = []
77-
resultset.each do |r|
78-
inst = LinkedData::Models::Instance.new(r[:s],nil,{})
79-
instances << inst
80-
end
81-
82-
if instances.empty?
83-
return []
31+
def self.get_instances_by_class(submission_id,class_id, page_no=1, size=50)
32+
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
33+
s = LinkedData::Models::OntologySubmission.find(submission_id).first
34+
35+
inst = self.instances_by_class_where_query(s,class_id).page(page_no,size).all
36+
37+
if inst.length > 0 # TODO test if "include=all" parameter is passed in the request
38+
self.load_unmapped s,inst # For getting all the properties # For getting all the properties
8439
end
85-
86-
include_instance_properties(submission_id,instances)
87-
return instances
8840
end
8941

90-
def self.get_instances_by_ontology(submission_id,page_no,size)
91-
query = <<-eos
92-
PREFIX owl: <http://www.w3.org/2002/07/owl#>
93-
SELECT ?s ?label WHERE
94-
{
95-
GRAPH <#{submission_id.to_s}> {
96-
?s a owl:NamedIndividual .
97-
}
98-
}
99-
eos
100-
epr = Goo.sparql_query_client(:main)
101-
graphs = [submission_id]
102-
resultset = epr.query(query, graphs: graphs)
103-
104-
total_size = resultset.size
105-
range_start = (page_no - 1) * size
106-
range_end = (page_no * size) - 1
107-
resultset = resultset[range_start..range_end]
108-
109-
instances = []
110-
resultset.each do |r|
111-
inst = LinkedData::Models::Instance.new(r[:s],r[:label],{})
112-
instances << inst
113-
end unless resultset.nil?
114-
115-
if instances.size > 0
116-
include_instance_properties(submission_id,instances)
42+
43+
def self.get_instances_by_ontology(submission_id,page_no=1,size=50)
44+
## TODO: pass directly an LinkedData::Models::OntologySubmission instance in the arguments instead of submission_id
45+
s = LinkedData::Models::OntologySubmission.find(submission_id).first
46+
inst = s.nil? ? [] : self.instances_by_class_where_query(s).page(page_no,size).all
47+
48+
if inst.length > 0 ## TODO test if "include=all" parameter is passed in the request
49+
self.load_unmapped s,inst # For getting all the properties
11750
end
118-
119-
page = Goo::Base::Page.new(page_no,size,total_size,instances)
120-
return page
12151
end
12252

123-
def self.include_instance_properties(submission_id,instances)
124-
index = Hash.new
125-
instances.each do |inst|
126-
index[inst.id.to_s] = inst
127-
end
128-
uris = index.keys.map { |x| x.to_s }
129-
uri_filter = uris.map { |x| "?s = <#{x}>"}.join(" || ")
130-
131-
query = <<-eos
132-
PREFIX owl: <http://www.w3.org/2002/07/owl#>
133-
SELECT ?s ?p ?o WHERE
134-
{
135-
GRAPH <#{submission_id.to_s}> {
136-
?s ?p ?o .
137-
}
138-
FILTER( #{uri_filter} )
139-
}
140-
eos
141-
epr = Goo.sparql_query_client(:main)
142-
graphs = [submission_id]
143-
resultset = epr.query(query, graphs: graphs)
144-
resultset.each do |sol|
145-
s = sol[:s]
146-
p = sol[:p]
147-
o = sol[:o]
148-
if not p.to_s["label"].nil?
149-
index[s.to_s].label = o.to_s
150-
else
151-
index[s.to_s].add_property_value(p,o)
152-
end
153-
end
53+
private
54+
55+
def self.instances_by_class_where_query(submission, class_id = nil )
56+
57+
where_condition = class_id.nil? ? nil :{types: RDF::URI.new(class_id.to_s)}
58+
LinkedData::Models::Instance.where(where_condition).in(submission).include(:types)
59+
60+
end
61+
62+
def self.load_unmapped(submission, models)
63+
LinkedData::Models::Instance.where.in(submission).models(models).include(:unmapped).all
15464
end
65+
66+
15567
end
15668
end

0 commit comments

Comments
 (0)