package demoapp;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import systemthelpers.GraphRunner;

import com.ibm.streamsx.topology.TStream;
import com.ibm.streamsx.topology.Topology;
import com.ibm.streamsx.topology.context.StreamsContextFactory;
import com.ibm.streamsx.topology.file.FileStreams;
import com.ibm.avatar.algebra.datamodel.TupleList;
import com.ibm.avatar.algebra.util.tokenize.TokenizerConfig;
import com.ibm.avatar.api.CompilationSummary;
import com.ibm.avatar.api.CompileAQL;
import com.ibm.avatar.api.CompileAQLParams;
import com.ibm.avatar.aql.compiler.CompilerWarning;
import com.ibm.streamsx.topology.function7.Function;
import com.ibm.streamsx.topology.function7.Predicate;

public class ProcessLines {

	public static void compileAQL(String inputModule, String modulePath,
			String outputURI) throws Exception {
		CompileAQLParams params = new CompileAQLParams();
		params.setInputModules(new String[] { inputModule });
		params.setModulePath(modulePath);
		params.setTokenizerConfig(new TokenizerConfig.Standard());
		params.setOutputURI(outputURI);
		CompilationSummary summary = CompileAQL.compile(params);
		for (CompilerWarning warn : summary.getCompilerWarning()) {
			System.err.println("TextAnalytics compiler warning: "
					+ warn.toString());
		}
	}

	public static void main(String argv[]) throws Exception {
		String filename = "PrideAndPrejudice.txt";
		String fullPath = (new File(filename)).getAbsolutePath();
		//String module = "myextractor";
		//String outputview = "DoubleNegative";

		final String module = System.getenv("STREAMS_INSTALL")+"/samples/com.ibm.streams.text/FeatureDemo/etc/getNames";
		final String outputview = "getNames.FullNameWithTitle";
		File outputDir = new File("compiledModules");
		if (!outputDir.exists()) {
			outputDir.mkdirs();
		}

		// Let's compile the AQL.
		compileAQL(module, null, outputDir.toURI().toString());
		// Establish the topology
		Topology flow = new Topology("ProcessLines");
		TStream<String> filenamestream = flow.strings(fullPath);
		TStream<String> lines = FileStreams.textFileReader(filenamestream);
		/*
		 * // Apply the extractor TStream<Map> systemT = lines.transform(new
		 * Function<String,Map>() { public Map<String,TupleList> apply(String
		 * line ) { return new HashMap<String,TupleList>(); } } , Map.class);
		 */
		// Apply the extractor
		TStream<Map> systemT = lines.transform(new GraphRunner(
				new String[] { "getNames" }, outputDir.getAbsolutePath()),
				Map.class);
		// Let's filter out empty lines.
		TStream<Map> justNonEmpty = systemT.filter(new Predicate<Map>() {
			public boolean test(Map inMap) {
				if (inMap.containsKey(outputview)) {
					TupleList tupList = (TupleList) inMap
							.get(outputview);
					return tupList.size() > 0;
				} else
					return false;
			}
		});

		justNonEmpty.print();

		String textToolkit = System.getenv("STREAMS_INSTALL")
				+ "/toolkits/com.ibm.streams.text";
		flow.addJarDependency(textToolkit + "/lib/TextAnalytics/lib/JSON4J.jar");
		flow.addJarDependency(textToolkit
				+ "/lib/TextAnalytics/lib/text-analytics/systemT.jar");
		StreamsContextFactory.getStreamsContext("STANDALONE").submit(flow);
	}

}

@@ -1456,17 +1456,103 @@
 ename = 
-%22
+System.getenv(%22STREAMS_INSTALL%22)+%22/samples/com.ibm.streams.text/splgen_externdict/data/
 PrideAnd
@@ -1630,85 +1630,8 @@
 h();
-%0A%09%09//String module = %22myextractor%22;%0A%09%09//String outputview = %22DoubleNegative%22;
 %0A%0A%09%09
@@ -1814,18 +1814,18 @@
 e output
-Di
+Ja
 r = new 
@@ -1830,16 +1830,20 @@
 w File(%22
+bin/
 compiled
@@ -1849,16 +1849,20 @@
 dModules
+.jar
 %22);%0A%09%09if
@@ -1867,17 +1867,16 @@
 if (
-!
 output
-Di
+Ja
 r.ex
@@ -1898,26 +1898,26 @@
 tput
-Dir.mkdirs
+Jar.delete
 ();%0A%09%09%7D%0A
 %0A%09%09/
@@ -1912,16 +1912,18 @@
 ();%0A%09%09%7D%0A
+%09%09
 %0A%09%09// Le
@@ -1942,16 +1942,188 @@
 the AQL.
+  We're going to put it into%0A%09%09// the bin directory, so it will be included when we deploy%0A%09%09// standalone or distributed.%0A%09%09// We could have used an absolute path instead.
 %0A%09%09compi
@@ -2148,18 +2148,18 @@
 , output
-Di
+Ja
 r.toURI(
@@ -2723,34 +2723,145 @@
 %22 %7D,
- outputDir.getAbsolutePath
+%0A%09%09%09%09// This is how we access the compiledModules.jar%0A%09%09%09%09ProcessLines.class.getClassLoader().getResource(%22compiledModules.jar%22).toString
 ()),
@@ -3324,84 +3324,8 @@
 t%22;%0A
-%09%09flow.addJarDependency(textToolkit + %22/lib/TextAnalytics/lib/JSON4J.jar%22);%0A
 %09%09fl
@@ -3484,16 +3484,22 @@
 it(flow)
+.get()
 ;%0A%09%7D%0A%0A%7D%0A

Update to use relative paths