This project has retired. For details please refer to its
Attic page.
JythonComputationFactory xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.jython.factories;
19
20 import org.apache.giraph.conf.GiraphConfiguration;
21 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22 import org.apache.giraph.factories.ComputationFactory;
23 import org.apache.giraph.graph.Computation;
24 import org.apache.giraph.jython.JythonComputation;
25 import org.apache.giraph.jython.JythonGiraphComputation;
26 import org.apache.giraph.jython.JythonOptions;
27 import org.apache.giraph.jython.JythonUtils;
28 import org.apache.giraph.scripting.ScriptLoader;
29 import org.apache.log4j.Logger;
30 import org.python.core.PyObject;
31 import org.python.util.PythonInterpreter;
32
33 import static com.google.common.base.Preconditions.checkNotNull;
34 import static org.apache.giraph.scripting.ScriptLoader.SCRIPTS_TO_LOAD;
35
36
37
38
39 public class JythonComputationFactory implements ComputationFactory {
40
41 private static volatile PyObject JYTHON_COMPUTATION_MODULE;
42
43
44 private static final Logger LOG = Logger.getLogger(JythonUtils.class);
45
46
47
48
49
50
51 private static void setPythonComputationModule(PyObject mod) {
52 JYTHON_COMPUTATION_MODULE = mod;
53 }
54
55
56
57
58
59
60 private static PyObject getPythonComputationModule() {
61 return JYTHON_COMPUTATION_MODULE;
62 }
63
64 @Override
65 public void initialize(ImmutableClassesGiraphConfiguration conf) {
66 PythonInterpreter interpreter = JythonUtils.getInterpreter();
67 String className = computationName(conf);
68 PyObject pyComputationModule = interpreter.get(className);
69 checkNotNull(pyComputationModule,
70 "Could not find Jython Computation class " + className +
71 " in loaded scripts: " + ScriptLoader.getLoadedScripts());
72 setPythonComputationModule(pyComputationModule);
73 }
74
75 @Override
76 public Computation createComputation(
77 ImmutableClassesGiraphConfiguration conf) {
78 checkNotNull(JYTHON_COMPUTATION_MODULE,
79 "Jython Computation class not set in loaded scripts: " +
80 ScriptLoader.getLoadedScripts());
81
82 PyObject pyComputationObj = JYTHON_COMPUTATION_MODULE.__call__();
83 Object computeObj = pyComputationObj.__tojava__(JythonComputation.class);
84 if (!(computeObj instanceof JythonComputation)) {
85 throw new IllegalStateException("getComputation: Jython object " +
86 computationName(conf) + " is not a JythonComputation");
87 }
88 JythonComputation jythonCompute = (JythonComputation) computeObj;
89 conf.configureIfPossible(jythonCompute);
90
91 JythonGiraphComputation giraphCompute =
92 new JythonGiraphComputation(jythonCompute);
93 giraphCompute.setConf(conf);
94
95 jythonCompute.setGiraphCompute(giraphCompute);
96
97 return giraphCompute;
98 }
99
100 @Override
101 public void checkConfiguration(ImmutableClassesGiraphConfiguration conf) {
102 if (SCRIPTS_TO_LOAD.isDefaultValue(conf)) {
103 throw new IllegalStateException("checkConfiguration: " +
104 SCRIPTS_TO_LOAD.getKey() + " not set in configuration");
105 }
106 if (!JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.contains(conf)) {
107 throw new IllegalStateException("checkConfiguration: " +
108 JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.getKey() +
109 " not set in configuration");
110 }
111 }
112
113 @Override
114 public String computationName(GiraphConfiguration conf) {
115 return JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.get(conf);
116 }
117 }