Example usage for com.mongodb AggregationOptions builder

List of usage examples for com.mongodb AggregationOptions builder

Introduction

In this page you can find the example usage for com.mongodb AggregationOptions builder.

Prototype

public static Builder builder() 

Source Link

Document

Creates a new Builder for AggregationOptions .

Usage

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   ww  w.j  a v  a  2s .  co m
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)
 *//*from ww w  . j  a v  a2 s .c om*/
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]);
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given model at the given timepoint (or the most recent one)
 * save result to MongoDB// w  ww.  ja v a  2s  . co m
 */
public void WriteModelPoseAt(double timestamp, String model_name, String pose_traj_db_name,
        String pose_traj_coll_name) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

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

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

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

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

    // writing results to a mongo collection
    try {

        // connect to client
        MongoClient mongoClient = new MongoClient(this.dbHost, 27017);

        // get the db
        DB pose_traj_db = mongoClient.getDB(pose_traj_db_name);

        // check if the collection already exists
        if (pose_traj_db.collectionExists(pose_traj_coll_name)) {
            System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!");
        }
        // create the collection
        else {
            // create collection
            DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name);

            System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'");

            // if query has a response, append metadata to it
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name)
                        .append("type", "trajectory").append("timestamp", timestamp)
                        .append("description", "Model pose query..");

                // get the first document as the next cursor and append the metadata to it
                BasicDBObject first_doc = (BasicDBObject) cursor.next();

                first_doc.append("metadata", meta_data);

                // insert document with metadata
                pose_traj_coll.insert(first_doc);
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given model at the given timepoint (or the most recent one)
 * view results as rviz markers //from w  w  w.  j ava 2s .  com
 */
