1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.jython;
1920import org.apache.giraph.graph.AbstractComputation;
21import org.apache.giraph.graph.GraphType;
22import org.apache.giraph.graph.Language;
23import org.apache.giraph.graph.Vertex;
24import org.apache.giraph.jython.wrappers.JythonWritableWrapper;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.io.WritableComparable;
27import org.python.core.PyObject;
2829import com.google.common.base.Preconditions;
3031import java.io.IOException;
3233/**34 * The {@link org.apache.giraph.graph.Computation} class for using35 * Jython with Giraph. This class implements the Giraph necessary36 * interfaces but it actually holds a reference to the37 * {@link JythonComputation} which does the real work.38 *39 * The two classes are linked and together they allow us to coerce Jython types40 * to Writables.41 *42 * @param <I> Vertex ID43 * @param <V> Vertex value44 * @param <E> Edge value45 * @param <M1> Incoming message value46 * @param <M2> Outgoing message value47 */48publicclass JythonGiraphComputation<I extends WritableComparable,
49 V extends Writable, E extends Writable, M1 extends Writable,
50 M2 extends Writable>
51extends AbstractComputation<I, V, E, M1, M2> {
52/** The user's computation class */53privatefinalJythonComputation jythonComputation;
5455/**56 * Constructor57 *58 * @param jythonComputation the user's Jython computation59 */60publicJythonGiraphComputation(JythonComputation jythonComputation) {
61this.jythonComputation = jythonComputation;
62 }
6364 @Override publicvoid compute(Vertex<I, V, E> vertex, Iterable<M1> messages)
65throws IOException {
66 jythonComputation.compute(vertex, messages);
67 }
6869/**70 * Wrap a vertex id in a {@link WritableComparable} wrapper if necessary71 *72 * @param object data to wrap73 * @return writable value74 */75public WritableComparable wrapIdIfNecessary(Object object) {
76return wrapIfNecessary(object, GraphType.VERTEX_ID);
77 }
7879/**80 * Wrap a user value (IVEMM) in a {@link Writable} wrapper if necessary81 *82 * @param object data to wrap83 * @param graphType type of data (IVEMM)84 * @param <W> writable type85 * @return writable value86 */87public <W extends Writable> W
88 wrapIfNecessary(Object object, GraphType graphType) {
89if (graphType.interfaceClass().isInstance(object)) {
90return (W) object;
91 }
92if (getConf().getValueLanguages().get(graphType) == Language.JYTHON &&
93 getConf().getValueNeedsWrappers().get(graphType)) {
94 Preconditions.checkArgument(object instanceof PyObject);
95return (W) new JythonWritableWrapper((PyObject) object);
96 } else {
97return (W) object;
98 }
99 }
100 }