clipsruby 0.0.20 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +92 -1
  3. data/ext/clipsruby/clipsruby.c +221 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed56e2c48e4ab0971ac56242518c127aa04368c3e2d6d1102f734ce577d89a54
4
- data.tar.gz: ca7df6a8e9eb6711ed51b14e3db1ee799e887fe0d5b90755705b14672c57bd57
3
+ metadata.gz: 4f7a174c8ca0889553334a3a6ad54580624d7d92695e3483aa44a130627b3fcb
4
+ data.tar.gz: a31141c4ce2490c74218316c89d08342dab315d5c35016c8dd488c2a344f8ae7
5
5
  SHA512:
6
- metadata.gz: 2017fc2246a1869a5971e3cd6c9471e8903c1435e71cb0735c729d2e3165be97a6bc6d48373a2b9e4b3d1343854a5ebd681a70934b81a78c7a8654c65db9bbfd
7
- data.tar.gz: 59734c2c151c11125d8c114f9aaae1b62f869969e272f3b97de7d37ba1c908b577c091fffe000c161484d5536795f20eab66f19cf53842c90bb969c58aa266cb
6
+ metadata.gz: 863a626437f0c4583e0f35618b4fd29c461b579833970e08999202dc35c2ac306a61b5eb3d029551187c7791d7b7fb22cb3d400d4106dea3c30eb1f1eccd7e73
7
+ data.tar.gz: aa4858ed68e383ff113accafb0f4652d4453448e93e6ecfa5c55a5903263925ae4ed114401158cb8d08b291a85eaf82891df0020a0b8f74cd9fbabdb228f4cec
data/README.md CHANGED
@@ -162,7 +162,7 @@ A light wrapper around the CLIPS find-fact function. Accepts a fact set template
162
162
  returns the first Facts in the environment that match as Ruby objects.
163
163
 
164
164
  ```ruby
165
- CLIPS::Environment.find_facts(env, "(?f my_deftemplate)")
165
+ CLIPS::Environment.find_fact(env, "(?f my_deftemplate)")
166
166
  env.find_fact("(?f my_deftemplate)")
167
167
  CLIPS::Environment.find_fact(env, "(?f my_deftemplate)", "(eq ?f:b \"asdf\")")
168
168
  env.find_fact("(?f my_deftemplate)", "(= ?f:a 1)")
@@ -302,6 +302,16 @@ CLIPS::Environment.set_current_module(env, defmodule)
302
302
  env.set_current_module(defmodule)
303
303
  ```
304
304
 
305
+ ### `CLIPS::Environment::Fact.pp_form`
306
+ ### `CLIPS::Environment::Fact#pp_form`
307
+
308
+ Returns a pretty printed string representation of the Fact
309
+
310
+ ```ruby
311
+ CLIPS::Environment::Fact.pp_form(fact)
312
+ fact.pp_form
313
+ ```
314
+
305
315
  ### `CLIPS::Environment::Fact.to_h`
306
316
  ### `CLIPS::Environment::Fact#to_h`
307
317
 
@@ -322,6 +332,16 @@ CLIPS::Environment::Fact.index(fact)
322
332
  fact.index
323
333
  ```
324
334
 
335
+ ### `CLIPS::Environment::Fact.deftemplate`
336
+ ### `CLIPS::Environment::Fact#deftemplate`
337
+
338
+ Returns the Deftemplate object for a fact
339
+
340
+ ```ruby
341
+ CLIPS::Environment::Fact.deftemplate(fact)
342
+ fact.deftemplate
343
+ ```
344
+
325
345
  ### `CLIPS::Environment::Fact.deftemplate_name`
326
346
  ### `CLIPS::Environment::Fact#deftemplate_name`
327
347
 
@@ -494,6 +514,77 @@ CLIPS::Environment::Defmodule.get_fact_list(defmodule)
494
514
  defmodule.get_fact_list
