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



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

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
25
# File 'lib/cellect/server/workflow.rb', line 21

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

.allObject

All currently loaded workflows



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

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

.namesObject

The names of all workflows currently loaded



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

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)



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

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



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

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)


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

def grouped?
  false
end

#load_dataObject

Loads subjects from the adapter



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

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)


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

def pairwise?
  !!pairwise
end

#prioritized?Boolean

Returns:

  • (Boolean)


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

def prioritized?
  !!prioritized
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  state == :ready
end

#remove(opts = { }) ⇒ Object

Removes a subject

Accepts a hash in the form:

subject_id: 1



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

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

#remove_user(user_id) ⇒ Object

Unload a user



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

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
}


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

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



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

def set_klass
  SET_KLASS[[prioritized, pairwise]]
end

#statusObject

General information about this workflow



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

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



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

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



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

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