Coverage Report - org.apache.giraph.jython.factories.JythonComputationFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
JythonComputationFactory
0%
0/33
0%
0/6
2
 
 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  0
 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  0
   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  0
     JYTHON_COMPUTATION_MODULE = mod;
 53  0
   }
 54  
 
 55  
   /**
 56  
    * Get python computation module stored
 57  
    *
 58  
    * @return python computation module
 59  
    */
 60  
   private static PyObject getPythonComputationModule() {
 61  0
     return JYTHON_COMPUTATION_MODULE;
 62  
   }
 63  
 
 64  
   @Override
 65  
   public void initialize(ImmutableClassesGiraphConfiguration conf) {
 66  0
     PythonInterpreter interpreter = JythonUtils.getInterpreter();
 67  0
     String className = computationName(conf);
 68  0
     PyObject pyComputationModule = interpreter.get(className);
 69  0
     checkNotNull(pyComputationModule,
 70  
         "Could not find Jython Computation class " + className +
 71  0
         " in loaded scripts: " + ScriptLoader.getLoadedScripts());
 72  0
     setPythonComputationModule(pyComputationModule);
 73  0
   }
 74  
 
 75  
   @Override
 76  
   public Computation createComputation(
 77  
       ImmutableClassesGiraphConfiguration conf) {
 78  0
     checkNotNull(JYTHON_COMPUTATION_MODULE,
 79  
         "Jython Computation class not set in loaded scripts: " +
 80  0
         ScriptLoader.getLoadedScripts());
 81  
 
 82  0
     PyObject pyComputationObj = JYTHON_COMPUTATION_MODULE.__call__();
 83  0
     Object computeObj = pyComputationObj.__tojava__(JythonComputation.class);
 84  0
     if (!(computeObj instanceof JythonComputation)) {
 85  0
       throw new IllegalStateException("getComputation: Jython object " +
 86  0
           computationName(conf) + " is not a JythonComputation");
 87  
     }
 88  0
     JythonComputation jythonCompute = (JythonComputation) computeObj;
 89  0
     conf.configureIfPossible(jythonCompute);
 90  
 
 91  0
     JythonGiraphComputation giraphCompute =
 92  
         new JythonGiraphComputation(jythonCompute);
 93  0
     giraphCompute.setConf(conf);
 94  
 
 95  0
     jythonCompute.setGiraphCompute(giraphCompute);
 96  
 
 97  0
     return giraphCompute;
 98  
   }
 99  
 
 100  
   @Override
 101  
   public void checkConfiguration(ImmutableClassesGiraphConfiguration conf) {
 102  0
     if (SCRIPTS_TO_LOAD.isDefaultValue(conf)) {
 103  0
       throw new IllegalStateException("checkConfiguration: " +
 104  0
           SCRIPTS_TO_LOAD.getKey() + " not set in configuration");
 105  
     }
 106  0
     if (!JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.contains(conf)) {
 107  0
       throw new IllegalStateException("checkConfiguration: " +
 108  0
           JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.getKey() +
 109  
           " not set in configuration");
 110  
     }
 111  0
   }
 112  
 
 113  
   @Override
 114  
   public String computationName(GiraphConfiguration conf) {
 115  0
     return JythonOptions.JYTHON_COMPUTATION_CLASS_NAME.get(conf);
 116  
   }
 117  
 }