Class: Qa::LinkedData::LdpathService

Inherits:
Object
  • Object
show all
Defined in:
app/services/qa/linked_data/ldpath_service.rb

Constant Summary collapse

VALUE_ON_ERROR =
[].freeze

Class Method Summary collapse

Class Method Details

.ldpath_evaluate(program:, graph:, subject_uri:, limit_to_context: Qa.config.limit_ldpath_to_context?) ⇒ Array<String>

Evaluate an ldpath for a specific subject uri in the context of a graph and return the extracted values.

Parameters:

  • program (Ldpath::Program)

    an executable program that will extract a value from a graph

  • graph (RDF::Graph)

    the graph from which the values will be extracted

  • subject_uri (RDF::URI)

    retrieved values will be limited to those with the subject uri

  • limit_to_context (Boolean) (defaults to: Qa.config.limit_ldpath_to_context?)

    if true, the evaluation process will not make any outside network calls. It will limit results to those found in the context graph.

Returns:

  • (Array<String>)

    the extracted values based on the ldpath



33
34
35
36
37
38
39
40
# File 'app/services/qa/linked_data/ldpath_service.rb', line 33

def self.ldpath_evaluate(program:, graph:, subject_uri:, limit_to_context: Qa.config.limit_ldpath_to_context?)
  return VALUE_ON_ERROR if program.blank?
  output = program.evaluate(subject_uri, context: graph, limit_to_context: limit_to_context)
  output.present? ? output['property'].uniq : nil
rescue => e
  Rails.logger.warn("WARNING: #{I18n.t('qa.linked_data.ldpath.evaluate_logger_error')} (cause: #{e.message}")
  raise StandardError, I18n.t("qa.linked_data.ldpath.evaluate_error") + "... cause: #{e.message}"
end

.ldpath_program(ldpath:, prefixes: {}) ⇒ Ldpath::Program

Create the ldpath program for a given ldpath.

Parameters:

Returns:

  • (Ldpath::Program)

    an executable program that will extract a value from a graph



16
17
18
19
20
21
22
23
24
# File 'app/services/qa/linked_data/ldpath_service.rb', line 16

def self.ldpath_program(ldpath:, prefixes: {})
  program_code = ""
  prefixes.each { |key, url| program_code << "@prefix #{key} : <#{url}> \;\n" }
  program_code << "property = #{ldpath} \;"
  Ldpath::Program.parse program_code
rescue => e
  Rails.logger.warn("WARNING: #{I18n.t('qa.linked_data.ldpath.parse_logger_error')}... cause: #{e.message}\n   ldpath_program=\n#{program_code}")
  raise StandardError, I18n.t("qa.linked_data.ldpath.parse_error") + "... cause: #{e.message}"
end