Example usage for com.mongodb Cursor close

List of usage examples for com.mongodb Cursor close

Introduction

In this page you can find the example usage for com.mongodb Cursor close.

Prototype

void close();

Source Link

Document

Terminates this cursor on the server.

Usage

From source file:datapreparation.MongoStatistics.java

public void usersOfUrl(String URL) {
    // TODO code application logic here

    // TODO code application logic here
    int limit = 0;
    String filename = "Users_Of_Url_" + URL + ".txt";

    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    MongoClient mongoClient;//from   ww  w .  ja  va 2 s . com

    try {
        mongoClient = new MongoClient("localhost");

        //use database
        DB db = mongoClient.getDB("users");

        //get collection
        DBCollection coll = db.getCollection("urls");

        // build the $projection operation
        //            DBObject fields = new BasicDBObject("user", 1);
        //            fields.put("_id", 0);
        //            BasicDBObject project = new BasicDBObject("$project", fields);

        //build the match operation
        DBObject matchFields = new BasicDBObject("url", URL);
        DBObject match = new BasicDBObject("$match", matchFields);

        // Now the $group operation
        DBObject groupFields = new BasicDBObject("_id", "$user");
        groupFields.put("count", new BasicDBObject("$sum", 1));
        DBObject group = new BasicDBObject("$group", groupFields);

        // Finally the $sort operation
        BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));

        // run aggregation
        List<DBObject> pipeline;
        if (limit == 0) {// without limits!
            pipeline = Arrays.asList(match, group, sort);
        } else {
            // create new BasicDBObject that limit query result in only 100 rows
            DBObject limitRes = new BasicDBObject("$limit", limit);
            pipeline = Arrays.asList(match, group, sort, limitRes);
        }
        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);

        writeToFile(cursor, filename, "User\t Count");

        cursor.close();
        mongoClient.close();

    } catch (IOException ex) {
        System.out.println("Something's Wrong! " + ex);
    }
}

From source file:datapreparation.MongoStatistics.java

public void topUrls() {
    // TODO code application logic here
    int limit = 0;
    String filename = "Top_Urls_More.txt";
    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    MongoClient mongoClient;//from   ww  w  .  j a  v a2  s  . co  m

    try {
        mongoClient = new MongoClient("localhost");

        //use database
        DB db = mongoClient.getDB("users");

        //get collection
        DBCollection coll = db.getCollection("urls");

        // build the $projection operation
        DBObject fields = new BasicDBObject("url", 1);
        fields.put("_id", 0);
        BasicDBObject project = new BasicDBObject("$project", fields);

        // Now the $group operation
        DBObject groupFields = new BasicDBObject("_id", "$url");
        groupFields.put("count", new BasicDBObject("$sum", 1));
        DBObject group = new BasicDBObject("$group", groupFields);

        // Finally the $sort operation
        BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));

        // run aggregation
        List<DBObject> pipeline;
        if (limit == 0) {// without limits!
            pipeline = Arrays.asList(project, group, sort);
        } else {
            // create new BasicDBObject that limit query result in only 100 rows
            DBObject limitRes = new BasicDBObject("$limit", limit);
            pipeline = Arrays.asList(project, group, sort, limitRes);
        }
        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);

        writeToFile2(cursor, filename, "URL\t Count");

        cursor.close();
        mongoClient.close();

    } catch (IOException ex) {
        System.out.println("Something's Wrong! " + ex);
    }
}

From source file:datapreparation.MongoStatistics.java

