clipsruby 0.0.11 → 0.0.14

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 +119 -0
  3. data/ext/clipsruby/clipsruby.c +276 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5181c11e5791e9e519d8324fb2b8a72843db339af9f0b0dbbd4c6afccf82c31
4
- data.tar.gz: cd1618021fa058de8c5b05cf7b2aedd77fb0164de1b11323f708d557be1633af
3
+ metadata.gz: f33a751deeebf96aa16135763365063f9facdd95c2cd7e7df7de0e3e90bea56b
4
+ data.tar.gz: f24d9c9901986b83ce38ffd97d5ef9f06c2409ae3b24de85094a73b78f39dbc4
5
5
  SHA512:
6
- metadata.gz: 79e7dc3122317674dc25526ac2957aaad06a36243ab3d611989582a1150782b2872d8662d8091191337511ecac8d51749d74106e6e26fd2e1768de829a2cc07d
7
- data.tar.gz: 02c6ce66e34dfa331ce5b8b77055debf027a39e4ed1d802a95a557bc86eb2b11abff778725dc1fb7d3251497c254ad3249406a90f65f0860dfdce709966bcc7f
6
+ metadata.gz: 1d8d10e392c06bb956d2c889e3090afce100ae4b9ef27a2a8463da231d5d00c645890b82376656bc7f17cdc33f248d49f8dd274ac341a7e60fdaf76d8a3d8496
7
+ data.tar.gz: caf7c237fb87e4e04d842593fb7cb23d7d65b4494a373cd59fb5fc775a48dbe6c71a7cd86fbfa2d2efc93b338994079f26b9f406f47390cdad52d352476e1846
data/README.md CHANGED
@@ -39,6 +39,102 @@ CLIPS::Environment.batch_star(env, "./my-batch.bat")
39
39
  env.batch_star("./my-batch.bat")
40
40
  ```
41
41
 
42
+ ### `CLIPS::Environment.save`
43
+ ### `env.save`
44
+
45
+ Pass the path in which we'll save the environment constructs.
46
+
47
+ ```ruby
48
+ CLIPS::Environment.save(env, "./my-save.bin")
49
+ env.save("./my-save.bin")
50
+ ```
51
+
52
+ ### `CLIPS::Environment.load`
53
+ ### `env.load`
54
+
55
+ Pass the path to a file that will load the constructs stored in the file
56
+ into the environment. Remember to `reset` the environment if you need.
57
+
58
+ ```ruby
59
+ CLIPS::Environment.load(env, "./my-load.bin")
60
+ env.load("./my-load.bin")
61
+ ```
62
+
63
+ ### `CLIPS::Environment.save_facts`
64
+ ### `env.save_facts`
65
+
66
+ Pass the path in which we'll save the environment facts.
67
+ The third argument is optional, and determines whether to store the visible or local facts.
68
+ By default, it'll store local.
69
+
70
+ ```ruby
71
+ CLIPS::Environment.save_facts(env, "./my-save-facts.bin")
72
+ env.save_facts("./my-save-facts.bin")
73
+ CLIPS::Environment.save_facts(env, "./my-save-facts.bin", :local)
74
+ env.save_facts("./my-save-facts.bin", :local)
75
+ CLIPS::Environment.save_facts(env, "./my-save-facts.bin", :visible)
76
+ env.save_facts("./my-save-facts.bin", :visible)
77
+ ```
78
+
79
+ ### `CLIPS::Environment.load_facts`
80
+ ### `env.load_facts`
81
+
82
+ Pass the path to a file that will load the facts stored in the file
83
+ into the environment.
84
+
85
+ ```ruby
86
+ CLIPS::Environment.load_facts(env, "./my-load-facts.bin")
87
+ env.load_facts("./my-load-facts.bin")
88
+ ```
89
+
90
+ ### `CLIPS::Environment.bsave`
91
+ ### `env.bsave`
92
+
93
+ Pass the path in which we'll save the binary representation of the environment constructs.
94
+
95
+ ```ruby
96
+ CLIPS::Environment.bsave(env, "./my-bsave.bin")
97
+ env.bsave("./my-bsave.bin")
98
+ ```
99
+
100
+ ### `CLIPS::Environment.bload`
101
+ ### `env.bload`
102
+
103
+ Pass the path to a binary file that will load the constructs stored in the file
104
+ into the environment. Remember to `reset` the environment if you need.
105
+
106
+ ```ruby
107
+ CLIPS::Environment.bload(env, "./my-bload.bin")
108
+ env.bload("./my-bload.bin")
109
+ ```
110
+
111
+ ### `CLIPS::Environment.bsave_facts`
112
+ ### `env.bsave_facts`
113
+
114
+ Pass the path in which we'll save the binary representation of the environment facts.
115
+ The third argument is optional, and determines whether to store the visible or local facts.
116
+ By default, it'll store local.
117
+
118
+ ```ruby
119
+ CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin")
120
+ env.bsave_facts("./my-bsave-facts.bin")
121
+ CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin", :local)
122
+ env.bsave_facts("./my-bsave-facts.bin", :local)
123
+ CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin", :visible)
124
+ env.bsave_facts("./my-bsave-facts.bin", :visible)
125
+ ```
126
+
127
+ ### `CLIPS::Environment.bload_facts`
128
+ ### `env.bload_facts`
129
+
130
+ Pass the path to a binary file that will load the facts stored in the file
131
+ into the environment.
132
+
133
+ ```ruby
134
+ CLIPS::Environment.bload_facts(env, "./my-bload-facts.bin")
135
+ env.bload_facts("./my-bload-facts.bin")
136
+ ```
137
+
42
138
  ### `CLIPS::Environment.assert_string`
43
139
  ### `CLIPS::Environment#assert_string`
