ensembl-hive-java  2.5
DigitFactory.java
Go to the documentation of this file.
1 /*
2  * Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
3  * Copyright [2016-2022] EMBL-European Bioinformatics Institute
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.ensembl.hive.longmult;
19 
20 import java.io.FileDescriptor;
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.stream.Collectors;
26 
28 import org.ensembl.hive.Job;
29 import org.slf4j.LoggerFactory;
30 
31 public class DigitFactory extends BaseRunnable {
32 
33  public static final String SUB_TASKS = "sub_tasks";
34  public static final String TAKE_TIME = "take_time";
35  public static final String A_MULTIPLIER = "a_multiplier";
36  public static final String B_MULTIPLIER = "b_multiplier";
37  public static final String DIGIT = "digit";
38  public static final String PARTIAL_PRODUCT = "partial_product";
39 
40  @Override
41  protected Map<String, Object> getParamDefaults() {
42  return toMap(TAKE_TIME, 0);
43  }
44 
45  @Override
46  protected void fetchInput(Job job) {
47  getLog().debug("Fetching b_multiplier");
48  String bMultiplier = numericParamToStr(job.paramRequired(B_MULTIPLIER)
49  .toString());
50  getLog().debug("b_multiplier=" + bMultiplier);
51  // split the multiplier by digits and store each digit in a hash
52  List<Map<String, Object>> subTasks = Arrays
53  .asList(bMultiplier.split("(?!^)")).stream()
54  .filter(c -> c.matches("[2-9]")).distinct().map(c -> toMap(DIGIT, c))
55  .collect(Collectors.toList());
56  getLog().debug("subTasks=" + subTasks);
57  job.getParameters().setParam(SUB_TASKS, subTasks);
58  }
59 
60  @Override
61  protected void run(Job job) {
62  sleep(job);
63  }
64 
65  protected static void sleep(Job job) {
66  try {
67  Long time = numericParamToLong(job.getParameters().getParam(
68  TAKE_TIME));
69  LoggerFactory.getLogger(DigitFactory.class.getPackage().getName())
70  .info("Sleeping for " + time + "s");
71  Thread.sleep(1000 * time);
72  } catch (InterruptedException e) {
73  // swallow exception
74  }
75  }
76 
77  @Override
78  protected void writeOutput(Job job) {
79  Object subTasks = job.getParameters().getParam(SUB_TASKS);
80  getLog().debug("Writing output " + subTasks + " on branch 2");
81  dataflow(job.getParameters(), (List)subTasks, 2);
82  }
83 
84 }
Map< String, Object > dataflow(ParamContainer params, Collection< Object > outputIds)
void setParam(String paramName, Object value)
static Map< String, Object > toMap(Object... o)
static Long numericParamToLong(Object param)
Object getParam(String paramName)
static String numericParamToStr(Object param)
ParamContainer getParameters()
Definition: Job.java:86
Object paramRequired(String paramName)
Definition: Job.java:141
Map< String, Object > getParamDefaults()