Class: Solis::Query::Construct

Inherits:
Object
  • Object
show all
Defined in:
lib/solis/query/construct.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Construct

Returns a new instance of Construct.



6
7
8
9
10
11
12
# File 'lib/solis/query/construct.rb', line 6

def initialize(model)
  @model = model
  @sparql_endpoint = @model.class.sparql_endpoint
  @sparql_client = SPARQL::Client.new(@sparql_endpoint, graph: @model.class.graph_name, read_timeout: 120)
  @construct_cache = File.absolute_path(Solis::Options.instance.get[:cache])
  @moneta = Moneta.new(:File, dir: "#{@construct_cache}/construct", expires: Solis::Options.instance.get[:cache_expire])
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/solis/query/construct.rb', line 14

def exists?
  File.exist?(file_path)
end

#file_pathObject



24
25
26
# File 'lib/solis/query/construct.rb', line 24

def file_path
  "#{ConfigFile.path}/constructs/#{@model.name.tableize.singularize}.sparql"
end

#file_path_hashObject



28
29
30
# File 'lib/solis/query/construct.rb', line 28

def file_path_hash
  UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, file_path).to_s
end

#loadObject



18
19
20
21
22
# File 'lib/solis/query/construct.rb', line 18

def load
  construct_path = file_path
  raise Solis::Error::NotFoundError, "Construct not found at #{construct_path} " unless exists?
  File.read(construct_path)
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/solis/query/construct.rb', line 32

def run
  construct_query = load
  sparql_repository = @sparql_endpoint

  from_cache = Graphiti.context[:object].from_cache || '1'
  if construct_query && construct_query =~ /construct/
    if @moneta.key?(file_path_hash) && from_cache.eql?('1')
      sparql_repository = @moneta[file_path_hash]
    else
      @sparql_client = SPARQL::Client.new(@sparql_endpoint, read_timeout: 120)
      result = @sparql_client.query(construct_query)
      repository=RDF::Repository.new
      result.each {|s| repository << [s[:s], s[:p], s[:o]]}
      sparql_repository = repository
      @moneta.store(file_path_hash, repository, expires: ConfigFile[:solis][:cache_expire] || 86400)
    end
  elsif construct_query && construct_query =~ /insert/
    unless @moneta.key?(file_path_hash)
      clear_construct
      result = @sparql_client.query(construct_query)
      LOGGER.info(result[0]['callret-0'].value) unless result.empty?
      @moneta.store(file_path_hash, repository, expires: ConfigFile[:solis][:cache_expire] || 86400) unless result[0]['callret-0'].value =~ /0 triples/
    end
  end

  #SPARQL::Client.new(@sparql_endpoint, graph: @model.class.graph_name, read_timeout: 120)
  SPARQL::Client.new(sparql_repository, read_timeout: 120)
end