clipsruby 0.0.10 → 0.0.12

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 +59 -0
  3. data/ext/clipsruby/clipsruby.c +210 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e97c3ac1ef91f6eace7ed6f45290fd832c3bf5cf862f0da15bf0c4efd59d3b7
4
- data.tar.gz: f33b1fd96f3e2c505e47e23cea1a3244bb50f345382f44ad52f88ef409008c77
3
+ metadata.gz: 829a0ce02dc7dbe02fb75f5f3324b001240ae4a742de842c95067291481b7b85
4
+ data.tar.gz: d258273d013a710125000c07a3cfad654e22a81fe300c222e0d34b8fd805e1ff
5
5
  SHA512:
6
- metadata.gz: a933862a2f9d59f2efcbdeb3c67c7fb9d0f1b87467a56ed8e8a38d91954a79acbbde5a997b111161444128a891f25d272c6941bf65e688415875147748b427a3
7
- data.tar.gz: 17cc9ec2838a396877c30da3201bdf4c9641d13a918555bc2e46f0ec6531983802377a079d529f063ee27ae4d9ef27e47ae734e82eecbdfcf27e6c4cf93b85d1
6
+ metadata.gz: 30b76872939a39ca16c17f130db788c4aa55cadd7fd2d1ee07e28e1f0daba2d406ff431472f2e666bbe20083703dfc0e78ade13af51fbc0cd2fb9a40e2c7dac7
7
+ data.tar.gz: d0c18d5c756b9318682276aac714721ae2c5c7160324880985cd3e65619086e9ca9f5498c93c65101f19e80038aae7c25bc0f44fe33c3eef2dde55c090608008
data/README.md CHANGED
@@ -39,6 +39,54 @@ CLIPS::Environment.batch_star(env, "./my-batch.bat")
39
39
  env.batch_star("./my-batch.bat")
40
40
  ```
41
41
 
42
+ ### `CLIPS::Environment.bsave`
43
+ ### `env.bsave`
44
+
45
+ Pass the path in which we'll save the binary representation of the environment constructs.
46
+
47
+ ```ruby
48
+ CLIPS::Environment.bsave(env, "./my-bsave.bin")
49
+ env.bsave("./my-bsave.bin")
50
+ ```
51
+
52
+ ### `CLIPS::Environment.bload`
53
+ ### `env.bload`
54
+
55
+ Pass the path to a binary 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.bload(env, "./my-bload.bin")
60
+ env.bload("./my-bload.bin")
61
+ ```
62
+
63
+ ### `CLIPS::Environment.bsave_facts`
64
+ ### `env.bsave_facts`
65
+
66
+ Pass the path in which we'll save the binary representation of 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.bsave(env, "./my-bsave-facts.bin")
72
+ env.bsave("./my-bsave-facts.bin")
73
+ CLIPS::Environment.bsave(env, "./my-bsave-facts.bin", :local)
74
+ env.bsave("./my-bsave-facts.bin", :local)
75
+ CLIPS::Environment.bsave(env, "./my-bsave-facts.bin", :visible)
76
+ env.bsave("./my-bsave-facts.bin", :visible)
77
+ ```
78
+
79
+ ### `CLIPS::Environment.bload_facts`
80
+ ### `env.bload_facts`
81
+
82
+ Pass the path to a binary file that will load the facts stored in the file
83
+ into the environment.
84
+
85
+ ```ruby
86
+ CLIPS::Environment.bload(env, "./my-bload-facts.bin")
87
+ env.bload("./my-bload-facts.bin")
88
+ ```
89
+
42
90
  ### `CLIPS::Environment.assert_string`
43
91
  ### `CLIPS::Environment#assert_string`
44
92
 
@@ -208,6 +256,17 @@ CLIPS::Environment::Fact.retract(fact)
208
256
  fact.retract
209
257
  ```
210
258
 
259
+ ### `CLIPS::Environment::Fact.modify`
260
+ ### `CLIPS::Environment::Fact#modify`
261
+
262
+ Modifies a non-implicit deftemplate fact from the environment.
263
+ Will not work with non-deftemplate facts!
264
+
265
+ ```ruby
266
+ CLIPS::Environment::Fact.modify(fact, bar: 123)
267
+ fact.modify(foo: "my new foo!")
268
+ ```
269
+
211
270
  ## Running the tests
212
271
 
213
272
  Simply do `rake compile` and then `rake test` in order to run the tests.
@@ -747,6 +747,107 @@ static VALUE clips_environment_static_reset(VALUE self, VALUE rbEnvironment)
747
747
  return clips_environment_reset(rbEnvironment);
748
748
  }
749
749
 
