First try on a (ruby based) policy engine
[platform/upstream/libsolv.git] / src / policy-ruby.c
1 /*
2  * Ruby policy backend for SAT solver
3  * 
4  */
5 #include <stdio.h>
6 #include "policy.h"
7
8 #include "ruby.h"
9
10 static const Pool *pool;
11 static VALUE cPolicy;
12
13 int
14 policy_init( const Pool *p )
15 {
16   ruby_init();
17   ruby_init_loadpath();
18   rb_set_safe_level(0); //FIXME
19
20   /* give the ruby code a name */
21   ruby_script("satsolver_policy");
22
23   cPolicy = rb_define_class( "SatPolicy", rb_cObject );
24
25   /* load the policy implementation */
26   rb_require( "satsolver_policy" );
27   
28   pool = p;
29
30   return 0;
31 }
32
33
34 int
35 policy_exit( void )
36 {
37   pool = NULL;
38   return 0;
39 }
40
41 /*-----------------------------------------------*/
42 int
43 policy_printrules( void )
44 {
45   static VALUE id = Qnil;
46   
47   /* check if ruby implementation available */
48   if (NIL_P( id )) {
49     id = rb_intern( "printrules" );
50     if (rb_respond_to( cPolicy, id ) == Qfalse) {
51       id = Qfalse;
52     }
53   }
54   
55   /* call ruby, if available */
56   if (RTEST( id )) {
57     return RTEST( rb_funcall( cPolicy, id, 0 ) );
58   }
59
60   /* default: false */
61   return 0;
62 }