Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 Kevin 1
import java.util.ArrayList;
2
import java.util.LinkedList;
3
 
4
// Class to parse a specific set of commands
5
public class CmdParser {
6
	BSTree<String, CityRecord> GISTree;	// Binary Search Tree
7
	PRQuadTree CTree;					// PR QuadTree
8
 
9
	public CmdParser() {
10
		// Initialize BSTree and PRQuadTree
11
		GISTree = new BSTree<String, CityRecord>();
12
		CTree = new PRQuadTree();
13
	}
14
 
15
	// Calls specific functions depending on input
16
	public void Parse(String input) {
17
		// Split the passed line into tokens
18
		String delim = "[ ]+";
19
		String[] tokens = input.split(delim);
20
 
21
		// Echo the command to the output
22
		for (String token : tokens) {
23
			System.out.printf(token + " ");
24
		}
25
		System.out.printf("\n");
26
 
27
		// Parse tokens to determine what action to take
28
		if (tokens[0].compareToIgnoreCase("insert") == 0) {
29
			Cmd_Insert(tokens);
30
		} else if (tokens[0].compareToIgnoreCase("remove") == 0) {
31
			Cmd_Remove(tokens);
32
		} else if (tokens[0].compareToIgnoreCase("find") == 0) {
33
			Cmd_Find(tokens);
34
		} else if (tokens[0].compareToIgnoreCase("search") == 0) {
35
			Cmd_Search(tokens);
36
		} else if (tokens[0].compareToIgnoreCase("debug") == 0) {
37
			Cmd_Debug();
38
		} else if (tokens[0].compareToIgnoreCase("makenull") == 0) {
39
			Cmd_Makenull();
40
		} else {
41
			System.out.printf(">> \"%s\" command is not supported\n", tokens[0]);
42
		}
43
		System.out.printf("\n");
44
	}
45
 
46
	// Parse insert command
47
	private void Cmd_Insert(String[] tokens) {
48
		// Check to make sure there is a correct number of arguments for the command
49
		if (tokens.length != 4) {
50
			System.out.printf(">> Arguments must be in format \"insert x y name\"\n");
51
		} else {
52
			try {
53
				// Parse and check each argument token
54
				int x = Integer.parseInt(tokens[1]);
55
				int y = Integer.parseInt(tokens[2]);
56
				String name = tokens[3];
57
 
58
				if (x < 0 || x > 16383 || y < 0 || y > 16383) {
59
					System.out.printf(">> Insert failed: Coordinate values out of range\n");
60
				} else {
61
					// If all arguments are ok, create a new record with the specified values
62
					CityRecord newRecord = new CityRecord(name, x, y);
63
					// Check if record with same coordinates already exists
64
					LinkedList<Record> list = new LinkedList<Record>();
65
					CTree.search(x,  y, 0, list);
66
					if (list.size() != 0) {
67
						System.out.printf(">> Insert failed: City at (%d,%d) already exists\n", x, y);
68
					} else {
69
						// Insert record into both trees
70
						System.out.printf(">> Inserting city %s (%d,%d)\n", name, x, y);
71
						GISTree.insert(name, newRecord);
72
						CTree.insert(newRecord);
73
					}
74
				}
75
			} catch (NumberFormatException e) {
76
				System.out.printf(">> Insert failed: Unable to parse given coordinates\n");
77
			}
78
		}
79
	}
80
 
81
	// Parse remove command
82
	private void Cmd_Remove(String[] tokens) {
83
		// Check to make sure there is a correct number of arguments for the command
84
		if (tokens.length < 2 || tokens.length > 3) {
85
			System.out.printf(">> Arguments must be in format \"remove x y\" or \" remove name\"\n");
86
		} else {
87
			// A single argument means search by name
88
			if (tokens.length == 2) {
89
				String name = tokens[1];
90
				// First check if city exists
91
				CityRecord record = GISTree.find(name);
92
				if (record != null) {
93
					// Remove city if found
94
					System.out.printf(">> Removing city %s (%d,%d)\n", 
95
							record.getName(), record.getX(), record.getY());
96
					GISTree.remove(name, record.getX(), record.getY());
97
					CTree.remove(record.getX(), record.getY());
98
				} else {
99
					System.out.printf(">> Remove failed: City %s not found\n", name);
100
				}
101
			// Otherwise search by coordinates
102
			} else {
103
				try {
104
					// Parse and check each argument token
105
					int x = Integer.parseInt(tokens[1]);
106
					int y = Integer.parseInt(tokens[2]);
107
					if (x < 0 || x > 16383 || y < 0 || y > 16383) {
108
						System.out.printf(">> Remove failed: Coordinate values out of range\n");
109
					} else {
110
						// Check if city with coordinates exists
111
						LinkedList<Record> list = new LinkedList<Record>();
112
						CTree.search(x,  y, 0, list);
113
						if (list.size() == 0) {
114
							System.out.printf(">> Remove failed: City with coordinates (%d,%d) not found\n", x, y);
115
						} else {
116
							// Remove using coordinates
117
							System.out.printf(">> Removing city %s (%d,%d)\n",
118
									((CityRecord)list.get(0)).getName(), list.get(0).getX(), list.get(0).getY());
119
							CTree.remove(x, y);
120
							GISTree.remove(((CityRecord)list.get(0)).getName(), x, y);
121
						}
122
					}
123
				} catch (NumberFormatException e) {
124
					System.out.printf(">> Remove failed: Unable to parse given coordinates\n");
125
				}
126
			}
127
		}
128
	}
129
 
130
	// Parse find command
131
	private void Cmd_Find(String[] tokens) {
132
		// Check to make sure there is a correct number of arguments for the command
133
		if (tokens.length != 2) {
134
			System.out.printf(">> Arguments must be in format \"find name\"\n");
135
		} else {
136
			String name = tokens[1];
137
			// Get all records with the matching name
138
			ArrayList<CityRecord> records = GISTree.findAll(name);
139
			if (records.size() != 0) {
140
				// For each city found, print the city details
141
				for (CityRecord record : records) {
142
					System.out.printf(">> City found: %s (%d,%d)\n", 
143
							record.getName(), record.getX(), record.getY());
144
				}
145
			} else {
146
				System.out.printf(">> Find failed: City %s not found\n", name);
147
			}
148
		}
149
	}
150
 
151
	// Parse search command
152
	private void Cmd_Search(String[] tokens) {
153
		// Check to make sure there is a correct number of arguments for the command
154
		if (tokens.length != 4) {
155
			System.out.printf(">> Arguments must be in format \"search x y radius\"\n");
156
		} else {
157
			try {
158
				// Parse and check each argument token
159
				int x = Integer.parseInt(tokens[1]);
160
				int y = Integer.parseInt(tokens[2]);
161
				int r = Integer.parseInt(tokens[3]);
162
 
163
				if (r < 0 || r > 16383) {
164
					System.out.printf(">> Search failed: Radius value out of range\n");
165
				} else if (x < -16383 || x > 16383 || y < -16383 || y > 16383) {
166
					System.out.printf(">> Search failed: Coordinate values out of range\n");
167
				} else {
168
					// Get a list of all cities found within specified radius of point
169
					LinkedList<Record> results = new LinkedList<Record>();
170
					int nodesLookedAt = CTree.search(x, y, r, results);
171
					System.out.printf(">> %d nodes visited:\n", nodesLookedAt);
172
					if (results.size() == 0) {
173
						System.out.printf(">> No records found with specified coordinates\n");
174
					} else {
175
						// Print the found cities
176
						for (Record record : results) {
177
							System.out.printf(">> %s (%d,%d)\n", ((CityRecord)record).getName(), record.getX(), record.getY());
178
						}
179
					}
180
				}
181
			} catch (NumberFormatException e) {
182
				System.out.printf(">> Search failed: Unable to parse given coordinates\n");
183
			}
184
		}
185
	}
186
 
187
	// Parse debug command
188
	private void Cmd_Debug() {
189
		System.out.printf(">> ");
190
		CTree.debug(CTree.getRoot());
191
		System.out.printf("\n");
192
	}
193
 
194
	// Parse makenull command
195
	private void Cmd_Makenull() {
196
		GISTree.clear();
197
		CTree.clear();
198
		System.out.printf(">> Makenull successful\n");
199
	}
200
}