750
+ static VALUE clips_environment_bsave(VALUE self, VALUE path)
751
+ {
752
+ Environment *env;
753
+
754
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
755
+
756
+ if (Bsave(env, StringValueCStr(path))) {
757
+ return Qtrue;
758
+ } else {
759
+ return Qfalse;
760
+ }
761
+ }
762
+
763
+ static VALUE clips_environment_static_bsave(VALUE self, VALUE rbEnvironment, VALUE path)
764
+ {
765
+ return clips_environment_bsave(rbEnvironment, path);
766
+ }
767
+
768
+ static VALUE clips_environment_bload(VALUE self, VALUE path)
769
+ {
770
+ Environment *env;
771
+
772
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
773
+
774
+ if (Bload(env, StringValueCStr(path))) {
775
+ return Qtrue;
776
+ } else {
777
+ return Qfalse;
778
+ }
779
+ }
780
+
781
+ static VALUE clips_environment_static_bload(VALUE self, VALUE rbEnvironment, VALUE path)
782
+ {
783
+ return clips_environment_bload(rbEnvironment, path);
784
+ }
785
+
786
+ static VALUE clips_environment_bsave_facts(int argc, VALUE *argv, VALUE rbEnvironment) {
787
+ VALUE path, scope;
788
+ Environment *env;
789
+
790
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
791
+
792
+ rb_scan_args(argc, argv, "11", &path, &scope);
793
+ if (NIL_P(scope)) {
794
+ scope = ID2SYM(rb_intern("local"));
795
+ }
796
+
797
+ if (scope == ID2SYM(rb_intern("visible"))) {
798
+ return INT2NUM(BinarySaveFacts(env, StringValueCStr(path), VISIBLE_SAVE));
799
+ } else if (scope == ID2SYM(rb_intern("local"))) {
800
+ return INT2NUM(BinarySaveFacts(env, StringValueCStr(path), LOCAL_SAVE));
801
+ } else {
802
+ rb_warn("could not bsave_facts: unsupported scope.");
803
+ return Qnil;
804
+ }
805
+ }
806
+
807
+ static VALUE clips_environment_static_bsave_facts(int argc, VALUE *argv, VALUE klass) {
808
+ VALUE rbEnvironment, path, scope;
809
+ Environment *env;
810
+
811
+ rb_scan_args(argc, argv, "21", &rbEnvironment, &path, &scope);
812
+
813
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
814
+
815
+ if (NIL_P(scope)) {
816
+ scope = ID2SYM(rb_intern("local"));
817
+ }
818
+
819
+ int number_of_facts_saved;
820
+
821
+ if (scope == ID2SYM(rb_intern("visible"))) {
822
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), VISIBLE_SAVE);
823
+ } else if (scope == ID2SYM(rb_intern("local"))) {
824
+ number_of_facts_saved = BinarySaveFacts(env, StringValueCStr(path), LOCAL_SAVE);
825
+ } else {
826
+ rb_warn("could not bsave_facts: unsupported scope.");
827
+ return Qnil;
828
+ }
829
+ return INT2NUM(number_of_facts_saved);
830
+ }
831
+
832
+ static VALUE clips_environment_bload_facts(VALUE self, VALUE path)
833
+ {
834
+ Environment *env;
835
+
836
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
837
+ int number_of_facts_loaded = BinaryLoadFacts(env, StringValueCStr(path));
838
+
839
+ if (number_of_facts_loaded == -1) {
840
+ return Qnil;
841
+ } else {
842
+ return INT2NUM(number_of_facts_loaded);
843
+ }
844
+ }
845
+
846
+ static VALUE clips_environment_static_bload_facts(VALUE self, VALUE rbEnvironment, VALUE path)
847
+ {
848
+ return clips_environment_bload_facts(rbEnvironment, path);
849
+ }
850
+
750
851
  static VALUE clips_environment_fact_slot_names(VALUE self)
751
852
  {
752
853
  Fact *fact;
@@ -829,6 +930,103 @@ static VALUE clips_environment_fact_static_retract(VALUE self, VALUE rbFact)
829
930
  return clips_environment_fact_retract(rbFact);
830
931
  }
831
932
 