44
140
 
@@ -59,6 +155,19 @@ fact = CLIPS::Environment.assert_hash(env, :my_deftemplate, a: 1, b: "asdf", c:
59
155
  fact2 = env.assert_hash(:my_deftemplate, d: 4.5, e: :asdf)
60
156
  ```
61
157
 
158
+ ### `CLIPS::Environment.find_fact`
159
+ ### `CLIPS::Environment#find_fact`
160
+
161
+ A light wrapper around the CLIPS find-fact function. Accepts a fact set template and query,
162
+ returns the first Facts in the environment that match as Ruby objects.
163
+
164
+ ```ruby
165
+ CLIPS::Environment.find_facts(env, "(?f my_deftemplate)")
166
+ env.find_fact("(?f my_deftemplate)")
167
+ CLIPS::Environment.find_fact(env, "(?f my_deftemplate)", "(eq ?f:b \"asdf\")")
168
+ env.find_fact("(?f my_deftemplate)", "(= ?f:a 1)")
169
+ ```
170
+
62
171
  ### `CLIPS::Environment.find_all_facts`
63
172
  ### `CLIPS::Environment#find_all_facts`
64
173
 
@@ -167,6 +276,16 @@ CLIPS::Environment::Fact.to_h(fact)
167
276
  fact.to_h
168
277
  ```
169
278
 
279
+ ### `CLIPS::Environment::Fact.index`
280
+ ### `CLIPS::Environment::Fact#index`
281
+
282
+ Returns the index of the fact in working memory
283
+
284
+ ```ruby
285
+ CLIPS::Environment::Fact.index(fact)
286
+ fact.index
287
+ ```
288
+
170
289
  ### `CLIPS::Environment::Fact.deftemplate_name`
171
290
  ### `CLIPS::Environment::Fact#deftemplate_name`
172
291
 
@@ -642,6 +642,32 @@ static VALUE clips_environment_static_eval(VALUE self, VALUE rbEnvironment, VALU
642
642
  return clips_environment_eval(rbEnvironment, string);
643
643
  }
644
644
 
645
+ static VALUE clips_environment_find_fact(int argc, VALUE *argv, VALUE environment) {
646
+ VALUE fact_set_template, query;
647
+
648
+ rb_scan_args(argc, argv, "11", &fact_set_template, &query);
649
+ if (NIL_P(query)) {
650
+ query = rb_str_new_cstr("TRUE");
651
+ }
652
+
653
+ return clips_environment_eval(
654
+ environment,
655
+ rb_sprintf("(find-fact (%s) %s)", StringValueCStr(fact_set_template), StringValueCStr(query)));
656
+ }
657
+
658
+ static VALUE clips_environment_static_find_fact(int argc, VALUE *argv, VALUE klass) {
659
+ VALUE rbEnvironment, fact_set_template, query;
660
+
661
+ rb_scan_args(argc, argv, "21", &rbEnvironment, &fact_set_template, &query);
662
+ if (NIL_P(query)) {
663
+ query = rb_str_new_cstr("TRUE");
664
+ }
665
+
666
+ return clips_environment_eval(
667
+ rbEnvironment,
668
+ rb_sprintf("(find-fact (%s) %s)", StringValueCStr(fact_set_template), StringValueCStr(query)));
669
+ }
670
+
645
671
  static VALUE clips_environment_find_all_facts(int argc, VALUE *argv, VALUE environment) {
646
672
  VALUE fact_set_template, query;
647
673
 
@@ -747,6 +773,222 @@ static VALUE clips_environment_static_reset(VALUE self, VALUE rbEnvironment)
747
773
  return clips_environment_reset(rbEnvironment);
748
774
  }
749
775
 
776
+ static VALUE clips_environment_save(VALUE self, VALUE path)
777
+ {
778
+ Environment *env;
779
+
780
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
781
+
782
+ if (Save(env, StringValueCStr(path))) {
783
+ return Qtrue;
784
+ } else {
785
+ return Qfalse;
786
+ }
787
+ }
788
+
789
+ static VALUE clips_environment_static_save(VALUE self, VALUE rbEnvironment, VALUE path)
790
+ {
791
+ return clips_environment_save(rbEnvironment, path);
792
+ }
793
+
794
+ static VALUE clips_environment_load(VALUE self, VALUE path)
795
+ {
796
+ Environment *env;
797
+
798
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
799
+
800
+ switch (Load(env, StringValueCStr(path)))
801
+ {
802
+ case LE_NO_ERROR:
803
+ break;
804
+ case LE_OPEN_FILE_ERROR:
805
+ rb_warn("could not load: issue opening file.");
806
+ return Qnil;
807
+ case LE_PARSING_ERROR:
808
+ rb_warn("could not load: parsing issue.");
809
+ return Qnil;
810
+ }
811
+ return Qtrue;
812
+ }
813
+
814
+ static VALUE clips_environment_static_load(VALUE self, VALUE rbEnvironment, VALUE path)
815
+ {
816
+ return clips_environment_load(rbEnvironment, path);
817
+ }
818
+
819
+ static VALUE clips_environment_save_facts(int argc, VALUE *argv, VALUE rbEnvironment) {
820
+ VALUE path, scope;
821
+ Environment *env;
822
+
823
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
824
+
825
+ rb_scan_args(argc, argv, "11", &path, &scope);
826
+ if (NIL_P(scope)) {
827
+ scope = ID2SYM(rb_intern("local"));
828
+ }
829
+
830
+ int number_of_facts_saved;
831
+
832
+ if (scope == ID2SYM(rb_intern("visible"))) {
833
+ number_of_facts_saved = SaveFacts(env, StringValueCStr(path), VISIBLE_SAVE);
834
+ } else if (scope == ID2SYM(rb_intern("local"))) {
835
+ number_of_facts_saved = SaveFacts(env, StringValueCStr(path), LOCAL_SAVE);
836
+ } else {
837
+ rb_warn("could not bsave_facts: unsupported scope.");
838
+ return Qnil;
839
+ }
840
+ return INT2NUM(number_of_facts_saved);
841
+ }
842
+
843
+ static VALUE clips_environment_static_save_facts(int argc, VALUE *argv, VALUE klass) {
844
+ VALUE rbEnvironment, path, scope;
845
+ Environment *env;
846
+
847
+ rb_scan_args(argc, argv, "21", &rbEnvironment, &path, &scope);
848
+
849
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
850
+
851
+ if (NIL_P(scope)) {
852
+ scope = ID2SYM(rb_intern("local"));
853
+ }
854
+
855
+ int number_of_facts_saved;
856
+
857
+ if (scope == ID2SYM(rb_intern("visible"))) {
858
+ number_of_facts_saved = SaveFacts(env, StringValueCStr(path), VISIBLE_SAVE);
859
+ } else if (scope == ID2SYM(rb_intern("local"))) {
860
+ number_of_facts_saved = SaveFacts(env, StringValueCStr(path), LOCAL_SAVE);
861
+ } else {
862
+ rb_warn("could not bsave_facts: unsupported scope.");
863
+ return Qnil;
864
+ }
865
+ return INT2NUM(number_of_facts_saved);
866
+ }
867
+
868
+ static VALUE clips_environment_load_facts(VALUE self, VALUE path)
869
+ {
870
+ Environment *env;
871
+
872
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
873
+ int number_of_facts_loaded = LoadFacts(env, StringValueCStr(path));
874
+
875
+ if (number_of_facts_loaded == -1) {
876
+ return Qnil;
877
+ } else {
878
+ return INT2NUM(number_of_facts_loaded);
879
+ }
880
+ }
881
+
882
+ static VALUE clips_environment_static_load_facts(VALUE self, VALUE rbEnvironment, VALUE path)
883
+ {
884
+ return clips_environment_load_facts(rbEnvironment, path);
885
+ }
886
+
887
+ static VALUE clips_environment_bsave(VALUE self, VALUE path)
888
+ {
889
+ Environment *env;
890
+
891
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
892
+
893
+ if (Bsave(env, StringValueCStr(path))) {
894
+ return Qtrue;
895
+ } else {
896
+ return Qfalse;
897
+ }
898
+ }
899
+
900
+ static VALUE clips_environment_static_bsave(VALUE self, VALUE rbEnvironment, VALUE path)
901
+ {
902
+ return clips_environment_bsave(rbEnvironment, path);
903
+ }
904
+
905
+ static VALUE clips_environment_bload(VALUE self, VALUE path)
906
+ {
907
+ Environment *env;
908
+
909
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
910
+
911
+ if (Bload(env, StringValueCStr(path))) {
912
+ return Qtrue;
913
+ } else {
914
+ return Qfalse;
915
+ }
916
+ }
917
+
918
+ static VALUE clips_environment_static_bload(VALUE self, VALUE rbEnvironment, VALUE path)
919
+ {
920
+ return clips_environment_bload(rbEnvironment, path);
921
+ }
922
+
923
+ static VALUE clips_environment_bsave_facts(int argc, VALUE *argv, VALUE rbEnvironment) {
924
+ VALUE path, scope;
925
+ Environment *env;
926
+
927
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
928
+
929
+ rb_scan_args(argc, argv, "11", &path, &scope);
930
+ if (NIL_P(scope)) {
931
+ scope = ID2SYM(rb_intern("local"));
932
+ }
933
+
934
+ int number_of_facts_saved;
935
+
936
+ if (scope == ID2SYM(rb_intern("visible"))) {
937
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), VISIBLE_SAVE);
938
+ } else if (scope == ID2SYM(rb_intern("local"))) {
939
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), LOCAL_SAVE);
940
+ } else {
941
+ rb_warn("could not bsave_facts: unsupported scope.");
942
+ return Qnil;
943
+ }
944
+
945
+ return INT2NUM(number_of_facts_saved);
946
+ }
947
+
948
+ static VALUE clips_environment_static_bsave_facts(int argc, VALUE *argv, VALUE klass) {
949
+ VALUE rbEnvironment, path, scope;
950
+ Environment *env;
951
+
952
+ rb_scan_args(argc, argv, "21", &rbEnvironment, &path, &scope);
953
+
954
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
955
+
956
+ if (NIL_P(scope)) {
957
+ scope = ID2SYM(rb_intern("local"));
958
+ }
959
+
960
+ int number_of_facts_saved;
961
+
962
+ if (scope == ID2SYM(rb_intern("visible"))) {
963
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), VISIBLE_SAVE);
964
+ } else if (scope == ID2SYM(rb_intern("local"))) {
965
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), LOCAL_SAVE);
966
+ } else {
967
+ rb_warn("could not bsave_facts: unsupported scope.");
968
+ return Qnil;
969
+ }
970
+ return INT2NUM(number_of_facts_saved);
971
+ }
972
+
973
+ static VALUE clips_environment_bload_facts(VALUE self, VALUE path)
974
+ {
975
+ Environment *env;
976
+
977
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
978
+ int number_of_facts_loaded = BinaryLoadFacts(env, StringValueCStr(path));
979
+
980
+ if (number_of_facts_loaded == -1) {
981
+ return Qnil;
982
+ } else {
983
+ return INT2NUM(number_of_facts_loaded);
984
+ }
985
+ }
986
+
987
+ static VALUE clips_environment_static_bload_facts(VALUE self, VALUE rbEnvironment, VALUE path)
988
+ {
989
+ return clips_environment_bload_facts(rbEnvironment, path);
990
+ }
991
+
750
992
  static VALUE clips_environment_fact_slot_names(VALUE self)