public void timeIntervals() {
    // TODO code application logic here
    int limit = 0;
    String filename = "Times.txt";
    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    MongoClient mongoClient;/* w  w  w  .  j av  a 2s  .  co  m*/

    try {
        mongoClient = new MongoClient("localhost");

        //use database
        DB db = mongoClient.getDB("users");

        //get collection
        DBCollection coll = db.getCollection("urls");

        // build the $projection operation
        DBObject fields = new BasicDBObject("time", 1);
        fields.put("_id", 0);
        BasicDBObject project = new BasicDBObject("$project", fields);

        // Now the $group operation
        DBObject groupFields = new BasicDBObject("_id", "$time");
        //groupFields.put("count", new BasicDBObject("$sum", 1));
        DBObject group = new BasicDBObject("$group", groupFields);

        // Finally the $sort operation
        //BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));

        // run aggregation
        List<DBObject> pipeline;
        if (limit == 0) {// without limits!
            pipeline = Arrays.asList(project, group);
        } else {
            // create new BasicDBObject that limit query result in only 100 rows
            DBObject limitRes = new BasicDBObject("$limit", limit);
            pipeline = Arrays.asList(project, group, limitRes);
        }
        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);

        writeToFile3(cursor, filename, "Times");

        cursor.close();
        mongoClient.close();

    } catch (IOException ex) {
        System.out.println("Something's Wrong! " + ex);
    }
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

public double[] GetActorPoseAt(String actorName, double timestamp) {
    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the actor name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("actors.name", actorName));

    // create the pipeline operations, first the $match
    DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name));

    // sort the results in descending order on the timestamp (keep most recent result first)
    DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));

    // $limit the result to 1, we only need one pose
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind actors in order to output only the queried actor
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$actors.pos");
    proj_fields.put("rot", "$actors.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors,
            match_actor, project);//from  w  w  w.  j a v  a  2s  . c o  m

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // if query has a response, return the pose
    if (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // close cursor
        cursor.close();
        // get the pose
        return new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("w"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("z") };
    } else {
        System.out.println("Java - GetActorPose - No results found, returning empty list..");
        return new double[0];
    }
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Traj of the actor between the given timepoints
 *///from w  w w .ja v a 2  s.  com
