Class: Cellect::Server::Workflow
- Inherits:
-
Object
- Object
- Cellect::Server::Workflow
- Includes:
- Celluloid
- Defined in:
- lib/cellect/server/workflow.rb
Direct Known Subclasses
Constant Summary collapse
- SET_KLASS =
Provide a lookup for matching sets to workflow criteria
{ # priority, pairwise [ false, false ] => DiffSet::RandomSet, [ false, true ] => DiffSet::PairwiseRandomSet, [ true, false ] => DiffSet::PrioritySet, [ true, true ] => DiffSet::PairwisePrioritySet }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#pairwise ⇒ Object
Returns the value of attribute pairwise.
-
#prioritized ⇒ Object
Returns the value of attribute prioritized.
-
#state ⇒ Object
Returns the value of attribute state.
-
#subjects ⇒ Object
Returns the value of attribute subjects.
-
#users ⇒ Object
Returns the value of attribute users.
Class Method Summary collapse
-
.[](name) ⇒ Object
Look up and/or load a workflow.
-
.[]=(name, opts) ⇒ Object
Load a workflow.
-
.all ⇒ Object
All currently loaded workflows.
-
.names ⇒ Object
The names of all workflows currently loaded.
Instance Method Summary collapse
-
#add(opts = { }) ⇒ Object
Adds or updates a subject.
-
#add_seen_for(user_id, *subject_ids) ⇒ Object
Add subjects to a users seen set.
- #grouped? ⇒ Boolean
-
#initialize(name, pairwise: false, prioritized: false) ⇒ Workflow
constructor
Sets up a new workflow and starts the data loading.
-
#load_data ⇒ Object
Loads subjects from the adapter.
- #pairwise? ⇒ Boolean
- #prioritized? ⇒ Boolean
- #ready? ⇒ Boolean
-
#remove(opts = { }) ⇒ Object
Removes a subject.
-
#remove_user(user_id) ⇒ Object
Unload a user.
-
#sample(opts = { }) ⇒ Object
Get a sample of subjects for a user.
-
#set_klass ⇒ Object
Looks up the set class.
-
#status ⇒ Object
General information about this workflow.
-
#unseen_for(user_id, limit: 5) ⇒ Object
Get unseen subjects for a user.
-
#user(id) ⇒ Object
Look up and/or load a user.
Constructor Details
#initialize(name, pairwise: false, prioritized: false) ⇒ Workflow
Sets up a new workflow and starts the data loading
33 34 35 36 37 38 39 40 |
# File 'lib/cellect/server/workflow.rb', line 33 def initialize(name, pairwise: false, prioritized: false) self.name = name self.users = { } self.pairwise = !!pairwise self.prioritized = !!prioritized self.subjects = set_klass.new load_data end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def name @name end |
#pairwise ⇒ Object
Returns the value of attribute pairwise.
7 8 9 |
# File 'lib/cellect/server/workflow.rb', line 7 def pairwise @pairwise end |
#prioritized ⇒ Object
Returns the value of attribute prioritized.
7 8 9 |
# File 'lib/cellect/server/workflow.rb', line 7 def prioritized @prioritized end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def state @state end |
#subjects ⇒ Object
Returns the value of attribute subjects.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def subjects @subjects end |
#users ⇒ Object
Returns the value of attribute users.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def users @users end |
Class Method Details
.[](name) ⇒ Object
Look up and/or load a workflow
10 11 12 13 |
# File 'lib/cellect/server/workflow.rb', line 10 def self.[](name) Cellect::Server.adapter.load_workflows(name) unless Actor[name] Actor[name].actors.first end |
.[]=(name, opts) ⇒ Object
Load a workflow
16 17 18 |
# File 'lib/cellect/server/workflow.rb', line 16 def self.[]=(name, opts) Actor[name] = supervise name, pairwise: opts['pairwise'], prioritized: opts['prioritized'] end |
.all ⇒ Object
All currently loaded workflows
28 29 30 |
# File 'lib/cellect/server/workflow.rb', line 28 def self.all names.collect{ |name| Workflow[name] } end |
.names ⇒ Object
The names of all workflows currently loaded
21 22 23 24 25 |
# File 'lib/cellect/server/workflow.rb', line 21 def self.names actor_names = Celluloid.actor_system.registry.names.collect &:to_s workflow_actors = actor_names.select{ |key| key =~ /^workflow_/ } workflow_actors.collect{ |name| name.sub(/^workflow_/, '').to_sym } end |
Instance Method Details
#add(opts = { }) ⇒ Object
Adds or updates a subject
Accepts a hash in the form:
subject_id: 1,
priority: 0.5 # (if the workflow is prioritized)
98 99 100 101 102 103 104 |
# File 'lib/cellect/server/workflow.rb', line 98 def add(opts = { }) if prioritized? subjects.add opts[:subject_id], opts[:priority] else subjects.add opts[:subject_id] end end |
#add_seen_for(user_id, *subject_ids) ⇒ Object
Add subjects to a users seen set
64 65 66 67 68 |
# File 'lib/cellect/server/workflow.rb', line 64 def add_seen_for(user_id, *subject_ids) [subject_ids].flatten.compact.each do |subject_id| user(user_id).seen.add subject_id end end |
#grouped? ⇒ Boolean
124 125 126 |
# File 'lib/cellect/server/workflow.rb', line 124 def grouped? false end |
#load_data ⇒ Object
Loads subjects from the adapter
43 44 45 46 47 48 49 50 |
# File 'lib/cellect/server/workflow.rb', line 43 def load_data self.state = :initializing self.subjects = set_klass.new Cellect::Server.adapter.load_data_for(name).each do |hash| subjects.add hash['id'], hash['priority'] end self.state = :ready end |
#pairwise? ⇒ Boolean
116 117 118 |
# File 'lib/cellect/server/workflow.rb', line 116 def pairwise? !!pairwise end |
#prioritized? ⇒ Boolean
120 121 122 |
# File 'lib/cellect/server/workflow.rb', line 120 def prioritized? !!prioritized end |
#ready? ⇒ Boolean
128 129 130 |
# File 'lib/cellect/server/workflow.rb', line 128 def ready? state == :ready end |
#remove(opts = { }) ⇒ Object
Removes a subject
Accepts a hash in the form:
subject_id: 1
112 113 114 |
# File 'lib/cellect/server/workflow.rb', line 112 def remove(opts = { }) subjects.remove opts[:subject_id] end |
#remove_user(user_id) ⇒ Object
Unload a user
71 72 73 74 |
# File 'lib/cellect/server/workflow.rb', line 71 def remove_user(user_id) removed = self.users.delete user_id removed.terminate if removed end |
#sample(opts = { }) ⇒ Object
Get a sample of subjects for a user
Accepts a hash in the form:
{
user_id: 123,
limit: 5
}
83 84 85 86 87 88 89 |
# File 'lib/cellect/server/workflow.rb', line 83 def sample(opts = { }) if opts[:user_id] unseen_for opts[:user_id], limit: opts[:limit] else subjects.sample opts[:limit] end end |
#set_klass ⇒ Object
Looks up the set class
142 143 144 |
# File 'lib/cellect/server/workflow.rb', line 142 def set_klass SET_KLASS[[prioritized, pairwise]] end |
#status ⇒ Object
General information about this workflow
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/cellect/server/workflow.rb', line 147 def status { state: state, grouped: false, prioritized: prioritized, pairwise: pairwise, subjects: subjects.size, users: users.length } end |
#unseen_for(user_id, limit: 5) ⇒ Object
Get unseen subjects for a user
59 60 61 |
# File 'lib/cellect/server/workflow.rb', line 59 def unseen_for(user_id, limit: 5) subjects.subtract user(user_id).seen, limit end |
#user(id) ⇒ Object
Look up and/or load a user
53 54 55 56 |
# File 'lib/cellect/server/workflow.rb', line 53 def user(id) self.users[id] ||= User.supervise id, workflow_name: name users[id].actors.first end |