| 1 | = Example two: replication = |
| 2 | |
| 3 | {{{ |
| 4 | function job_issued($job, $inst, $user) { |
| 5 | $insts = $job->get_instances(); |
| 6 | if (count($insts) > 1) { |
| 7 | $job->set_priority(0); |
| 8 | } |
| 9 | } |
| 10 | }}} |
| 11 | |
| 12 | {{{ |
| 13 | function job_finished($job, $inst) { |
| 14 | $response = null; |
| 15 | if (get_str('submit', true)) { |
| 16 | $response->have_ellipse = 0; |
| 17 | } else { |
| 18 | $response->have_ellipse = 1; |
| 19 | $response->cx = get_int('pic_x'); |
| 20 | $response->cy = get_int('pic_y'); |
| 21 | } |
| 22 | $inst->update_info($response); |
| 23 | |
| 24 | // see if job is done |
| 25 | // |
| 26 | $insts = $job->get_finished_instances(); |
| 27 | $n = count($insts); |
| 28 | |
| 29 | $results = null; |
| 30 | foreach ($insts as $inst) { |
| 31 | $results[] = $inst->get_info(); |
| 32 | } |
| 33 | for ($i=0; $i<$n-1; $i++) { |
| 34 | $r1 = $results[$i]; |
| 35 | for ($j=$i+1; $j<$n; $j++) { |
| 36 | $r2 = $results[$j]; |
| 37 | if (compatible($r1, $r2)) { |
| 38 | $job->update_state(BOSSA_JOB_DONE); |
| 39 | return; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | if ($n >= 10) { |
| 44 | $job->update_state(BOSSA_JOB_INCONCLUSIVE); |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // two results are compatible if neither found an ellipse, |
| 50 | // or they both did and centers are within 20 pixels |
| 51 | // |
| 52 | function compatible($r1, $r2) { |
| 53 | if ($r1->have_ellipse) { |
| 54 | if ($r2->have_ellipse) { |
| 55 | $dx = ($r1->cx - $r2->cx); |
| 56 | $dy = ($r1->cy - $r2->cy); |
| 57 | $dsq = $dx*$dx + $dy*$dy; |
| 58 | return ($dsq < 400); |
| 59 | } else return false; |
| 60 | } else { |
| 61 | return !$r2->have_ellipse; |
| 62 | } |
| 63 | } |
| 64 | }}} |