933
+ static int _clips_environment_fact_modify(VALUE key, VALUE value, VALUE args)
934
+ {
935
+ const char *cslot_name;
936
+ switch(TYPE(key))
937
+ {
938
+ case T_SYMBOL:
939
+ cslot_name = rb_id2name(SYM2ID(key));
940
+ break;
941
+ case T_STRING:
942
+ cslot_name = StringValueCStr(key);
943
+ break;
944
+ default:
945
+ rb_raise(rb_eTypeError, "Slot name must be a String or a Symbol");
946
+ return ST_CONTINUE;
947
+ }
948
+
949
+ VALUE *fm_and_env = (VALUE*)args;
950
+ FactModifier *fm = (FactModifier*) fm_and_env[0];
951
+ Environment *env = (Environment*) fm_and_env[1];
952
+ CLIPSValue cv = VALUE_to_CLIPSValue(value, env);
953
+ handle_pse_error(FMPutSlot(fm, cslot_name, &cv), cslot_name);
954
+
955
+ return ST_CONTINUE;
956
+ }
957
+
958
+ static VALUE clips_environment_fact_modify(VALUE self, VALUE hash)
959
+ {
960
+ Fact *fact;
961
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
962
+
963
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
964
+ Environment *env;
965
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
966
+
967
+ FactModifier *fm = CreateFactModifier(env, fact);
968
+ switch (FMError(env))
969
+ {
970
+ case FME_NO_ERROR:
971
+ break;
972
+ case FME_RETRACTED_ERROR:
973
+ rb_warn("Could not modify fact; fact is retracted!");
974
+ return Qnil;
975
+ case FME_IMPLIED_DEFTEMPLATE_ERROR:
976
+ rb_warn("Could not modify fact; cannot use modify to modify non-deftemplated facts!");
977
+ return Qnil;
978
+ }
979
+ void *args[2] = { (void *)fm, (void *)env };
980
+ rb_hash_foreach(hash, _clips_environment_fact_modify, (VALUE)args);
981
+ Fact *modified_fact = FMModify(fm);
982
+ FMDispose(fm);
983
+
984
+ switch (FMError(env))
985
+ {
986
+ case FME_NO_ERROR:
987
+ break;
988
+ case FME_NULL_POINTER_ERROR:
989
+ rb_warn("Could not modify fact. This might be a bug in clipsruby!");
990
+ return Qnil;
991
+ case FME_RETRACTED_ERROR:
992
+ rb_warn("Could not modify fact; fact is retracted!");
993
+ return Qnil;
994
+ case FME_COULD_NOT_MODIFY_ERROR:
995
+ rb_warn("Could not modify fact. Pattern matching of a fact or instance is already occurring.");
996
+ return Qnil;
997
+ case FME_RULE_NETWORK_ERROR:
998
+ rb_warn("Could not modify fact. An error occurs while the assertion was being processed in the rule network.");
999
+ return Qnil;
1000
+ }
1001
+
1002
+ VALUE rb_fact =
1003
+ TypedData_Wrap_Struct(CLASS_OF(self), &Fact_type, modified_fact);
1004
+
1005
+ rb_iv_set(rb_fact, "@environment", rbEnvironment);
1006
+
1007
+ return rb_fact;
1008
+ }
1009
+
1010
+ static VALUE clips_environment_fact_static_modify(VALUE self, VALUE rbFact, VALUE hash)
1011
+ {
1012
+ return clips_environment_fact_modify(rbFact, hash);
1013
+ }
1014
+
1015
+ static VALUE clips_environment_fact_index(VALUE self)
1016
+ {
1017
+ Fact *fact;
1018
+
1019
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
1020
+
1021
+ return INT2NUM(FactIndex(fact));
1022
+ }
1023
+
1024
+ static VALUE clips_environment_fact_static_index(VALUE self, VALUE rbFact)
1025
+ {
1026
+ return clips_environment_fact_index(rbFact);
1027
+ }
1028
+
1029
+
832
1030
  void Init_clipsruby(void)
833
1031
  {
834
1032
  VALUE rbCLIPS = rb_define_module("CLIPS");
@@ -858,6 +1056,14 @@ void Init_clipsruby(void)
858
1056
  rb_define_method(rbEnvironment, "clear", clips_environment_clear, 0);
859
1057
  rb_define_singleton_method(rbEnvironment, "reset", clips_environment_static_reset, 1);
860
1058
  rb_define_method(rbEnvironment, "reset", clips_environment_reset, 0);
1059
+ rb_define_singleton_method(rbEnvironment, "bsave", clips_environment_static_bsave, 2);
1060
+ rb_define_method(rbEnvironment, "bsave", clips_environment_bsave, 1);
1061
+ rb_define_singleton_method(rbEnvironment, "bload", clips_environment_static_bload, 2);
1062
+ rb_define_method(rbEnvironment, "bload", clips_environment_bload, 1);
1063
+ rb_define_singleton_method(rbEnvironment, "bsave_facts", clips_environment_static_bsave_facts, -1);
1064
+ rb_define_method(rbEnvironment, "bsave_facts", clips_environment_bsave_facts, -1);
1065
+ rb_define_singleton_method(rbEnvironment, "bload_facts", clips_environment_static_bload_facts, 2);
1066
+ rb_define_method(rbEnvironment, "bload_facts", clips_environment_bload_facts, 1);
861
1067
 
862
1068
  VALUE rbFact = rb_define_class_under(rbEnvironment, "Fact", rb_cObject);
863
1069
  rb_define_singleton_method(rbFact, "deftemplate_name", clips_environment_fact_static_deftemplate_name, 1);
@@ -870,6 +1076,10 @@ void Init_clipsruby(void)
870
1076
  rb_define_method(rbFact, "get_slot", clips_environment_fact_get_slot, 1);
871
1077
  rb_define_singleton_method(rbFact, "retract", clips_environment_fact_static_retract, 1);
872
1078
  rb_define_method(rbFact, "retract", clips_environment_fact_retract, 0);
1079
+ rb_define_singleton_method(rbFact, "modify", clips_environment_fact_static_modify, 2);
1080
+ rb_define_method(rbFact, "modify", clips_environment_fact_modify, 1);
1081
+ rb_define_singleton_method(rbFact, "index", clips_environment_fact_static_index, 1);
1082
+ rb_define_method(rbFact, "index", clips_environment_fact_index, 0);
873
1083
 
874
1084
  VALUE rbInstance = rb_define_class_under(rbEnvironment, "Instance", rb_cObject);
875
1085
  }
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.10
4
+ version: 0.0.12
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