Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- *
- * @author Owner
- */
- public class AStar {
- public AStar()
- {
- }
- public Node[] PathGen(Node start, Node goal,boolean accesible) throws Exception
- {
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:Nodes.db");
- Statement stat = conn.createStatement();
- PriorityQueue<Node> open=new PriorityQueue<Node>();
- open.add(start);
- HashMap hm=new HashMap();
- hm.put(start.label,start);
- hm.put(goal.label, goal);
- while(open.size()!=0)
- {
- //System.out.println(open.size());
- Node hold= open.remove();
- if(hold ==goal) return path(hold);
- hold.traversed=true;
- hold.g=0;
- double x1= hold.x-goal.x;
- double y1 =hold.y-goal.y;
- double z1= hold.z-goal.z;
- hold.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
- for(int i =0; i<hold.accesible.length;i++)
- {
- String u=hold.accesible[i];
- Node c;
- if(hm.containsKey(u))
- {
- c=(Node)(hm.get(u));
- }
- else
- {
- int xx,yy,zz;
- ResultSet rs1 = stat.executeQuery("select * from node where (label ==\""+u+"\");");
- xx=rs1.getInt("x");
- System.out.println("hello");
- yy=rs1.getInt("y");
- zz=rs1.getInt("z");
- c=new Node(u,xx,yy,zz);
- c.accesible=new String[rs1.getInt("num1")];
- c.nonaccesible=new String[rs1.getInt("num2")];
- ResultSet rs2=stat.executeQuery("select * from sArr where label =\""+u+"\"");
- int j=0;
- while(rs2.next())
- {
- if(rs2.getBoolean("bool"))
- {
- c.accesible[i]=rs2.getString("point");
- }
- else
- {
- c.nonaccesible[i]=rs2.getString("point");
- }
- }
- //rs1.close();
- rs2.close();
- hm.put(c.label,c);
- }
- if(c.traversed) continue;
- double tempg=hold.g;
- double x=hold.x-c.x;
- double y=hold.y-c.y;
- double z=hold.z-c.z;
- double dist=Math.sqrt(x*x+y*y+z*z);
- tempg+=dist;
- //System.out.println(tempg);
- if(tempg < c.g )
- {
- open.remove(c);
- c.from=hold;
- c.g=tempg;
- x1= c.x-goal.x;
- y1 =c.y-goal.y;
- z1= c.z-goal.z;
- c.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
- c.f= c.g+c.h;
- c.traversed=true;
- open.add(c);
- }
- }
- /*if(!accesible&& hold.nonaccesible!=null)
- {
- for(int i =0; i<hold.nonaccesible.length;i++)
- {
- String u=hold.nonaccesible[i];
- Node c;
- if(hm.containsKey(u))
- {
- c=(Node)(hm.get(u));
- }
- else
- {
- int xx,yy,zz;
- String acce[],nacce[];
- ResultSet rs = stat.executeQuery("select label, x,y,z,acc,nacc from people where label ="+u+";");
- xx=rs.getInt("x");
- yy=rs.getInt("y");
- zz=rs.getInt("z");
- c=new Node(u,xx,yy,zz);
- c.accesible=(String[])rs.getObject("acc");
- c.nonaccesible=(String[])rs.getObject("nacc");
- }
- if(c.traversed) continue;
- double tempg=hold.g;
- double x=hold.x-c.x;
- double y=hold.y-c.y;
- double z=hold.z-c.z;
- double dist=Math.sqrt(x*x+y*y+z*z);
- tempg+=dist;
- if(tempg < c.g )
- {
- open.remove(c);
- c.from=hold;
- c.g=tempg;
- x1= c.x-goal.x;
- y1 =c.y-goal.y;
- z1= c.z-goal.z;
- c.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
- c.f= c.g+c.h;
- open.add(c);
- }
- }
- }*/
- }
- System.out.println("fuck");
- return null;
- }
- public Node[] path( Node end) throws ClassCastException
- {
- ArrayList<Node> l =new ArrayList<Node>();
- l.add(end);
- while(end.from!=null)
- {
- end=end.from;
- l.add(end);
- }
- Object p[] =(l.toArray());
- Node o[] =new Node[p.length];
- for(int i=0; i<o.length;i++)
- {
- o[i]=(Node)(p[o.length-i-1]);
- }
- return o;
- }
- }
- public class Node implements Comparable {
- public String label;
- public double x,y,z, f,g,h;
- public String accesible[];//all accesible paths
- public String nonaccesible[]; //those routes that are non accesible
- boolean traversed;
- public Node from;
- public Node()
- {
- label =null;
- x=0;
- y=0;
- z=0;
- f=Double.MAX_VALUE;
- g=Double.MAX_VALUE;
- h=-1;
- traversed=false;
- from=null;
- }
- public Node(String l,double x1, double y1, double z1)
- {
- label=l;
- x=x1;
- y=y1;
- z=z1;
- f=Double.MAX_VALUE;
- g=Double.MAX_VALUE;
- h=Double.MAX_VALUE;
- traversed=false;
- from=null;
- }
- public int compareTo(Object g)
- {
- Node l=(Node) g;
- return (int)(f - l.f);
- }
- }
- public class Main {
- Node end;
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception{
- new Main();
- }
- public Main() throws Exception
- {
- initialize();
- AStar temp=new AStar();
- Node A=new Node("A",83,80,0);
- String a[] ={"B"};
- A.accesible= a;
- Node G=new Node("A",83,80,0);
- String g[] ={"F"};
- A.accesible= g;
- Node t[]= temp.PathGen(A, G, true);
- for(int i=0; i<t.length;i++)
- {
- System.out.println(t[i].label);
- }
- }
- /*
- public Node sample()
- {
- Node a,b,c,d,e,f,g;
- a=new Node(83,80,0);
- b=new Node(200,192,0);
- c=new Node(50,187,0);
- d=new Node(166,133,0);
- e=new Node(360,205,0);
- f=new Node(333,77,0);
- g=new Node(489,133,0);
- a.accesible=new Node[1];
- a.accesible[0]=b;
- b.accesible=new Node[4];
- b.accesible[0]=a;
- b.accesible[1]=c;
- b.accesible[2]=f;
- b.accesible[3]=e;
- c.accesible=new Node[2];
- c.accesible[0]=b;
- c.accesible[1]=d;
- d.accesible=new Node[1];
- d.accesible[0]=c;
- d.nonaccesible=new Node[1];
- d.nonaccesible[0]=e;
- e.accesible=new Node[2];
- e.accesible[0]=b;
- e.accesible[1]=f;
- e.nonaccesible=new Node[1];
- e.nonaccesible[0]=d;
- f.accesible=new Node[3];
- f.accesible[0]=b;
- f.accesible[1]=e;
- f.accesible[2]=g;
- g.accesible=new Node[1];
- g.accesible[0]=f;
- end=g;
- return b;
- }
- */
- public void initialize() throws Exception
- {
- Class.forName("org.sqlite.JDBC");
- Connection conn = DriverManager.getConnection("jdbc:sqlite:Nodes.db");
- Statement stat = conn.createStatement();
- stat.executeUpdate("drop table if exists node;");
- stat.executeUpdate("drop table if exists sArr;");
- stat.executeUpdate("create table node (label, x,y,z,num1,num2);");
- stat.executeUpdate("create table sArr (label, point,bool);");
- PreparedStatement prep = conn.prepareStatement(
- "insert into node values (?, ? ,? ,?,? ,?);");
- PreparedStatement prep2 = conn.prepareStatement(
- "insert into sArr values (?, ? ,?);");
- prep.setString(1, "A");
- prep.setInt(2, 83);
- prep.setInt(3, 80);
- prep.setInt(4, 0);
- prep.setInt(5,1);
- prep.setInt(6,0);
- prep2.setString(1, "A");
- prep2.setString(2,"B");
- prep2.setBoolean(3, true);
- prep.addBatch();
- prep2.addBatch();
- //Node A=new Node("A",83,80,0);
- //String g[] ={"B"};
- //A.accesible= g;
- prep.setString(1, "B");
- prep.setInt(2, 200);
- prep.setInt(3, 192);
- prep.setInt(4, 0);
- prep.setInt(5,4);
- prep.setInt(6,0);
- prep.addBatch();
- prep2.setString(1, "B");
- prep2.setString(2, "A");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "B");
- prep2.setString(2, "C");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "B");
- prep2.setString(2, "F");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "B");
- prep2.setString(2, "E");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep.setString(1, "C");
- prep.setInt(2, 50);
- prep.setInt(3, 187);
- prep.setInt(4, 0);
- prep.setInt(5,2);
- prep.setInt(6,0);
- prep.addBatch();
- prep2.setString(1, "C");
- prep2.setString(2, "B");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "C");
- prep2.setString(2, "D");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep.setString(1, "D");
- prep.setInt(2, 166);
- prep.setInt(3, 133);
- prep.setInt(4, 0);
- prep.setInt(5,1);
- prep.setInt(6,1);
- prep.addBatch();
- prep2.setString(1, "D");
- prep2.setString(2, "C");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "D");
- prep2.setString(2, "E");
- prep2.setBoolean(3,false);
- prep2.addBatch();
- prep.setString(1, "E");
- prep.setInt(2, 360);
- prep.setInt(3, 205);
- prep.setInt(4, 0);
- prep.setInt(5,2);
- prep.setInt(6,1);
- prep.addBatch();
- prep2.setString(1, "E");
- prep2.setString(2, "B");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "E");
- prep2.setString(2, "F");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "E");
- prep2.setString(2, "D");
- prep2.setBoolean(3,false);
- prep2.addBatch();
- prep.setString(1, "F");
- prep.setInt(2, 333);
- prep.setInt(3, 77);
- prep.setInt(4, 0);
- prep.setInt(5,3);
- prep.setInt(6,0);
- prep.addBatch();
- prep2.setString(1, "F");
- prep2.setString(2, "B");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "F");
- prep2.setString(2, "E");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep2.setString(1, "F");
- prep2.setString(2, "G");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- prep.setString(1, "G");
- prep.setInt(2, 489);
- prep.setInt(3, 133);
- prep.setInt(4, 0);
- prep.setInt(5,2);
- prep.setInt(6,0);
- prep.addBatch();
- prep2.setString(1, "G");
- prep2.setString(2, "F");
- prep2.setBoolean(3,true);
- prep2.addBatch();
- conn.setAutoCommit(false);
- prep.executeBatch();
- prep2.executeBatch();
- conn.setAutoCommit(true);
- conn.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement