clipsruby 0.0.10 → 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.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/ext/clipsruby/clipsruby.c +85 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5181c11e5791e9e519d8324fb2b8a72843db339af9f0b0dbbd4c6afccf82c31
|
4
|
+
data.tar.gz: cd1618021fa058de8c5b05cf7b2aedd77fb0164de1b11323f708d557be1633af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79e7dc3122317674dc25526ac2957aaad06a36243ab3d611989582a1150782b2872d8662d8091191337511ecac8d51749d74106e6e26fd2e1768de829a2cc07d
|
7
|
+
data.tar.gz: 02c6ce66e34dfa331ce5b8b77055debf027a39e4ed1d802a95a557bc86eb2b11abff778725dc1fb7d3251497c254ad3249406a90f65f0860dfdce709966bcc7f
|
data/README.md
CHANGED
@@ -208,6 +208,17 @@ CLIPS::Environment::Fact.retract(fact)
|
|
208
208
|
fact.retract
|
209
209
|
```
|
210
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
|
+
|
211
222
|
## Running the tests
|
212
223
|
|
213
224
|
Simply do `rake compile` and then `rake test` in order to run the tests.
|
data/ext/clipsruby/clipsruby.c
CHANGED
@@ -829,6 +829,89 @@ static VALUE clips_environment_fact_static_retract(VALUE self, VALUE rbFact)
|
|
829
829
|
return clips_environment_fact_retract(rbFact);
|
830
830
|
}
|
831
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
|
+
|
832
915
|
void Init_clipsruby(void)
|
833
916
|
{
|
834
917
|
VALUE rbCLIPS = rb_define_module("CLIPS");
|
@@ -870,6 +953,8 @@ void Init_clipsruby(void)
|
|
870
953
|
rb_define_method(rbFact, "get_slot", clips_environment_fact_get_slot, 1);
|
871
954
|
rb_define_singleton_method(rbFact, "retract", clips_environment_fact_static_retract, 1);
|
872
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);
|
873
958
|
|
874
959
|
VALUE rbInstance = rb_define_class_under(rbEnvironment, "Instance", rb_cObject);
|
875
960
|
}
|