Module: Tripod::Finders::ClassMethods

Defined in:
lib/tripod/finders.rb

Instance Method Summary collapse

Instance Method Details

#_create_and_hydrate_resources(uris_and_graphs) ⇒ Object

 create and hydrate the resources identified in uris_and_graphs. Note: if any of the graphs are not set, those resources can still be constructed, but not persisted back to DB.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tripod/finders.rb', line 115

def _create_and_hydrate_resources(uris_and_graphs)

  graph = describe_uris(uris_and_graphs.keys) #uses the resource_class on the criteria object
  repo = add_data_to_repository(graph)

  resources = []

  uris_and_graphs.each_pair do |u,g|

    # instantiate a new resource
    r = self.new(u,g)

    # make a graph of data for this resource's uri
    data_graph = RDF::Graph.new
    repo.query( [RDF::URI.new(u), :predicate, :object] ) do |statement|
      data_graph << statement
    end

    # use it to hydrate this resource
    r.hydrate!(:graph => data_graph)
    r.new_record = false
    resources << r
  end

  resources
end

#_select_uris_and_graphs(sparql, opts = {}) ⇒ Object

based on the query passed in, build a hash of uris->graphs

Parameters:

  • sparql. (String)

    The sparql query

  • opts. (Hash)

    A hash of options.

  • options (Hash)

    a customizable set of options



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tripod/finders.rb', line 149

def _select_uris_and_graphs(sparql, opts={})
  select_results = Tripod::SparqlClient::Query.select(sparql)

  uris_and_graphs = {}

  select_results.each do |r|
    uri_variable = opts[:uri_variable] || 'uri'
    graph_variable = opts[:graph_variable] || 'graph'
    uris_and_graphs[ r[uri_variable]["value"] ] = r[graph_variable]["value"]
  end

  uris_and_graphs
end

#allObject

 execute a query to return all objects (restricted by this class’s rdf_type if specified) returns a criteria object



78
79
80
# File 'lib/tripod/finders.rb', line 78

def all
  where('?uri ?p ?o')
end

#countObject



82
83
84
# File 'lib/tripod/finders.rb', line 82

def count
  self.all.count
end

#describe_uris(uris) ⇒ Object

returns a graph of triples which describe the uris passed in.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tripod/finders.rb', line 91

def describe_uris(uris)
  graph = RDF::Graph.new

  if uris.length > 0
    uris_sparql_str = uris.map{ |u| "<#{u.to_s}>" }.join(" ")

    # Do a big describe statement, and read the results into an in-memory repo
    triples_string = Tripod::SparqlClient::Query.query("DESCRIBE #{uris_sparql_str}", "application/n-triples")

    RDF::Reader.for(:ntriples).new(triples_string) do |reader|
      reader.each_statement do |statement|
        graph << statement
      end
    end

  end

  graph
end

#find(uri, graph_uri = nil) ⇒ Resource

Find a Resource by its uri (and, optionally, by its graph if there are more than one).

Examples:

Find a single resource by a uri.

Person.find('http://ric')
Person.find(RDF::URI('http://ric'))

Find a single resource by uri and graph

Person.find('http://ric', 'http://example.com/people')
Person.find(RDF::URI('http://ric'), Person.find(RDF::URI('http://example.com/people')))

Parameters:

  • uri (String, RDF::URI)

    The uri of the resource to find

  • graph_uri (String, RDF::URI) (defaults to: nil)

    The uri of the graph from which to get the resource

Returns:

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tripod/finders.rb', line 24

def find(uri, graph_uri=nil)

  unless graph_uri
    # do a quick select to see what graph to use.
    select_query = "SELECT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1"
    result = Tripod::SparqlClient::Query.select(select_query)
    if result.length > 0
      graph_uri = result[0]["g"]["value"]
    else
      raise Tripod::Errors::ResourceNotFound.new(uri)
    end
  end

  # instantiate and hydrate the resource
  resource = self.new(uri, graph_uri.to_s)

  resource.hydrate!
  resource.new_record = false

  # check that there are triples for the resource (catches case when someone has deleted data
  # between our original check for the graph and hydrating the object.
  raise Tripod::Errors::ResourceNotFound.new(uri) if resource.repository.empty?

  # return the instantiated, hydrated resource
  resource
end

#find_by_sparql(sparql_query, opts = {}) ⇒ Array

Find a collection of Resources by a SPARQL select statement which returns their uris. Under the hood, this only executes two queries: a select, then a describe.

Examples:

Person.find_by_sparql('SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } } LIMIT 3')

Parameters:

  • sparql_query. (String)

    A sparql query which returns a list of uris of the objects.

  • opts. (Hash)

    A hash of options.

  • options (Hash)

    a customizable set of options

Returns:

  • (Array)

    An array of hydrated resources of this class’s type.



64
65
66
67
# File 'lib/tripod/finders.rb', line 64

def find_by_sparql(sparql_query, opts={})
  uris_and_graphs = _select_uris_and_graphs(sparql_query, opts)
  _create_and_hydrate_resources(uris_and_graphs)
end

#firstObject



86
87
88
# File 'lib/tripod/finders.rb', line 86

def first
  self.all.first
end

#where(sparql_snippet) ⇒ Object

 execute a where clause on this resource. returns a criteria object



71
72
73
74
# File 'lib/tripod/finders.rb', line 71

def where(sparql_snippet)
  criteria = Tripod::Criteria.new(self)
  criteria.where(sparql_snippet)
end