clipsruby 0.0.6 → 0.0.8

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 +134 -0
  3. data/ext/clipsruby/clipsruby.c +57 -25
  4. metadata +6 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e3ac17e072a0fec194ad7a163acefb487ed0b2d822c04ba71f28e517278fe7a
4
- data.tar.gz: 76b21dc4e2c25ae2017d94c064ac33f9bd2e3ff9d6a9b9505e5bd2f298b1d837
3
+ metadata.gz: 8bca9526858c7578fb8f1347584b76bdf99c5f8f74d301554751941bd3e3dde8
4
+ data.tar.gz: '018e3bd14394a440793cd5006c75e2ce6fa0e2395ec2c85304bcb2a4e7757118'
5
5
  SHA512:
6
- metadata.gz: a665cd9fd033552cad6807dea0616cf5ebb9f41e1d07f37357c57d85e3e3db50c4845792626e47d01c8c0a6a1a1454983b8f32979624bfadc0d08388d96cefc1
7
- data.tar.gz: 2ac05ff02cbb504aba1eab34cfcd5f7ff4c800cd4021de5bf956edec010725af34a5dcaa531b91b633761f9198b5835316b1b3ca20d639c0860297da5b40e46e
6
+ metadata.gz: 77e80952f44f747a30e40ebdc433bf46734a5c48e172557c438ec84b94de91ba486ee7791974837442a229c401b06b56658fc33eb2dd9251c617ce3a4eadaab5
7
+ data.tar.gz: ad93fe9c5b7803738e449a1b373cffde734c84baef745364cfe9f866a5585e82cd757fd750263d2ca02c204f1cff002eb69e04f955ab6fa80f75c7eca197a890
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # clipsruby
2
+
3
+ ## Description
4
+
5
+ Use the CLIPS programming language from within Ruby
6
+
7
+ ## API
8
+
9
+ ### `CLIPS.create_environment`
10
+ ### `CLIPS::Environment.new`
11
+
12
+ Create a new CLIPS environment in which you may define Rules,
13
+ assert Facts, and instantiate Objects.
14
+
15
+ ```ruby
16
+ env = CLIPS.create_environment
17
+ env2 = CLIPS::Environment.new
18
+ ```
19
+
20
+ ### `CLIPS::Environment.assert_string`
21
+ ### `CLIPS::Environment#assert_string`
22
+
23
+ Assert a string as a Fact in the CLIPS environment.
24
+
25
+ ```ruby
26
+ fact = CLIPS::Environment.assert_string(env, "(foo bar)")
27
+ fact2 = env.assert_string("(bat baz)")
28
+ ```
29
+
30
+ ### `CLIPS::Environment.assert_hash`
31
+ ### `CLIPS::Environment#assert_hash`
32
+
33
+ Asserts a Deftemplate fact into the CLIPS environment
34
+
35
+ ```ruby
36
+ fact = CLIPS::Environment.assert_hash(env, :my_deftemplate, a: 1, b: "asdf", c: :C)
37
+ fact2 = env.assert_hash(:my_deftemplate, d: 4.5, e: :asdf)
38
+ ```
39
+
40
+ ### `CLIPS::Environment.find_all_facts`
41
+ ### `CLIPS::Environment#find_all_facts`
42
+
43
+ A light wrapper around the CLIPS find-all-facts function. Accepts a fact set template and query,
44
+ returns Facts in the environment that match as Ruby objects.
45
+
46
+ ```ruby
47
+ CLIPS::Environment.find_all_facts(env, "(?f my_deftemplate)")
48
+ env.find_all_facts("(?f my_deftemplate)")
49
+ CLIPS::Environment.find_all_facts(env, "(?f my_deftemplate)", "(eq ?f:b \"asdf\")")
50
+ env.find_all_facts("(?f my_deftemplate)", "(= ?f:a 1)")
51
+ ```
52
+
53
+ ### `CLIPS::Environment._eval`
54
+ ### `CLIPS::Environment#_eval`
55
+
56
+ Evaluates a passed string in the CLIPS environment and returns the results.
57
+
58
+ ```ruby
59
+ CLIPS::Environment._eval(env, "(find-all-facts ((?f my_deftemplate)) TRUE)")
60
+ env._eval("(find-all-facts ((?f my_deftemplate)) TRUE)")
61
+ ```
62
+
63
+ ### `CLIPS::Environment.build`
64
+ ### `CLIPS::Environment#build`
65
+
66
+ Build the constructs in the env as defined in a string
67
+
68
+ ```ruby
69
+ CLIPS::Environment.build(env, "(deftemplate my_template (slot a) (slot b) (slot c))")
70
+ env.build("(defrule do-a-thing (my_template (a ?a)) => (println \"a: \" ?a))")
71
+ ```
72
+
73
+ ### `CLIPS::Environment.add_udf`
74
+ ### `CLIPS::Environment#add_udf`
75
+
76
+ Adds a ruby method as a User Defined Function (udf) to the CLIPS environment
77
+
78
+ ```ruby
79
+ class CLIPS::Environment
80
+ def foo(a, b=2)
81
+ a + b
82
+ end
83
+
84
+ def bar(word)
85
+ "You said #{word}!"
86
+ end
87
+ end
88
+
89
+ env.add_udf(:foo)
90
+ CLIPS::Environment.add_udf(env, :bar)
91
+ ```
92
+
93
+ ### `CLIPS::Environment.run`
94
+ ### `CLIPS::Environment#run`
95
+
96
+ Runs the rules engine, executing items on the agenda.
97
+ Optionally may take the number of items to run from the agenda as an argument.
98
+
99
+ ```ruby
100
+ CLIPS::Environment.run(env, 1)
101
+ CLIPS::Environment.run(env)
102
+ env.run(1)
103
+ env.run
104
+ ```
105
+
106
+ ### `CLIPS::Environment.facts`
107
+ ### `CLIPS::Environment#facts`
108
+
109
+ Print all Facts defined in the CLIPS environment
110
+
111
+ ```ruby
112
+ CLIPS::Environment.facts(env)
113
+ env.facts
114
+ ```
115
+
116
+ ### `CLIPS::Environment::Fact.to_h`
117
+ ### `CLIPS::Environment::Fact#to_h`
118
+
119
+ Returns a hash representing the fact slot names and their values
120
+
121
+ ```ruby
122
+ CLIPS::Environment::Fact.to_h(fact)
123
+ fact.to_h
124
+ ```
125
+
126
+ ### `CLIPS::Environment::Fact.deftemplate_name`
127
+ ### `CLIPS::Environment::Fact#deftemplate_name`
128
+
129
+ Returns the name of the Deftemplate for a fact as a symbol
130
+
131
+ ```ruby
132
+ CLIPS::Environment::Fact.deftemplate_name(fact)
133
+ fact.deftemplate_name
134
+ ```
@@ -350,7 +350,7 @@ static void CLIPSValue_to_VALUE(CLIPSValue *from, VALUE *value, VALUE *rbEnviron
350
350
  } else if (from->lexemeValue == FalseSymbol(env)) {
351
351
  *value = Qfalse;
352
352
  } else {
353
- *value = rb_str_new2(from->lexemeValue->contents);
353
+ *value = ID2SYM(rb_intern(from->lexemeValue->contents));
354
354
  }
