private void createRuntimeInstances() { try { Jiffle jiffle = new Jiffle(); Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put(WORLD_NAME, Jiffle.ImageRole.SOURCE); imageParams.put(NEXT_WORLD_NAME, Jiffle.ImageRole.DEST); // First create a runtime for the toroidal world URL url = getClass().getResource("life-toroid.jfl"); File file = new File(url.toURI()); jiffle.setScript(file); jiffle.setImageParams(imageParams); jiffle.compile(); toroidRuntime = jiffle.getRuntimeInstance(); // Now create a second runtime for the hard-edged world url = getClass().getResource("life-edges.jfl"); file = new File(url.toURI()); jiffle.setScript(file); jiffle.setImageParams(imageParams); jiffle.compile(); edgeRuntime = jiffle.getRuntimeInstance(); // Set the active runtime activeRuntime = worldType == WorldType.EDGES ? edgeRuntime : toroidRuntime; } catch (Exception ex) { throw new RuntimeException(ex); } }
Set<GetSourceValue> positions = Jiffle.getReadPositions(script, Arrays.asList(sourceName)); if (positions.isEmpty()) { return null;
public void getSourceFromJiffleObject(String script) throws JiffleException { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); // You have to compile the script before getting the runtime // source otherwise an Exception will be thrown jiffle.compile(); // Get the Java source. The boolean argument specifies that we // want the input script copied into the class javadocs String runtimeSource = jiffle.getRuntimeSource(true); } // docs end getSourceFromJiffleObject
/** * Creates a new instance by compiling the script read from {@code scriptFile}. * Using this constructor is equivalent to: * <pre><code> * Jiffle jiffle = new Jiffle(); * jiffle.setScript(scriptFile); * jiffle.setImageParams(params); * jiffle.compile(); * </code></pre> * * @param scriptFile file containing the Jiffle script * * @param params defines the names and roles of image variables * referred to in the script. * * @throws JiffleException on compilation errors */ public Jiffle(File scriptFile, Map<String, Jiffle.ImageRole> params) throws it.geosolutions.jaiext.jiffle.JiffleException { init(); setScript(scriptFile); setImageParams(params); compile(); }
/** * Gets the Java run-time class code generated from the compiled script. * * @return the run-time source code * * @throws JiffleException if the script has not been set yet or if * compilation errors occur */ public String getRuntimeSource() throws JiffleException { if (script == null) { throw new IllegalStateException("Jiffle script has not been set yet"); } Jiffle jiffle = new Jiffle(script, imageParams); return jiffle.getRuntimeSource(Jiffle.RuntimeModel.DIRECT, true); }
/** * Compiles a script read from a file and submits it for execution. * * @param scriptFile file containing the Jiffle script * @throws Exception on problems compiling the script */ public void compileAndRun(File scriptFile) throws Exception { Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("result", Jiffle.ImageRole.DEST); Jiffle jiffle = new Jiffle(scriptFile, imageParams); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); WritableRenderedImage destImage = ImageUtilities.createConstantImage(WIDTH, HEIGHT, 0d); runtime.setDestinationImage("result", destImage); executor.submit(runtime, new NullProgressListener()); }
@Test public void compileValidScript() throws Exception { System.out.println(" compile valid script"); String script = "dest = 42;"; imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setScript(script); jiffle.setImageParams(imageParams); jiffle.compile(); assertTrue(jiffle.isCompiled()); }
private void setupSingleDestScript() throws JiffleException { String script = "dest = 42;"; jiffle.setScript(script); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); jiffle.compile(); }
@Test(expected=JiffleException.class) public void getRuntimeBeforeCompiling() throws Exception { System.out.println(" getRuntimeInstance before compiling"); String script = "dest = 42;"; jiffle.setScript(script); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); JiffleRuntime runtime = jiffle.getRuntimeInstance(); } }
/** * Compiles a script read from a file and submits it for execution. * * @param scriptFile file containing the Jiffle script * @throws Exception on an error in the Jiffle compiler */ public void compileAndRun(File scriptFile) throws Exception { Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("result", Jiffle.ImageRole.DEST); Jiffle jiffle = new Jiffle(scriptFile, imageParams); Map<String, RenderedImage> images = new HashMap<>(); images.put("result", ImageUtilities.createConstantImage(WIDTH, HEIGHT, Double.valueOf(0d))); if (jiffle.isCompiled()) { JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); final TiledImage destImg = ImageUtilities.createConstantImage(WIDTH, HEIGHT, 0d); runtime.setDestinationImage("result", destImg); runtime.evaluateAll(null); ImageFrame frame = new ImageFrame(destImg, "Jiffle image demo"); frame.setVisible(true); } }
/** * Creates an instance of the default runtime class. * <p> * The default runtime class implements {@link JiffleDirectRuntime} and * extends an abstract base class provided by the Jiffle compiler. Objects * of this class evaluate the Jiffle script and write results directly to * the destination image(s). Client code can call either of the methods: * <ul> * <li>{@code evaluate(int x, int y)} * <li>{@code evaluateAll(JiffleProgressListener listener} * </ul> * The {@code Jiffle} object must be compiled before calling this method. * * @return the runtime object * @throws JiffleException if the script has not been compiled or if errors * occur in creating the runtime instance */ public JiffleDirectRuntime getRuntimeInstance() throws it.geosolutions.jaiext.jiffle.JiffleException { return (JiffleDirectRuntime) getRuntimeInstance(RuntimeModel.DIRECT); }
@Test(expected=JiffleException.class) public void compileWithNoScript() throws Exception { System.out.println(" compile with no script set"); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); jiffle.compile(); }
@Test public void scriptWithParamsConstructor() throws Exception { System.out.println(" Jiffle(script, imageParams)"); String script = "dest = 42;"; imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle = new Jiffle(script, imageParams); assertTrue(jiffle.isCompiled()); }
Jiffle.Result<ParseTree> parseResult = parseScript(theScript); if (parseResult.messages.isError()) { reportMessages(parseResult); return; Jiffle.Result<Map<String, Jiffle.ImageRole>> r = getScriptImageParams( tree); if (r.messages.isError()) { reportMessages(r); return; setImageParams(r.result); reportMessages(optionsWorker.messages); InitBlockWorker initWorker = new InitBlockWorker(tree); reportMessages(initWorker.messages); VarWorker vw = new VarWorker(tree, imageParams); ExpressionWorker expressionWorker = new ExpressionWorker(tree, vw); reportMessages(expressionWorker.messages);
@Test(expected=JiffleException.class) public void passingEmptyScriptToConstructor() throws Exception { System.out.println(" Jiffle(script, imageParams) with empty script"); String script = ""; imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle = new Jiffle(script, imageParams); }
@Test(expected=UnsupportedOperationException.class) public void tryToModifyImageParams() { System.out.println(" trying to modify map returned by getImageParams"); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); Map<String, Jiffle.ImageRole> unmodifiableMap = jiffle.getImageParams(); // this should provoke an exception unmodifiableMap.clear(); }