package org.fujaba.graphengine.unitTests;

import org.junit.Test;
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.junit.Assert;

import net.sourceforge.jeval.EvaluationException;
import net.sourceforge.jeval.Evaluator;

public class TestUseExpressionLibrary {

	
	
	@Test
	public void testUseExpressionLibrary() {
		Evaluator evaluator = new Evaluator();
		try {
			/**
			 * Add the variables to our instance of the Evaluator class.
			 */
			evaluator.putVariable("a", "'Hello'");
			evaluator.putVariable("b", "'World'");
			evaluator.putVariable("c", "7");
			evaluator.putVariable("d", "3.5");
			evaluator.putVariable("e", "1");
			evaluator.putVariable("f", "0");
			/**
			 * This sample simply outputs the variables.
			 */
			System.out.println(evaluator.evaluate("#{a}"));
			System.out.println(evaluator.evaluate("#{b}"));
			/**
			 * This sample outputs a preloaded math varibles.
			 */
			System.out.println(evaluator.evaluate("#{PI}"));
			/**
			 * This sample adds the variables together to form a sentence.
			 */
			System.out.println(evaluator.evaluate("#{c} + #{d} == 10.5"));
			System.out.println(evaluator.evaluate("#{c} * #{d} == 24.5"));
			
			System.out.println(evaluator.evaluate("#{e} && #{f}"));
			System.out.println(evaluator.evaluate("#{e} || #{f}"));
		} catch (EvaluationException ee) {
			System.out.println(ee);
		}
		
	}
	
	@Test
	public void testUseExpressionLibraryOnFerrymansGraph() {
		Graph graph = getFerrymansGraph();
		
		String expression = "#{type} == 'Cargo' && #{species} == 'Wolf' && #{count} < 5";

		boolean matched = false;
		for (Node node: graph.getNodes()) {
			Evaluator eval = buildNodeEvaluator(node);
			try {
				matched = "1.0".equals(eval.evaluate(expression));
			} catch (Throwable t) {
				matched = false;
			}
			if (matched) {
				break;
			}
		}
		Assert.assertTrue(matched);
	}
	
	private Evaluator buildNodeEvaluator(Node node) {
		Evaluator evaluator = new Evaluator();
		for (String key: node.getAttributes().keySet()) {
			if (node.getAttribute(key) instanceof String) {
				evaluator.putVariable(key, "'" + node.getAttribute(key) + "'");
			} else {
				evaluator.putVariable(key, "" + node.getAttribute(key));
			}
		}
		return evaluator;
	}
	
	private Graph getFerrymansGraph() {
		Graph ferrymansGraph = new Graph();
		Node wolf = new Node(), goat = new Node(), cabbage = new Node(), ferry = new Node(), north = new Node(), south = new Node();
		ferrymansGraph.addNode(wolf).addNode(goat).addNode(cabbage).addNode(ferry).addNode(north).addNode(south);
		wolf.setAttribute("type", "Cargo").setAttribute("species", "Wolf").setAttribute("count", 1).addEdge("eats", goat).addEdge("at", north);
		goat.setAttribute("type", "Cargo").setAttribute("species", "Goat").setAttribute("count", 1).addEdge("eats", cabbage).addEdge("at", north);
		cabbage.setAttribute("type", "Cargo").setAttribute("species", "Cabbage").setAttribute("count", 5).addEdge("at", north);
		ferry.setAttribute("type", "Ferry").setAttribute("count", 1).addEdge("at", north);
		north.setAttribute("type", "Bank").setAttribute("side", "north").setAttribute("count", 1).addEdge("opposite", south);
		south.setAttribute("type", "Bank").setAttribute("side", "south").setAttribute("count", 1).addEdge("opposite", north);
		return ferrymansGraph;
	}
	
}

@@ -1633,16 +1633,17 @@
 olf' && 
+(
 #%7Bcount%7D
@@ -1646,16 +1646,58 @@
 unt%7D %3C 5
+ %7C%7C #%7Bcount%7D %3E 1000) && #%7Bexistant%7D == 1.0
 %22;%0D%0A%0D%0A%09%09
@@ -2274,24 +2274,173 @@
 y) + %22'%22);%0D%0A
+%09%09%09%7D else if (node.getAttribute(key) instanceof Boolean) %7B%0D%0A%09%09%09%09evaluator.putVariable(key, %22%22 + ((boolean)node.getAttribute(key) ? %221.0%22 : %220.0%22));%0D%0A
 %09%09%09%7D else %7B%0D
@@ -2941,32 +2941,63 @@
 ute(%22count%22, 1).
+setAttribute(%22existant%22, true).
 addEdge(%22eats%22, 
@@ -3111,32 +3111,63 @@
 ute(%22count%22, 1).
+setAttribute(%22existant%22, true).
 addEdge(%22eats%22, 
@@ -3298,16 +3298,47 @@
 nt%22, 5).
+setAttribute(%22existant%22, true).
 addEdge(
@@ -3407,32 +3407,63 @@
 ute(%22count%22, 1).
+setAttribute(%22existant%22, true).
 addEdge(%22at%22, no
@@ -3553,32 +3553,63 @@
 ute(%22count%22, 1).
+setAttribute(%22existant%22, true).
 addEdge(%22opposit
@@ -3713,16 +3713,47 @@
 nt%22, 1).
+setAttribute(%22existant%22, true).
 addEdge(

fix in test usage of expression-library