27 |
package org.fenfire.functional; |
package org.fenfire.functional; |
28 |
import org.fenfire.util.*; |
import org.fenfire.util.*; |
29 |
import java.lang.reflect.*; |
import java.lang.reflect.*; |
30 |
|
import java.util.Collections; |
31 |
|
import java.util.Map; |
32 |
|
import java.util.HashMap; |
33 |
|
|
34 |
/** An object that manages a DAG of Function instances, enabling |
/** An object that manages a DAG of Function instances, enabling |
35 |
* transparent and super-lazy caching. |
* transparent and super-lazy caching. |
41 |
public static boolean dbg = false; |
public static boolean dbg = false; |
42 |
private void p(String s) { System.out.println(s); } |
private void p(String s) { System.out.println(s); } |
43 |
|
|
44 |
|
/** The id of the hint for which background thread group |
45 |
|
* a function should use, if it's not run directly. |
46 |
|
* This is useful for using Libvob OpenGL, since |
47 |
|
* OpenGL objects should only be handled in one thread. |
48 |
|
* The value for the OpenGL thread is "OPENGL". |
49 |
|
*/ |
50 |
|
public final static String HINT_BGGROUP= |
51 |
|
"urn:urn-5:Avc8CAG5IlAxiPld5A-eHFpe33lA".intern(); |
52 |
|
|
53 |
|
/** The id of the hint about function speed. |
54 |
|
* If non-null, assumes the function is slow. |
55 |
|
*/ |
56 |
|
public final static String HINT_SLOW= |
57 |
|
"urn:urn-5:6tZ8QgByvj22e8zbzPb4FMb0ATUJ".intern(); |
58 |
|
|
59 |
|
/** The id of the hint for the placeholder return object. |
60 |
|
* The object this hint is set to |
61 |
|
* is returned for super-lazily cached |
62 |
|
* functions when the value is not in the cache. |
63 |
|
*/ |
64 |
|
public final static String HINT_PLACEHOLDER= |
65 |
|
"urn:urn-5:VXY-8ssXNGZFa488N3+dpuMK95GE".intern(); |
66 |
|
|
67 |
|
/** The id of the hint for the error placeholder return object. |
68 |
|
* If an error has occurred evaluating the function, |
69 |
|
* the value of this hint is returned. |
70 |
|
*/ |
71 |
|
public final static String HINT_ERRORPLACEHOLDER= |
72 |
|
"urn:urn-5:eqali8fJteYllPEPaU+0jSooXUOP".intern(); |
73 |
|
|
74 |
/** Hints about a Function class. |
/** Hints about a Function class. |
75 |
* Hints tell the Functional API about a Function: is it slow to evaluate, |
* Hints tell the Functional API about a Function: is it slow to evaluate, |
76 |
* does it need to be evaluated in the OpenGL thread &c. |
* does it need to be evaluated in the OpenGL thread &c. |
86 |
|
|
87 |
protected static class DefaultHints implements Hints { |
protected static class DefaultHints implements Hints { |
88 |
public DefaultHints(HintsMaker maker) { |
public DefaultHints(HintsMaker maker) { |
89 |
this.bgGroup = maker.bgGroup; |
hints = Collections.unmodifiableMap( |
90 |
this.isSlow = maker.isSlow; |
(Map)(maker.hints.clone())); |
|
this.placeholder = maker.placeholder; |
|
91 |
} |
} |
92 |
public final Object bgGroup; |
public final Map hints; |
|
public final boolean isSlow; |
|
|
public final Object placeholder; |
|
93 |
} |
} |
94 |
|
|
95 |
/** An interface for creating Hints objects. |
/** An interface for creating Hints objects. |
96 |
|
* This interface is structured so it can be used as follows: |
97 |
|
* <pre> |
98 |
|
* public class FooFunction implements Function { |
99 |
|
* static public Functional.Hints functionalHints = |
100 |
|
* (new Functional.HintsMaker()) |
101 |
|
* .setHint(Functional.HINT_PLACEHOLDER, "X") |
102 |
|
* .setHint(Functional.HINT_BGGROUP, "OPENGL") |
103 |
|
* .make(); |
104 |
|
* |
105 |
|
* } |
106 |
|
* </pre> |
107 |
*/ |
*/ |
108 |
public static class HintsMaker { |
public static class HintsMaker { |
109 |
private Object bgGroup = null; |
HashMap hints = new HashMap(); |
|
private boolean isSlow = false; |
|
|
private Object placeholder = null; |
|
|
|
|
|
/** This function must be run in a background object |
|
|
* of the given group if it's not run directly. |
|
|
* This is useful for using Libvob OpenGL, since |
|
|
* OpenGL objects should only be handled in one thread. |
|
|
* Default: null. |
|
|
*/ |
|
|
public void setBackgroundGroup(Object id) { |
|
|
this.bgGroup = id; |
|
|
} |
|
|
|
|
|
/** Whether this function usually consumes considerable time |
|
|
* to generate its output, given all its inputs. |
|
|
* Evaluations of functions given as parameters to this function |
|
|
* are not counted. |
|
|
* Default: not slow. |
|
|
*/ |
|
|
public void setSlow(boolean isSlow) { |
|
|
this.isSlow = isSlow; |
|
|
} |
|
110 |
|
|
111 |
/** Set the placeholder object to be returned if the function value |
/** Set the value of a hint. |
112 |
* is not ready yet. |
* @return this object |
|
* Default: null. |
|
113 |
*/ |
*/ |
114 |
public void setPlaceholder(Object o) { |
public HintsMaker setHint(String id, Object value) { |
115 |
this.placeholder = o; |
hints.put(id, value); |
116 |
|
return this; |
117 |
} |
} |
118 |
|
|
119 |
/** Create the Hints object. |
/** Create the Hints object. |