355
355
  break;
356
356
  case INTEGER_TYPE:
@@ -402,7 +402,7 @@ static void UDFValue_to_VALUE(UDFValue *from, VALUE *value, VALUE *rbEnvironment
402
402
  } else if (from->lexemeValue == FalseSymbol(env)) {
403
403
  *value = Qfalse;
404
404
  } else {
405
- *value = rb_str_new2(from->lexemeValue->contents);
405
+ *value = ID2SYM(rb_intern(from->lexemeValue->contents));
406
406
  }
407
407
  break;
408
408
  case INTEGER_TYPE:
@@ -437,7 +437,6 @@ void UDFGenericFunction(
437
437
  VALUE *current_argv = argv;
438
438
  UDFValue theArg;
439
439
  VALUE method = (VALUE)(context->context);
440
- VALUE theValue;
441
440
  VALUE rbEnvironment = rb_funcall(method, rb_intern("receiver"), 0);
442
441
  while (UDFHasNextArgument(context))
443
442
  {
@@ -602,22 +601,6 @@ static VALUE clips_environment_static_run(int argc, VALUE *argv, VALUE klass) {
602
601
  return INT2NUM(Run(env, NUM2INT(integer)));
603
602
  }
604
603
 
605
- /*
606
- static VALUE clips_environment_find_all_facts(VALUE self)
607
- {
608
- Fact *fact;
609
-
610
- TypedData_Get_Struct(self, Fact, &Fact_type, fact);
611
-
612
- return ID2SYM(rb_intern(DeftemplateName(FactDeftemplate(fact))));
613
- }
614
-
615
- static VALUE clips_environment_static_find_all_facts(VALUE self, VALUE rbFact)
616
- {
617
- return clips_environment_fact_deftemplate_name(rbFact);
618
- }
619
- */
620
-
621
604
  static VALUE clips_environment_eval(VALUE self, VALUE string)
622
605
  {
623
606
  Environment *env;
@@ -648,6 +631,59 @@ static VALUE clips_environment_static_eval(VALUE self, VALUE rbEnvironment, VALU
648
631
  return clips_environment_eval(rbEnvironment, string);
649
632
  }
650
633
 
634
+ static VALUE clips_environment_find_all_facts(int argc, VALUE *argv, VALUE environment) {
635
+ VALUE fact_set_template, query;
636
+
637
+ rb_scan_args(argc, argv, "11", &fact_set_template, &query);
638
+ if (NIL_P(query)) {
639
+ query = rb_str_new_cstr("TRUE");
640
+ }
641
+
642
+ return clips_environment_eval(
643
+ environment,
644
+ rb_sprintf("(find-all-facts (%s) %s)", StringValueCStr(fact_set_template), StringValueCStr(query)));
645
+ }
646
+
647
+ static VALUE clips_environment_static_find_all_facts(int argc, VALUE *argv, VALUE klass) {
648
+ VALUE rbEnvironment, fact_set_template, query;
649
+
650
+ rb_scan_args(argc, argv, "21", &rbEnvironment, &fact_set_template, &query);
651
+ if (NIL_P(query)) {
652
+ query = rb_str_new_cstr("TRUE");
653
+ }
654
+
655
+ return clips_environment_eval(
656
+ rbEnvironment,
657
+ rb_sprintf("(find-all-facts (%s) %s)", StringValueCStr(fact_set_template), StringValueCStr(query)));
658
+ }
659
+
660
+ static VALUE clips_environment_fact_to_h(VALUE self)
661
+ {
662
+ Fact *fact;
663
+ CLIPSValue key, value;
664
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
665
+
666
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
667
+
668
+ FactSlotNames(fact, &key);
669
+
670
+ VALUE hash = rb_hash_new();
671
+ for (size_t i = 0; i < key.multifieldValue->length; i++)
672
+ {
673
+ VALUE innerValue;
674
+ GetFactSlot(fact, key.multifieldValue->contents[i].lexemeValue->contents, &value);
675
+ CLIPSValue_to_VALUE(&value, &innerValue, &rbEnvironment);
676
+ rb_hash_aset(hash, rb_str_new2(key.multifieldValue->contents[i].lexemeValue->contents), innerValue);
677
+ }
678
+
679
+ return hash;
680
+ }
681
+
682
+ static VALUE clips_environment_fact_static_to_h(VALUE self, VALUE rbFact)
683
+ {
684
+ return clips_environment_fact_to_h(rbFact);
685
+ }
686
+
651
687
  void Init_clipsruby(void)
652
688
  {
653
689
  VALUE rbCLIPS = rb_define_module("CLIPS");
@@ -667,20 +703,16 @@ void Init_clipsruby(void)
667
703
  rb_define_method(rbEnvironment, "add_udf", clips_environment_add_udf, -1);
668
704
  rb_define_singleton_method(rbEnvironment, "run", clips_environment_static_run, -1);
669
705
  rb_define_method(rbEnvironment, "run", clips_environment_run, -1);
670
- /*
671
- rb_define_singleton_method(rbEnvironment, "find_all_facts", clips_environment_static_find_all_facts, 2);
672
- rb_define_method(rbEnvironment, "find_all_facts", clips_environment_find_all_facts, 1);
673
- */
674
706
  rb_define_singleton_method(rbEnvironment, "_eval", clips_environment_static_eval, 2);
675
707
  rb_define_method(rbEnvironment, "_eval", clips_environment_eval, 1);
708
+ rb_define_singleton_method(rbEnvironment, "find_all_facts", clips_environment_static_find_all_facts, -1);
709
+ rb_define_method(rbEnvironment, "find_all_facts", clips_environment_find_all_facts, -1);
676
710
 
677
711
  VALUE rbFact = rb_define_class_under(rbEnvironment, "Fact", rb_cObject);
678
712
  rb_define_singleton_method(rbFact, "deftemplate_name", clips_environment_fact_static_deftemplate_name, 1);
679
713
  rb_define_method(rbFact, "deftemplate_name", clips_environment_fact_deftemplate_name, 0);
680
- /*
681
714
  rb_define_singleton_method(rbFact, "to_h", clips_environment_fact_static_to_h, 1);
682
715
  rb_define_method(rbFact, "to_h", clips_environment_fact_to_h, 0);
683
- */
684
716
 
685
717
  VALUE rbInstance = rb_define_class_under(rbEnvironment, "Instance", rb_cObject);
686
718
  }
metadata CHANGED
@@ -1,22 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipsruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
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-04 00:00:00.000000000 Z
11
+ date: 2024-09-05 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
15
15
  executables: []
16
16
  extensions:
17
17
  - ext/clipsruby/extconf.rb
18
- extra_rdoc_files: []
18
+ extra_rdoc_files:
19
+ - README.md
19
20
  files:
21
+ - README.md
20
22
  - ext/clipsruby/agenda.c
21
23
  - ext/clipsruby/agenda.h
22
24
  - ext/clipsruby/analysis.c
@@ -365,6 +367,7 @@ homepage: https://rubygems.org/gems/clipsruby
365
367
  licenses:
366
368
  - MIT
367
369
  metadata:
370
+ documentation_uri: https://github.com/mrryanjohnston/clipsruby/blob/main/README.md
368
371
  source_code_uri: https://github.com/mrryanjohnston/clipsruby
369
372
  post_install_message:
370
373
  rdoc_options: []