package de.ugoe.cs.eventbench.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import org.junit.*;

import de.ugoe.cs.eventbench.data.Event;
import static org.junit.Assert.*;

/**
 * The class <code>ModelFlattenerTest</code> contains tests for the class <code>{@link ModelFlattener}</code>.
 *
 * @author Steffen Herbold
 * @version 1.0
 */
public class ModelFlattenerTest {
	
	List<Event<?>> sequence;
	
	private static void assertCollectionContent(Collection<?> c1, Collection<?> c2) {
		assertEquals(c1.size(), c2.size());
		for( Object obj : c1 ) {
			assertTrue(c2.contains(obj));
		}
	}
	
	@Test
	public void testFlattenHighOrderMarkovModel_1()
		throws Exception {
		ModelFlattener fixture = new ModelFlattener();
		HighOrderMarkovModel model = new HighOrderMarkovModel(2, new Random());
		Collection<List<Event<?>>> sequences = new ArrayList<List<Event<?>>>();
		sequences.add(sequence);
		model.train(sequences);
		
		Collection<Event<?>> expectedSymbols = new HashSet<Event<?>>();
		expectedSymbols.add(new Event<Object>("a-=-END"));
		expectedSymbols.add(new Event<Object>("a-=-b"));
		expectedSymbols.add(new Event<Object>("a-=-c"));
		expectedSymbols.add(new Event<Object>("a-=-d"));
		expectedSymbols.add(new Event<Object>("b-=-r"));
		expectedSymbols.add(new Event<Object>("c-=-a"));
		expectedSymbols.add(new Event<Object>("d-=-a"));
		expectedSymbols.add(new Event<Object>("r-=-a"));
		expectedSymbols.add(new Event<Object>("START-=-a"));

		FirstOrderMarkovModel result = fixture.flattenHighOrderMarkovModel(model);
		
		assertCollectionContent(expectedSymbols, result.getEvents());
		
		TrieNode<Event<?>> root = result.trie.find(null);
		TrieNode<Event<?>> root_aEnd = root.getChild(new Event<Object>("a-=-END"));
		TrieNode<Event<?>> root_ab = root.getChild(new Event<Object>("a-=-b"));
		TrieNode<Event<?>> root_ab_br = root_ab.getChild(new Event<Object>("b-=-r"));
		TrieNode<Event<?>> root_ac = root.getChild(new Event<Object>("a-=-c"));
		TrieNode<Event<?>> root_ac_ca = root_ac.getChild(new Event<Object>("c-=-a"));
		TrieNode<Event<?>> root_ad = root.getChild(new Event<Object>("a-=-d"));
		TrieNode<Event<?>> root_ad_da = root_ad.getChild(new Event<Object>("d-=-a"));
		TrieNode<Event<?>> root_br = root.getChild(new Event<Object>("b-=-r"));
		TrieNode<Event<?>> root_br_ra = root_br.getChild(new Event<Object>("r-=-a"));
		TrieNode<Event<?>> root_ca = root.getChild(new Event<Object>("c-=-a"));
		TrieNode<Event<?>> root_ca_ad = root_ca.getChild(new Event<Object>("a-=-d"));
		TrieNode<Event<?>> root_da = root.getChild(new Event<Object>("d-=-a"));
		TrieNode<Event<?>> root_da_ab = root_da.getChild(new Event<Object>("a-=-b"));
		TrieNode<Event<?>> root_ra = root.getChild(new Event<Object>("r-=-a"));
		TrieNode<Event<?>> root_ra_ac = root_ra.getChild(new Event<Object> ("a-=-c"));
		TrieNode<Event<?>> root_ra_aEnd = root_ra.getChild(new Event<Object>("a-=-END"));
		TrieNode<Event<?>> root_startA = root.getChild(new Event<Object>("START-=-a"));
		TrieNode<Event<?>> root_startA_ab = root_startA.getChild(new Event<Object>("a-=-b"));
		
		assertEquals(1, root_aEnd.getCount());
		assertTrue(root_aEnd.isLeaf());
		assertEquals(2, root_ab.getCount());
		assertEquals(1, root_ab.getChildren().size());
		assertEquals(2, root_ab_br.getCount());
		assertTrue(root_ab_br.isLeaf());
		assertEquals(1, root_ac.getCount());
		assertEquals(1, root_ac.getChildren().size());
		assertEquals(1, root_ac_ca.getCount());
		assertTrue(root_ac_ca.isLeaf());
		assertEquals(1, root_ad.getCount());
		assertEquals(1, root_ad.getChildren().size());
		assertEquals(1, root_ad_da.getCount());
		assertTrue(root_ad_da.isLeaf());
		assertEquals(2, root_br.getCount());
		assertEquals(1, root_br.getChildren().size());
		assertEquals(2, root_br_ra.getCount());
		assertTrue(root_br_ra.isLeaf());
		assertEquals(1, root_ca.getCount());
		assertEquals(1, root_ca.getChildren().size());
		assertEquals(1, root_ca_ad.getCount());
		assertTrue(root_ca_ad.isLeaf());
		assertEquals(1, root_da.getCount());
		assertEquals(1, root_da.getChildren().size());
		assertEquals(1, root_da_ab.getCount());
		assertTrue(root_da_ab.isLeaf());
		assertEquals(2, root_ra.getCount());
		assertEquals(2, root_ra.getChildren().size());
		assertEquals(1, root_ra_ac.getCount());
		assertTrue(root_ra_ac.isLeaf());
		assertEquals(1, root_ra_aEnd.getCount());
		assertTrue(root_ra_aEnd.isLeaf());
		assertEquals(1, root_startA.getCount());
		assertEquals(1, root_startA.getChildren().size());
		assertEquals(1, root_startA_ab.getCount());
		assertTrue(root_startA_ab.isLeaf());		
	}

	@Test
	public void testFlattenPredictionByPartialMatch_1()
		throws Exception {
		ModelFlattener fixture = new ModelFlattener();
		PredictionByPartialMatch model = new PredictionByPartialMatch(1, new Random());

		FirstOrderMarkovModel result = fixture.flattenPredictionByPartialMatch(model);
		
		assertEquals(null, result);
	}

	@Before
	public void setUp()
		throws Exception {
		sequence = new ArrayList<Event<?>>();
		sequence.add(new Event<String>("a"));
		sequence.add(new Event<String>("b"));
		sequence.add(new Event<String>("r"));
		sequence.add(new Event<String>("a"));
		sequence.add(new Event<String>("c"));
		sequence.add(new Event<String>("a"));
		sequence.add(new Event<String>("d"));
		sequence.add(new Event<String>("a"));
		sequence.add(new Event<String>("b"));
		sequence.add(new Event<String>("r"));
		sequence.add(new Event<String>("a"));
	}

	public static void main(String[] args) {
		new org.junit.runner.JUnitCore().run(ModelFlattenerTest.class);
	}
}