495
515
  ```
496
516
 
517
+ ### `CLIPS::Environment.find_deftemplate`
518
+ ### `CLIPS::Environment#find_deftemplate`
519
+
520
+ Finds a deftemplate by name and returns a CLIPS::Environment::Deftemplate object
521
+
522
+ ```ruby
523
+ CLIPS::Environment.find_deftemplate(:a)
524
+ env.find_deftemplate("foo")
525
+ ```
526
+
527
+ ### `CLIPS::Environment::Deftemplate.name`
528
+ ### `CLIPS::Environment::Deftemplate#name`
529
+
530
+ Returns the name of a deftemplate as a symbol
531
+
532
+ ```ruby
533
+ CLIPS::Environment::Deftemplate.name(deftemplate)
534
+ deftemplate.name
535
+ ```
536
+
537
+ ### `CLIPS::Environment::Deftemplate.pp_form`
538
+ ### `CLIPS::Environment::Deftemplate#pp_form`
539
+
540
+ Returns a pretty printed string representation of the Deftemplate
541
+
542
+ ```ruby
543
+ CLIPS::Environment::Deftemplate.pp_form(deftemplate)
544
+ deftemplate.pp_form
545
+ ```
546
+
547
+ ### `CLIPS::Environment::Deftemplate.assert_hash`
548
+ ### `CLIPS::Environment::Deftemplate#assert_hash`
549
+
550
+ Asserts a hash in the clips environment that a Deftemplate is defined in.
551
+ Returns the Fact object that was just asserted
552
+
553
+ ```ruby
554
+ CLIPS::Environment::Deftemplate.assert_hash(deftemplate, foo: :bar)
555
+ deftemplate.assert_hash(foo: :bar)
556
+ ```
557
+
558
+ ### `CLIPS::Environment.find_deffacts`
559
+ ### `CLIPS::Environment#find_deffacts`
560
+
561
+ Finds a deffacts by name and returns a CLIPS::Environment::Deffacts object
562
+
563
+ ```ruby
564
+ CLIPS::Environment.find_deffacts(:a)
565
+ env.find_deffacts("foo")
566
+ ```
567
+
568
+ ### `CLIPS::Environment::Deffacts.name`
569
+ ### `CLIPS::Environment::Deffacts#name`
570
+
571
+ Returns the name of a deffacts as a symbol
572
+
573
+ ```ruby
574
+ CLIPS::Environment::Deffacts.name(deffacts)
575
+ deffacts.name
576
+ ```
577
+
578
+ ### `CLIPS::Environment::Deffacts.pp_form`
579
+ ### `CLIPS::Environment::Deffacts#pp_form`
580
+
581
+ Returns a pretty printed string representation of the Deffacts
582
+
583
+ ```ruby
584
+ CLIPS::Environment::Deffacts.pp_form(deffacts)
585
+ deffacts.pp_form
586
+ ```
587
+
497
588
  ## Running the tests
498
589
 
499
590
  Simply do `rake compile` and then `rake test` in order to run the tests.
@@ -1,6 +1,18 @@
1
1
  #include "clips.h"
2
2
  #include "ruby.h"
3
3
 
4
+ size_t deffacts_size(const void *data)
5
+ {
6
+ return sizeof(Deffacts);
7
+ }
8
+
9
+ static const rb_data_type_t Deffacts_type = {
10
+ .function = {
11
+ .dsize = deffacts_size
12
+ },
13
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
14
+ };
15
+
4
16
  size_t deftemplate_size(const void *data)
