1 Google's Protocol Buffers
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • Google's Protocol Buffers Library
        • Google's Protocol Buffers
          • Overview
          • The SWI-Prolog Implementation
          • Wiretypes
          • Tags
          • Basic Usage
          • Alternation, Aggregation, Encapsulation, and Enumeration
          • Groups (deprecated)
          • Advanced Topics

1.5 Basic Usage

A protobuf wire-stream is a byte string that is comprised of zero or more of the above multi-byte wire-stream primitives. Templates are lists of Prolog terms. Each term corresponds to a production rule in the DCG. The purpose of the template is to provide a recipe and value set for encoding and decoding a particular message. Each term in the template has an arity of two. The term's functor is the local "host type". Argument 1 is its tag, which must always be ground, and argument 2 is its associated value, which may or may not be ground.

Note: It is an error to attempt to encode a message using a template that is not ground. Decoding a message into a template that has unbound variables has the effect of unifying the variables with their corresponding values in the wire-stream.

Map a Prolog structure to a Protocol Buffer:

command(add(X,Y), Proto) :-

   Proto = protobuf([atom(1, command),
                     atom(2, add),
                     integer(3, X),
                     integer(4, Y)
                    ]).

Later on:

   ... prepare X, Y for command ...

   command(add(X,Y), Proto),

   protobuf_message(Proto, Msg),

   ... send the message ...

Proto is the protobuf template. Each template describes exactly one message. Msg is the wire-stream. If you are interworking with other systems and languages, then the protobuf templates that you supply to protobuf_message/2 must be equivalent to those described in the .proto file that is used on the other side.