clipsruby 0.0.9 → 0.0.11

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 +75 -0
  3. data/ext/clipsruby/clipsruby.c +222 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ede586e45827a7552e087b61a6f454a0d5c0c779e9d94b34deb2b13d1c7b5846
4
- data.tar.gz: d46fdf1c8b9efd85600c976d414d4919ad8cd3c27171d15685f5dc059dd7c1e3
3
+ metadata.gz: b5181c11e5791e9e519d8324fb2b8a72843db339af9f0b0dbbd4c6afccf82c31
4
+ data.tar.gz: cd1618021fa058de8c5b05cf7b2aedd77fb0164de1b11323f708d557be1633af
5
5
  SHA512:
6
- metadata.gz: 7a4ee2975ebcb75e69f6dd5efd2a3c4c3d9b1c31fdd27a1e3fcbc918342d894bcae35cacdfbfd7c3ecdd3535474ccefe456dcc7ff7fb571b7254c4baf78812e1
7
- data.tar.gz: 689887738a961e056658085ef49b1aa52de1679a630db22f6ca8e5b57bd562a65e87574f4b1f5dd96a4caf957aad22311f434356d7a5ea59272674a790bd5d58
6
+ metadata.gz: 79e7dc3122317674dc25526ac2957aaad06a36243ab3d611989582a1150782b2872d8662d8091191337511ecac8d51749d74106e6e26fd2e1768de829a2cc07d
7
+ data.tar.gz: 02c6ce66e34dfa331ce5b8b77055debf027a39e4ed1d802a95a557bc86eb2b11abff778725dc1fb7d3251497c254ad3249406a90f65f0860dfdce709966bcc7f
data/README.md CHANGED
@@ -28,6 +28,17 @@ env = CLIPS.create_environment
28
28
  env2 = CLIPS::Environment.new
29
29
  ```
30
30
 
31
+ ### `CLIPS::Environment.batch_star`
32
+ ### `env.batch_star`
33
+
34
+ Pass the path to a file that will be "batched" into the environment,
35
+ allowing many CLIPS commands to be entered at once.
36
+
37
+ ```ruby
38
+ CLIPS::Environment.batch_star(env, "./my-batch.bat")
39
+ env.batch_star("./my-batch.bat")
40
+ ```
41
+
31
42
  ### `CLIPS::Environment.assert_string`
32
43
  ### `CLIPS::Environment#assert_string`
33
44
 
@@ -114,6 +125,28 @@ env.run(1)
114
125
  env.run
115
126
  ```
116
127
 
128
+ ### `CLIPS::Environment.clear`
129
+ ### `CLIPS::Environment#clear`
130
+
131
+ Removes constructs and data from the environment.
132
+
133
+ ```ruby
134
+ CLIPS::Environment.clear(env)
135
+ env.clear
136
+ ```
137
+
138
+ ### `CLIPS::Environment.reset`
139
+ ### `CLIPS::Environment#reset`
140
+
141
+ Removes all facts and instances.
142
+ Creates facts and instances defined in deffacts and definstances constructs.
143
+ Resets the values of global variables in the specified environment.
144
+
145
+ ```ruby
146
+ CLIPS::Environment.reset(env)
147
+ env.reset
148
+ ```
149
+
117
150
  ### `CLIPS::Environment.facts`
118
151
  ### `CLIPS::Environment#facts`
119
152
 
@@ -144,6 +177,48 @@ CLIPS::Environment::Fact.deftemplate_name(fact)
144
177
  fact.deftemplate_name