751
993
  {
752
994
  Fact *fact;
@@ -911,6 +1153,20 @@ static VALUE clips_environment_fact_static_modify(VALUE self, VALUE rbFact, VALU
911
1153
  return clips_environment_fact_modify(rbFact, hash);
912
1154
  }
913
1155
 
1156
+ static VALUE clips_environment_fact_index(VALUE self)
1157
+ {
1158
+ Fact *fact;
1159
+
1160
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
1161
+
1162
+ return INT2NUM(FactIndex(fact));
1163
+ }
1164
+
1165
+ static VALUE clips_environment_fact_static_index(VALUE self, VALUE rbFact)
1166
+ {
1167
+ return clips_environment_fact_index(rbFact);
1168
+ }
1169
+
914
1170
 
915
1171
  void Init_clipsruby(void)
916
1172
  {
@@ -933,6 +1189,8 @@ void Init_clipsruby(void)
933
1189
  rb_define_method(rbEnvironment, "run", clips_environment_run, -1);
934
1190
  rb_define_singleton_method(rbEnvironment, "_eval", clips_environment_static_eval, 2);
935
1191
  rb_define_method(rbEnvironment, "_eval", clips_environment_eval, 1);
1192
+ rb_define_singleton_method(rbEnvironment, "find_fact", clips_environment_static_find_fact, -1);
1193
+ rb_define_method(rbEnvironment, "find_fact", clips_environment_find_fact, -1);
936
1194
  rb_define_singleton_method(rbEnvironment, "find_all_facts", clips_environment_static_find_all_facts, -1);
937
1195
  rb_define_method(rbEnvironment, "find_all_facts", clips_environment_find_all_facts, -1);
938
1196
  rb_define_singleton_method(rbEnvironment, "batch_star", clips_environment_static_batch_star, 2);
@@ -941,6 +1199,22 @@ void Init_clipsruby(void)
941
1199
  rb_define_method(rbEnvironment, "clear", clips_environment_clear, 0);
942
1200
  rb_define_singleton_method(rbEnvironment, "reset", clips_environment_static_reset, 1);
943
1201
  rb_define_method(rbEnvironment, "reset", clips_environment_reset, 0);
1202
+ rb_define_singleton_method(rbEnvironment, "save", clips_environment_static_save, 2);
1203
+ rb_define_method(rbEnvironment, "save", clips_environment_save, 1);
1204
+ rb_define_singleton_method(rbEnvironment, "load", clips_environment_static_load, 2);
1205
+ rb_define_method(rbEnvironment, "load", clips_environment_load, 1);
1206
+ rb_define_singleton_method(rbEnvironment, "save_facts", clips_environment_static_save_facts, -1);
1207
+ rb_define_method(rbEnvironment, "save_facts", clips_environment_save_facts, -1);
1208
+ rb_define_singleton_method(rbEnvironment, "load_facts", clips_environment_static_load_facts, 2);
1209
+ rb_define_method(rbEnvironment, "load_facts", clips_environment_load_facts, 1);
1210
+ rb_define_singleton_method(rbEnvironment, "bsave", clips_environment_static_bsave, 2);
1211
+ rb_define_method(rbEnvironment, "bsave", clips_environment_bsave, 1);
1212
+ rb_define_singleton_method(rbEnvironment, "bload", clips_environment_static_bload, 2);
1213
+ rb_define_method(rbEnvironment, "bload", clips_environment_bload, 1);
1214
+ rb_define_singleton_method(rbEnvironment, "bsave_facts", clips_environment_static_bsave_facts, -1);
1215
+ rb_define_method(rbEnvironment, "bsave_facts", clips_environment_bsave_facts, -1);
1216
+ rb_define_singleton_method(rbEnvironment, "bload_facts", clips_environment_static_bload_facts, 2);
1217
+ rb_define_method(rbEnvironment, "bload_facts", clips_environment_bload_facts, 1);
944
1218
 
945
1219
  VALUE rbFact = rb_define_class_under(rbEnvironment, "Fact", rb_cObject);
946
1220
  rb_define_singleton_method(rbFact, "deftemplate_name", clips_environment_fact_static_deftemplate_name, 1);
@@ -955,6 +1229,8 @@ void Init_clipsruby(void)
955
1229
  rb_define_method(rbFact, "retract", clips_environment_fact_retract, 0);
956
1230
  rb_define_singleton_method(rbFact, "modify", clips_environment_fact_static_modify, 2);
957
1231
  rb_define_method(rbFact, "modify", clips_environment_fact_modify, 1);
1232
+ rb_define_singleton_method(rbFact, "index", clips_environment_fact_static_index, 1);
1233
+ rb_define_method(rbFact, "index", clips_environment_fact_index, 0);
958
1234
 
959
1235
  VALUE rbInstance = rb_define_class_under(rbEnvironment, "Instance", rb_cObject);
960
1236
  }
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.11
4
+ version: 0.0.14
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-06 00:00:00.000000000 Z
11
+ date: 2024-09-07 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