This project has retired. For details please refer to its Attic page.
JythonComputationFactory xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Factory for creating Jython Computation from python scripts
38   */
39  public class JythonComputationFactory implements ComputationFactory {
40    /** The Jython compute function, cached here for fast access */
41    private static volatile PyObject JYTHON_COMPUTATION_MODULE;
42  
43    /** Logger */
44    private static final Logger LOG = Logger.getLogger(JythonUtils.class);
45  
46    /**
47     * Set static python computation module stored
48     *
49     * @param mod python computation module
50     */
51    private static void setPythonComputationModule(PyObject mod) {
52      JYTHON_COMPUTATION_MODULE = mod;
53    }
54  
55    /**
56     * Get python computation module stored
57     *
58     * @return python computation module
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 }