145
178
  ```
146
179
 
180
+ ### `CLIPS::Environment::Fact.slot_names`
181
+ ### `CLIPS::Environment::Fact#slot_names`
182
+
183
+ Returns an array representing the slot names of a fact
184
+
185
+ ```ruby
186
+ CLIPS::Environment::Fact.slot_names(fact)
187
+ fact.slot_names
188
+ ```
189
+
190
+ ### `CLIPS::Environment::Fact.get_slot`
191
+ ### `CLIPS::Environment::Fact#get_slot`
192
+
193
+ Gets the value stored in the specified slot of a fact.
194
+ Can either pass a string or symbol for the slot name argument.
195
+
196
+ ```ruby
197
+ CLIPS::Environment::Fact.get_slot(fact, 'bar')
198
+ fact.get_slot(:foo)
199
+ ```
200
+
201
+ ### `CLIPS::Environment::Fact.retract`
202
+ ### `CLIPS::Environment::Fact#retract`
203
+
204
+ Retracts a fact from the environment.
205
+
206
+ ```ruby
207
+ CLIPS::Environment::Fact.retract(fact)
208
+ fact.retract
209
+ ```
210
+
211
+ ### `CLIPS::Environment::Fact.modify`
212
+ ### `CLIPS::Environment::Fact#modify`
213
+
214
+ Modifies a non-implicit deftemplate fact from the environment.
215
+ Will not work with non-deftemplate facts!
216
+
217
+ ```ruby
218
+ CLIPS::Environment::Fact.modify(fact, bar: 123)
219
+ fact.modify(foo: "my new foo!")
220
+ ```
221
+
147
222
  ## Running the tests
148
223
 
149
224
  Simply do `rake compile` and then `rake test` in order to run the tests.
