Project

General

Profile

Statistics
| Branch: | Revision:

cool / src / lib / gmlmip_stub.c @ 7c4d2eb4

History | View | Annotate | Download (2.52 KB)

1 93d587d0 Thorsten Wißmann
#define __STDC_LIMIT_MACROS
2
#define __STDC_FORMAT_MACROS
3
4
#include <utility> // for pair
5
#include <vector>
6 79c5fa44 Thorsten Wißmann
#include <set>
7 93d587d0 Thorsten Wißmann
8
extern "C" {
9
  #include <caml/mlvalues.h>
10
  #include <caml/alloc.h>
11
  #include <caml/memory.h>
12
  #include <caml/custom.h>
13
}
14
15
#include "GMLMIP-0.1/onestep.h"
16
17
//
18
using namespace std;
19
20
extern "C" {
21
  CAMLprim value gmlRules_stub(value modalities);
22
}
23
24
template<class T>
25 79c5fa44 Thorsten Wißmann
value set2CamlList(const set<T>& vec, value (*f)(const T&)) {
26 93d587d0 Thorsten Wißmann
    CAMLlocal2( cli, cons );
27
    cli = Val_emptylist;
28
29 79c5fa44 Thorsten Wißmann
    for (typename set<T>::const_reverse_iterator it = vec.rbegin();
30
         it != vec.rend();
31
         ++it)
32
    {
33 93d587d0 Thorsten Wißmann
        cons = caml_alloc(2, 0);
34
35 79c5fa44 Thorsten Wißmann
        Store_field( cons, 0, f(*it) );  // head
36
        Store_field( cons, 1, cli );     // tail
37 93d587d0 Thorsten Wißmann
38
        cli = cons;
39
    }
40
41
    return cli ;
42
}
43
44
45
46 79c5fa44 Thorsten Wißmann
static CAMLprim value pair2caml(const int& t) {
47 93d587d0 Thorsten Wißmann
    CAMLlocal1( abc );
48
    abc = caml_alloc(2, 0);
49 79c5fa44 Thorsten Wißmann
    bool neg = t < 0;
50 9a1f1a68 Thorsten Wißmann
    Store_field( abc, 0, Val_int((neg ?(-t) : t) - 1));
51 79c5fa44 Thorsten Wißmann
    Store_field( abc, 1, Val_bool(!neg));
52 93d587d0 Thorsten Wißmann
    return abc;
53
}
54
55 79c5fa44 Thorsten Wißmann
static CAMLprim value set2caml(const set<int>& vt) {
56
    return set2CamlList(vt, pair2caml);
57 93d587d0 Thorsten Wißmann
}
58
59 79c5fa44 Thorsten Wißmann
static CAMLprim value setset2caml(const set<set<int> >& vt) {
60
    return set2CamlList(vt, set2caml);
61
}
62
63 ed7be8e6 Thorsten Wißmann
static void printModVec(FILE* f, const vector<pair<pair<bool,int>,int> >& modvec) {
64
    fprintf(f, "GML-Input: /\\ (");
65
    for (int i = 0; i < modvec.size(); i++) {
66
        bool isdia = modvec[i].first.first;
67
        int cnt = modvec[i].first.second;
68
        int underl = modvec[i].second;
69
        if (i > 0) fprintf(f, ",");
70
        if (isdia) {
71
            fprintf(f, " <%d> p%d", cnt, underl);
72
        } else {
73
            fprintf(f, " [%d] p%d", cnt, underl);
74
        }
75
    }
76
    fprintf(f, " )\n");
77
}
78 79c5fa44 Thorsten Wißmann
79 93d587d0 Thorsten Wißmann
CAMLprim value gmlRules_stub(value modalities) {
80
    // parse caml-modalities to a C++ vector
81
    CAMLparam1( modalities );
82
    value block = modalities;
83
    vector<pair<pair<bool,int>,int> > modvec;
84
    while (Is_block(block)) {
85
        CAMLlocal3( vpos, vcnt, vformula );
86
        vpos = Field(Field(block, 0), 0);
87
        vcnt = Field(Field(block, 0), 1);
88
        vformula = Field(Field(block, 0), 2);
89
        modvec.push_back(make_pair(make_pair(
90
            Bool_val(vpos),
91
            Int_val(vcnt)),
92 9a1f1a68 Thorsten Wißmann
            1+Int_val(vformula)));
93 ad3ec9e9 Thorsten Wißmann
        block = Field(block, 1);
94 93d587d0 Thorsten Wißmann
    }
95
    // Do one rule step and save result in rulevec
96
    GMLConclusion rulevec = gmlmip_onestep_gml(modvec);
97
    // convert rulevec into ocaml list of pairs
98
    CAMLlocal1( res );
99 79c5fa44 Thorsten Wißmann
    res = set2CamlList(rulevec, setset2caml);
100 93d587d0 Thorsten Wißmann
    CAMLreturn( res );
101
}