carfield.com.hk AbstractGraphSearch.java 2002-01-20T16:00:00Z 2002-01-20T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import java.util.Vector; abstract public class AbstractGraphSearch { public void addNode(String name, int x, int y) { System.out.println(&quot;Adding node: &quot; + name + &quot;, &quot; + x + &quot;, &quot; + y); nodeNames[numNodes] = name; node_x[numNodes] = x; node_y[numNodes] = y; numNodes++; } public int getNumNodes() { return numNodes; } public int getNumLinks() { return numLinks; } public String getNodeName(int index) { try { return nodeNames[index]; } catch (Exception e) { System.out.println(&quot;Error in getNodeName: &quot; + e); } return &quot;no name&quot;; // error condition } public int getNodeX(int index) { try { return node_x[index]; } catch (Exception e) { System.out.println(&quot;Error in getNodePosition: &quot; + e); } return 0; // error condition } public int getNodeY(int index) { try { return node_y[index]; } catch (Exception e) { System.out.println(&quot;Error in getNodePosition: &quot; + e); } return 0; // error condition } public int getLink1(int index) { return link_1[index]; } public int getLink2(int index) { return link_2[index]; } public void addLink(int node1, int node2) { link_1[numLinks] = node1; link_2[numLinks] = node2; int dist_squared = (node_x[node1] - node_x[node2]) * (node_x[node1] - node_x[node2]) + (node_y[node1] - node_y[node2]) * (node_y[node1] - node_y[node2]); lengths[numLinks] = (int)Math.sqrt(dist_squared); numLinks++; } public void addLink(String name1, String name2) { int index1 = -1, index2 = -1; for (int i=0; i&lt;numNodes; i++) { if (name1.equals(nodeNames[i])) index1 = i; if (name2.equals(nodeNames[i])) index2 = i; } if (index1 != -1 &amp;&amp; index2 != -1) addLink(index1, index2); } /** findPath - abstract method that is defined in subclasses */ abstract public int [] findPath(int start_node, int goal_node); // return an array of node indices protected int getNodeIndex(String name) { for (int i=0; i&lt;numNodes; i++) { if (name.equals(nodeNames[i])) return i; } return -1; // error condition } final public static int MAX = 50; // max number of nodes and max number of links protected int [] path = new int[AbstractGraphSearch.MAX]; protected int num_path = 0; // for nodes: protected String [] nodeNames = new String[MAX]; protected int [] node_x = new int[MAX]; protected int [] node_y = new int[MAX]; // for links between nodes: protected int [] link_1 = new int[MAX]; protected int [] link_2 = new int[MAX]; protected int [] lengths = new int[MAX]; protected int numNodes = 0; protected int numLinks = 0; protected int goalNodeIndex = -1, startNodeIndex = -1; } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-20T16:00:00Z BreadthFirstSearch.java 2002-01-20T16:00:00Z 2002-01-20T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">public class BreadthFirstSearch extends AbstractGraphSearch { /** findPath - abstract method in super class */ public int [] findPath(int start_node, int goal_node) { // return an array of node indices System.out.println(&quot;Entered BreadthFirstSearch.findPath(&quot; + start_node + &quot;, &quot; + goal_node + &quot;)&quot;); // data structures for depth first search: boolean [] alreadyVisitedFlag = new boolean[numNodes]; int [] predecessor = new int[numNodes]; IntQueue queue = new IntQueue(numNodes + 2); for (int i=0; i&lt;numNodes; i++) { alreadyVisitedFlag[i] = false; predecessor[i] = -1; } alreadyVisitedFlag[start_node] = true; queue.addToBackOfQueue(start_node); outer: while (queue.isEmpty() == false) { int head = queue.peekAtFrontOfQueue(); int [] connected = connected_nodes(head); if (connected != null) { for (int i=0; i&lt;connected.length; i++) { if (alreadyVisitedFlag[connected[i]] == false) { predecessor[connected[i]] = head; queue.addToBackOfQueue(connected[i]); if (connected[i] == goal_node) break outer; // we are done } } alreadyVisitedFlag[head] = true; queue.removeFromQueue(); // ignore return value } } // now calculate the shortest path from the predecessor array: int [] ret = new int[numNodes + 1]; int count = 0; ret[count++] = goal_node; for (int i=0; i&lt;numNodes; i++) { ret[count] = predecessor[ret[count - 1]]; count++; if (ret[count - 1] == start_node) break; // back to starting node } int [] ret2 = new int[count]; for (int i=0; i&lt;count; i++) { ret2[i] = ret[count - 1 - i]; } return ret2; } protected class IntQueue { public IntQueue(int num) { queue = new int[num]; head = tail = 0; len = num; } public IntQueue() { this(400); } private int [] queue; int tail, head, len; public void addToBackOfQueue(int n) { queue[tail] = n; if (tail &gt;= (len - 1)) { tail = 0; } else { tail++; } } public int removeFromQueue() { int ret = queue[head]; if (head &gt;= (len - 1)) { head = 0; } else { head++; } return ret; } public boolean isEmpty() { return head == (tail + 1); } public int peekAtFrontOfQueue() { return queue[head]; } } protected int [] connected_nodes(int node) { int [] ret = new int[AbstractGraphSearch.MAX]; int num = 0; for (int n=0; n&lt;numNodes; n++) { boolean connected = false; // See if there is a link between node 'node' and 'n': for (int i=0; i&lt;numLinks; i++) { if (link_1[i] == node) { if (link_2[i] == n) { connected = true; break; } } if (link_2[i] == node) { if (link_1[i] == n) { connected = true; break; } } } if (connected) { ret[num++] = n; } } if (num == 0) return null; int [] ret2 = new int[num]; for (int i=0; i&lt;num; i++) { ret2[i] = ret[i]; } return ret2; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-20T16:00:00Z DepthFirstSearch.java 2002-01-20T16:00:00Z 2002-01-20T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">public class DepthFirstSearch extends AbstractGraphSearch { /** findPath - abstract method in super class */ public int [] findPath(int start_node, int goal_node) { // return an array of node indices System.out.println(&quot;Entered DepthFirstSearch.findPath(&quot; + start_node + &quot;, &quot; + goal_node + &quot;)&quot;); path[0] = start_node; return findPathHelper(path, 1, goal_node); } public int [] findPathHelper(int [] path, int num_path, int goal_node) { System.out.println(&quot;Entered DepthFirstSearch.findPathHelper(...,&quot; + num_path + &quot;, &quot; + goal_node + &quot;)&quot;); if (goal_node == path[num_path - 1]) { int [] ret = new int[num_path]; for (int i=0; i&lt;num_path; i++) ret[i] = path[i]; return ret; // we are done! } int [] new_nodes = connected_nodes(path, num_path); if (new_nodes != null) { for (int j=0; j&lt;new_nodes.length; j++) { path[num_path] = new_nodes[j]; int [] test = findPathHelper(path, num_path + 1, goal_node); if (test != null) { if (test[test.length - 1] == goal_node) { return test; } } } } return null; } protected int [] connected_nodes(int [] path, int num_path) { // find all nodes connected to the last node on 'path' // that are not already on 'path' int [] ret = new int[AbstractGraphSearch.MAX]; int num = 0; int last_node = path[num_path - 1]; for (int n=0; n&lt;numNodes; n++) { // see if node 'n' is already on 'path': boolean keep = true; for (int i=0; i&lt;num_path; i++) { if (n == path[i]) { keep = false; break; } } boolean connected = false; if (keep) { // now see if there is a link between node 'last_node' and 'n': for (int i=0; i&lt;numLinks; i++) { if (link_1[i] == last_node) { if (link_2[i] == n) { connected = true; break; } } if (link_2[i] == last_node) { if (link_1[i] == n) { connected = true; break; } } } if (connected) { ret[num++] = n; } } } if (num == 0) return null; int [] ret2 = new int[num]; for (int i=0; i&lt;num; i++) { ret2[i] = ret[i]; } return ret2; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-20T16:00:00Z GraphBreadthFirstSearch.java 2002-01-20T16:00:00Z 2002-01-20T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*; import java.awt.*; public class GraphBreadthFirstSearch extends JFrame { JPanel jPanel1 = new JPanel(); public GraphBreadthFirstSearch() { BreadthFirstSearch engine = new BreadthFirstSearch(); // Define a test network before calling the super class 'init': engine.addNode(&quot;0&quot;, 20, 40); engine.addNode(&quot;1&quot;, 60, 60); engine.addNode(&quot;2&quot;, 100, 40); engine.addNode(&quot;3&quot;, 50, 110); engine.addNode(&quot;4&quot;, 140, 80); engine.addNode(&quot;5&quot;, 160, 150); engine.addNode(&quot;6&quot;, 200, 80); engine.addNode(&quot;7&quot;, 160, 40); engine.addNode(&quot;8&quot;, 240, 120); engine.addNode(&quot;9&quot;, 260, 90); engine.addLink(0,1); engine.addLink(1,2); engine.addLink(2,3); engine.addLink(2,4); engine.addLink(4,5); engine.addLink(4,6); engine.addLink(6,8); engine.addLink(8,9); engine.addLink(2,7); engine.addLink(7,9); System.out.println(&quot;Before calculating path&quot;); path = engine.findPath(0, 9); System.out.println(&quot;After calculating path:&quot;); for (int i=0; i&lt;path.length; i++) { System.out.println(&quot; node # &quot; + path[i]); } this.engine = engine; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private int [] path = null; private BreadthFirstSearch engine = null; public static void main(String[] args) { GraphBreadthFirstSearch graphBreadthFirstSearch1 = new GraphBreadthFirstSearch(); } private void jbInit() throws Exception { this.setDefaultCloseOperation(3); jPanel1.setBackground(Color.white); this.getContentPane().add(jPanel1, BorderLayout.CENTER); this.setSize(290, 180); this.setVisible(true); } protected void paintNode(Graphics g, String name, int x, int y) { int len = name.length() * 10 + 6; int x1 = x - (len / 2); int x2 = x + (len / 2); int y1 = y - 10; int y2 = y + 10; g.setColor(Color.cyan); g.fill3DRect(x1, y1, len, 20, true); g.setColor(Color.black); g.drawString(name, x1 + 4, y2 - 6); } public void paint(Graphics g) { super.paint(g); if (engine != null &amp;&amp; path != null) { int numNodes = engine.getNumNodes(); int numLinks = engine.getNumLinks(); for (int i=0; i&lt;numLinks; i++) { int l1 = engine.getLink1(i); int l2 = engine.getLink2(i); int x1 = engine.getNodeX(l1); int y1 = engine.getNodeY(l1); int x2 = engine.getNodeX(l2); int y2 = engine.getNodeY(l2); g.setColor(Color.lightGray); g.drawLine(x1, y1, x2, y2); } for (int i=1; i&lt;path.length; i++) { int x1 = engine.getNodeX(path[i-1]); int y1 = engine.getNodeY(path[i-1]); int x2 = engine.getNodeX(path[i]); int y2 = engine.getNodeY(path[i]); g.setColor(Color.black); g.drawLine(x1, y1, x2, y2); } for (int i=0; i&lt;numNodes; i++) { int x1 = engine.getNodeX(i); int y1 = engine.getNodeY(i); paintNode(g, engine.getNodeName(i), x1, y1); } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-20T16:00:00Z GraphDepthFirstSearch.java 2002-01-20T16:00:00Z 2002-01-20T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*; import java.awt.*; public class GraphDepthFirstSearch extends JFrame { JPanel jPanel1 = new JPanel(); public GraphDepthFirstSearch() { DepthFirstSearch engine = new DepthFirstSearch(); // Define a test network before calling the super class 'init': engine.addNode(&quot;0&quot;, 20, 40); engine.addNode(&quot;1&quot;, 60, 60); engine.addNode(&quot;2&quot;, 100, 40); engine.addNode(&quot;3&quot;, 50, 110); engine.addNode(&quot;4&quot;, 140, 80); engine.addNode(&quot;5&quot;, 160, 150); engine.addNode(&quot;6&quot;, 200, 80); engine.addNode(&quot;7&quot;, 160, 40); engine.addNode(&quot;8&quot;, 240, 120); engine.addNode(&quot;9&quot;, 260, 90); engine.addLink(0,1); engine.addLink(1,2); engine.addLink(2,3); engine.addLink(2,4); engine.addLink(4,5); engine.addLink(4,6); engine.addLink(6,8); engine.addLink(8,9); engine.addLink(2,7); engine.addLink(7,9); System.out.println(&quot;Before calculating path&quot;); path = engine.findPath(0, 9); System.out.println(&quot;After calculating path:&quot;); for (int i=0; i&lt;path.length; i++) { System.out.println(&quot; node # &quot; + path[i]); } this.engine = engine; try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private int [] path = null; private DepthFirstSearch engine = null; public static void main(String[] args) { GraphDepthFirstSearch graphDepthFirstSearch1 = new GraphDepthFirstSearch(); } private void jbInit() throws Exception { this.setDefaultCloseOperation(3); jPanel1.setBackground(Color.white); this.getContentPane().add(jPanel1, BorderLayout.CENTER); this.setSize(290, 180); this.setVisible(true); } protected void paintNode(Graphics g, String name, int x, int y) { int len = name.length() * 10 + 6; int x1 = x - (len / 2); int x2 = x + (len / 2); int y1 = y - 10; int y2 = y + 10; g.setColor(Color.cyan); g.fill3DRect(x1, y1, len, 20, true); g.setColor(Color.black); g.drawString(name, x1 + 4, y2 - 6); } public void paint(Graphics g) { super.paint(g); if (engine != null &amp;&amp; path != null) { int numNodes = engine.getNumNodes(); int numLinks = engine.getNumLinks(); for (int i=0; i&lt;numLinks; i++) { int l1 = engine.getLink1(i); int l2 = engine.getLink2(i); int x1 = engine.getNodeX(l1); int y1 = engine.getNodeY(l1); int x2 = engine.getNodeX(l2); int y2 = engine.getNodeY(l2); g.setColor(Color.lightGray); g.drawLine(x1, y1, x2, y2); } for (int i=1; i&lt;path.length; i++) { int x1 = engine.getNodeX(path[i-1]); int y1 = engine.getNodeY(path[i-1]); int x2 = engine.getNodeX(path[i]); int y2 = engine.getNodeY(path[i]); g.setColor(Color.black); g.drawLine(x1, y1, x2, y2); } for (int i=0; i&lt;numNodes; i++) { int x1 = engine.getNodeX(i); int y1 = engine.getNodeY(i); paintNode(g, engine.getNodeName(i), x1, y1); } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-20T16:00:00Z Maze.java 2002-01-19T16:00:00Z 2002-01-19T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import java.awt.Dimension; /** * Class Maze - private class for representing search space as a two-dimensional maze */ public class Maze { public static short OBSTICLE = -1; public static short START_LOC_VALUE = -2; public static short GOAL_LOC_VALUE = -3; private int width = 0; private int height = 0; public Dimension startLoc = new Dimension(); public Dimension goalLoc = new Dimension(); /** * The maze (or search space) data is stored as a short integer rather than * as a boolean so that bread-first style searches can use the array to store * search depth. A value of -1 indicates a barrier in the maze. */ private short [][]maze; public Maze(int width, int height) { System.out.println(&quot;New maze of size &quot; + width + &quot; by &quot; + height); this.width = width; this.height = height; maze = new short[width+2][height+2]; for (int i=0; i&lt;width+2; i++) { for (int j=0; j&lt;height+2; j++) { maze[i][j] = 0; } } for (int i=0; i&lt;height+2; i++) { maze[0][i] = maze[width+1][i] = OBSTICLE; } for (int i=0; i&lt;width+2; i++) { maze[i][0] = maze[i][height+1] = OBSTICLE; } /** * Randomize the maze by putting up arbitray obsticals */ int max_obsticles = (width * height) / 3; for (int i=0; i&lt;max_obsticles; i++) { int x = (int)(Math.random()*width); int y = (int)(Math.random()*height); setValue(x, y, OBSTICLE); } /** * Specify the starting location */ startLoc.width = 0; startLoc.height = 0; setValue(0, 0, (short)0); /** * Specify the goal location */ goalLoc.width = width - 1; goalLoc.height = height - 1; setValue(width - 1, height - 1, GOAL_LOC_VALUE); } synchronized public short getValue(int x, int y) { return maze[x+1][y+1]; } synchronized public void setValue(int x, int y, short value) { maze[x+1][y+1] = value; } public int getWidth() { return width; } public int getHeight() { return height; } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-19T16:00:00Z MazeBreadthFirstSearch.java 2002-01-19T16:00:00Z 2002-01-19T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; /** * Title: MazeBreadthFirstSearch&lt;p&gt; * Description: Demo program for Java AI Programming&lt;p&gt; * Copyright: Copyright (c) Mark Watson, Released under Open Source Artistic License&lt;p&gt; * Company: Mark Watson Associates&lt;p&gt; * @author Mark Watson * @version 1.0 */ public class MazeBreadthFirstSearch extends javax.swing.JFrame { JPanel jPanel1 = new JPanel(); BreadthFirstSearchEngine currentSearchEngine = null; public MazeBreadthFirstSearch() { try { jbInit(); } catch (Exception e) { System.out.println(&quot;GUI initilization error: &quot; + e); } currentSearchEngine = new BreadthFirstSearchEngine(10, 10); repaint(); } public void paint(Graphics g_unused) { if (currentSearchEngine == null) return; Maze maze = currentSearchEngine.getMaze(); int width = maze.getWidth(); int height = maze.getHeight(); System.out.println(&quot;Size of current maze: &quot; + width + &quot; by &quot; + height); Graphics g = jPanel1.getGraphics(); BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB); Graphics g2 = image.getGraphics(); g2.setColor(Color.white); g2.fillRect(0, 0, 320, 320); g2.setColor(Color.black); for (int x=0; x&lt;width; x++) { for (int y=0; y&lt;height; y++) { short val = maze.getValue(x,y); if ( val == Maze.OBSTICLE) { g2.setColor(Color.lightGray); g2.fillRect(6 + x * 28, 3 + y * 29, 29, 30); } else if (val == Maze.START_LOC_VALUE || (x==0 &amp;&amp; y==0)) { g2.setColor(Color.blue); g2.drawString(&quot;S&quot;, 16 + x * 28, 19 + y * 29); } else if (val == Maze.GOAL_LOC_VALUE) { g2.setColor(Color.red); g2.drawString(&quot;G&quot;, 16 + x * 28, 19 + y * 29); } } } // redraw the path in black: g2.setColor(Color.black); Dimension [] path = currentSearchEngine.getPath(); for (int i=1; i&lt; (path.length-1); i++) { int x = path[i].width; int y = path[i].height; short val = maze.getValue(x,y); g2.drawString(&quot;&quot; + (path.length - i), 16 + x * 28, 19 + y * 29); } g.drawImage(image, 30, 40, 320, 320, null); } public static void main(String[] args) { MazeBreadthFirstSearch mazeSearch1 = new MazeBreadthFirstSearch(); } private void jbInit() throws Exception { this.setContentPane(jPanel1); this.setCursor(null); this.setDefaultCloseOperation(3); this.setTitle(&quot;MazeBreadthFirstSearch&quot;); this.getContentPane().setLayout(null); jPanel1.setBackground(Color.white); jPanel1.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION); jPanel1.setDoubleBuffered(false); jPanel1.setRequestFocusEnabled(false); jPanel1.setLayout(null); this.setSize(370, 420); this.setVisible(true); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-19T16:00:00Z MazeDepthFirstSearch.java 2002-01-19T16:00:00Z 2002-01-19T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; /** * Title: MazeDepthFirstSearch&lt;p&gt; * Description: Demo program for Java AI Programming&lt;p&gt; * Copyright: Copyright (c) Mark Watson, Released under Open Source Artistic License&lt;p&gt; * Company: Mark Watson Associates&lt;p&gt; * @author Mark Watson * @version 1.0 */ public class MazeDepthFirstSearch extends javax.swing.JFrame { JPanel jPanel1 = new JPanel(); DepthFirstSearchEngine currentSearchEngine = null; public MazeDepthFirstSearch() { try { jbInit(); } catch (Exception e) { System.out.println(&quot;GUI initilization error: &quot; + e); } currentSearchEngine = new DepthFirstSearchEngine(10, 10); repaint(); } public void paint(Graphics g_unused) { if (currentSearchEngine == null) return; Maze maze = currentSearchEngine.getMaze(); int width = maze.getWidth(); int height = maze.getHeight(); System.out.println(&quot;Size of current maze: &quot; + width + &quot; by &quot; + height); Graphics g = jPanel1.getGraphics(); BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB); Graphics g2 = image.getGraphics(); g2.setColor(Color.white); g2.fillRect(0, 0, 320, 320); g2.setColor(Color.black); for (int x=0; x&lt;width; x++) { for (int y=0; y&lt;height; y++) { short val = maze.getValue(x,y); if ( val == Maze.OBSTICLE) { g2.setColor(Color.lightGray); g2.fillRect(6 + x * 28, 3 + y * 29, 29, 30); } else if (val == Maze.START_LOC_VALUE || val == 1) { g2.setColor(Color.blue); g2.drawString(&quot;S&quot;, 16 + x * 28, 19 + y * 29); } else if (val == Maze.GOAL_LOC_VALUE) { g2.setColor(Color.red); g2.drawString(&quot;G&quot;, 16 + x * 28, 19 + y * 29); } else if (val &gt; 0) { //g2.setColor(Color.green); //g2.drawString(&quot;&quot; + val, 16 + x * 28, 19 + y * 29); } } } // redraw the path in black: g2.setColor(Color.black); Dimension [] path = currentSearchEngine.getPath(); for (int i=1; i&lt; path.length; i++) { int x = path[i].width; int y = path[i].height; short val = maze.getValue(x,y); g2.drawString(&quot;&quot; + val, 16 + x * 28, 19 + y * 29); } g.drawImage(image, 30, 40, 320, 320, null); } public static void main(String[] args) { MazeDepthFirstSearch mazeSearch1 = new MazeDepthFirstSearch(); } private void jbInit() throws Exception { this.setContentPane(jPanel1); this.setCursor(null); this.setDefaultCloseOperation(3); this.setTitle(&quot;MazeDepthFirstSearch&quot;); this.getContentPane().setLayout(null); jPanel1.setBackground(Color.white); jPanel1.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION); jPanel1.setDoubleBuffered(false); jPanel1.setRequestFocusEnabled(false); jPanel1.setLayout(null); this.setSize(370, 420); this.setVisible(true); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2002-01-19T16:00:00Z NLBean.java 2001-12-08T16:00:00Z 2001-12-08T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">// mwa.products.NLBean.java // // Copyright 1997, Mark Watson. // package nlbean; import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.text.*; import java.util.*; import java.beans.*; import java.sql.*; import java.util.Vector; public class NLBean extends Panel implements Serializable { protected NLEngine engine = null; public NLBean() { super(); reset(); } private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { System.out.println(&quot;Beginning writeObject&quot;); s.defaultWriteObject(); System.out.println(&quot;Ending writeObject&quot;); } private void resetExamples() { choiceChanged = false; choice.removeAll(); choice.addItem(&quot;Examples &quot;); for (int i=(examples.length - 1); i&gt;= 0; i--) { choice.insert(examples[i], 1); } } private void resetSynonyms() { engine.clearSynonyms(); for (int i=0; i&lt;synonyms.length; i++) { addSynonym(synonyms[i]); } } // Set up USER INTERFACE: private void reset() { engine = new NLEngine(); AWTsetup(); } private void AWTsetup() { Frame help_frame = new Frame(); help = new Help(help_frame); setFont(new Font(&quot;Dialog&quot;, Font.PLAIN, 12)); setLayout(null); Label l1 = new Label(&quot;Natural Language Database Access&quot;); l1.setFont(new Font(&quot;Dialog&quot;, Font.BOLD, 28)); add(l1); l1.setBounds(2, 1, 600, 34); list1 = new java.awt.List(3, false); for (int i=0; i&lt;databaseNames.length; i++) list1.add(databaseNames[i]); list2 = new java.awt.List(3, false); list3 = new java.awt.List(3, false); add(list1); add(list2); add(list3); list1.setBounds(2, 40, 220, 90); list2.setBounds(232, 40, 170, 90); list3.setBounds(412, 40, 170, 90); list1.addMouseListener(new MouseSelect1()); list2.addMouseListener(new MouseSelect2()); list3.addMouseListener(new MouseSelect3()); Button q_button = new Button(&quot;Do query&quot;); q_button.addMouseListener(new MouseQuery()); add(q_button); q_button.setBounds(2, 140, 160, 30); Button help_button = new Button(&quot;Help&quot;); help_button.addMouseListener(new MouseHelp()); add(help_button); help_button.setBounds(172, 140, 40, 30); Label label22 = new Label(&quot;Query:&quot;); label22.setFont(new Font(&quot;Dialog&quot;, Font.BOLD, 14)); add(label22); label22.setBounds(2, 180, 60, 22); label22.setVisible(true); // inputText = new SmartTextField(&quot;list Salary where EmpName equals Mark&quot;, 64); inputText = new TextField(&quot;list Salary where EmpName equals Mark&quot;, 64); add(inputText); inputText.setBounds(80, 180, 500, 22); choice = new Choice(); choiceChanged = false; choice.addItem(&quot;Examples &quot;); for (int i=(examples.length - 1); i&gt;=0; i--) choice.insert(examples[i], 1); choice.addItemListener(new ChoiceListener()); add(choice); choice.setBounds(2, 210, 582, 25); Label label23 = new Label(&quot;Generated SQL:&quot;); label23.setFont(new Font(&quot;Dialog&quot;, Font.BOLD, 12)); add(label23); label23.setBounds(2, 240, 120, 30); sqlText = new TextArea(&quot;&quot;,1,80,TextArea.SCROLLBARS_NONE); sqlText.setEditable(false); add(sqlText); sqlText.setBounds(130, 240, 455, 40); outputText = new TextArea(&quot;NLBean(tm) natural language interface\nCopyright 1997, Mark Watson. All rights reserved.\n&quot;, 8, 74); add(outputText); outputText.setBounds(2, 285, 582, 150); Label l1x = new Label(&quot;NLBean Copyright 1997-1999 by Mark Watson. www.markwatson.com&quot;); l1x.setFont(new Font(&quot;Dialog&quot;, Font.ITALIC, 14)); add(l1x); l1x.setBounds(5, 442, 540, 16); // * * * list1_last_selection=-1; list2_last_selection=-1; list3_last_selection=-1; setBounds(20, 20, 590, 464); setupHelper(); } private void setupHelper() { if (loadSynonyms) { for (int i=0; i&lt;synonyms.length; i++) addSynonym(synonyms[i]); } if (loadDB) { for (int i=0; i&lt;databaseNames.length; i++) { engine.addDB(databaseNames[i], userNames[i], passwords[i], tableLists[i]); } } engine.initDB(); } // Suggest spelling corrections, or other words: // synchronized private String[] suggestedWords(String a_word) { // return engine.suggestedWords(a_word); // } synchronized private void putText(String s, boolean replace_flag) { if (replace_flag==false) { outputText.append(s); } else { outputText.setText(s); } } synchronized private String inText(String new_val, boolean set_flag) { if (set_flag) { inputText.setText(new_val); return &quot;&quot;; } return inputText.getText(); } synchronized private void query() { System.out.println(&quot;Entering query()&quot;); sqlText.setText(&quot;&quot;); String a_query = inText(&quot;&quot;, false); System.out.println(&quot;query(): a_query=&quot; + a_query); String sql_query=null; if (a_query.startsWith(&quot;SELECT&quot;) || a_query.startsWith(&quot;select&quot;)) { sql_query = a_query; } else { engine.parse(a_query); sql_query = engine.getSQL(); } if (sql_query==null) { System.out.println(&quot;No SQL for &quot; + a_query); return; } System.out.println(&quot;SQL query: &quot; + sql_query); sqlText.setText(sql_query); try { engine.createResultSet(sql_query, databaseNames[0], userNames[0], passwords[0]); putText(&quot;Query results:\n&quot;, false); String data = engine.getRows(sql_query, databaseNames[0], userNames[0], passwords[0]); putText(engine.toEnglish(data) + &quot;\n&quot;, false); } catch (Exception e) { e.printStackTrace(); } } private boolean usingAWT = false; private boolean loadSynonyms = true; private boolean loadDB = true; private void addSynonym(String def_string) { int pos = def_string.indexOf(&quot;=&quot;); String description = def_string.substring(0,pos); String column = def_string.substring(pos+1); if (engine!=null) { engine.addSynonym(column, description); } } // Use an inner classes for mouse event handling: class MouseQuery extends MouseAdapter implements Serializable { // The magic: access the public query method in the // containing class: synchronized public void mouseReleased(MouseEvent mevt) { query(); } } // Use an inner classes for mouse event handling: class MouseSelect1 extends MouseAdapter implements Serializable { synchronized public void mouseReleased(MouseEvent mevt) { if (list1_last_selection != list1.getSelectedIndex()) { list1_last_selection = list1.getSelectedIndex(); String s=&quot;&quot;; if (list1_last_selection &gt;= 0 &amp;&amp; list1_last_selection &lt; tableLists.length) { s = tableLists[list1_last_selection]; } System.out.println(&quot;s=&quot; + s); String temp [] = Util.parseStrings(s); list2.removeAll(); list3.removeAll(); for (int i=0; i&lt;temp.length; i++) list2.addItem(temp[i]); } } } // Use an inner classes for mouse event handling: class MouseSelect2 extends MouseAdapter implements Serializable { synchronized public void mouseReleased(MouseEvent mevt) { if (list2_last_selection != list2.getSelectedIndex()) { list2_last_selection = list2.getSelectedIndex(); list3.removeAll(); String sel1 [] = list1.getSelectedItems(); if (sel1!=null) { if (sel1.length&gt;0) { String sel2 [] = list2.getSelectedItems(); if (sel2!=null) { if (sel2.length&gt;0) { String user=&quot;&quot;; String pass=&quot;&quot;; if (list1_last_selection &gt;= 0 &amp;&amp; list1_last_selection &lt; userNames.length) { user = userNames[list1_last_selection]; } try { String cols[] = engine.getColumnNames(sel2[0], sel1[0], user, pass); if (cols!=null) { for (int j=0; j&lt;cols.length; j++) { list3.addItem(cols[j]); } } } catch (Exception e) { } } } } } } } } // Use an inner classes for mouse event handling: class MouseSelect3 extends MouseAdapter implements Serializable { synchronized public void mouseReleased(MouseEvent mevt) { if (list3_last_selection != list3.getSelectedIndex()) { System.out.println(&quot;TESTING 3rd list selection&quot;); list3_last_selection = list3.getSelectedIndex(); String [] sel1x = list1.getSelectedItems(); if (sel1x!=null) { if (sel1x.length&gt;0) { String sel2 [] = list2.getSelectedItems(); String sel3 [] = list3.getSelectedItems(); if (sel2!=null &amp;&amp; sel3!=null) { if (sel3.length&gt;0) { String user=&quot;&quot;; String pass=&quot;&quot;; if (list1_last_selection &gt;= 0 &amp;&amp; list1_last_selection &lt; userNames.length) { user = userNames[list1_last_selection]; pass = passwords[list1_last_selection]; } try { String r = engine.getRows(&quot;SELECT &quot; + sel3[0] + &quot; FROM &quot; + sel2[0], sel1x[0], user, pass); if (r==null) return; putText(r + &quot;\n&quot;, false); } catch (Exception e) { } } } } } } } } private Choice choice; transient private boolean choiceChanged = false; class ChoiceListener implements ItemListener { public void itemStateChanged(ItemEvent ie) { System.out.println(&quot;choice menu: &quot; + ie.paramString()); System.out.println(&quot;choice menu: &quot; + (String)ie.getItem()); String sel = (String)ie.getItem(); if (sel.equals(&quot;Examples&quot;)) return; inText(sel, true); if (choiceChanged==false) { choice.remove(0); choiceChanged=true; } System.out.println(&quot;choice menu: &lt;returning&gt;&quot;); } } private Help help; class MouseHelp extends MouseAdapter implements Serializable { public void mouseReleased(MouseEvent mevt) { help.setVisible(true); } } public static void main(String args[]) { Frame f = new Frame(); NLBean sc = new NLBean(); f.add(sc); f.setTitle(&quot;NLBean version 4.0&quot;); f.pack(); f.show(); f.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } private TextArea outputText; private /* Smart */ TextField inputText; private TextArea sqlText; private java.awt.List list1, list2, list3; private int list1_last_selection, list2_last_selection, list3_last_selection; // properties for specifying a few sample natural language // queries to place at the beginning of the 'Example' // 'choice' control. NOTE: this program automatically // adds additional examples that it generates from examining // the field names of the database tables. private String [] examples = { &quot;list email address where name equals Mark&quot;, &quot;list salary where employee name equals Mark&quot;, &quot;list salary where hire date is after 1993/1/5 and employee name equals Mark&quot;, &quot;list name, phone number, and email address where name equals Mark&quot;, &quot;list employee name, salary, and hire date where hire date is after January 10, 1993&quot;, &quot;list salary where hire date is after January 1, 1993 or employee name equals Carol&quot;, &quot;list product name where cost is less than $20&quot; }; // Set up for synonyms: private String [] synonyms = { &quot;employee name=EmpName&quot;, &quot;hire date=HireDate&quot;, &quot;phone number=PhoneNumber&quot;, &quot;email address=Email&quot;, &quot;product name=productname&quot;, &quot;products=productname&quot;, &quot;product=productname&quot; }; // for four properties each for up to three databases: // Demo database information private String [] databaseNames = {&quot;jdbc:idb:database/nlbean.prp&quot;}; private String [] userNames = {&quot;Admin&quot;}; private String [] passwords = {&quot;sammy&quot;}; private String [] tableLists= {&quot;NameTable;products;Employees&quot;}; // // Inner class to handle text input field: // transient protected int startWordPos=0, stopWordPos=0; class SmartTextField extends TextField implements Serializable { public SmartTextField(String s, int width) { super(s, width); helper(); } public SmartTextField(String s) { super(s); helper(); } public SmartTextField() { super(); helper(); } private void helper() { addMouseListener(new MouseText()); addKeyListener(new MyKeyAdapter()); setEditable(true); setFont(new Font(&quot;default&quot;, Font.PLAIN, 12)); words = new String[20]; for (int i=0; i&lt;20; i++) words[i]=&quot;&quot;; num_words=0; } transient private String words[] = null; transient int num_words; public String getWord(int charPos) { startWordPos=stopWordPos=-1; String s = getText(); // find start of word: int start = charPos-1; try { while (start &gt;= 0 &amp;&amp; s.charAt(start)!=' ') { start--; } start++; } catch (Exception e) {} // find the end of word: int stop=charPos; try { while (stop &lt; s.length() &amp;&amp; s.charAt(stop)!=' ') { stop++; } } catch (Exception e) {} if (start&lt;0) start=0; //if (stop&gt;s.length()-1) stop=s.length()-1; System.out.println(&quot;getWord(): start=&quot; + start + &quot;, stop=&quot; + stop); //if (stop &gt; s.length() - 2) stop = s.length() - 1; int stp = stop; if (stop&gt;my_char_pos) { stop=my_char_pos; System.out.println(&quot; stop reset to &quot; + stop); } if (start &lt; stop) { startWordPos=start; stopWordPos=stp; System.out.println(&quot;getWord() returning: |&quot; + s.substring(start, stop) + &quot;|&quot;); return s.substring(start, stop); } else return &quot;&quot;; } public void setUpperCase(int charPos) { String s = getText(); // find start of word: int start = charPos-1; try { while (start &gt;= 0 &amp;&amp; s.charAt(start)!=' ') { start--; } start++; } catch (Exception e) {} // find the end of word: int stop=charPos; try { while (stop &lt; s.length() &amp;&amp; s.charAt(stop)!=' ') { stop++; } } catch (Exception e) {} if (start&lt;0) start=0; if (stop&gt;s.length()) stop=s.length(); if (stop&gt;my_char_pos) stop=my_char_pos; if (start &lt; stop) { StringBuffer sb = new StringBuffer(s); for (int i=start; i&lt;stop; i++) { char ch = sb.charAt(i); if (ch &gt;= 'a' &amp;&amp; ch &lt;= 'z') ch += 'A' - 'a'; sb.setCharAt(i, ch); } setText(new String(sb)); setCaretPosition(stop); } } transient private int charWidth = -1; transient private FontMetrics fm = null; public void paint(Graphics g) { super.paint(g); if (fm==null) { fm = g.getFontMetrics(); } } private int getPixelWidth(String s) { if (fm==null) return 1; return fm.stringWidth(s); } transient private int my_char_pos=0; public int getCharPos(int pixel) { String s = getText(); my_char_pos=-1; if (getPixelWidth(s) &lt; pixel) return -1; // to right of text int len = s.length(); for (int i=1; i&lt;len; i++) { if (getPixelWidth(s.substring(0, i)) &gt; pixel - 12) { my_char_pos=i; return i; } } my_char_pos = len-1; return len-1; } // Handle mouse events over the input text window: class MouseText extends MouseAdapter implements Serializable { public void mousePressed(MouseEvent mevt) { int x = mevt.getX(); int charPos = getCharPos(x); if (charPos&lt;0) return; my_char_pos = 1000; // special for mouse click only! String word = getWord(charPos); System.out.println(&quot;MouseText.mousePressed(): getWord returned: &quot; + word); if (word.length() &lt; 1) return; System.out.println(&quot;x=&quot; + x + &quot;, charPos=&quot; + charPos + &quot;, word=&quot; + word); } } class MyKeyAdapter extends KeyAdapter implements Serializable { public void keyTyped(KeyEvent ke) { if (ke.getKeyChar()==KeyEvent.VK_SPACE || ke.getKeyChar()==KeyEvent.VK_COMMA || ke.getKeyChar()==KeyEvent.VK_PERIOD) { // Handle new word: //System.out.println(&quot;New word. all text:&quot; + getText()); int charPos = getCaretPosition(); my_char_pos = charPos; System.out.println(&quot;** charPos=&quot; + charPos); String word = getWord(charPos); System.out.println(&quot;** word =&quot; + word); } } } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-08T16:00:00Z NLEngine.java 2001-12-08T16:00:00Z 2001-12-08T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">// nlbean.NLEngineLocal.java // // Copyright 1997, 1999 Mark Watson. // package nlbean; import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.text.*; import java.util.*; import java.beans.*; import java.sql.*; import java.util.Vector; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class NLEngine implements Serializable { public NLEngine() { // Create a DBInfo object and initialize it with the current // database properties: dbinfo = new DBInfo(); nlp = new NLP(dbinfo); // lexicon_data = nlp.lexicon_data; } public void addDB(String name, String user, String passwd, String tbls) { if (numDB &lt; 4) { databaseName[numDB] = name; userName[numDB] = user; password[numDB] = passwd; tableList[numDB] = tbls; numDB++; } } public void initDB() { for (int d=0; d&lt;numDB; d++) { if (databaseName[d]!=null) { String tl[] = Util.parseStrings(tableList[d]); if (tl!=null) { for (int i=0; i&lt;tl.length; i++) { try { String cols[] = DBInterface.getColumnNames(tl[i], databaseName[d], userName[d], password[d]); if (cols!=null) { // lexicon_data.addTableName(tl[i]); dbinfo.addTable(tl[i], databaseName[d], userName[d], password[d], cols); // if (lexicon_data!=null) { // for (int ii=0; ii&lt;cols.length; ii++) { // System.out.println(&quot;adding column name to lexicon: &quot; + cols[ii]); // lexicon_data.addColumnName(cols[ii]); // } // } else { // System.out.println(&quot;null lexicon_data&quot;); // } } } catch (Exception e) { } } } } } dbinfo.debug(); } // NLP data and methods: public void parse(String s) { // Remove any commas from the input text: for (int i=0; i&lt;20; i++) { int idx = s.indexOf(&quot;,&quot;); if (idx&gt;-1) { s = s.substring(0,idx) + s.substring(idx+1); } else { break; } } // Remove any periods from the input text: for (int i=0; i&lt;10; i++) { int idx = s.indexOf(&quot;.&quot;); if (idx&gt;-1) { s = s.substring(0,idx) + s.substring(idx+1); } else { break; } } // remove extra spaces and convert to lower case: s = &quot; &quot; + Util.removeExtraSpaces(s).toLowerCase() + &quot; &quot;; // before calling the NLP class parse method, we // need to replace synonyms: numSynReplacements = 0; for (int i=0; i&lt;numSynonyms; i++) { // allow for multiple uses of the same synonym: for (int repeat=0; repeat&lt;4; repeat++) { int idx = s.indexOf(synonymDescription[i]); if (idx&gt;-1) { s = s.substring(0,idx+1) + synonymColumnName[i] + s.substring(idx + synonymDescription[i].length() - 1); syns[numSynReplacements] = synonymColumnName[i]; origs[numSynReplacements] = synonymDescription[i]; numSynReplacements++; } else { break; } } } // remove extra spaces (just to make sure!): s = Util.removeExtraSpaces(s); nlp.parse(s); } // See if a word is &quot;known&quot; (this includes table and column names): // public boolean goodWord(String s) { // if (lexicon_data.wordInLexicon(s.toLowerCase())) return true; // return false; // } public String getSQL() { return nlp.getSQL(); } private int num_rows_from_database = 0; public String [] breakLines(String s) { String [] ret = new String[40]; int count = 0; num_rows_from_database = 0; try { StringReader sr = new StringReader(s); BufferedReader br = new BufferedReader(sr); while (true) { String s2 = br.readLine(); if (s2 == null || s2.length() &lt; 1) break; num_rows_from_database++; // change for InstantDB: comma separated terms: int index = 0; while (true) { index = s2.indexOf(&quot;,&quot;); if (index == -1) { if (count &gt; 38) break; ret[count++] = s2.trim(); break; } else { if (count &gt; 38) break; ret[count++] = s2.substring(0, index); s2 = s2.substring(index + 1); } } } String [] ret2 = new String[count]; for (int i=0; i&lt;count; i++) ret2[i] = ret[i]; return ret2; } catch (Exception e) { } return null; } public String toEnglish(String r) { System.out.println(&quot;NLEngineLocal.toEnglish(&quot; + r + &quot;)&quot;); return nlp.toEnglish(breakLines(r), syns, origs, numSynReplacements, num_rows_from_database); } public void createResultSet(String sql_query, String database, String user, String password) { //SQL.createResultSet(sql_query, database, user, password); } public String [] getColumnNames(String sql_query, String database, String user, String password) { try { return DBInterface.getColumnNames(sql_query, database, user, password); } catch (Exception e) { } return null; } public String getRows(String sql_query, String database, String user, String password) { try { return DBInterface.query(sql_query, database, user, password); } catch (Exception e) { } return null; } public DBInfo dbinfo; private NLP nlp; // Allow developers to define a few synonyms for a particular // application: private String synonymColumnName[] = new String[11]; private String synonymDescription[] = new String[11]; private int numSynonyms = 0; // For use in generating nglish output for queries: private String syns[] = new String[11]; private String origs[] = new String[11]; private int numSynReplacements=0; public void clearSynonyms() { numSynonyms = 0; } public void addSynonym(String column, String description) { if (numSynonyms&lt;10) { synonymColumnName[numSynonyms] = column; synonymDescription[numSynonyms] = &quot; &quot; + description + &quot; &quot;; numSynonyms++; } } // for four properties each for up to five databases: private String databaseName[] = new String[5]; private String userName[] = new String[5]; private String password[] = new String[5]; private String tableList[] = new String[5]; private int numDB=0; } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-08T16:00:00Z Prolog.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z family.pl 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><TEXTAREA name="code" class="pl" rows="16" cols="100">%% sample file for Appendix B father(louis, ken). father(louis, paul). father(ken, ron). father(ken, mark). grandfather(X,Z) :- father(X,Y), father(Y,Z). </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z p0.pl 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><TEXTAREA name="code" class="pl" rows="16" cols="100">%% define words: determiner(D) :- member(D,[the,a,an]). noun(N) :- member(N,[dog, street, ball, bat, boy]). adj(A) :- member(A,[fast, little, big]). prep(P) :- member(P,[down, under]). verb(V) :- member(V,[ran, caught, yelled, see, saw]). %% parse noun phrases: noun_phrase([D,N]) :- determiner(D), noun(N). noun_phrase([N]) :- noun(N). %% parse prepositional phase: prep_phrase(PP) :- prep(P), noun_phrase(NP), append([P],NP,PP). %% parse verb phrases: verb_phrase(VP) :- verb(V), noun_phrase(NP), append(V,NP,VP). verb_phrase(VP) :- verb(V), prep_phrase(PP), append([V],PP,VP). verb_phrase([V]) :- verb(V). %% parse entences: sentence(S) :- noun_phrase(NP), verb_phrase(VP), append(NP,VP,S). %% throw away test code: test :- sentence([the,dog, ran]). test1 :- sentence([the,dog, ran, down, the, street]). </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z program.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z prologop.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z prologtokenizer.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z rack.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z solver.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z term.class 2000-06-25T16:00:00Z 2000-06-25T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2000-06-25T16:00:00Z