目前国内尚没有与BIP语言相关的中文资料,因此我决定将BIP官网的教程翻译成中文,供大家学习参考。我接触BIP语言的时间也不长,可能还存在一些理解偏差,如有错误请指出,谢谢!
原文链接:http://www-verimag.imag.fr/TOOLS/DCS/bip/doc/latest/html/tutorial.html
以下章节使用了一些简单的例子来指导大家如何使用BIP语言。第一部分给出了BIP语言基础用法的一些示例。第二部分用一些可以运行的示例演示了如何通过BIP引擎让BIP语言与C++进行交互。
重要:所有本章节中的例子都可以在网上下载:http://www-verimag.imag.fr/TOOLS/DCS/bip/doc/examples/。每一个例子都有一个build.sh脚本,可以用来编译对应的例子。还有个build_all.sh脚本可以用来一次性编译所有例子。
package HelloPackage port type HelloPort_t() atom type HelloAtom() port HelloPort_t p() place START,END initial to START on p from START to END end compound type HelloCompound() component HelloAtom c1() end end
$ bipc.sh -I . -p HelloPackage -d "HelloCompound()"\ --gencpp-output output $ mkdir output/build $ cd output/build $ cmake .. [...] $ make [...]
$ ./system ... [BIP ENGINE]: initialize components... [BIP ENGINE]: state #0: 1 internal port: [BIP ENGINE]: [0] ROOT.c1._iport_decl__p [BIP ENGINE]: -> choose [0] ROOT.c1._iport_decl__p [BIP ENGINE]: state #1: deadlock!
@cpp(include="stdio.h") package HelloPackage port type HelloPort_t() atom type HelloAtom(int id) export port HelloPort_t p() place START,END initial to START on p from START to END do {printf("Hello World from %d\n", id);} end connector type ThreeRendezVous(HelloPort_t p1, HelloPort_t p2, HelloPort_t p3) define p1 p2 p3 end compound type HelloCompound() component HelloAtom c1(1), c2(2), c3(3) connector ThreeRendezVous connect(c1.p, c2.p, c3.p) end end
$ bipc.sh -I . -p HelloPackage -d "HelloCompound()"\ --gencpp-output output $ mkdir output/build $ cd output/build $ cmake .. [...] $ make [...]
... [BIP ENGINE]: initialize components... [BIP ENGINE]: state #0: 1 interaction: [BIP ENGINE]: [0] ROOT.connect: ROOT.c1.p() ROOT.c2.p() ROOT.c3.p() [BIP ENGINE]: -> choose [0] ROOT.connect: ROOT.c1.p() ROOT.c2.p() ROOT.c3.p() Hello World from 1 Hello World from 2 Hello World from 3 [BIP ENGINE]: state #1: deadlock!
@cpp(include="stdio.h") package HelloPackage port type HelloPort_t(int d) atom type HelloSender(int id) data int myd export port HelloPort_t p(myd) place START, END initial to START do { myd = id; } on p from START to END do { printf("I'm %d, sending Hello World....\n", myd); } end atom type HelloReceiver(int id) data int myd export port HelloPort_t sync(myd) place START,END initial to START on sync from START to END provided (id == 1 || id == 3) do { printf("I'm %d, Hello World received from %d\n", id, myd); } end connector type OneToThree(HelloPort_t s, HelloPort_t r1, HelloPort_t r2, HelloPort_t r3) define s' r1 r2 r3 on s r1 r2 r3 down { r1.d = s.d; r2.d = s.d; r3.d = s.d; } on s r1 r2 down { r1.d = s.d; r2.d = s.d; } on s r1 r3 down { r1.d = s.d; r3.d = s.d; } on s r2 r3 down { r2.d = s.d; r3.d = s.d; } on s r1 down { r1.d = s.d; } on s r2 down { r2.d = s.d; } on s r3 down { r3.d = s.d; } end compound type HelloCompound() component HelloSender s(0) component HelloReceiver r1(1), r2(2), r3(3) connector OneToThree brd(s.p, r1.sync, r2.sync, r3.sync) end end
... [BIP ENGINE]: initialize components... [BIP ENGINE]: state #0: 1 interaction: [BIP ENGINE]: [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.r1.p({d}=0;) ROOT.r3.p({d}=0;) [BIP ENGINE]: -> choose [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.r1.p({d}=0;) ROOT.r3.p({d}=0;) I'm 0, sending Hello World.... I'm 1, Hello World received from 0 I'm 3, Hello World received from 0 [BIP ENGINE]: state #1: deadlock!
@cpp(include="stdio.h") package HelloPackage // [...] definitions of HelloPort_t, HelloSender and HelloReceiver connector type SyncRecvs(HelloPort_t r1, HelloPort_t r2, HelloPort_t r3) data int d export port HelloPort_t ep(d) define r1' r2' r3' on r1 r2 r3 down { r1.d = d; r2.d = d; r3.d = d; } on r1 r2 down { r1.d = d; r2.d = d; } on r1 r3 down { r1.d = d; r3.d = d; } on r2 r3 down { r2.d = d; r3.d = d; } on r1 down { r1.d = d; } on r2 down { r2.d = d; } on r3 down { r3.d = d; } end connector type OneToOne(HelloPort_t s, HelloPort_t c) define s' c on s c down { c.d = s.d; } end compound type HelloCompound() component HelloSender s(0) component HelloReceiver r1(1), r2(2), r3(3) connector SyncRecvs sync(r1.p, r2.p, r3.p) connector OneToOne brd(s.p, sync.ep) end end
... [BIP ENGINE]: initialize components... [BIP ENGINE]: state #0: 1 interaction: [BIP ENGINE]: [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.sync.ep({d}=135026452;) [BIP ENGINE]: -> choose [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.sync.ep({d}=135026452;) I'm 0, sending Hello World.... I'm 1, Hello World received from 0 I'm 3, Hello World received from 0 [BIP ENGINE]: state #1: deadlock!
@cpp(include="stdio.h") package HelloPackage // [...] definitions of HelloPort_t, HelloSender, HelloReceiver, // SyncReceivers and OneToOne compound type RecvsCompound() component HelloReceiver c1(1), c2(2), c3(3) connector SyncRecvs sync(c1.p, c2.p, c3.p) export port sync.ep as p end compound type HelloCompound() component HelloSender s(0) component RecvsCompound rcvrs() connector OneToOne brd(s.p, rcvrs.p) end end
... [BIP ENGINE]: initialize components... [BIP ENGINE]: state #0: 1 interaction: [BIP ENGINE]: [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.rcvrs.p({d}=135034644;) [BIP ENGINE]: -> choose [0] ROOT.brd: ROOT.s.p({d}=0;) ROOT.rcvrs.p({d}=135034644;) I'm 0, sending Hello World.... I'm 1, Hello World received from 0 I'm 3, Hello World received from 0 [BIP ENGINE]: state #1: deadlock!
ROOT.rcvrs.p({d}=135038644;)
[WARNING] In path/to/HelloPackage.bip: 'up' maybe missing: data associated with exported port won't be "fresh" : 70: 71: on r1 r2 ------------^ 72: down { 73: r1.d = cd;