public void ViewModelPoseAt(double timestamp, String model_name, String markerID, String markerType,
        String color, float scale) {
    // $and list for querying the $match in the aggregation
    BasicDBList time_and_name = new BasicDBList();

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

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

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

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

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

    System.out.println("Java - ViewModelPoseAt");
    if (cursor.hasNext()) {
        // Traj as dynamic array
        ArrayList<Vector3d> pos = new ArrayList<Vector3d>();

        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        pos.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        this.CreateMarkers(pos, markerID, markerType, color, scale);
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given model at the given timepoint (or the most recent one)
 * view results as rviz mesh marker /*from   w w w .j a v  a2 s.c  o m*/
 */
public void ViewModelMeshAt(double timestamp, String model_name, String meshPath, String markerID) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

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

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

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

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

    if (cursor.hasNext()) {
        // Traj as dynamic array
        ArrayList<Vector3d> pos = new ArrayList<Vector3d>();

        // get the first document as the next cursor
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double[] translation = new double[] { ((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) curr_doc.get("pos")).getDouble("z") };
        double[] orientation = this.quatFromEulerRad(((BasicDBObject) curr_doc.get("rot")).getDouble("x"),
                ((BasicDBObject) curr_doc.get("rot")).getDouble("y"),
                ((BasicDBObject) curr_doc.get("rot")).getDouble("z"));
        this.CreateMeshMarker(translation, orientation, meshPath, markerID);
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Get the poses of the model links meshes at the given timestamp
 * view results as rviz markers//from ww  w.j a  va 2s . c om
 */
public void ViewNestedMeshesAt(double timestamp, String model_name, String meshFolderPath, String markerID) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_fields = new BasicDBObject("_id", 0);
    proj_fields.put("timestamp", 1);
    proj_fields.put("links_pos", "$models.links.pos");
    proj_fields.put("links_rot", "$models.links.rot");
    proj_fields.put("links_name", "$models.links.name");

    DBObject project = new BasicDBObject("$project", proj_fields);

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

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

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

    // get the result of the query
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();
        System.out.println(curr_doc);

        // get the list of links pos
        BasicDBList pos_list = (BasicDBList) curr_doc.get("links_pos");
        BasicDBList rot_list = (BasicDBList) curr_doc.get("links_rot");
        BasicDBList names_list = (BasicDBList) curr_doc.get("links_name");

        // Dynamic arrays of the links positions, rotations and names
        List<double[]> translations = new ArrayList<double[]>();
        List<double[]> orientations = new ArrayList<double[]>();
        List<String> names = new ArrayList<String>();

        // pos_list and rot_list and names_list length should be always the same
        for (int i = 0; i < pos_list.size(); ++i) {
            // add the pos's
            translations.add(new double[] { ((BasicDBObject) pos_list.get(i)).getDouble("x"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("y"),
                    ((BasicDBObject) pos_list.get(i)).getDouble("z") });

            // add the rots
            orientations.add(this.quatFromEulerRad(((BasicDBObject) rot_list.get(i)).getDouble("x"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("y"),
                    ((BasicDBObject) rot_list.get(i)).getDouble("z")));

            // add the names
            names.add((String) names_list.get(i));
        }
        // create the nested mesh markers
        this.CreateNestedMeshMarkers(translations, orientations, names, meshFolderPath, markerID);
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given link at the given timepoint (or the most recent one)
 * save result in MongoDB// ww  w  .j a  v a  2  s  .c  om
 */
public void WriteLinkPoseAt(double timestamp, String model_name, String link_name, String pose_traj_db_name,
        String pose_traj_coll_name) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

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

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project_links, unwind_links, match_link, project);

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

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

    // writing results to a mongo collection
    try {

        // connect to client
        MongoClient mongoClient = new MongoClient(this.dbHost, 27017);

        // get the db
        DB pose_traj_db = mongoClient.getDB(pose_traj_db_name);

        // check if the collection already exists
        if (pose_traj_db.collectionExists(pose_traj_coll_name)) {
            System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!");
        }
        // create the collection
        else {
            // create collection
            DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name);

            System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'");

            // if query has a response, append metadata to it
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name)
                        .append("type", "trajectory").append("timestamp", timestamp)
                        .append("description", "Link pose query..");

                // get the first document as the next cursor and append the metadata to it
                BasicDBObject first_doc = (BasicDBObject) cursor.next();

                first_doc.append("metadata", meta_data);

                // insert document with metadata
                pose_traj_coll.insert(first_doc);
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given link at the given timepoint (or the most recent one)
 * save result in MongoDB/*from   w w w .ja  va 2 s. co  m*/
 */
public void ViewLinkPoseAt(double timestamp, String model_name, String link_name, String markerID,
        String markerType, String color, float scale) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

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

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project_links, unwind_links, match_link, project);

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

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

    if (cursor.hasNext()) {
        // Traj as dynamic array
        ArrayList<Vector3d> pose = new ArrayList<Vector3d>();

        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        pose.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        this.CreateMarkers(pose, markerID, markerType, color, scale);
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given collision at the given timepoint (or the most recent one)
 * save result in MongoDB/*from   www.  j av a2 s  . co m*/
 */
public void WriteCollisionPoseAt(double timestamp, String model_name, String link_name, String collision_name,
        String pose_traj_db_name, String pose_traj_coll_name) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_collision_fields = new BasicDBObject("timestamp", 1);
    proj_collision_fields.put("models.links.collisions", 1);
    DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields);

    // $unwind the collisions
    DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions");

    // $match for the given collision name from the unwinded collisions
    DBObject match_collision = new BasicDBObject("$match",
            new BasicDBObject("models.links.collisions.name", collision_name));

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

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project_links, unwind_links, match_link, project_collisions, unwind_collisions,
            match_collision, project);

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

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

    // writing results to a mongo collection
    try {

        // connect to client
        MongoClient mongoClient = new MongoClient(this.dbHost, 27017);

        // get the db
        DB pose_traj_db = mongoClient.getDB(pose_traj_db_name);

        // check if the collection already exists
        if (pose_traj_db.collectionExists(pose_traj_coll_name)) {
            System.out.println("!!! Collection: \'" + pose_traj_coll_name + "\' already exists!");
        }
        // create the collection
        else {
            // create collection
            DBCollection pose_traj_coll = pose_traj_db.getCollection(pose_traj_coll_name);

            System.out.println("Writing most recent pose to \'" + pose_traj_coll_name + "\'");

            // if query has a response, append metadata to it
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", pose_traj_coll_name)
                        .append("type", "trajectory").append("timestamp", timestamp)
                        .append("description", "Collision pose query..");

                // get the first document as the next cursor and append the metadata to it
                BasicDBObject first_doc = (BasicDBObject) cursor.next();

                first_doc.append("metadata", meta_data);

                // insert document with metadata
                pose_traj_coll.insert(first_doc);
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

From source file:org.knowrob.knowrob_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the Pose of the given collision at the given timepoint (or the most recent one)
 * view results as rviz markers//  ww w  .  j a  va  2  s .  c o m
 */
public void ViewCollisionPoseAt(double timestamp, String model_name, String link_name, String collision_name,
        String markerID, String markerType, String color, float scale) {

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

    // add the timestamp and the model name
    time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp)));
    time_and_name.add(new BasicDBObject("models.name", model_name));

    // 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 models in order to output only the queried model
    DBObject unwind_models = new BasicDBObject("$unwind", "$models");

    // $match for the given model name from the unwinded models
    DBObject match_model = new BasicDBObject("$match", new BasicDBObject("models.name", model_name));

    // build the $projection operation
    DBObject proj_links_fields = new BasicDBObject("_id", 0);
    proj_links_fields.put("timestamp", 1);
    proj_links_fields.put("models.links", 1);
    DBObject project_links = new BasicDBObject("$project", proj_links_fields);

    // $unwind the links
    DBObject unwind_links = new BasicDBObject("$unwind", "$models.links");

    // $match for the given link name from the unwinded links
    DBObject match_link = new BasicDBObject("$match", new BasicDBObject("models.links.name", link_name));

    // build the final $projection operation
    DBObject proj_collision_fields = new BasicDBObject("timestamp", 1);
    proj_collision_fields.put("models.links.collisions", 1);
    DBObject project_collisions = new BasicDBObject("$project", proj_collision_fields);

    // $unwind the collisions
    DBObject unwind_collisions = new BasicDBObject("$unwind", "$models.links.collisions");

    // $match for the given collision name from the unwinded collisions
    DBObject match_collision = new BasicDBObject("$match",
            new BasicDBObject("models.links.collisions.name", collision_name));

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

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_models,
            match_model, project_links, unwind_links, match_link, project_collisions, unwind_collisions,
            match_collision, project);

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

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

    if (cursor.hasNext()) {
        // Traj as dynamic array
        ArrayList<Vector3d> pose = new ArrayList<Vector3d>();

        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        pose.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        this.CreateMarkers(pose, markerID, markerType, color, scale);
    }
}