@@ -290,6 +290,17 @@ static VALUE clips_environment_assert_hash(VALUE self, VALUE deftemplate_name, V
290
290
  TypedData_Get_Struct(self, Environment, &Environment_type, env);
291
291
 
292
292
  FactBuilder *fb = CreateFactBuilder(env, cdeftemplate_name);
293
+ switch (FBError(env))
294
+ {
295
+ case FBE_NO_ERROR:
296
+ break;
297
+ case FBE_DEFTEMPLATE_NOT_FOUND_ERROR:
298
+ rb_warn("Could not assert fact; deftemplate not found!");
299
+ return Qnil;
300
+ case FBE_IMPLIED_DEFTEMPLATE_ERROR:
301
+ rb_warn("Could not assert fact; cannot use assert_hash to create non-deftemplated facts!");
302
+ return Qnil;
303
+ }
293
304
  void *args[2] = { (void *)fb, (void *)env };
294
305
  rb_hash_foreach(hash, _clips_environment_assert_hash, (VALUE)args);
295
306
  Fact *fact = FBAssert(fb);
@@ -702,6 +713,205 @@ static VALUE clips_environment_static_batch_star(VALUE self, VALUE rbEnvironment
702
713
  return clips_environment_batch_star(rbEnvironment, path);
703
714
  }
704
715
 
716
+ static VALUE clips_environment_clear(VALUE self)
717
+ {
718
+ Environment *env;
719
+
720
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
721
+
722
+ if (Clear(env)) {
723
+ return Qtrue;
724
+ } else {
725
+ return Qfalse;
726
+ }
727
+ }
728
+
729
+ static VALUE clips_environment_static_clear(VALUE self, VALUE rbEnvironment)
730
+ {
731
+ return clips_environment_clear(rbEnvironment);
732
+ }
733
+
734
+ static VALUE clips_environment_reset(VALUE self)
735
+ {
736
+ Environment *env;
737
+
738
+ TypedData_Get_Struct(self, Environment, &Environment_type, env);
739
+
740
+ Reset(env);
741
+
742
+ return Qnil;
743
+ }
744
+
745
+ static VALUE clips_environment_static_reset(VALUE self, VALUE rbEnvironment)
746
+ {
747
+ return clips_environment_reset(rbEnvironment);
748
+ }
749
+
750
+ static VALUE clips_environment_fact_slot_names(VALUE self)
751
+ {
752
+ Fact *fact;
753
+ CLIPSValue slot_names;
754
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
755
+
756
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
757
+
758
+ FactSlotNames(fact, &slot_names);
759
+
760
+ VALUE array;
761
+
762
+ CLIPSValue_to_VALUE(&slot_names, &array, &rbEnvironment);
763
+
764
+ return array;
765
+ }
766
+
767
+ static VALUE clips_environment_fact_static_slot_names(VALUE self, VALUE rbFact)
768
+ {
769
+ return clips_environment_fact_slot_names(rbFact);
770
+ }
771
+
772
+ static VALUE clips_environment_fact_get_slot(VALUE self, VALUE slot_name)
773
+ {
774
+ Fact *fact;
775
+ CLIPSValue slot_value;
776
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
777
+
778
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
779
+
780
+ switch (TYPE(slot_name)) {
781
+ case T_STRING:
782
+ GetFactSlot(fact, StringValueCStr(slot_name), &slot_value);
783
+ break;
784
+ case T_SYMBOL:
785
+ GetFactSlot(fact, rb_id2name(SYM2ID(slot_name)), &slot_value);
786
+ break;
787
+ default:
788
+ rb_warn("slot name must be string or symbol in call to get_slot!");
789
+ return Qnil;
790
+ }
791
+
792
+ VALUE out;
793
+
794
+ CLIPSValue_to_VALUE(&slot_value, &out, &rbEnvironment);
795
+
796
+ return out;
797
+ }
798
+
799
+ static VALUE clips_environment_fact_static_get_slot(VALUE self, VALUE rbFact, VALUE slot_name)
800
+ {
801
+ return clips_environment_fact_get_slot(rbFact, slot_name);
802
+ }
803
+
804
+ static VALUE clips_environment_fact_retract(VALUE self)
805
+ {
806
+ Fact *fact;
807
+
808
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
809
+
810
+ switch (Retract(fact)) {
811
+ case RE_NO_ERROR:
812
+ break;
813
+ case RE_NULL_POINTER_ERROR:
814
+ rb_warn("could not retract fact; null pointer error. This could be a bug in clipsruby!");
815
+ return Qfalse;
816
+ case RE_COULD_NOT_RETRACT_ERROR:
817
+ rb_warn("could not retract fact! is pattern matching currently happening with this fact?");
818
+ return Qfalse;
819
+ case RE_RULE_NETWORK_ERROR:
820
+ rb_warn("could not retract fact! An error occurs while the retraction was being processed in the rule network");
821
+ return Qfalse;
822
+ }
823
+
824
+ return Qtrue;
825
+ }
826
+
827
+ static VALUE clips_environment_fact_static_retract(VALUE self, VALUE rbFact)
828
+ {
829
+ return clips_environment_fact_retract(rbFact);
830
+ }
831
+
832
+ static int _clips_environment_fact_modify(VALUE key, VALUE value, VALUE args)
833
+ {
834
+ const char *cslot_name;
835
+ switch(TYPE(key))
836
+ {
837
+ case T_SYMBOL:
838
+ cslot_name = rb_id2name(SYM2ID(key));
839
+ break;
840
+ case T_STRING:
841
+ cslot_name = StringValueCStr(key);
842
+ break;
843
+ default:
844
+ rb_raise(rb_eTypeError, "Slot name must be a String or a Symbol");
845
+ return ST_CONTINUE;
846
+ }
847
+
848
+ VALUE *fm_and_env = (VALUE*)args;
849
+ FactModifier *fm = (FactModifier*) fm_and_env[0];
850
+ Environment *env = (Environment*) fm_and_env[1];
851
+ CLIPSValue cv = VALUE_to_CLIPSValue(value, env);
852
+ handle_pse_error(FMPutSlot(fm, cslot_name, &cv), cslot_name);
853
+
854
+ return ST_CONTINUE;
855
+ }
856
+
857
+ static VALUE clips_environment_fact_modify(VALUE self, VALUE hash)
858
+ {
859
+ Fact *fact;
860
+ TypedData_Get_Struct(self, Fact, &Fact_type, fact);
861
+
862
+ VALUE rbEnvironment = rb_iv_get(self, "@environment");
863
+ Environment *env;
864
+ TypedData_Get_Struct(rbEnvironment, Environment, &Environment_type, env);
865
+
866
+ FactModifier *fm = CreateFactModifier(env, fact);
867
+ switch (FMError(env))
868
+ {
869
+ case FME_NO_ERROR:
870
+ break;
871
+ case FME_RETRACTED_ERROR:
872
+ rb_warn("Could not modify fact; fact is retracted!");
873
+ return Qnil;
874
+ case FME_IMPLIED_DEFTEMPLATE_ERROR:
875
+ rb_warn("Could not modify fact; cannot use modify to modify non-deftemplated facts!");
876
+ return Qnil;
877
+ }
878
+ void *args[2] = { (void *)fm, (void *)env };
879
+ rb_hash_foreach(hash, _clips_environment_fact_modify, (VALUE)args);
880
+ Fact *modified_fact = FMModify(fm);
881
+ FMDispose(fm);
882
+
883
+ switch (FMError(env))
884
+ {
885
+ case FME_NO_ERROR:
886
+ break;
887
+ case FME_NULL_POINTER_ERROR:
888
+ rb_warn("Could not modify fact. This might be a bug in clipsruby!");
889
+ return Qnil;
890
+ case FME_RETRACTED_ERROR:
891
+ rb_warn("Could not modify fact; fact is retracted!");
892
+ return Qnil;
893
+ case FME_COULD_NOT_MODIFY_ERROR:
894
+ rb_warn("Could not modify fact. Pattern matching of a fact or instance is already occurring.");
895
+ return Qnil;
896
+ case FME_RULE_NETWORK_ERROR:
897
+ rb_warn("Could not modify fact. An error occurs while the assertion was being processed in the rule network.");
898
+ return Qnil;
899
+ }
900
+
901
+ VALUE rb_fact =
902
+ TypedData_Wrap_Struct(CLASS_OF(self), &Fact_type, modified_fact);
903
+
904
+ rb_iv_set(rb_fact, "@environment", rbEnvironment);
905
+
906
+ return rb_fact;
907
+ }
908
+
909
+ static VALUE clips_environment_fact_static_modify(VALUE self, VALUE rbFact, VALUE hash)
910
+ {
911
+ return clips_environment_fact_modify(rbFact, hash);
912
+ }
913
+
914
+
705
915
  void Init_clipsruby(void)
706
916
  {
707
917
  VALUE rbCLIPS = rb_define_module("CLIPS");
@@ -727,12 +937,24 @@ void Init_clipsruby(void)
727
937
  rb_define_method(rbEnvironment, "find_all_facts", clips_environment_find_all_facts, -1);
728
938
  rb_define_singleton_method(rbEnvironment, "batch_star", clips_environment_static_batch_star, 2);
729
939
  rb_define_method(rbEnvironment, "batch_star", clips_environment_batch_star, 1);
940
+ rb_define_singleton_method(rbEnvironment, "clear", clips_environment_static_clear, 1);
941
+ rb_define_method(rbEnvironment, "clear", clips_environment_clear, 0);
942
+ rb_define_singleton_method(rbEnvironment, "reset", clips_environment_static_reset, 1);
943
+ rb_define_method(rbEnvironment, "reset", clips_environment_reset, 0);
730
944
 
731
945
  VALUE rbFact = rb_define_class_under(rbEnvironment, "Fact", rb_cObject);
732
946
  rb_define_singleton_method(rbFact, "deftemplate_name", clips_environment_fact_static_deftemplate_name, 1);
733
947
  rb_define_method(rbFact, "deftemplate_name", clips_environment_fact_deftemplate_name, 0);
734
948
  rb_define_singleton_method(rbFact, "to_h", clips_environment_fact_static_to_h, 1);
735
949
  rb_define_method(rbFact, "to_h", clips_environment_fact_to_h, 0);
950
+ rb_define_singleton_method(rbFact, "slot_names", clips_environment_fact_static_slot_names, 1);
951
+ rb_define_method(rbFact, "slot_names", clips_environment_fact_slot_names, 0);
952
+ rb_define_singleton_method(rbFact, "get_slot", clips_environment_fact_static_get_slot, 2);
953
+ rb_define_method(rbFact, "get_slot", clips_environment_fact_get_slot, 1);
954
+ rb_define_singleton_method(rbFact, "retract", clips_environment_fact_static_retract, 1);
955
+ rb_define_method(rbFact, "retract", clips_environment_fact_retract, 0);
956
+ rb_define_singleton_method(rbFact, "modify", clips_environment_fact_static_modify, 2);
957
+ rb_define_method(rbFact, "modify", clips_environment_fact_modify, 1);
736
958
 
737
959
  VALUE rbInstance = rb_define_class_under(rbEnvironment, "Instance", rb_cObject);
738
960
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipsruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Johnston