Subversion Repositories Code-Repo

Rev

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

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