001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gis.display;
031
032import java.awt.Color;
033import java.io.ByteArrayInputStream;
034import java.io.File;
035import java.io.FileReader;
036import java.io.InputStream;
037import java.util.Random;
038import java.util.StringTokenizer;
039
040import javax.swing.JFrame;
041
042import org.apache.xerces.parsers.DOMParser;
043import org.w3c.dom.Document;
044import org.w3c.dom.Element;
045import org.w3c.dom.Node;
046import org.w3c.dom.NodeList;
047import org.xml.sax.InputSource;
048
049import edu.psu.geovista.datamining.data.HD.BasicCell;
050import edu.psu.geovista.datamining.data.HD.BasicInstance;
051import edu.psu.geovista.datamining.vis.hd.HDClusterViewer_New;
052
053/**
054 * Name: ENMPCPFrame.java Purpose:Given an XML string that representing GARP
055 * presaming result,it retrieves lat/lon info and the environmental data for
056 * each sample and display a PCP panel for interactive explore high-dimensional
057 * data. Knwon problem(s): (1) Other than x/y, the rest attributes are numbered
058 * sequentially (stratring from 0). The attribute names could be obtained from
059 * the workflow and passed to this file. But I found the attibute names from the
060 * workflow does not aggree with the presampling results. I may need to look
061 * into GARP C++ source codes. (2)The GeoVista components, on which this package
062 * is built on, are still under development. The functionality of the four
063 * selection modes are not clear to me. But the basic functonality is clear.
064 * Author: Jianting Zhang Date: August, 2005
065 */
066public class ENMPCPFrame extends JFrame {
067        Color[] colors = new Color[] { Color.red, Color.green, Color.blue };
068        HDClusterViewer_New pcp = new HDClusterViewer_New();
069        Random rd = new Random();
070
071        /**
072         * Constructor for the ENMPCPFrame object
073         * 
074         *@param title
075         *            Description of the Parameter
076         *@exception Exception
077         *                Description of the Exception
078         */
079        public ENMPCPFrame(String title) throws Exception {
080                setTitle(title);
081                setSize(700, 700);
082                this.getContentPane().add(pcp);
083
084        }
085
086        /**
087         * Sets the data attribute of the ENMPCPFrame object
088         * 
089         *@param sampleString
090         *            The new data value
091         *@exception Exception
092         *                Description of the Exception
093         */
094        public void setData(String sampleString) throws Exception {
095
096                DOMParser parser = new DOMParser();
097                InputStream is = new ByteArrayInputStream(sampleString.getBytes());
098                parser.parse(new InputSource(is));
099                Document d = parser.getDocument();
100                NodeList rnl = d.getElementsByTagName("EnvCellSet");
101                NodeList fnl = rnl.item(0).getChildNodes();
102                int len = fnl.getLength();
103                int numCells = 0;
104                int num_attr = -1;
105                for (int i = 0; i < len; i++) {
106                        Node n = fnl.item(i);
107                        /*
108                         * System.out.println(pn.getClass().getName());
109                         * System.out.println("name="+pn.getNodeName());
110                         * System.out.println("attr="+pn.getAttributes());
111                         */
112                        if (!n.getNodeName().equalsIgnoreCase("EnvCell")) {
113                                continue;
114                        }
115                        Element e = (Element) n;
116                        String x = e.getAttribute("X");
117                        String y = e.getAttribute("Y");
118                        String vals = e.getFirstChild().getNodeValue();
119                        StringTokenizer st = new StringTokenizer(vals, " ,");
120                        if (num_attr < 0) {
121                                num_attr = st.countTokens();
122                        } else {
123                                assert (num_attr == st.countTokens());
124                        }
125                        // System.out.println(x+" "+y+" "+vals);
126                        numCells++;
127                }
128                System.out.println(numCells + "  " + num_attr);
129
130                int dims[] = new int[num_attr + 2];
131                dims[0] = 0;
132                dims[1] = 1;
133                String[] names = new String[num_attr + 2];
134                names[0] = "X";
135                names[1] = "Y";
136                for (int i = 0; i < num_attr; i++) {
137                        dims[i + 2] = i + 2;
138                        names[i + 2] = "a" + i;
139                }
140
141                int k = 0;
142                int num_groups = 50;
143                int observ_pos = 0;
144                BasicCell[] dataBasicCells = new BasicCell[num_groups];
145                for (int i = 0; i < num_groups; i++) {
146                        dataBasicCells[i] = new BasicCell(i, dims, null, true);
147                }
148                for (int i = 0; i < len; i++) {
149                        Node n = fnl.item(i);
150                        if (!n.getNodeName().equalsIgnoreCase("EnvCell")) {
151                                continue;
152                        }
153                        float[] values = new float[num_attr + 2];
154                        Element e = (Element) n;
155                        String x = e.getAttribute("X");
156                        values[0] = (new Float(x)).floatValue();
157                        String y = e.getAttribute("Y");
158                        values[1] = (new Float(y)).floatValue();
159                        String vals = e.getFirstChild().getNodeValue();
160                        StringTokenizer st = new StringTokenizer(vals, " ,");
161                        for (int j = 0; j < num_attr; j++) {
162                                values[j + 2] = (new Float(st.nextToken())).floatValue();
163                                // +rd.nextInt(100)*0.02f;
164                        }
165                        /*
166                         * for(int j=0;j<num_attr+1;j++) System.out.print(values[j]+" ");
167                         * System.out.println();
168                         */
169                        BasicInstance dcell = new BasicInstance(numCells - 1, k, values,
170                                        values);
171                        int group_no = (int) (values[observ_pos + 2]) * 25 + k % 25;
172                        // System.out.println(k+"  "+p+"  "+group_no);
173                        dataBasicCells[group_no].addInstance(dcell);
174                        k++;
175                        assert (k <= numCells);
176                }
177                for (int i = 0; i < 25; i++) {
178                        dataBasicCells[i].setHDColor(colors[0]);
179                        dataBasicCells[25 + i].setHDColor(colors[1]);
180                }
181
182                pcp.setAttributeNames(names);
183                pcp.setCells(dataBasicCells);
184        }
185
186        /**
187         * The main program for the ENMPCPFrame class
188         * 
189         *@param args
190         *            The command line arguments
191         *@exception Exception
192         *                Description of the Exception
193         */
194        public static void main(String[] args) throws Exception {
195                ENMPCPFrame fm = new ENMPCPFrame("test");
196                fm.setVisible(true);
197
198                File f = new File("z:/GISVis/miscs/CellSet.xml");
199                char[] s = new char[(int) f.length()];
200                FileReader fr = new FileReader(f);
201                fr.read(s);
202                String sampleString = new String(s);
203                // System.out.println(sampleString);
204                fm.setData(sampleString);
205        }
206}