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

Attributes included from SoftwareAgent

#entity_behavior

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EntityConsumer

#assign_generator_activity!, #entities, #entity_source, #set_entity_source!

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.

Parameters:

  • opts (Hash) (defaults to: {})

    a hash of options

See Also:

  • Audumbla::Enrichment


44
45
46
47
48
# File 'lib/krikri/enricher.rb', line 44

def initialize(opts = {})
  @chain = deep_sym(opts.fetch(:chain) { {} })
  @entity_behavior = self.class.entity_behavior
  assign_generator_activity!(opts)
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



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

def chain
  @chain
end

#generator_uriObject (readonly)

Returns the value of attribute generator_uri.



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

def generator_uri
  @generator_uri
end

Class Method Details

.queue_nameObject



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

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/krikri/enricher.rb', line 73

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

#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.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/krikri/enricher.rb', line 56

def run(activity_uri = nil)
  entities.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