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(); }
@Test public void resetScript() throws Exception { System.out.println(" resetScript"); String script1 = "dest = 42;"; String script2 = "dest = foo;"; jiffle.setScript(script1); jiffle.setScript(script2); String result = jiffle.getScript(); assertFalse(result.contains(script1)); assertTrue(result.contains(script2)); }
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 compileScriptWithoutImageParams() throws Exception { System.out.println(" compile script with missing image params"); String script = "dest = 42;"; jiffle.setScript(script); jiffle.compile(); }
protected void compileScript(String script) throws JiffleException { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); } }
@Test public void setScript() throws Exception { System.out.println(" set and get the script"); String script = "dest = 42;"; jiffle.setScript(script); String result = jiffle.getScript(); assertTrue(result.contains(script)); }
@Test(expected=JiffleParserException.class) public void compileInvalidScriptThrowsException() throws Exception { System.out.println(" compile invalid script and get exception"); // script with an uninitialized variable String script = "dest = x;"; imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setScript(script); 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(); } }
private Jiffle getCompiledJiffle(String script) throws JiffleException { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); return jiffle; }
@Test(expected=JiffleException.class) public void emptyImagesBlock() throws Exception { System.out.println(" empty images block and no parameters causes exception"); String script = "images { } dest = 42;" ; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); Map<String, Jiffle.ImageRole> params = new HashMap<>(); params.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(params); jiffle.compile(); return jiffle.getRuntimeInstance(); }
@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 JiffleDirectRuntime getRuntimeWithImagesBlock() throws Exception { String script = "images {" + " src1=read; src2=read; src3=read;" + " dest1=write; dest2=write; dest3=write;" + "}" + "dest1 = src1; dest2 = src2; dest3 = src3;" ; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); return jiffle.getRuntimeInstance(); }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); return jiffle.getRuntimeInstance(); } }
@Test public void compileInvalidScriptAndCheckStatus() throws Exception { System.out.println(" compile invalid script and check status"); // script with an uninitialized variable String script = "dest = x;"; imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setScript(script); jiffle.setImageParams(imageParams); try { jiffle.compile(); } catch (JiffleParserException ignored) {} assertFalse(jiffle.isCompiled()); }
@Test public void getImageScopeVarNames() throws Exception { String script = "images { dest=write; } " + "init { foo = 1; bar = 2; foz = 3; baz = 4; } " + "dest = 42;" ; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); String[] names = runtime.getVarNames(); List<String> expected = new ArrayList<>(); expected.add("foo"); expected.add("bar"); expected.add("foz"); expected.add("baz"); assertEquals(expected.size(), names.length); for (int i = 0; i < names.length; i++) { assertTrue(expected.contains(names[i])); expected.remove(names[i]); } }
private JiffleDirectRuntime getRuntime(String script) throws Exception { Jiffle jiffle = new Jiffle(); jiffle.setScript(script); Map<String, Jiffle.ImageRole> imageParams = new HashMap<>(); imageParams.put("dest", Jiffle.ImageRole.DEST); jiffle.setImageParams(imageParams); jiffle.compile(); JiffleDirectRuntime runtime = jiffle.getRuntimeInstance(); WritableRenderedImage destImg = ImageUtilities.createConstantImage(WIDTH, WIDTH, 0d); runtime.setDestinationImage("dest", destImg); return runtime; }
@Test public void noDestImage() throws Exception { System.out.println(" destination-less script with images block"); String script = String.format( "images { inimage = read; } \n" + "init { n = 0; } \n" + "n += inimage >= %d;", NUM_PIXELS - 5); Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); directRuntimeInstance = jiffle.getRuntimeInstance(); directRuntimeInstance.setSourceImage("inimage", createSequenceImage()); directRuntimeInstance.evaluateAll(null); Double var = directRuntimeInstance.getVar("n"); assertNotNull(var); assertEquals(5, var.intValue()); }
private void assertScriptResult(String script, Evaluator e, String srcVarName, String destVarName) throws Exception { RenderedImage srcImg = null; WritableRenderedImage destImg = null; Jiffle jiffle = new Jiffle(); jiffle.setScript(script); jiffle.compile(); directRuntimeInstance = (JiffleDirectRuntime) jiffle.getRuntimeInstance(); if (srcVarName != null && srcVarName.length() > 0) { srcImg = createSequenceImage(); directRuntimeInstance.setSourceImage(srcVarName, srcImg); } if (destVarName != null && destVarName.length() > 0) { destImg = ImageUtilities.createConstantImage(IMG_WIDTH, IMG_WIDTH, 0d); directRuntimeInstance.setDestinationImage(destVarName, destImg); } directRuntimeInstance.evaluateAll(null); assertImage(srcImg, destImg, e); }