Class: Krikri::Enricher

Inherits:
Object
  • Object
show all
Includes:
EntityConsumer, SoftwareAgent
Defined in:
lib/krikri/enricher.rb

Overview

A SoftwareAgent that runs enrichment processes.

Examples:


To enrich records that were mapped by the mapping activity with ID 3:

# Define which enrichments are run, and thier parameters:
chain = {
  'Krikri::Enrichments::StripHtml' => {
    input_fields: [{sourceResource: :title}]
  },
  'Krikri::Enrichments::StripWhitespace' => {
    input_fields: [{sourceResource: :title}]
  }
}
Krikri::Enricher.enqueue({
  generator_uri: 'http://ldp.local.dp.la/ldp/activity/3',
  chain: chain
})

See Also:

  • SoftwareAgent#enqueue
  • Audumbla::Enrichment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EntityConsumer

#assign_generator_activity!

Methods included from SoftwareAgent

#agent_name

Constructor Details

#initialize(opts = {}) ⇒ Enricher

Create a new Enricher, given a hash of options:

generator_uri:  the LDP URI of the Activity that generated the mapped
   records that this one will enrich.
chain:  a hash specifying the input_fields and output_fields, as
   illustrated above, which will be passed to the Enrichment.

See Also:

  • Audumbla::Enrichment

Parameters:

  • (defaults to: {})

    a hash of options



53
54
55
56
57
# File 'lib/krikri/enricher.rb', line 53

def initialize(opts = {})
  @generator_uri = RDF::URI(opts.fetch(:generator_uri))
  @chain = deep_sym(opts.fetch(:chain) { {} })
  assign_generator_activity!(opts)
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



30
31
32
# File 'lib/krikri/enricher.rb', line 30

def chain
  @chain
end

#generator_uriObject (readonly)

Returns the value of attribute generator_uri.



30
31
32
# File 'lib/krikri/enricher.rb', line 30

def generator_uri
  @generator_uri
end

Class Method Details

.queue_nameObject



32
33
34
# File 'lib/krikri/enricher.rb', line 32

def self.queue_name
  :enrichment
end

Instance Method Details

#chain_enrichments!(agg) ⇒ Object

Given an aggregation, take each enrichment specified by the ‘chain’ given in our instantiation, and apply that enrichment, with the given options, modifying the aggregation in-place.



83
84
85
86
87
88
89
90
91
92
# File 'lib/krikri/enricher.rb', line 83

def chain_enrichments!(agg)
  chain.keys.each do |e|
    enrichment = enrichment_cache(e)
    if enrichment.is_a? Audumbla::FieldEnrichment
      agg = do_field_enrichment(agg, enrichment, chain[e])
    else
      agg = do_basic_enrichment(agg, enrichment, chain[e])
    end
  end
end

#entity_behaviorObject



40
41
42
# File 'lib/krikri/enricher.rb', line 40

def entity_behavior
  @entity_behavior ||= Krikri::AggregationEntityBehavior
end

#run(activity_uri = nil) ⇒ Object

Run the enrichmnt.

Take each record that was affected by the activity defined by our instantiation, and apply each enrichment from the enrichment chain.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/krikri/enricher.rb', line 65

def run(activity_uri = nil)
  mapped_records = generator_activity.entities
  mapped_records.each do |rec|
    begin
      chain_enrichments!(rec)
      activity_uri ? rec.save_with_provenance(activity_uri) : rec.save
    rescue => e
      Krikri::Logger.log(:error, 
                         "Enrichment error: #{e.message}\n#{e.backtrace}")
    end
  end
end