| Did you know ... | Search Documentation: |
| Example: Vectors of Numbers |
This is an example of using the low-level interface.
In the Prolog client:
vector_type(double(_List), 2).
vector_type(float(_List), 3).
vector_type(integer(_List), 4).
vector_type(integer64(_List), 5).
vector_type(integer32(_List), 6).
vector_type(unsigned(_List), 7).
vector_type(codes(_List), 8).
vector_type(atom(_List), 9).
vector_type(string(_List), 10).
vector(Type, B):-
vector_type(Type, Tag),
Proto = protobuf([ repeated(Tag, Type) ]),
protobuf_message(Proto, B).
A protobuf description that is compatible with the above wire stream follows:
message Vector {
repeated double double_values = 2;
repeated float float_values = 3;
repeated sint32 integer_values = 4;
repeated fixed64 integer64_values = 5;
repeated fixed32 integer32_values = 6;
repeated uint32 unsigned_values = 7;
repeated bytes bytes_values = 8;
repeated string atom_values = 9;
repeated string string_values = 10;
}
A typical application might consist of an abstract adapter class along with a collection of concrete subclasses that refine an abstract behavior in order to hide the interaction with the underlying protobuf interpreter. An example of such a class written in C++ may be found in the demos.
On the Prolog side:
:- meta_predicate ~>(0,0).
:- op(950, xfy, ~>).
~>(P, Q) :-
setup_call_cleanup(P, (true; fail), assertion(Q)).
write_as_proto(Vector) :-
vector(Vector, WireStream),
open('tmp99.tmp', write, S, [encoding(octet),type(binary)])
~> close(S),
format(S, '~s', [WireStream]), !.
testv1(V) :-
read_file_to_codes('tmp99.tmp', Codes, [encoding(octet),type(binary)]),
vector(V, Codes).
Run the Prolog side:
?- X is pi, write_as_proto(double([-2.2212, -7.6675, X, 0, 1.77e-9, 2.54e222])). X = 3.14159. ?- testv1(Vector). Vector = double([-2.2212, -7.6675, 3.14159, 0.0, 1.77e-09, 2.54e+222]) ?-
Verify the wire stream using the protobuf compiler's decoder:
$ protoc --decode=Vector pb_vector.proto <tmp99.tmp double_values: -2.2212 double_values: -7.6675 double_values: 3.1415926535897931 double_values: 0 double_values: 1.77e-09 double_values: 2.5400000000000002e+222