Class: Cellect::Server::Workflow

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/cellect/server/workflow.rb

Direct Known Subclasses

GroupedWorkflow

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
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pairwise: false, prioritized: false) ⇒ Workflow

Sets up a new workflow and starts the data loading



38
39
40
41
42
43
44
45
# File 'lib/cellect/server/workflow.rb', line 38

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

Class Attribute Details

.workflow_namesObject

Returns the value of attribute workflow_names.



7
8
9
# File 'lib/cellect/server/workflow.rb', line 7

def workflow_names
  @workflow_names
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/cellect/server/workflow.rb', line 11

def name
  @name
end

#pairwiseObject

Returns the value of attribute pairwise.



12
13
14
# File 'lib/cellect/server/workflow.rb', line 12

def pairwise
  @pairwise
end

#prioritizedObject

Returns the value of attribute prioritized.



12
13
14
# File 'lib/cellect/server/workflow.rb', line 12

def prioritized
  @prioritized
end

#stateObject

Returns the value of attribute state.



11
12
13
# File 'lib/cellect/server/workflow.rb', line 11

def state
  @state
end

#subjectsObject

Returns the value of attribute subjects.



11
12
13
# File 'lib/cellect/server/workflow.rb', line 11

def subjects
  @subjects
end

#usersObject

Returns the value of attribute users.



11
12
13
# File 'lib/cellect/server/workflow.rb', line 11

def users
  @users
end

Class Method Details

.[](name) ⇒ Object

Look up and/or load a workflow



15
16
17
18
# File 'lib/cellect/server/workflow.rb', line 15

def self.[](name)
  Cellect::Server.adapter.load_workflows(name) unless Actor[name]
  Actor[name].actors.first
end

.[]=(name, opts) ⇒ Object

Load a workflow



21
22
23
24
# File 'lib/cellect/server/workflow.rb', line 21

def self.[]=(name, opts)
  Workflow.workflow_names[name] = true
  Actor[name] = supervise name, pairwise: opts['pairwise'], prioritized: opts['prioritized']
end

.allObject

All currently loaded workflows



33
34
35
# File 'lib/cellect/server/workflow.rb', line 33

def self.all
  names.collect{ |name| Workflow[name] }
end

.namesObject

The names of all workflows currently loaded



27
28
29
30
# File 'lib/cellect/server/workflow.rb', line 27

def self.names
  actor_names = Celluloid.actor_system.registry.names.collect &:to_s
  actor_names.select{ |key| workflow_names[key] }
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)



103
104
105
106
107
108
109
# File 'lib/cellect/server/workflow.rb', line 103

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



69
70
71
72
73
# File 'lib/cellect/server/workflow.rb', line 69

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

Returns:

  • (Boolean)


129
130
131
# File 'lib/cellect/server/workflow.rb', line 129

def grouped?
  false
end

#load_dataObject

Loads subjects from the adapter



48
49
50
51
52
53
54
55
# File 'lib/cellect/server/workflow.rb', line 48

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

Returns:

  • (Boolean)


121
122
123
# File 'lib/cellect/server/workflow.rb', line 121

def pairwise?
  !!pairwise
end

#prioritized?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/cellect/server/workflow.rb', line 125

def prioritized?
  !!prioritized
end

#ready?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/cellect/server/workflow.rb', line 133

def ready?
  state == :ready
end

#remove(opts = { }) ⇒ Object

Removes a subject

Accepts a hash in the form:

subject_id: 1



117
118
119
# File 'lib/cellect/server/workflow.rb', line 117

def remove(opts = { })
  subjects.remove opts[:subject_id]
end

#remove_user(user_id) ⇒ Object

Unload a user



76
77
78
79
# File 'lib/cellect/server/workflow.rb', line 76

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
}


88
89
90
91
92
93
94
# File 'lib/cellect/server/workflow.rb', line 88

def sample(opts = { })
  if opts[:user_id]
    unseen_for opts[:user_id], limit: opts[:limit]
  else
    subjects.sample opts[:limit]
  end
end

#set_klassObject

Looks up the set class



147
148
149
# File 'lib/cellect/server/workflow.rb', line 147

def set_klass
  SET_KLASS[[prioritized, pairwise]]
end

#statusObject

General information about this workflow



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cellect/server/workflow.rb', line 152

def status
  {
    name: name,
    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



64
65
66
# File 'lib/cellect/server/workflow.rb', line 64

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



58
59
60
61
# File 'lib/cellect/server/workflow.rb', line 58

def user(id)
  self.users[id] ||= User.supervise id, workflow_name: name
  users[id].actors.first
end