Remove the JacobRunnable class and use java.lang.Runnable instead
Project: http://git-wip-us.apache.org/repos/asf/ode-jacob/repo Commit: http://git-wip-us.apache.org/repos/asf/ode-jacob/commit/1559bfe0 Tree: http://git-wip-us.apache.org/repos/asf/ode-jacob/tree/1559bfe0 Diff: http://git-wip-us.apache.org/repos/asf/ode-jacob/diff/1559bfe0 Branch: refs/heads/master Commit: 1559bfe07773d9548ecc640e08753f3283ea8201 Parents: a6c65fa Author: Hadrian Zbarcea <[email protected]> Authored: Thu Jul 25 21:29:08 2013 -0400 Committer: Hadrian Zbarcea <[email protected]> Committed: Thu Jul 25 21:29:08 2013 -0400 ---------------------------------------------------------------------- .../java/org/apache/ode/jacob/JacobObject.java | 8 +- .../org/apache/ode/jacob/JacobRunnable.java | 99 -------------------- .../java/org/apache/ode/jacob/JacobThread.java | 2 +- .../java/org/apache/ode/jacob/oo/ClassUtil.java | 18 ++++ .../java/org/apache/ode/jacob/vpu/JacobVPU.java | 20 +--- .../apache/ode/jacob/examples/cell/CELL_.java | 4 +- .../ode/jacob/examples/cell/JacobCellTest.java | 4 +- .../ode/jacob/examples/eratosthenes/Sieve.java | 12 +-- .../jacob/examples/helloworld/HelloWorld.java | 18 ++-- .../ode/jacob/examples/sequence/Sequence.java | 6 +- .../ode/jacob/examples/synch/SynchPrinter.java | 10 +- 11 files changed, 57 insertions(+), 144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/main/java/org/apache/ode/jacob/JacobObject.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/ode/jacob/JacobObject.java b/src/main/java/org/apache/ode/jacob/JacobObject.java index bc0eb61..73505d7 100644 --- a/src/main/java/org/apache/ode/jacob/JacobObject.java +++ b/src/main/java/org/apache/ode/jacob/JacobObject.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.ode.jacob.oo.Channel; import org.apache.ode.jacob.oo.ChannelListener; +import org.apache.ode.jacob.oo.ClassUtil; import org.apache.ode.jacob.vpu.JacobVPU; /** @@ -32,7 +33,9 @@ import org.apache.ode.jacob.vpu.JacobVPU; */ @SuppressWarnings("serial") public abstract class JacobObject implements Serializable { - public abstract Set<Method> getImplementedMethods(); + public Set<Method> getImplementedMethods() { + return null; + } /** * Get the unadorned (no package) name of this class. @@ -56,7 +59,7 @@ public abstract class JacobObject implements Serializable { * * @param concretion the concretion of a process template */ - protected static void instance(JacobRunnable concretion) { + protected static void instance(Runnable concretion) { JacobVPU.activeJacobThread().instance(concretion); } @@ -111,6 +114,7 @@ public abstract class JacobObject implements Serializable { public Method getMethod(String methodName) { Set<Method> implementedMethods = getImplementedMethods(); + implementedMethods = implementedMethods == null ? ClassUtil.runMethodSet() : implementedMethods; for (Method m : implementedMethods) { if (m.getName().equals(methodName)) { return m; http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/main/java/org/apache/ode/jacob/JacobRunnable.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/ode/jacob/JacobRunnable.java b/src/main/java/org/apache/ode/jacob/JacobRunnable.java deleted file mode 100644 index 7cf6a05..0000000 --- a/src/main/java/org/apache/ode/jacob/JacobRunnable.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.ode.jacob; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Set; - - -/** - * Base class for process abstractions. An abstraction is a parameterized - * process template, whose instantiation is termed a <em>concretion</em>. - * Abstractions may define a set of bound channel names or other parameters - * which are resolved at the time of the concretion. For example the process - * term abstraction of a memory cell: - * <code>Cell(s,v) := s ? { read(...) = ... & write(...) = ... }</code> would - * be represented by the following Java class: <code> - * <pre> - * public class CellProcess extends JacobRunnable { - * private Cell s; - * - * private Object v; - * - * public CellProcess(Cell s, Object v) { - * this.s = s; - * this.v = v; - * } - * - * public void run() { - * object(new ReceiveProcess { read(...) {...} - * write(...) {...} } ); - * } - * } - * </pre> - * </code> An example of the Java expression representing the concretion of this - * abstraction would look like: <code> - * <pre> - * . - * . - * // (new c) CellProcess(c,v) - * Integer v = Integer.valueOf(0); - * Cell c = (Cell)newChannel(Cell.class); - * instance(new Cell(c, v)); - * . - * . - * </pre> - * </code> - * - * @author Maciej Szefler <a href="mailto:[email protected]" /> - */ -@SuppressWarnings("serial") -public abstract class JacobRunnable extends JacobObject { - private static final Set<Method> IMPLEMENTED_METHODS; - - static { - try { - Method m = JacobRunnable.class.getMethod("run", new Class[]{}); - IMPLEMENTED_METHODS = Collections.singleton(m); - } catch (NoSuchMethodException e) { - throw new AssertionError(e); - } - } - - public Set<Method> getImplementedMethods() { - return IMPLEMENTED_METHODS; - } - - /** - * Peform the template reduction, i.e. do whatever it is that the - * templetized process does. This method may do some combination of in-line - * Java, and JACOB operations. - * <p> - * <em>Note that JACOB operations are performed in parallel, so the - * sequencing of JACOB operations is irrelevant</em> - */ - public abstract void run(); - - public String toString() { - return getClassName() + "(...)"; - } - -} http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/main/java/org/apache/ode/jacob/JacobThread.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/ode/jacob/JacobThread.java b/src/main/java/org/apache/ode/jacob/JacobThread.java index a33f5dc..0c1d1c2 100644 --- a/src/main/java/org/apache/ode/jacob/JacobThread.java +++ b/src/main/java/org/apache/ode/jacob/JacobThread.java @@ -42,7 +42,7 @@ public interface JacobThread { /** * Create a process instance i.e. a concretion of a process abstraction. */ - public void instance(JacobRunnable concretion); + public void instance(Runnable concretion); /** * Send a message (object invocation). This method shouldn't really be used http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/main/java/org/apache/ode/jacob/oo/ClassUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/ode/jacob/oo/ClassUtil.java b/src/main/java/org/apache/ode/jacob/oo/ClassUtil.java index 992d123..a7bbd03 100644 --- a/src/main/java/org/apache/ode/jacob/oo/ClassUtil.java +++ b/src/main/java/org/apache/ode/jacob/oo/ClassUtil.java @@ -19,14 +19,32 @@ package org.apache.ode.jacob.oo; import java.lang.reflect.Method; +import java.util.HashSet; import java.util.Set; public final class ClassUtil { + public static final Method RUN_METHOD; + private static final Set<Method> RUN_METHOD_SET = new HashSet<Method>(); + + static { + try { + // Resolve the {@link Runnable#run} method once statically + RUN_METHOD = Runnable.class.getMethod("run", new Class[]{}); + RUN_METHOD_SET.add(RUN_METHOD); + } catch (Exception e) { + throw new Error("Cannot resolve 'run()' method", e); + } + } + private ClassUtil() { // Utility class } + public static Set<Method> runMethodSet() { + return RUN_METHOD_SET; + } + public static Set<Method> getImplementedMethods(Set<Method> methods, Class<?> clazz) { // TODO: this can be optimized (some 20 times faster in my tests) by keeping a private // map of interfaces to methods: Map<Class<?>, Method[]> and just do lookups http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/main/java/org/apache/ode/jacob/vpu/JacobVPU.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/ode/jacob/vpu/JacobVPU.java b/src/main/java/org/apache/ode/jacob/vpu/JacobVPU.java index 985a402..f9eddf8 100644 --- a/src/main/java/org/apache/ode/jacob/vpu/JacobVPU.java +++ b/src/main/java/org/apache/ode/jacob/vpu/JacobVPU.java @@ -25,10 +25,10 @@ import java.util.Map; import java.util.Stack; import org.apache.ode.jacob.JacobObject; -import org.apache.ode.jacob.JacobRunnable; import org.apache.ode.jacob.JacobThread; import org.apache.ode.jacob.oo.Channel; import org.apache.ode.jacob.oo.ChannelListener; +import org.apache.ode.jacob.oo.ClassUtil; import org.apache.ode.jacob.oo.CompositeProcess; import org.apache.ode.jacob.oo.ReceiveProcess; import org.apache.ode.jacob.oo.Synch; @@ -51,16 +51,6 @@ public final class JacobVPU { // Thread-local for associating a thread with a VPU. Needs to be stored in a stack to allow reentrance. private static final ThreadLocal<Stack<JacobThread>> ACTIVE_THREAD = new ThreadLocal<Stack<JacobThread>>(); - private static final Method REDUCE_METHOD; - - static { - try { - // Resolve the {@link JacobRunnable#run} method once statically - REDUCE_METHOD = JacobRunnable.class.getMethod("run", new Class[]{}); - } catch (Exception e) { - throw new Error("Cannot resolve 'run()' method", e); - } - } /** * Persisted cross-VPU state (state of the channels) @@ -176,9 +166,9 @@ public final class JacobVPU { * the injected process. This method is equivalent to the parallel operator, * but is intended to be used from outside of an active {@link JacobThread}. */ - public void inject(JacobRunnable concretion) { + public void inject(Runnable concretion) { LOG.debug("injecting {}", concretion); - addReaction(concretion, REDUCE_METHOD, new Class[]{}, + addReaction((JacobObject)concretion, ClassUtil.RUN_METHOD, new Class[]{}, (LOG.isInfoEnabled() ? concretion.toString() : null)); } @@ -270,11 +260,11 @@ public final class JacobVPU { } } - public void instance(JacobRunnable template) { + public void instance(Runnable template) { LOG.trace(">> [{}] : {}", _cycle, template); _statistics.numReductionsStruct++; - addReaction(template, REDUCE_METHOD, new Class[]{}, + addReaction((JacobObject)template, ClassUtil.RUN_METHOD, new Class[]{}, LOG.isInfoEnabled() ? template.toString() : null); } http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/cell/CELL_.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/cell/CELL_.java b/src/test/java/org/apache/ode/jacob/examples/cell/CELL_.java index e320085..35917b7 100644 --- a/src/test/java/org/apache/ode/jacob/examples/cell/CELL_.java +++ b/src/test/java/org/apache/ode/jacob/examples/cell/CELL_.java @@ -18,7 +18,7 @@ */ package org.apache.ode.jacob.examples.cell; -import org.apache.ode.jacob.JacobRunnable; +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.oo.ReceiveProcess; import org.apache.ode.jacob.oo.Val; @@ -29,7 +29,7 @@ import org.apache.ode.jacob.oo.Val; * Cell(self, val) = self ? [ read(r) = { Cell(self, val) | r ! val(val) } & write(newVal) = { Cell(self, newVal) } ] * </code> */ -public class CELL_<T> extends JacobRunnable { +public class CELL_<T> extends JacobObject implements Runnable { private static final long serialVersionUID = 1550566086202728251L; private Cell _self; http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/cell/JacobCellTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/cell/JacobCellTest.java b/src/test/java/org/apache/ode/jacob/examples/cell/JacobCellTest.java index 5215b1a..5ab4015 100644 --- a/src/test/java/org/apache/ode/jacob/examples/cell/JacobCellTest.java +++ b/src/test/java/org/apache/ode/jacob/examples/cell/JacobCellTest.java @@ -25,7 +25,7 @@ import java.io.IOException; import junit.framework.TestCase; -import org.apache.ode.jacob.JacobRunnable; +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.oo.Val; import org.apache.ode.jacob.vpu.ExecutionQueueImpl; import org.apache.ode.jacob.vpu.JacobVPU; @@ -58,7 +58,7 @@ public class JacobCellTest extends TestCase { } @SuppressWarnings("serial") - static class CellTest1 extends JacobRunnable { + static class CellTest1 extends JacobObject implements Runnable { public void run() { Cell cell = newChannel(Cell.class, "cell"); Val ret = newChannel(Val.class, "val"); http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/eratosthenes/Sieve.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/eratosthenes/Sieve.java b/src/test/java/org/apache/ode/jacob/examples/eratosthenes/Sieve.java index 201fac6..ae55528 100644 --- a/src/test/java/org/apache/ode/jacob/examples/eratosthenes/Sieve.java +++ b/src/test/java/org/apache/ode/jacob/examples/eratosthenes/Sieve.java @@ -18,7 +18,7 @@ */ package org.apache.ode.jacob.examples.eratosthenes; -import org.apache.ode.jacob.JacobRunnable; +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.oo.ReceiveProcess; import org.apache.ode.jacob.oo.Synch; import org.apache.ode.jacob.vpu.ExecutionQueueImpl; @@ -36,7 +36,7 @@ import org.apache.ode.jacob.vpu.JacobVPU; * * @author Maciej Szefler <a href="mailto:[email protected]">mbs</a> */ -public class Sieve extends JacobRunnable { +public class Sieve extends JacobObject implements Runnable { private static final long serialVersionUID = -1303509567096202776L; private static int _cnt = 0; @@ -58,7 +58,7 @@ public class Sieve extends JacobRunnable { * Counter(out, n) := out.val(n) | Counter(out, n+1) * </em></pre> */ - private static class Counter extends JacobRunnable { + private static class Counter extends JacobObject implements Runnable { private static final long serialVersionUID = 4739323750438991003L; private NaturalNumberStream _out; @@ -92,7 +92,7 @@ public class Sieve extends JacobRunnable { * * */ - private static final class Head extends JacobRunnable { + private static final class Head extends JacobObject implements Runnable { private static final long serialVersionUID = 1791641314141082728L; NaturalNumberStream _in; @@ -125,7 +125,7 @@ public class Sieve extends JacobRunnable { } } - private static final class Print extends JacobRunnable { + private static final class Print extends JacobObject implements Runnable { private static final long serialVersionUID = -3134193737519487672L; private NaturalNumberStream _in; @@ -154,7 +154,7 @@ public class Sieve extends JacobRunnable { * ! in ? [val(n)={ if(n mod prime <> 0) out.val(n) } * </em></prime> */ - private static class PrimeFilter extends JacobRunnable { + private static class PrimeFilter extends JacobObject implements Runnable { private static final long serialVersionUID = 1569523200422202448L; private int _prime; http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/helloworld/HelloWorld.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/helloworld/HelloWorld.java b/src/test/java/org/apache/ode/jacob/examples/helloworld/HelloWorld.java index 705e2e6..92dc03c 100644 --- a/src/test/java/org/apache/ode/jacob/examples/helloworld/HelloWorld.java +++ b/src/test/java/org/apache/ode/jacob/examples/helloworld/HelloWorld.java @@ -18,7 +18,7 @@ */ package org.apache.ode.jacob.examples.helloworld; -import org.apache.ode.jacob.JacobRunnable; +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.examples.sequence.Sequence; import org.apache.ode.jacob.oo.Channel; import org.apache.ode.jacob.oo.ReceiveProcess; @@ -44,13 +44,13 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory; * */ @SuppressWarnings("serial") -public class HelloWorld extends JacobRunnable { +public class HelloWorld extends JacobObject implements Runnable { public static interface Callback<T, R extends Channel> extends Channel { public void invoke(T value, R callback); } - static class ReliablePrinterProcess extends JacobRunnable { + static class ReliablePrinterProcess extends JacobObject implements Runnable { private Callback<String, Synch> in; @JsonCreator @@ -71,7 +71,7 @@ public class HelloWorld extends JacobRunnable { } } - static class ReliableStringEmitterProcess extends JacobRunnable { + static class ReliableStringEmitterProcess extends JacobObject implements Runnable { private String str; private Callback<String, Synch> to; @@ -102,7 +102,7 @@ public class HelloWorld extends JacobRunnable { } } - static class PrinterProcess extends JacobRunnable { + static class PrinterProcess extends JacobObject implements Runnable { private Val _in; @JsonCreator @@ -122,7 +122,7 @@ public class HelloWorld extends JacobRunnable { } } - static class StringEmitterProcess extends JacobRunnable { + static class StringEmitterProcess extends JacobObject implements Runnable { private String str; private Val to; @@ -137,7 +137,7 @@ public class HelloWorld extends JacobRunnable { } } - static class ForwarderProcess extends JacobRunnable { + static class ForwarderProcess extends JacobObject implements Runnable { private Val in; private Val out; @@ -224,11 +224,11 @@ public class HelloWorld extends JacobRunnable { } @Override - protected JacobRunnable doStep(int step, Synch done) { + protected Runnable doStep(int step, Synch done) { return new SequenceItemEmitter(greetings[step], done, out); } - static class SequenceItemEmitter extends JacobRunnable { + static class SequenceItemEmitter extends JacobObject implements Runnable { private final String string; private final Synch done; private final Val out; http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/sequence/Sequence.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/sequence/Sequence.java b/src/test/java/org/apache/ode/jacob/examples/sequence/Sequence.java index d137501..aa7ae05 100644 --- a/src/test/java/org/apache/ode/jacob/examples/sequence/Sequence.java +++ b/src/test/java/org/apache/ode/jacob/examples/sequence/Sequence.java @@ -18,7 +18,7 @@ */ package org.apache.ode.jacob.examples.sequence; -import org.apache.ode.jacob.JacobRunnable; +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.oo.ReceiveProcess; import org.apache.ode.jacob.oo.Synch; @@ -29,7 +29,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Abstract process that executes a number of steps sequentially. */ @SuppressWarnings("serial") -public abstract class Sequence extends JacobRunnable { +public abstract class Sequence extends JacobObject implements Runnable { private final SequenceData data = new SequenceData(); /** @@ -62,7 +62,7 @@ public abstract class Sequence extends JacobRunnable { * @param done notification after step completion * @return runnable process */ - protected abstract JacobRunnable doStep(int step, Synch done); + protected abstract Runnable doStep(int step, Synch done); public static class SequenceData { public int _steps; http://git-wip-us.apache.org/repos/asf/ode-jacob/blob/1559bfe0/src/test/java/org/apache/ode/jacob/examples/synch/SynchPrinter.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/ode/jacob/examples/synch/SynchPrinter.java b/src/test/java/org/apache/ode/jacob/examples/synch/SynchPrinter.java index 41e095b..22a82f6 100644 --- a/src/test/java/org/apache/ode/jacob/examples/synch/SynchPrinter.java +++ b/src/test/java/org/apache/ode/jacob/examples/synch/SynchPrinter.java @@ -18,14 +18,14 @@ */ package org.apache.ode.jacob.examples.synch; -import org.apache.ode.jacob.JacobRunnable; +import static org.apache.ode.jacob.oo.ProcessUtil.receive; + +import org.apache.ode.jacob.JacobObject; import org.apache.ode.jacob.oo.ReceiveProcess; import org.apache.ode.jacob.oo.Synch; import org.apache.ode.jacob.vpu.ExecutionQueueImpl; import org.apache.ode.jacob.vpu.JacobVPU; -import static org.apache.ode.jacob.oo.ProcessUtil.receive; - /** * Example JACOB process illustrating the use of {@link SynchPrint} * @@ -33,7 +33,7 @@ import static org.apache.ode.jacob.oo.ProcessUtil.receive; */ public class SynchPrinter { - public static final class SystemPrinter extends JacobRunnable { + public static final class SystemPrinter extends JacobObject implements Runnable { private static final long serialVersionUID = -8516348116865575605L; private SynchPrint _self; @@ -55,7 +55,7 @@ public class SynchPrinter { } } - public static final class Tester extends JacobRunnable { + public static final class Tester extends JacobObject implements Runnable { private static final long serialVersionUID = 7899682832271627464L; public void run() {
