Creating Requests

A Simple Example

Lets see a basic example in action (taken from the C# sample app).

Here we request a OLS calculation.

// C#
var model = new pn.Lm
{
    kind = pn.Kind.OLS,
    is_async = false,
    X = X,
    Y = Y,
    add_intercept = true,
    x_labels = new List<string>(new string[] { }),
    y_labels = new List<string>(new string[] { })
};

We’re not done yet. We need to create a work request. This is the helper function that is part of the PC class which helps us fill in the routing and metadata information for the request (further explained down below in this section).The Bond codegen has created a function called pn.Request() which we initially populate with Route and Profile .

// C#
public pn.Request RequestWork()
{
    var r = new pn.Request();
    r.route.addr_orgn = addrOrgn;
    r.route.addr_dest = addrDest;
    r.profile.txn_id = Guid.NewGuid().ToString("N");
    r.profile.find_id = "";
    r.profile.reason = pn.Reason.work;
    return r;
}

In this next step, we pack it into the polymorphic container.

// C#
var request = Pack<pn.Lm>.Run(ref model, pc.RequestWork);
// now push 'request' to engine

The static class Pack is implemented as follows. It is generic and will work with packing all the core calculation requests. You are free to implement your own. The best place to learn about Bond mechanics is through their documentation. We highly recommend you go through and skim their docs as that is the machinery we are using here.

// C#
public static class PackPoly<T>
{
    public static Bonded<T> Run(T child)
    {
        var ob = new OutputBuffer();
        Serialize.To(new CompactBinaryWriter<OutputBuffer>(ob), child);
        return new Bond.Bonded<T>(Deserialize<T>.From(new CompactBinaryReader<InputBuffer>(new InputBuffer(ob.Data))));
    }
}

public delegate pn.Request RequestType();

public static class Pack<T> where T : pn.ModelSpec
{
    public static pn.Request Run(ref T model, RequestType requestType)
    {
        var request = requestType();
        var pm = new pn.PolyModel
        {
            child = PackPoly<T>.Run(model)
        };
        request.polyModel = PackPoly<pn.PolyModel>.Run(pm);
        return request;
    }
}

Another Example

Lets take another example.

// C#
var model = new pn.Stub
{
    is_async = false,
    kind = pn.Kind.DFLT
};

var request = Pack<pn.Stub>.Run(ref model, pc.RequestWork);
// now push 'request' to engine

This slice is taken from how to check if your connection to the engine is up. Here, you’re creating a Stub model which is async. You will receive a ping back from the engine if all is good.

Last Example

Lets take a look at how to request a GLM calculation:

// C#
var model = new pn.Glm
{
    kind = pn.Kind.GLM,
    is_async = false,
    X = X,
    Y = Y,
    family = pn.Family.gaussian,
    link = pn.Link.canonical,
    add_intercept = true,
    x_labels = new List<string>(new string[] { }),
    y_labels = new List<string>(new string[] { })
};

var request = Pack<pn.Glm>.Run(ref model, pc.RequestWork);
// now push 'request' to engine

Note, in all these examples you created a model object and packed it using the serialization mechanism Pack.

A Note on Bond types

type here refers to bond types. How they are translated to your languages depends on Bond. For example a Bond vector<double> translates to a native C++ std::vector<double> and in C# it translates to List<double>.

We highly recommend you skim through Bond docs.

type fields can be composite fields.
type fields can be Bonded fields. See Reference

Components of a Request

There are 3 fundamental structures to creating a request for calculation.

1/3: Route

The important field here is where the information is going to be sent.

| var             | type    | comment                           |
| --------------- | ------- | --------------------------------- |
| addr_orgn       | string  | where the request originates from |
| addr_dest       | string  | where the response should be sent |

2/3: Profile

Basic metadata information of the payload.

| var             | type    | comment                                          |
| --------------- | ------- | ------------------------------------------------ |
| txn_id          | string  | guid to keep track of request                    |
| find_id         | string  | relevant only when requesting cancel of fetching |
|                 |         | asynchronous job results                         |
| status          | Status  | engine-side indication of job status             |
| reason          | Reason  | purpose of the request                           |
| time_of_request | string  | relevant only when requesting async jobs         |

3/3: PolyModel

This is the struct that ultimately needs to be serialized and sent to the engine. We provide helper functions in the sample apps on how to do that. To unserstand what a Bonded field is, see Bonded<T>.

| var             | type              |                                        |
| --------------- | ----------------- |--------------------------------------- |
| route           | Route             | routing information shown above        |
| profile         | Profile           | profile information shown above        |
| polyModel       | Bonded(PolyModel) | polymorphic container                  |