InteractiveWorldLearner.java
Go to the documentation of this file.
00001 package edu.wpi.rail.jinteractiveworld.ros;
00002 
00003 import edu.wpi.rail.jinteractiveworld.data.DataSet;
00004 import edu.wpi.rail.jinteractiveworld.model.EMModel;
00005 import edu.wpi.rail.jinteractiveworld.ros.msgs.interactiveworldmsgs.*;
00006 import edu.wpi.rail.jinteractiveworld.ros.srvs.interactiveworldmsgs.LearnModels;
00007 import edu.wpi.rail.jrosbridge.Ros;
00008 import edu.wpi.rail.jrosbridge.Service;
00009 import edu.wpi.rail.jrosbridge.callback.CallServiceCallback;
00010 import edu.wpi.rail.jrosbridge.messages.geometry.Point;
00011 import edu.wpi.rail.jrosbridge.services.ServiceRequest;
00012 import java.sql.*;
00013 import java.util.ArrayList;
00014 
00015 public class InteractiveWorldLearner {
00016 
00017         public static final String NODE_NAME = "interactive_world_learner";
00018 
00019         public static final String LEARN_HYPOTHESES_SERVICE_NAME = "/" + InteractiveWorldLearner.NODE_NAME + "/learn_hypotheses";
00020 
00021         public static final String DB_CLASS = "com.mysql.jdbc.Driver";
00022 
00023         public static final String DB_CREATE = "CREATE TABLE IF NOT EXISTS `iwmodels` (" +
00024                         "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT," +
00025                         "  `condition_id` int(10) unsigned NOT NULL," +
00026                         "  `value` mediumtext NOT NULL," +
00027                         "  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," +
00028                         "  `modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'," +
00029                         "  PRIMARY KEY (`id`)," +
00030                         "  UNIQUE KEY `condition_id` (`condition_id`)" +
00031                         ") ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
00032 
00033         public static final String DB_FOREIGN_KEY = "ALTER TABLE `iwmodels`" +
00034                         "  ADD CONSTRAINT `iwmodels_ibfk_1` FOREIGN KEY (`condition_id`) REFERENCES `conditions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;";
00035 
00036         public static final String DB_ENTRY_CHECK = "SELECT `id` FROM `iwmodels` WHERE `condition_id`=?";
00037 
00038         public static final String DB_ENTRY_DELETE = "DELETE FROM `iwmodels` WHERE `condition_id`=?";
00039 
00040         public static final String DB_INSERT_MODEL = "INSERT INTO `iwmodels` (`condition_id`, `value`) VALUES (?,?)";
00041 
00042         private Ros ros;
00043         private Service learnHypotheses;
00044 
00045         public InteractiveWorldLearner() {
00046                 ros = new Ros();
00047                 System.out.print("Attempting connection to rosbridge...");
00048                 if (!ros.connect()) {
00049                         System.out.println("No connection made. Verify rosbridge_websocket is running.");
00050                         System.exit(-1);
00051                 }
00052                 System.out.println(" success!");
00053 
00054                 // setup the services
00055                 learnHypotheses = new Service(ros, InteractiveWorldLearner.LEARN_HYPOTHESES_SERVICE_NAME, "interactive_world_msgs/LearnModels");
00056                 learnHypotheses.advertiseService(new LearnModelsCallback());
00057         }
00058 
00059         public static void spin() {
00060                 while (true) {
00061                         try {
00062                                 Thread.sleep(1000);
00063                         } catch (InterruptedException e) {
00064                                 e.printStackTrace();
00065                         }
00066                 }
00067         }
00068 
00069         private class LearnModelsCallback implements CallServiceCallback {
00070 
00071                 @Override
00072                 public void handleServiceCall(ServiceRequest request) {
00073                         System.out.println("Model learning request received...");
00074                         // parse the message
00075                         LearnModels.Request req = LearnModels.Request.fromJsonObject(request.toJsonObject());
00076 
00077                         // go trough each
00078                         PlacementSet[] placementSets = req.getData().getData();
00079                         ArrayList<Model> models = new ArrayList<Model>();
00080                         for (int i=0; i<placementSets.length; i++) {
00081                                 // create a data set
00082                                 PlacementSet placementSet = placementSets[i];
00083                                 if (placementSet.getPlacements().length > 1) {
00084                                         Item item = placementSet.getItem();
00085                                         Room room = placementSet.getRoom();
00086                                         Surface surface = placementSet.getSurface();
00087                                         String referenceFrame = placementSet.getReferenceFrameId();
00088                                         DataSet ds = new DataSet(item, room, surface, referenceFrame);
00089                                         Placement[] placements = placementSet.getPlacements();
00090                                         for (int j = 0; j < placements.length; j++) {
00091                                                 Placement p = placements[j];
00092                                                 Point position = p.getPosition();
00093                                                 ds.add(position.getX(), position.getY(), position.getY(), p.getRotation());
00094                                         }
00095 
00096                                         // create the clustering model
00097                                         EMModel em = new EMModel(ds);
00098                                         models.add(new Model(em));
00099                                 }
00100                         }
00101 
00102                         Model[] modelsArray = new Model[models.size()];
00103                         models.toArray(modelsArray);
00104                         TaskModels taskModels = new TaskModels(modelsArray);
00105                         System.out.println("Model learning done.");
00106 
00107                         // store the models in the RMS
00108                         System.out.println("Attempting connection to the RMS...");
00109                         try {
00110                                 Class.forName(InteractiveWorldLearner.DB_CLASS);
00111                                 String url = "jdbc:mysql://" + req.getHost() + ":" + req.getPort() + "/" + req.getDatabase();
00112                                 Connection connection = DriverManager.getConnection(url, req.getUser(), req.getPassword());
00113 
00114                                 // check for the table
00115                                 DatabaseMetaData dbm = connection.getMetaData();
00116                                 ResultSet tables = dbm.getTables(null, null, "iwmodels", null);
00117                                 if (!tables.next()) {
00118                                         // create the table
00119                                         Statement create = connection.createStatement();
00120                                         create.executeUpdate(InteractiveWorldLearner.DB_CREATE);
00121                                         Statement foreignKey = connection.createStatement();
00122                                         foreignKey.executeUpdate(InteractiveWorldLearner.DB_FOREIGN_KEY);
00123                                 }
00124 
00125                                 // check if we should clear out an old entry
00126                                 final PreparedStatement check = connection.prepareStatement(InteractiveWorldLearner.DB_ENTRY_CHECK);
00127                                 final int i1 = 1;
00128                                 final int i2 = 2;
00129                                 check.setInt(i1, req.getConditionId());
00130                                 final ResultSet existing = check.executeQuery();
00131                                 if(existing.next()) {
00132                                         // delete the old one
00133                                         final PreparedStatement delete = connection.prepareStatement(InteractiveWorldLearner.DB_ENTRY_DELETE);
00134                                         delete.setInt(i1, req.getConditionId());
00135                                         delete.executeUpdate();
00136                                 }
00137 
00138                                 // create our entry
00139                                 final PreparedStatement insert = connection.prepareStatement(InteractiveWorldLearner.DB_INSERT_MODEL);
00140                                 insert.setInt(i1, req.getConditionId());
00141                                 insert.setString(i2, taskModels.toString());
00142                                 insert.executeUpdate();
00143 
00144                                 // close the connection
00145                                 connection.close();
00146                                 System.out.println("Models stored to the RMS.");
00147                         } catch (ClassNotFoundException|SQLException e) {
00148                                 System.err.println("Could not connect the the RMS: " + e.getMessage());
00149                         }
00150 
00151                         // send back the response
00152                         LearnModels.Response resp = new LearnModels.Response(true);
00153                         learnHypotheses.sendResponse(resp, request.getId());
00154                 }
00155         }
00156 
00157         public static void main(String[] args) {
00158                 // create the learner object
00159                 InteractiveWorldLearner l = new InteractiveWorldLearner();
00160                 InteractiveWorldLearner.spin();
00161         }
00162 }


jinteractiveworld
Author(s): Russell Toris
autogenerated on Sun Dec 14 2014 11:27:03