/*******************************************************************************
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Yurii Lahodiuk (yura_lagodiuk@ukr.net)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package com.lagodiuk.decisiontree.demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import com.lagodiuk.decisiontree.DecisionTree;
import com.lagodiuk.decisiontree.DecisionTreeBuilder;
import com.lagodiuk.decisiontree.Item;
import com.lagodiuk.decisiontree.Predicate;
import com.lagodiuk.decisiontree.RandomForest;

public class Demo5 {

	public static void main(String[] args) {

		DecisionTreeBuilder builder =
				DecisionTree
						.createBuilder()
						.setTrainingSet(makeTrainingSet())
						.setDefaultPredicates(Predicate.GTE, Predicate.LTE)
						.setMinimalNumberOfItems(7);

		RandomForest forest = RandomForest.create(builder, 10);

		JFrame f1 = new JFrame();
		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f1.setSize(300, 300);
		f1.setLocationRelativeTo(null);
		JPanel p = new JPanel();
		f1.add(p);
		f1.setVisible(true);

		BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();

		for (int x = 0; x < 200; x++) {
			for (int y = 0; y < 200; y++) {
				Item item = makeEmptyItem((x - 100) / 5, (y - 100) / 5);

				Map<Object, Integer> result = forest.classify(item);

				int red = 0;
				int green = 0;

				for (Object colour : result.keySet()) {
					if ("red".equals(colour)) {
						red = result.get(colour);
					} else if ("green".equals(colour)) {
						green = result.get(colour);
					}
				}

				g.setColor(new Color(((float) red) / (red + green), ((float) green) / (red + green), 0));

				g.drawOval(x, y, 2, 2);
			}
		}

		for (;;) {
			p.getGraphics().drawImage(bi, 0, 0, null);
		}
	}

	private static List<Item> makeTrainingSet() {
		Random random = new Random();

		List<Item> items = new LinkedList<Item>();
		for (double i = 0; i <= 700; i++) {
			items.add(makeItem((random.nextDouble() - random.nextDouble()) * 14, (random.nextDouble() - random.nextDouble()) * 14));
		}

		return items;
	}

	private static Item makeItem(double x, double y) {
		Item item = new Item();

		if ((((x * x) + (y * y)) <= (10 * 10)) && (Math.random() < 0.9)) {
			item.setCategory("red");
			if ((((x * x) + (y * y)) <= (5 * 5)) && (Math.random() < 0.8)) {
				item.setCategory("green");
			}
		} else {
			item.setCategory("green");
		}

		item.setAttribute("x", x);
		item.setAttribute("y", y);

		return item;
	}

	private static Item makeEmptyItem(double x, double y) {
		Item item = new Item();

		item.setAttribute("x", x);
		item.setAttribute("y", y);

		return item;
	}

	public static JFrame display(DefaultMutableTreeNode root, int width, int height) {
		JTree tree = new JTree(root);

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new JScrollPane(tree), BorderLayout.CENTER);
		for (int i = 0; i < tree.getRowCount(); i++) {
			tree.expandRow(i);
		}
		frame.setSize(width, height);
		// put frame at center of screen
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		return frame;
	}

}

@@ -1364,37 +1364,45 @@
 mo;%0A
+%0A
 import 
-java.awt.BorderLayout;
+com.lagodiuk.decisiontree.*;%0A
 %0Aimp
@@ -1413,18 +1413,17 @@
 java
-.awt.Color
+x.swing.*
 ;%0Aim
@@ -1436,24 +1436,17 @@
 ava.awt.
-Graphics
+*
 ;%0Aimport
@@ -1580,401 +1580,8 @@
 m;%0A%0A
-import javax.swing.JFrame;%0Aimport javax.swing.JPanel;%0Aimport javax.swing.JScrollPane;%0Aimport javax.swing.JTree;%0Aimport javax.swing.tree.DefaultMutableTreeNode;%0A%0Aimport com.lagodiuk.decisiontree.DecisionTree;%0Aimport com.lagodiuk.decisiontree.DecisionTreeBuilder;%0Aimport com.lagodiuk.decisiontree.Item;%0Aimport com.lagodiuk.decisiontree.Predicate;%0Aimport com.lagodiuk.decisiontree.RandomForest;%0A%0A
 publ
@@ -1735,16 +1735,17 @@
 ningSet(
+ 
 makeTrai
@@ -1753,16 +1753,17 @@
 ingSet()
+ 
 )%0A%09%09%09%09%09%09
@@ -1784,16 +1784,17 @@
 dicates(
+ 
 Predicat
@@ -1813,16 +1813,17 @@
 cate.LTE
+ 
 )%0A%09%09%09%09%09%09
@@ -1847,17 +1847,19 @@
 OfItems(
-7
+ 7 
 );%0A%0A%09%09Ra
@@ -2792,15 +2792,20 @@
 %0A%0A%09%09
-for (;;
+while( true 
 ) %7B%0A
@@ -3739,493 +3739,6 @@
 %0A%09%7D%0A
-%0A%09public static JFrame display(DefaultMutableTreeNode root, int width, int height) %7B%0A%09%09JTree tree = new JTree(root);%0A%0A%09%09JFrame frame = new JFrame();%0A%09%09frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);%0A%09%09frame.add(new JScrollPane(tree), BorderLayout.CENTER);%0A%09%09for (int i = 0; i %3C tree.getRowCount(); i++) %7B%0A%09%09%09tree.expandRow(i);%0A%09%09%7D%0A%09%09frame.setSize(width, height);%0A%09%09// put frame at center of screen%0A%09%09frame.setLocationRelativeTo(null);%0A%09%09frame.setVisible(true);%0A%09%09return frame;%0A%09%7D%0A%0A
 %7D%0A

minor. endless for replaced with while( true )