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_sim_games.MongoSimGames.java

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * save result in MongoDB/*from  w w w  .j  a  v a  2  s. c o  m*/
 */
public void WriteModelTrajectory(double start_ts, double end_ts, String model_name, String traj_db_name,
        String traj_coll_name) {

    // 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 models
    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, 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 traj_db = mongoClient.getDB(traj_db_name);

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

            System.out.println("Java - Writing to \'" + traj_db_name + "." + traj_coll_name + "\'");

            // if cursor not empty, append matadata to the first doc
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", traj_coll_name).append("type", "trajectory")
                        .append("start", start_ts).append("end", end_ts)
                        .append("description", "Model trajectory..");

                // 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
                traj_coll.insert(first_doc);
            }
            // if query returned no values for these timestamps, get the pose at the nearest timestamp
            else {
                // write the pose to the given db and coll
                this.WriteModelPoseAt(start_ts, model_name, traj_db_name, traj_coll_name);
            }

            // insert rest of trajectory
            while (cursor.hasNext()) {
                traj_coll.insert(cursor.next());
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers//from  ww  w  . j a v a 2  s  .c om
 */
public ArrayList<Vector3d> ViewModelTrajectory(double start_ts, double end_ts, String model_name,
        String markerID) {
    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

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

        if (markerID != null) {
            traj.add(new Vector3d(((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("z")));
        }

    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelPoseAt(start_ts, model_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));

        if (markerID != null) {
            traj.add(new Vector3d(((BasicDBObject) first_doc.get("rot")).getDouble("x"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("y"),
                    ((BasicDBObject) first_doc.get("rot")).getDouble("z")));
        }
    }

    // create the markers
    if (markerID != null)
        this.CreateMarkers(traj, markerID);
    return traj;
}

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

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers/*ww w  .ja  va  2s  .  c o m*/
 */
public void ViewModelTrajectory(double start_ts, double end_ts, String model_name, String markerID,
        String markerType, String color, float scale, double deltaT) {

    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelPoseAt(start_ts, model_name, markerID, markerType, color, scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}

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

License:Open Source License

/**
 * Query the trajectory of the given model with double timestamps
 * view as rviz markers//from   www . j  a v a 2  s . c  o  m
 */
public void ViewModelMeshTrajectory(double start_ts, double end_ts, String model_name, String meshPath,
        String markerID, double deltaT) {

    // 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 models
    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, 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);

    // timestamp used for deltaT
    double prev_ts = 0;

    // marker ID suffix
    int marker_suff = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

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

        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewModelMeshAt(start_ts, model_name, markerID, meshPath);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            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"));
            // create the markers
            this.CreateMeshMarker(translation, orientation, meshPath, markerID + marker_suff);

            // update current ts
            prev_ts = curr_ts;

            // append to marker suffix
            marker_suff++;
        }
    }
}

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

License:Open Source License

/**
 * Query the trajectory of the given link of the given model with double timestamps
 * save result in MongoDB/*from  www  .  ja  va 2s.com*/
 */
public void WriteLinkTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String traj_db_name, String traj_coll_name) {

    // 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 models
    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, 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);

    try {

        MongoClient mongoClient = new MongoClient(this.dbHost, 27017);

        DB traj_db = mongoClient.getDB(traj_db_name);

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

            System.out.println("Java - Writing to \'" + traj_db_name + "." + traj_coll_name + "\'");

            // if cursor not empty, append matadata to the first doc
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", traj_coll_name).append("type", "trajectory")
                        .append("start", start_ts).append("end", end_ts)
                        .append("description", "Link trajectory..");

                // 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
                traj_coll.insert(first_doc);
            }
            // if query returned no values for these timestamps, get the pose at the nearest timestamp
            else {
                // write the pose to the given db and coll
                this.WriteLinkPoseAt(start_ts, model_name, link_name, traj_db_name, traj_coll_name);
            }

            // insert rest of trajectory
            while (cursor.hasNext()) {
                traj_coll.insert(cursor.next());
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

/**
 * Query the trajectory of the given link of the given model with double timestamps
 * save result in MongoDB/* w ww .  j  a  va  2s.  c o  m*/
 */
public void ViewLinkTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String markerID) {

    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewLinkPoseAt(start_ts, model_name, link_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }

    // create the markers
    this.CreateMarkers(traj, markerID);
}

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