5
17
  {
6
18
  return sizeof(Deftemplate);
@@ -50,6 +62,26 @@ static const rb_data_type_t Fact_type = {
50
62
  .flags = RUBY_TYPED_FREE_IMMEDIATELY
51
63
  };
52
64
 
65
+ static VALUE clips_environment_fact_deftemplate(VALUE self)
66
+ {
67
+ VALUE rbDeftemplate, rbEnvironment;
68
+ Fact *fact;
69
+ Deftemplate *template;
70
+
71
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
72
+
73
+ template = FactDeftemplate(fact);
74
+ rbEnvironment = rb_iv_get(self, "@environment");
75
+ rbDeftemplate = TypedData_Wrap_Struct(rb_const_get(rb_obj_class(rbEnvironment), rb_intern("Deftemplate")), &Deftemplate_type, template);
76
+ rb_iv_set(rbDeftemplate, "@environment", rbEnvironment);
77
+ return rbDeftemplate;
78
+ }
79
+
80
+ static VALUE clips_environment_fact_static_deftemplate(VALUE self, VALUE rbFact)
81
+ {
82
+ return clips_environment_fact_deftemplate(rbFact);
83
+ }
84
+
53
85
  static VALUE clips_environment_fact_deftemplate_name(VALUE self)
54
86
  {
55
87
  Fact *fact;
@@ -103,6 +135,11 @@ VALUE defmodule_alloc(VALUE self)
103
135
  return TypedData_Wrap_Struct(self, &Defmodule_type, NULL);
104
136
  }
105
137
 
138
+ VALUE deffacts_alloc(VALUE self)
139
+ {
140
+ return TypedData_Wrap_Struct(self, &Deffacts_type, NULL);
141
+ }
142
+
106
143
  VALUE deftemplate_alloc(VALUE self)
107
144
  {
108
145
  return TypedData_Wrap_Struct(self, &Deftemplate_type, NULL);
@@ -391,6 +428,64 @@ static VALUE clips_environment_static_assert_hash(VALUE self, VALUE environment,
391
428
  return clips_environment_assert_hash(environment, deftemplate_name, hash);
392
429
  }
393
430
 
431
+ static VALUE clips_environment_deftemplate_assert_hash(VALUE self, VALUE hash)
432
+ {
433
+ const char *cdeftemplate_name;
434
+
435
+ Environment *env;
436
+ Deftemplate *template;
437
+
438
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
439
+ TypedData_Get_Struct(self, Deftemplate, &Deftemplate_type, template);
440
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
441
+
442
+ cdeftemplate_name = DeftemplateName(template);
443
+
444
+ FactBuilder *fb = CreateFactBuilder(env, cdeftemplate_name);
445
+ switch (FBError(env))
446
+ {
447
+ case FBE_NO_ERROR:
448
+ break;
449
+ case FBE_DEFTEMPLATE_NOT_FOUND_ERROR:
450
+ rb_warn("Could not assert fact; deftemplate not found!");
451
+ return Qnil;
452
+ case FBE_IMPLIED_DEFTEMPLATE_ERROR:
453
+ rb_warn("Could not assert fact; cannot use assert_hash to create non-deftemplated facts!");
454
+ return Qnil;
455
+ }
456
+ void *args[2] = { (void *)fb, (void *)env };
457
+ rb_hash_foreach(hash, _clips_environment_assert_hash, (VALUE)args);
458
+ Fact *fact = FBAssert(fb);
459
+ FBDispose(fb);
460
+
461
+ switch (FBError(env))
462
+ {
463
+ case FBE_NO_ERROR:
464
+ break;
465
+ case FBE_NULL_POINTER_ERROR:
466
+ rb_warn("Could not assert fact. This might be a bug in clipsruby!");
467
+ return Qnil;
468
+ case FBE_COULD_NOT_ASSERT_ERROR:
469
+ rb_warn("Could not assert fact. Pattern matching of a fact or instance is already occurring.");
470
+ return Qnil;
471
+ case FBE_RULE_NETWORK_ERROR:
472
+ rb_warn("Could not assert fact. An error occurs while the assertion was being processed in the rule network.");
473
+ return Qnil;
474
+ }
475
+
476
+ VALUE rb_fact =
477
+ TypedData_Wrap_Struct(rb_const_get(CLASS_OF(rbEnvironment), rb_intern("Fact")), &Fact_type, fact);
478
+
479
+ rb_iv_set(rb_fact, "@environment", rbEnvironment);
480
+
481
+ return rb_fact;
482
+ }
483
+
484
+ static VALUE clips_environment_deftemplate_static_assert_hash(VALUE self, VALUE deftemplate, VALUE hash)
485
+ {
486
+ return clips_environment_deftemplate_assert_hash(deftemplate, hash);
487
+ }
488
+
394
489
  static void CLIPSValue_to_VALUE(CLIPSValue *from, VALUE *value, VALUE *rbEnvironment)
395
490
  {
396
491
  Environment *env;
@@ -1255,6 +1350,65 @@ static VALUE clips_environment_static_find_defrule(VALUE self, VALUE rbEnvironme
1255
1350
  return clips_environment_find_defrule(rbEnvironment, defrule_name);
1256
1351
  }
1257
1352
 
1353
+ static VALUE clips_environment_find_deffacts(VALUE self, VALUE deffacts_name)
1354
+ {
1355
+ Environment *env;
1356
+ Deffacts *deffacts;
1357
+
1358
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
1359
+
1360
+ switch (TYPE(deffacts_name)) {
1361
+ case T_STRING:
1362
+ deffacts = FindDeffacts(env, StringValueCStr(deffacts_name));
1363
+ break;
1364
+ case T_SYMBOL:
1365
+ deffacts = FindDeffacts(env, rb_id2name(SYM2ID(deffacts_name)));
1366
+ break;
1367
+ default:
1368
+ rb_warn("deffacts name must be a symbol or string");
1369
+ return Qnil;
1370
+ }
1371
+
1372
+ if (deffacts == NULL) {
1373
+ return Qnil;
1374
+ } else {
1375
+ return TypedData_Wrap_Struct(rb_const_get(CLASS_OF(self), rb_intern("Deffacts")), &Deffacts_type, deffacts);
1376
+ }
1377
+ }
1378
+
1379
+ static VALUE clips_environment_static_find_deffacts(VALUE self, VALUE rbEnvironment, VALUE deffacts_name)
1380
+ {
1381
+ return clips_environment_find_deffacts(rbEnvironment, deffacts_name);
1382
+ }
1383
+
1384
+ static VALUE clips_environment_deffacts_name(VALUE self)
1385
+ {
1386
+ Deffacts *deffacts;
1387
+
1388
+ TypedData_Get_Struct(self, Deffacts, &Deffacts_type, deffacts);
1389
+
1390
+ return ID2SYM(rb_intern(DeffactsName(deffacts)));
1391
+ }
1392
+
1393
+ static VALUE clips_environment_deffacts_static_name(VALUE self, VALUE rbDeffacts)
1394
+ {
1395
+ return clips_environment_deffacts_name(rbDeffacts);
1396
+ }
1397
+
1398
+ static VALUE clips_environment_deffacts_pp_form(VALUE self)
1399
+ {
1400
+ Deffacts *deffacts;
1401
+
1402
+ TypedData_Get_Struct(self, Deffacts, &Deffacts_type, deffacts);
1403
+
1404
+ return rb_str_new2(DeffactsPPForm(deffacts));
1405
+ }
1406
+
1407
+ static VALUE clips_environment_deffacts_static_pp_form(VALUE self, VALUE rbDeffacts)
1408
+ {
1409
+ return clips_environment_deffacts_pp_form(rbDeffacts);
1410
+ }
1411
+
1258
1412
  static VALUE clips_environment_get_current_module(VALUE self)
1259
1413
  {
1260
1414
  Environment *env;
@@ -1392,6 +1546,58 @@ static VALUE clips_environment_deftemplate_static_pp_form(VALUE self, VALUE rbDe
1392
1546
  return clips_environment_deftemplate_pp_form(rbDeftemplate);
1393
1547
  }
1394
1548
 
1549
+ static VALUE clips_environment_fact_pp_form(int argc, VALUE *argv, VALUE self)
1550
+ {
1551
+ Fact *fact;
1552
+ VALUE ignore_defaults, toReturn;
1553
+ Environment *env;
1554
+
1555
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
1556
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
1557
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
1558
+ StringBuilder *sb = CreateStringBuilder(env, 0);
1559
+
1560
+ rb_scan_args(argc, argv, "01", &ignore_defaults);
1561
+
1562
+ if (ignore_defaults == Qtrue) {
1563
+ FactPPForm(fact, sb, true);
1564
+ } else {
1565
+ FactPPForm(fact, sb, false);
1566
+ }
1567
+
1568
+ toReturn = rb_str_new2(sb->contents);
1569
+
1570
+ SBDispose(sb);
1571
+
1572
+ return toReturn;
1573
+ }
1574
+
1575
+ static VALUE clips_environment_fact_static_pp_form(int argc, VALUE *argv, VALUE klass)
1576
+ {
1577
+ Fact *fact;
1578
+ VALUE self, ignore_defaults, toReturn;
1579
+ Environment *env;
1580
+
1581
+ rb_scan_args(argc, argv, "11", &self, &ignore_defaults);
1582
+
1583
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
1584
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
1585
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
1586
+ StringBuilder *sb = CreateStringBuilder(env, 0);
1587
+
1588
+ if (ignore_defaults == Qtrue) {
1589
+ FactPPForm(fact, sb, true);
1590
+ } else {
1591
+ FactPPForm(fact, sb, false);
1592
+ }
1593
+
1594
+ toReturn = rb_str_new2(sb->contents);
1595
+
1596
+ SBDispose(sb);
1597
+
1598
+ return toReturn;
1599
+ }
1600
+
1395
1601
  static VALUE clips_environment_get_fact_list(int argc, VALUE *argv, VALUE rbEnvironment) {
1396
1602
  VALUE defmodule_or_defmodule_name;
1397
1603
  Environment *env;
@@ -1690,6 +1896,15 @@ void Init_clipsruby(void)
1690
1896
  rb_define_method(rbEnvironment, "set_current_module", clips_environment_set_current_module, 1);
1691
1897
  rb_define_singleton_method(rbEnvironment, "get_fact_list", clips_environment_static_get_fact_list, -1);
1692
1898
  rb_define_method(rbEnvironment, "get_fact_list", clips_environment_get_fact_list, -1);
1899
+ rb_define_singleton_method(rbEnvironment, "find_deffacts", clips_environment_static_find_deffacts, 2);
1900
+ rb_define_method(rbEnvironment, "find_deffacts", clips_environment_find_deffacts, 1);
1901
+
1902
+ VALUE rbDeffacts = rb_define_class_under(rbEnvironment, "Deffacts", rb_cObject);
1903
+ rb_define_alloc_func(rbDeffacts, deffacts_alloc);
1904
+ rb_define_singleton_method(rbDeffacts, "name", clips_environment_deffacts_static_name, 1);
1905
+ rb_define_method(rbDeffacts, "name", clips_environment_deffacts_name, 0);
1906
+ rb_define_singleton_method(rbDeffacts, "pp_form", clips_environment_deffacts_static_pp_form, 1);
1907
+ rb_define_method(rbDeffacts, "pp_form", clips_environment_deffacts_pp_form, 0);
1693
1908
 
1694
1909
  VALUE rbDeftemplate = rb_define_class_under(rbEnvironment, "Deftemplate", rb_cObject);
1695
1910
  rb_define_alloc_func(rbDeftemplate, deftemplate_alloc);
@@ -1697,6 +1912,8 @@ void Init_clipsruby(void)
1697
1912
  rb_define_method(rbDeftemplate, "name", clips_environment_deftemplate_name, 0);
1698
1913
  rb_define_singleton_method(rbDeftemplate, "pp_form", clips_environment_deftemplate_static_pp_form, 1);
1699
1914
  rb_define_method(rbDeftemplate, "pp_form", clips_environment_deftemplate_pp_form, 0);
1915
+ rb_define_singleton_method(rbDeftemplate, "assert_hash", clips_environment_deftemplate_static_assert_hash, 2);
1916
+ rb_define_method(rbDeftemplate, "assert_hash", clips_environment_deftemplate_assert_hash, 1);
1700
1917
 
1701
1918
  VALUE rbDefmodule = rb_define_class_under(rbEnvironment, "Defmodule", rb_cObject);
1702
1919
  rb_define_alloc_func(rbDefmodule, defmodule_alloc);
@@ -1711,6 +1928,8 @@ void Init_clipsruby(void)
1711
1928
 
1712
1929
  VALUE rbFact = rb_define_class_under(rbEnvironment, "Fact", rb_cObject);
1713
1930
  rb_define_alloc_func(rbFact, fact_alloc);
1931
+ rb_define_singleton_method(rbFact, "deftemplate", clips_environment_fact_static_deftemplate, 1);
1932
+ rb_define_method(rbFact, "deftemplate", clips_environment_fact_deftemplate, 0);
1714
1933
  rb_define_singleton_method(rbFact, "deftemplate_name", clips_environment_fact_static_deftemplate_name, 1);
1715
1934
  rb_define_method(rbFact, "deftemplate_name", clips_environment_fact_deftemplate_name, 0);
1716
1935
  rb_define_singleton_method(rbFact, "to_h", clips_environment_fact_static_to_h, 1);
@@ -1725,6 +1944,8 @@ void Init_clipsruby(void)
1725
1944
  rb_define_method(rbFact, "modify", clips_environment_fact_modify, 1);
1726
1945
  rb_define_singleton_method(rbFact, "index", clips_environment_fact_static_index, 1);
1727
1946
  rb_define_method(rbFact, "index", clips_environment_fact_index, 0);
1947
+ rb_define_singleton_method(rbFact, "pp_form", clips_environment_fact_static_pp_form, -1);
1948
+ rb_define_method(rbFact, "pp_form", clips_environment_fact_pp_form, -1);
1728
1949
 
1729
1950
  VALUE rbDefrule = rb_define_class_under(rbEnvironment, "Defrule", rb_cObject);
1730
1951
  rb_define_alloc_func(rbDefrule, defrule_alloc);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipsruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Johnston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-10 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Calling the CLIPS programming language from within Ruby
14
14
  email: mrryanjohnston@gmail.com