public double[][] GetActorTraj(String actorName, String start, String end, double deltaT) {
    // transform the knowrob time to double with 3 decimal precision
    final double start_ts = (double) Math.round(parseTime_d(start) * 1000) / 1000;
    final double end_ts = (double) Math.round(parseTime_d(end) * 1000) / 1000;

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the actors
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$actors.pos");
    proj_fields.put("rot", "$actors.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_actors, match_actor, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<double[]> traj_list = new ArrayList<double[]>();

    // if the query returned nothing, get the most recent pose
    if (!cursor.hasNext()) {
        System.out.println("Java - GetActorTraj - No results found, returning most recent pose..");
        // get the most recent pose
        traj_list.add(this.GetActorPoseAt(actorName, start));

        // cast from dynamic array to standard array
        return traj_list.toArray(new double[traj_list.size()][7]);
    }

    // timestamp used for deltaT
    double prev_ts = 0;

    // while query has a response, return the pose
    while (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        // get the curr timestamp
        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            // get the current pose
            traj_list.add(new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("w"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("z") });
            prev_ts = curr_ts;
            //System.out.println(curr_doc.toString());
        }
    }
    // close cursor
    cursor.close();

    // cast from dynamic array to standard array
    return traj_list.toArray(new double[traj_list.size()][7]);
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Pose of the actors bone at the given timepoint (or the most recent one)
 */// w ww .j a v  a 2s.  com
public double[] GetBonePoseAt(String actorName, String boneName, String timestampStr) {
    final double timestamp = (double) Math.round(parseTime_d(timestampStr) * 1000) / 1000;

    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the actor name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("actors.name", actorName));

    // create the pipeline operations, first the $match
    DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name));

    // sort the results in descending order on the timestamp (keep most recent result first)
    DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));

    // $limit the result to 1, we only need one pose
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind actors in order to output only the queried actor
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_bone_fields = new BasicDBObject("_id", 0);
    proj_bone_fields.put("timestamp", 1);
    proj_bone_fields.put("actors.bones", 1);
    DBObject project_bones = new BasicDBObject("$project", proj_bone_fields);

    // $unwind the bones
    DBObject unwind_bones = new BasicDBObject("$unwind", "$actors.bones");

    // $match for the given bone name from the unwinded bones
    DBObject match_bone = new BasicDBObject("$match", new BasicDBObject("actors.bones.name", boneName));

    // build the final $projection operation
    DBObject proj_fields = new BasicDBObject("timestamp", 1);
    proj_fields.put("pos", "$actors.bones.pos");
    proj_fields.put("rot", "$actors.bones.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors,
            match_actor, project_bones, unwind_bones, match_bone, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // if query has a response, return the pose
    if (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // close cursor since we only care about one value
        cursor.close();
        // get the pose
        return new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("w"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                ((BasicDBObject) first_doc.get("rot")).getDouble("z") };
    } else {
        System.out.println("Java - GetBonePose - No results found, returning empty list..");
        return new double[0];
    }
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Traj of the actors bone between the given timepoints
 *///from   w  w w  .  j a v a  2s. c  o  m
public double[][] GetBoneTraj(String actorName, String boneName, String start, String end, double deltaT) {
    // transform the knowrob time to double with 3 decimal precision
    final double start_ts = (double) Math.round(parseTime_d(start) * 1000) / 1000;
    final double end_ts = (double) Math.round(parseTime_d(end) * 1000) / 1000;

    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind the actors
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("pos", "$actors.pos");
    proj_fields.put("rot", "$actors.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_actors, match_actor, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // Traj as dynamic array
    ArrayList<double[]> traj_list = new ArrayList<double[]>();

    // if the query returned nothing, get the most recent pose
    if (!cursor.hasNext()) {
        System.out.println("Java - GetBoneTraj - No results found, returning most recent pose..");
        // get the most recent pose
        traj_list.add(this.GetBonePoseAt(actorName, boneName, start));

        // cast from dynamic array to standard array
        return traj_list.toArray(new double[traj_list.size()][7]);
    }

    // timestamp used for deltaT
    double prev_ts = 0;

    // if query has a response, return the pose
    while (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        // get the curr timestamp
        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            // get the current pose
            traj_list.add(new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("w"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("rot")).getDouble("z") });
            prev_ts = curr_ts;
            //System.out.println(curr_doc.toString());
        }
    }
    // close cursor
    cursor.close();

    // cast from dynamic array to standard array
    return traj_list.toArray(new double[traj_list.size()][7]);
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Names of the actor bones/*from  w  w  w.  j  ava  2  s.  c  om*/
 */
public String[] GetBonesNames(String actorName) {
    // create the pipeline operations, first the $match
    DBObject match_name = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // $limit the result to 1
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind actors in order to output only the queried actor
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("bones_names", "$actors.bones.name");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_name, limit_result, unwind_actors, match_actor, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // if query has a response, return the names
    if (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // close cursor since we only care about one value
        cursor.close();
        // get the bone names as list         
        BasicDBList names = (BasicDBList) first_doc.get("bones_names");
        // return as array of string
        return names.toArray(new String[names.size()]);
    } else // else return empty list
    {
        System.out.println("Java - GetBonesNames - No results found, returning empty list..");
        return new String[0];
    }
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Poses of the actor bones at the given timepoint (or the most recent one)
 *//*from   w ww .jav  a 2 s . c om*/
public double[][] GetBonesPosesAt(String actorName, String timestampStr) {
    // transform the knowrob time to double with 3 decimal precision
    final double timestamp = (double) Math.round(parseTime_d(timestampStr) * 1000) / 1000;

    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the actor name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("actors.name", actorName));

    // create the pipeline operations, first the $match
    DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name));

    // sort the results in descending order on the timestamp (keep most recent result first)
    DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));

    // $limit the result to 1, we only need one pose
    DBObject limit_result = new BasicDBObject("$limit", 1);

    // $unwind actors in order to output only the queried actor
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("bones_pos", "$actors.bones.pos");
    proj_fields.put("bones_rot", "$actors.bones.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors,
            match_actor, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // Poses as dynamic array
    ArrayList<double[]> pose_list = new ArrayList<double[]>();

    // if query has a response, return the pose
    if (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // close cursor since we only care about one value
        cursor.close();
        // get the pose

        BasicDBList pos_list = (BasicDBList) first_doc.get("bones_pos");
        BasicDBList rot_list = (BasicDBList) first_doc.get("bones_rot");

        // pos_list and rot_list length should be always the same
        for (int i = 0; i < pos_list.size(); ++i) {
            pose_list.add(new double[] { ((BasicDBObject) pos_list.get(i)).getDouble("x"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("z"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("w"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("x"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("y"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("z") });
        }
        // cast from dynamic array to standard array
        return pose_list.toArray(new double[pose_list.size()][7]);
    } else {
        System.out.println("Java - GetBonesPoses - No results found, returning empty list..");
        return new double[0][0];
    }
}

From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java

License:Open Source License

/**
 * Query the Trajectories of the actor bones at the given timepoint (or the most recent one)
 *//*w  ww .j a v a  2  s.co m*/
public double[][][] GetBonesTrajs(String actorName, String start, String end, double deltaT) {
    // transform the knowrob time to double with 3 decimal precision
    final double start_ts = (double) Math.round(parseTime_d(start) * 1000) / 1000;
    final double end_ts = (double) Math.round(parseTime_d(end) * 1000) / 1000;
    // create the pipeline operations, first with the $match check the times
    DBObject match_time = new BasicDBObject("$match",
            new BasicDBObject("timestamp", new BasicDBObject("$gte", start_ts).append("$lte", end_ts)));

    // $unwind actors in order to output only the queried actor
    DBObject unwind_actors = new BasicDBObject("$unwind", "$actors");

    // $match for the given actor name from the unwinded actors
    DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("bones_pos", "$actors.bones.pos");
    proj_fields.put("bones_rot", "$actors.bones.rot");
    DBObject project = new BasicDBObject("$project", proj_fields);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time, unwind_actors, match_actor, project);

    AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
            .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

    // get results
    Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions);

    // Trajectories as dynamic array (dynamic on the time part)
    ArrayList<double[][]> bone_trajs = new ArrayList<double[][]>();

    // the number of bones
    int nr_bones = 0;

    // if the query returned nothing, get the most recent pose
    if (!cursor.hasNext()) {
        System.out.println("Java - GetBonesTrajs - No results found, returning most recent poses..");
        // get the most recent pose
        bone_trajs.add(this.GetBonesPosesAt(actorName, start));

        // set the nr of bones
        nr_bones = bone_trajs.get(0).length;

        // cast from dynamic array to standard array
        return bone_trajs.toArray(new double[bone_trajs.size()][nr_bones][7]);
    }

    // timestamp used for deltaT
    double prev_ts = 0;

    // if query has a response, return the pose
    while (cursor.hasNext()) {
        // get the first document as the next cursor and append the metadata to it
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        // get the curr timestamp
        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            // get the list of bones pos and rot
            BasicDBList pos_list = (BasicDBList) curr_doc.get("bones_pos");
            BasicDBList rot_list = (BasicDBList) curr_doc.get("bones_rot");

            // set the nr of bones
            nr_bones = pos_list.size();

            // Poses as dynamic array (dynamic on the nr of bones part)
            ArrayList<double[]> pose_list = new ArrayList<double[]>();

            // pos_list and rot_list length should be always the same
            for (int i = 0; i < nr_bones; ++i) {
                pose_list.add(new double[] { ((BasicDBObject) pos_list.get(i)).getDouble("x"),
                        ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                        ((BasicDBObject) pos_list.get(i)).getDouble("z"),
                        ((BasicDBObject) rot_list.get(i)).getDouble("w"),
                        ((BasicDBObject) rot_list.get(i)).getDouble("x"),
                        ((BasicDBObject) rot_list.get(i)).getDouble("y"),
                        ((BasicDBObject) rot_list.get(i)).getDouble("z") });
            }
            // cast from dynamic array to standard array
            bone_trajs.add(pose_list.toArray(new double[nr_bones][7]));
            prev_ts = curr_ts;
        }
    }
    // close cursor
    cursor.close();

    // return the actor bones trajectories as float[][][] 
    return bone_trajs.toArray(new double[bone_trajs.size()][nr_bones][7]);
}