License:Open Source License

/**
 * Query the trajectory of the given link of the given model with double timestamps
 * save result in MongoDB//from www .j av  a 2  s  .co  m
 */
public void ViewLinkTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String markerID, String markerType, String color, float scale, double deltaT) {

    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewLinkPoseAt(start_ts, model_name, link_name, markerID, markerType, color, scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}

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

License:Open Source License

/**
 * Query the trajectory of the given collision of the given link of the given model from double timestamps
 * save result in MongoDB//from  w w  w . j a  va  2 s .  c om
 */
public void WriteCollisionTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String collision_name, String traj_db_name, String traj_coll_name) {

    // 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 models
    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, 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);

    try {

        MongoClient mongoClient = new MongoClient(this.dbHost, 27017);

        DB traj_db = mongoClient.getDB(traj_db_name);

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

            System.out.println("Java  - Writing to \'" + traj_db_name + "." + traj_coll_name + "\'");

            // if cursor not empty, append matadata to the first doc
            if (cursor.hasNext()) {
                // create metadata doc
                BasicDBObject meta_data = new BasicDBObject("name", traj_coll_name).append("type", "trajectory")
                        .append("start", start_ts).append("end", end_ts)
                        .append("description", "Collision trajectory..");

                // 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
                traj_coll.insert(first_doc);
            }
            // if query returned no values for these timestamps, get the pose at the nearest timestamp
            else {
                // write the pose to the given db and coll
                this.WriteCollisionPoseAt(start_ts, model_name, link_name, collision_name, traj_db_name,
                        traj_coll_name);
            }

            // insert rest of trajectory
            while (cursor.hasNext()) {
                traj_coll.insert(cursor.next());
            }
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

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

License:Open Source License

/**
 * Query the trajectory of the given collision of the given link of the given model from double timestamps
 * view results as rviz markers/*from w ww.  j av  a  2s  . com*/
 */
public void ViewCollisionTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String collision_name, String markerID) {

    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewCollisionPoseAt(start_ts, model_name, link_name, collision_name, markerID);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject first_doc = (BasicDBObject) cursor.next();
        // add position to trajectory
        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
    }

    // create the markers
    this.CreateMarkers(traj, markerID);
}

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

License:Open Source License

/**
 * Query the trajectory of the given collision of the given link of the given model from double timestamps
 * view results as rviz markers//from   www.j ava2  s. co  m
 */
public void ViewCollisionTrajectory(double start_ts, double end_ts, String model_name, String link_name,
        String collision_name, String markerID, String markerType, String color, float scale, double deltaT) {

    // 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 models
    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, 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);

    // Traj as dynamic array
    ArrayList<Vector3d> traj = new ArrayList<Vector3d>();

    // timestamp used for deltaT
    double prev_ts = 0;

    // if cursor not empty, append matadata to the first doc
    if (cursor.hasNext()) {
        // get the first document as the next cursor
        BasicDBObject first_doc = (BasicDBObject) cursor.next();

        traj.add(new Vector3d(((BasicDBObject) first_doc.get("pos")).getDouble("x"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("y"),
                ((BasicDBObject) first_doc.get("pos")).getDouble("z")));
        // set the timestamp
        prev_ts = first_doc.getDouble("timestamp");
    }
    // if query returned no values for these timestamps, get the pose at the nearest timestamp
    else {
        // write the pose to the given db and coll
        this.ViewCollisionPoseAt(start_ts, model_name, link_name, collision_name, markerID, markerType, color,
                scale);
    }
    // insert rest of trajectory
    while (cursor.hasNext()) {
        // get the current document
        BasicDBObject curr_doc = (BasicDBObject) cursor.next();

        double curr_ts = curr_doc.getDouble("timestamp");

        // if time diff > then deltaT add position to trajectory
        if (curr_ts - prev_ts > deltaT) {
            traj.add(new Vector3d(((BasicDBObject) curr_doc.get("pos")).getDouble("x"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("y"),
                    ((BasicDBObject) curr_doc.get("pos")).getDouble("z")));
            prev_ts = curr_ts;
        }
    }

    // create the markers
    this.CreateMarkers(traj, markerID, markerType, color, scale);
}