-
- All Superinterfaces:
Comparable<OpEnvironment>,Prioritized<OpEnvironment>
public interface OpEnvironment extends Prioritized<OpEnvironment>
An op environment is the top-level entry point into op execution. It provides all the built-in functionality of ops in a single place, including:- The pool of available ops, from which candidates are chosen.
- Type-safe, built-in method signatures for all op implementations.
- Selection (a.k.a. "matching") of op implementations from
OpRequestdescriptors.
Customizing the
OpEnvironmentallows customization of any or all of the above. Potential use cases include:- Limiting or extending the pool of available op implementations.
- Caching op outputs to improve subsequent time performance.
- Configuration of environment "hints" to improve performance in time or space.
- Author:
- Curtis Rueden, Gabriel Selzer
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static OpEnvironmentbuild()Generates anOpEnvironmentwith all available Ops.static OpEnvironmentbuildEmpty()Generates an emptyOpEnvironment, which can be populated with the Ops of the caller's choice.voiddiscoverEverything()voiddiscoverUsing(Discoverer... d)TypegenericType(Object obj)Discerns the generic type of an object instance.HintsgetDefaultHints()Returns a copy of the defaultHintsobject.default Stringhelp()Returns the descriptions for all Ops contained within thisOpEnvironmentdefault Stringhelp(String name)Returns the descriptions for all Ops contained within thisOpEnvironmentmatchingnameStringhelp(OpRequest request)Returns simple descriptions for all Ops identifiable by a given name within thisOpEnvironmentdefault StringhelpVerbose()Returns verbose descriptions for all Ops contained within thisOpEnvironmentdefault StringhelpVerbose(String name)Returns verbose descriptions for all Ops contained within thisOpEnvironmentStringhelpVerbose(OpRequest request)Returns verbose descriptions for all Ops contained within thisOpEnvironmentOpHistoryhistory()default SortedSet<OpInfo>infos()Obtains all Ops in theOpEnvironment, sorted by priority.SortedSet<OpInfo>infos(String name)Obtains all Ops in theOpEnvironmentthat are namedname, sorted by priority.SortedSet<OpInfo>infos(String name, Hints hints)SortedSet<OpInfo>infos(Hints hints)Obtains all Ops in theOpEnvironmentthat matchhints, sorted by prioritydefault OpBuilderop(String opName)Entry point for convenient Op calls, providing a builder-style interface to walk through the process step-by-step.default OpBuilderop(String opName, Hints hints)Asop(String)but using a providedHints.default <T> Top(String opName, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType)Returns an Op fitting the provided arguments.<T> Top(String opName, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType, Hints hints)Returns an Op fitting the provided arguments.default <T> TopFromInfoTree(InfoTree tree, Nil<T> specialType)<T> TopFromInfoTree(InfoTree tree, Nil<T> specialType, Hints hints)default <T> TopFromSignature(String signature, Nil<T> specialType)Returns an Op fitting the provided arguments.OpInfoopify(Class<?> opClass, double priority, String... names)default OpInfoopify(Class<?> opClass, String... names)voidregister(Object... objects)voidsetDefaultHints(Hints hints)Sets theHintsfor theOpEnvironment.InfoTreetreeFromSignature(String signature)<T> TtypeLambda(Nil<T> opType, T lambda)Enriches a lambda expression with its generic type.-
Methods inherited from interface org.scijava.priority.Prioritized
compareTo, priority
-
-
-
-
Method Detail
-
buildEmpty
static OpEnvironment buildEmpty()
Generates an emptyOpEnvironment, which can be populated with the Ops of the caller's choice.- Returns:
- an empty
OpEnvironment - See Also:
for an that is fully populated
-
build
static OpEnvironment build()
Generates anOpEnvironmentwith all available Ops.- Returns:
- an
OpEnvironmentwith all available Ops. - See Also:
for an that is empty
-
infos
default SortedSet<OpInfo> infos()
Obtains all Ops in theOpEnvironment, sorted by priority.- Returns:
- a
SortedSetcontaining all Ops contained in theOpEnvironment.
-
infos
SortedSet<OpInfo> infos(String name)
Obtains all Ops in theOpEnvironmentthat are namedname, sorted by priority.- Parameters:
name- theStringof all Ops to be returned.- Returns:
- a
SortedSetcontaining all Ops in theOpEnvironmentnamedname
-
infos
SortedSet<OpInfo> infos(Hints hints)
Obtains all Ops in theOpEnvironmentthat matchhints, sorted by priority- Parameters:
hints- theHintsused to filter available Ops.- Returns:
- a
SortedSetcontaining all Ops in theOpEnvironmentmatchinghints
-
infos
SortedSet<OpInfo> infos(String name, Hints hints)
- Parameters:
name- theStringof all Ops to be returned.hints- theHintsused to filter available Ops.- Returns:
- a
SortedSetcontaining all Ops in theOpEnvironmentnamednameand matchinghints
-
discoverUsing
void discoverUsing(Discoverer... d)
-
discoverEverything
void discoverEverything()
-
history
OpHistory history()
-
op
default <T> T op(String opName, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType)
Returns an Op fitting the provided arguments. NB implementations of this method likely depend on theHintsset bysetDefaultHints(Hints), which provides no guarantee of thread-safety. Users interested in parallel Op matching should consider usingop(String, Nil, Nil[], Nil, Hints)instead.
-
op
<T> T op(String opName, Nil<T> specialType, Nil<?>[] inTypes, Nil<?> outType, Hints hints)
Returns an Op fitting the provided arguments.- Type Parameters:
T- theTypeof the Op- Parameters:
opName- the name of the OpspecialType- the genericTypeof the OpinTypes- the arguments (inputs) to the OpoutType- the return of the Op (note that it may also be an argument)hints- theHintsthat should guide this matching call- Returns:
- an instance of an Op aligning with the search parameters
-
opFromSignature
default <T> T opFromSignature(String signature, Nil<T> specialType)
Returns an Op fitting the provided arguments.
-
op
default OpBuilder op(String opName)
Entry point for convenient Op calls, providing a builder-style interface to walk through the process step-by-step.
The general order of specification:
- The op name (and
Hints, if desired) - The number of input(s)
- The type or value(s) of input(s)
- One of:
- The type or value of the output
- Which input should be modified in-place
- No inputs →
ProducerorComputer - Inputs with an output value →
Computer - Inputs with an output type →
ComputerorFunction - Inputs with no output →
InplaceorFunctionwith unknown (Object) return
Examples:
OpEnvironment env = new DefaultOpEnvironment();env.op("create").outType(DoubleType.class).create();— run an Op creating an instance of the ImgLib2DoubleTypeenv.op("create").outType(DoubleType.class).create();— same as above.env.op("create", hints).outType(DoubleType.class).create();— same as above but matching with the providedHints.env.op("create").outType(Img.class).create();— run an Op creating a raw instance of an ImgLib2Img.env.op("create").outType(new Nil<Img<DoubleType>>(){}).create();— run an Op creating an instance of an ImgLib2Img<DoubleType>.env.op("math.add").inType(Integer.class, Integer.class).function();— get an instance of an Op to add two integers together. Return type will beObject.env.op("math.add").inType(Integer.class, Integer.class).function();— same as above.env.op("math.add").input(1, 1).outType(Double.class).apply();— run an Op combining two integers. Return type will beDouble.env.op("math.add").input(img1, img2).output(result).compute();— run an Op combining two images and storing the result in a pre-allocated image.env.op("filter.addPoissonNoise").input(img1).mutate();— run an Op adding poisson noise to an input image.
- Parameters:
opName- The name of the Op to run- Returns:
- The
OpBuilderinstance for builder chaining. - Throws:
OpMatchingException- if the Op request cannot be satisfied- See Also:
To specify a Hints instance to use,OpBuilder
- The op name (and
-
op
default OpBuilder op(String opName, Hints hints)
Asop(String)but using a providedHints.- Parameters:
opName- The name of the Op to runhints- TheHintsinstance to use for Op matching- Returns:
- The
OpBuilderinstance for builder chaining. - Throws:
OpMatchingException- if the Op request cannot be satisfied- See Also:
To use the default Hints for this OpEnvironment
-
typeLambda
<T> T typeLambda(Nil<T> opType, T lambda)
Enriches a lambda expression with its generic type. Its usage is necessary in order to find Ops that could take this lamdba expression as an argument.Suppose, for example, that a user has a lambda expression of type
Function<Double, Double>, and they wish to use theengine.adaptOp to transform it into aFunction<Iterable<Double>, Iterable<Double>>. Naively, they write:
but it does not work, because the Ops engine cannot know the generic type of theFunction<Double, Double> doubler = x -> 2*x; Function<Iterable<Double>, Iterable<Double>> iterableDoubler = ops.op("engine.adapt").input(doubler).outType(new Nil<>() {}).apply();doublerobject. One workaround is to specify the input generic type explicitly:
Another approach is to use thisFunction<Double, Double> doubler = x -> 2*x; Function<Iterable<Double>, Iterable<Double>> iterableDoubler = ops.op("engine.adapt") .inType(new Nil<Function<Double, Double>() {}) .outType(new Nil<>() {}).function().apply(doubler);typeLambdamethod as follows:
In the above example, theFunction<Double, Double> doubler = ops.typeLambda(new Nil<>() {}, x -> 2*x); Function<Iterable<Double>, Iterable<Double>> iterableDoubler = ops.op("engine.adapt").input(doubler).outType(new Nil<>() {}).apply();doublerobject returned bytypeLambdahas its generic type information embedded, rather than being a raw lambda where the generic type information is erased at runtime.Note:
typeLambdadoes not need to be used with anonymous subclasses; these already retain their type parameters at runtime. It is only lambda expressions that need to be passed to this method.- Type Parameters:
T- The lambda's functional type (e.g.Function<String, Double>)- Parameters:
opType- The generic type of the lambda expression to be embedded. (e.g.new Nil<Function<String, Double>>() {})lambda- A lambda expression fulfilling the functional interface contract of the statedopType.- Returns:
- An enriched version of the lambda object that embeds knowledge of
its generic type by implementing the
GenericTypedinterface.
-
register
void register(Object... objects)
Registers allOpInfoderived fromObjects within the passed array. Elements within thisObjectmight be:- Parameters:
objects- theObjects that should be made discoverable to thisOpEnvironment
-
setDefaultHints
void setDefaultHints(Hints hints)
Sets theHintsfor theOpEnvironment. Every Call toop(java.lang.String, org.scijava.types.Nil<T>, org.scijava.types.Nil<?>[], org.scijava.types.Nil<?>)that does not pass aHintswill copy the Hints passed here (to prevent multiple Op calls from accessing/changing the sameHints). In the case that noHintshave been given to the Environment beforeopis called, the implementation will create a suitable stand-in.Note that this method does not inherently provide any guarantees pertaining to thread-safety; this API is provided purely for convenience. Any calls to
op(String, Nil, Nil[], Nil)that require a specificHintsshould not use this method, instead opting forop(String, Nil, Nil[], Nil, Hints).- Parameters:
hints- theHintsto be set as default
-
getDefaultHints
Hints getDefaultHints()
Returns a copy of the defaultHintsobject.- Returns:
- the default
Hints
-
help
default String help()
Returns the descriptions for all Ops contained within thisOpEnvironment- Returns:
- a String describing all Ops in the environment matching
name
-
help
default String help(String name)
Returns the descriptions for all Ops contained within thisOpEnvironmentmatchingname
-
help
String help(OpRequest request)
Returns simple descriptions for all Ops identifiable by a given name within thisOpEnvironment
-
helpVerbose
default String helpVerbose()
Returns verbose descriptions for all Ops contained within thisOpEnvironment- Returns:
- a
Stringcontaining a (set of) lines for each Op
-
helpVerbose
default String helpVerbose(String name)
Returns verbose descriptions for all Ops contained within thisOpEnvironment
-
helpVerbose
String helpVerbose(OpRequest request)
Returns verbose descriptions for all Ops contained within thisOpEnvironment
-
-