Use the CLIPS programming language from within Ruby
This is available as a Ruby gem:
gem install clipsruby
From there, you should be able to require "clipsruby"
in your code
and use the below API as described.
Create a new CLIPS environment in which you may define Rules, assert Facts, and instantiate Objects.
env = CLIPS.create_environment
env2 = CLIPS::Environment.new
Evaluates a passed string in the CLIPS environment and returns the results.
CLIPS::Environment._eval(env, "(find-all-facts ((?f my_deftemplate)) TRUE)")
env._eval("(find-all-facts ((?f my_deftemplate)) TRUE)")
Build the constructs in the env as defined in a string
CLIPS::Environment.build(env, "(deftemplate my_template (slot a) (slot b) (slot c))")
env.build("(defrule do-a-thing (my_template (a ?a)) => (println \"a: \" ?a))")
Adds a ruby method as a User Defined Function (udf) to the CLIPS environment
class CLIPS::Environment
def foo(a, b=2)
a + b
end
def bar(word)
"You said #{word}!"
end
end
env.add_udf(:foo)
CLIPS::Environment.add_udf(env, :bar)
Runs the rules engine, executing items on the agenda. Optionally may take the number of items to run from the agenda as an argument.
CLIPS::Environment.run(env, 1)
CLIPS::Environment.run(env)
env.run(1)
env.run
Removes constructs and data from the environment.
CLIPS::Environment.clear(env)
env.clear
Removes all facts and instances. Creates facts and instances defined in deffacts and definstances constructs. Resets the values of global variables in the specified environment.
CLIPS::Environment.reset(env)
env.reset
Pass the path to a file that will be "batched" into the environment, allowing many CLIPS commands to be entered at once.
CLIPS::Environment.batch_star(env, "./my-batch.bat")
env.batch_star("./my-batch.bat")
Pass the path in which we'll save the environment constructs.
CLIPS::Environment.save(env, "./my-save.clp")
env.save("./my-save.clp")
Pass the path to a file that will load the constructs stored in the file
into the environment. Remember to reset
the environment if you need.
CLIPS::Environment.load(env, "./my-load.clp")
env.load("./my-load.clp")
Pass the path in which we'll save the environment facts. The third argument is optional, and determines whether to store the visible or local facts. By default, it'll store local.
CLIPS::Environment.save_facts(env, "./my-save-facts.clp")
env.save_facts("./my-save-facts.clp")
CLIPS::Environment.save_facts(env, "./my-save-facts.clp", :local)
env.save_facts("./my-save-facts.clp", :local)
CLIPS::Environment.save_facts(env, "./my-save-facts.clp", :visible)
env.save_facts("./my-save-facts.clp", :visible)
Pass the path to a file that will load the facts stored in the file into the environment.
CLIPS::Environment.load_facts(env, "./my-load-facts.clp")
env.load_facts("./my-load-facts.clp")
Pass the path in which we'll save the environment instances. The third argument is optional, and determines whether to store the visible or local instances. By default, it'll store local.
CLIPS::Environment.save_instances(env, "./my-save-instances.clp")
env.save_instances("./my-save-instances.clp")
CLIPS::Environment.save_instances(env, "./my-save-instances.clp", :local)
env.save_instances("./my-save-instances.clp", :local)
CLIPS::Environment.save_instances(env, "./my-save-instances.clp", :visible)
env.save_instances("./my-save-instances.clp", :visible)
Pass the path to a file that will load the instances stored in the file into the environment.
CLIPS::Environment.load_instances(env, "./my-load-instances.clp")
env.load_instances("./my-load-instances.clp")
Pass the path in which we'll save the binary representation of the environment constructs.
CLIPS::Environment.bsave(env, "./my-bsave.bin")
env.bsave("./my-bsave.bin")
Pass the path to a binary file that will load the constructs stored in the file
into the environment. Remember to reset
the environment if you need.
CLIPS::Environment.bload(env, "./my-bload.bin")
env.bload("./my-bload.bin")
Pass the path in which we'll save the binary representation of the environment facts. The third argument is optional, and determines whether to store the visible or local facts. By default, it'll store local.
CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin")
env.bsave_facts("./my-bsave-facts.bin")
CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin", :local)
env.bsave_facts("./my-bsave-facts.bin", :local)
CLIPS::Environment.bsave_facts(env, "./my-bsave-facts.bin", :visible)
env.bsave_facts("./my-bsave-facts.bin", :visible)
Pass the path to a binary file that will load the facts stored in the file into the environment.
CLIPS::Environment.bload_facts(env, "./my-bload-facts.bin")
env.bload_facts("./my-bload-facts.bin")
Pass the path in which we'll save the binary representation of the environment instances. The third argument is optional, and determines whether to store the visible or local instances. By default, it'll store local.
CLIPS::Environment.bsave_instances(env, "./my-bsave-instances.bin")
env.bsave_instances("./my-bsave-instances.bin")
CLIPS::Environment.bsave_instances(env, "./my-bsave-instances.bin", :local)
env.bsave_instances("./my-bsave-instances.bin", :local)
CLIPS::Environment.bsave_instances(env, "./my-bsave-instances.bin", :visible)
env.bsave_instances("./my-bsave-instances.bin", :visible)
Pass the path to a binary file that will load the facts stored in the file into the environment.
CLIPS::Environment.bload_facts(env, "./my-bload-facts.bin")
env.bload_facts("./my-bload-facts.bin")
Assert a string as a Fact in the CLIPS environment.
fact = CLIPS::Environment.assert_string(env, "(foo bar)")
fact2 = env.assert_string("(bat baz)")
Asserts a Deftemplate fact into the CLIPS environment
fact = CLIPS::Environment.assert_hash(env, :my_deftemplate, a: 1, b: "asdf", c: :C)
fact2 = env.assert_hash(:my_deftemplate, d: 4.5, e: :asdf)
Make an Instance in the CLIPS environment.
The first argument can be:
- a symbol/string representing a string you'd call
make-instance
with in CLIPS code - a symbol/string representing a Defclass in the environment
- a
CLIPS::Environment::Defclass
object
The second argument can be:
- a symbol/string representing the name of the instance you'd like
- a hash holding the key/value pairs for instance slot assignment
nil
The third argument can be:
- a hash holding the key/value pairs for instance slot assignment
instance = CLIPS::Environment.make_instance(env, "(of USER)")
instance2 = env.make_instance("(of USER)")
instance3 = CLIPS::Environment.make_instance(env, :USER)
instance4 = env.make_instance(:USER)
instance5 = CLIPS::Environment.make_instance(env, env.find_defclass(:USER))
instance6 = env.make_instance(env.find_defclass(:USER))
instance7 = CLIPS::Environment.make_instance(env, env.find_defclass(:my_class), foo: "Bar!")
instance8 = env.make_instance(env.find_defclass(:my_class), foo: "Bar!")
instance9 = CLIPS::Environment.make_instance(env, env.find_defclass(:my_class), :my_instance1, foo: "Bar!")
instance10 = env.make_instance(env.find_defclass(:my_class), :my_instance2, foo: "Bar.")
instance11 = CLIPS::Environment.make_instance(env, env.find_defclass(:my_class), nil, foo: "Bar?")
instance12 = env.make_instance(env.find_defclass(:my_class), nil, foo: "Bar...")
A light wrapper around the CLIPS find-fact function. Accepts a fact set template and query, returns the first Facts in the environment that match as Ruby objects.
CLIPS::Environment.find_fact(env, "(?f my_deftemplate)")
env.find_fact("(?f my_deftemplate)")
CLIPS::Environment.find_fact(env, "(?f my_deftemplate)", "(eq ?f:b \"asdf\")")
env.find_fact("(?f my_deftemplate)", "(= ?f:a 1)")
Return an array of Facts in the environment. Pass an argument of a symbol, string, or Defmodule object in order to only get Facts with deftemplates in that Defmodule. If you do not, it will return all Facts in all modules.
CLIPS::Environment.get_fact_list
env.get_fact_list
CLIPS::Environment.get_fact_list(:MAIN)
env.get_fact_list(:MAIN)
CLIPS::Environment.get_fact_list(defmodule)
env.get_fact_list(defmodule)
A light wrapper around the CLIPS find-all-facts function. Accepts a fact set template and query, returns Facts in the environment that match as Ruby objects.
CLIPS::Environment.find_all_facts(env, "(?f my_deftemplate)")
env.find_all_facts("(?f my_deftemplate)")
CLIPS::Environment.find_all_facts(env, "(?f my_deftemplate)", "(eq ?f:b \"asdf\")")
env.find_all_facts("(?f my_deftemplate)", "(= ?f:a 1)")
Print all Facts defined in the CLIPS environment
CLIPS::Environment.facts(env)
env.facts
Get the current module in the CLIPS environment
CLIPS::Environment.get_current_module(env)
env.get_current_module
Set the current module in the CLIPS environment
CLIPS::Environment.set_current_module(env, defmodule)
env.set_current_module(defmodule)
Finds a defrule by name and returns a CLIPS::Environment::Defrule object
CLIPS::Environment.find_defrule(:a)
env.find_defrule("foo")
Finds an instance by name and returns a CLIPS::Environment::Instance object.
Can take an optional second and third argument for Defmodule or Demodule name
and a boolean that represents whether or not the instance should be searched for
in imported modules.
If the second argument is nil
, find_instance
will search the current module only.
CLIPS::Environment.find_definstance(:a)
env.find_definstance("foo")
CLIPS::Environment.find_definstance(:a, :my_module)
env.find_definstance("foo", "my_module")
CLIPS::Environment.find_definstance(:a, :my_module, true)
env.find_definstance("foo", nil, true)
Finds a defmodule by name and returns a CLIPS::Environment::Defmodule object
CLIPS::Environment.find_defmodule(:a)
env.find_defmodule("foo")
Return an array of Activation objects in the current demodule.
CLIPS::Environment.get_activation_list
env.get_activation_list
Return an array of Defclass names as symbols in the environment. Pass an argument of a symbol, string, or Defmodule object in order to only get Defclasss in that Defmodule. If you do not, it will return all Defclass names in all modules.
CLIPS::Environment.get_defclass_list(env)
env.get_defclass_list
CLIPS::Environment.get_defclass_list(env, :MAIN)
env.get_defclass_list(:MAIN)
CLIPS::Environment.get_defclass_list(env, defmodule)
env.get_defclass_list(defmodule)
Return an array of Instances in the environment. Pass an argument of a
symbol, string, or Defclass object in order to only get Instances
in that Defclass. If you do not, it will return all Instances of all Defclasses.
If you do, you may also pass true
as the second argument
to include instances of subclasses of this Defclass.
CLIPS::Environment.get_instance_list(env)
env.get_instance_list
CLIPS::Environment.get_instance_list(env, :foo_class)
env.get_instance_list(:"my-class")
CLIPS::Environment.get_instance_list(env, defclass)
env.get_instance_list(defclass)
CLIPS::Environment.get_instance_list(env, defclass, true)
env.get_instance_list(defclass, true)
Return an array of Deftemplate names as symbols in the environment. Pass an argument of a symbol, string, or Defmodule object in order to only get Deftemplates in that Defmodule. If you do not, it will return all Deftemplate names in all modules.
CLIPS::Environment.get_deftemplate_list(env)
env.get_deftemplate_list
CLIPS::Environment.get_deftemplate_list(env, :MAIN)
env.get_deftemplate_list(:MAIN)
CLIPS::Environment.get_deftemplate_list(env, defmodule)
env.get_deftemplate_list(defmodule)
Return an array of Defrule names as symbols in the environment. Pass an argument of a symbol, string, or Defmodule object in order to only get Defrules in that Defmodule. If you do not, it will return all Defrule names in all modules.
CLIPS::Environment.get_defrule_list(env)
env.get_defrule_list
CLIPS::Environment.get_defrule_list(env, :MAIN)
env.get_defrule_list(:MAIN)
CLIPS::Environment.get_defrule_list(env, defmodule)
env.get_defrule_list(defmodule)
Return an array of Defmodule names as symbols in the environment.
CLIPS::Environment.get_defmodule_list(env)
env.get_defmodule_list
Finds a deftemplate by name and returns a CLIPS::Environment::Deftemplate object
CLIPS::Environment.find_deftemplate(:a)
env.find_deftemplate("foo")
Finds a defclass by name and returns a CLIPS::Environment::Defclass object
CLIPS::Environment.find_defclass(:a)
env.find_defclass("foo")
"Watch" or "Unwatch" a specific thing that happens in the CLIPS environment.
There are several things that may be watched (or unwatched) such that CLIPS will
report debugging information to STDOUT
. This gem provides two ways to watch
or unwatch a debug item: either pass the watch item as a symbol to watch
or
unwatch
, or use the corresponding watch_foo
or unwatch_foo
methods where
foo
is replaced by the watch item (as listed):
:all
:facts
:instances
:slots
:rules
:activations
:messages
:message_handlers
:generic_functions
:methods
:deffunctions
:compilations
:statistics
:globals
:focus
CLIPS::Environment.watch_facts
env.watch_statistics
CLIPS::Environment.unwatch(:facts)
env.unwatch(:statistics)
Returns a pretty printed string representation of the Fact
CLIPS::Environment::Fact.pp_form(fact)
fact.pp_form
Returns a boolean representing whether or not a Fact has been retracted
CLIPS::Environment::Fact.existp(fact)
fact.existp
Returns a hash representing the fact slot names and their values
CLIPS::Environment::Fact.to_h(fact)
fact.to_h
Returns the index of the fact in working memory
CLIPS::Environment::Fact.index(fact)
fact.index
Returns the Deftemplate object for a fact
CLIPS::Environment::Fact.deftemplate(fact)
fact.deftemplate
Returns the name of the Deftemplate for a fact as a symbol
CLIPS::Environment::Fact.deftemplate_name(fact)
fact.deftemplate_name
Returns an array representing the slot names of a fact
CLIPS::Environment::Fact.slot_names(fact)
fact.slot_names
Gets the value stored in the specified slot of a fact. Can either pass a string or symbol for the slot name argument.
CLIPS::Environment::Fact.get_slot(fact, 'bar')
fact.get_slot(:foo)
Retracts a fact from the environment.
CLIPS::Environment::Fact.retract(fact)
fact.retract
Modifies a non-implicit deftemplate fact from the environment. Will not work with non-deftemplate facts!
CLIPS::Environment::Fact.modify(fact, bar: 123)
fact.modify(foo: "my new foo!")
Unmakes an instance in the environment, deleting it.
CLIPS::Environment::Instance.unmake(instance)
instance.unmake
Deletes an instance in the environment.
CLIPS::Environment::Instance.delete(instance)
instance.delete
Returns the name of an Instance as a symbol
CLIPS::Environment::Instance.name(instance)
instance.name
Returns a pretty printed string representation of an Instance
CLIPS::Environment::Instance.pp_form(instance)
instance.pp_form
Returns the Defclass of an Instance
CLIPS::Environment::Instance.defclass(instance)
instance.defclass
Returns a hash representing the instance slot names and their values.
Pass a true
as an argument to get inhereted slots as well.
CLIPS::Environment::Instance.to_h(instance)
instance.to_h
CLIPS::Environment::Instance.to_h(instance, true)
instance.to_h(true)
Returns the name of an Defclass as a symbol
CLIPS::Environment::Defclass.name(defclass)
defclass.name
Returns a pretty printed string representation of an Defclass
CLIPS::Environment::Defclass.pp_form(defclass)
defclass.pp_form
Returns an array representing the slot names of a Defclass.
Pass true
as an argument to get inherited slots, as well.
CLIPS::Environment::Defclass.slots(defclass)
defclass.slots
CLIPS::Environment::Defclass.slots(defclass, true)
defclass.slots(true)
Returns the name of the defmodule in which the defclass is defined
CLIPS::Environment::Defclass.defmodule_name(defclass)
defclass.defmodule_name
Returns the defmodule in which the defclass is defined
CLIPS::Environment::Defclass.defmodule(defclass)
defclass.defmodule
Return an array of Instances of the Defclass.
Pass true
as an argument to include instances of subclasses
of this Defclass.
CLIPS::Environment::Defclass.get_instance_list(defclass)
CLIPS::Environment::Defclass.get_instance_list(defclass, true)
defclass.get_instance_list
defclass.get_instance_list(true)
Return an array of superclasses of the Defclass.
Pass true
as an argument to include inherited superclasses.
CLIPS::Environment::Defclass.superclasses(defclass)
CLIPS::Environment::Defclass.superclasses(defclass, true)
defclass.superclasses
defclass.superclasses(true)
Return an array of subclasses of the Defclass.
Pass true
as an argument to include inherited subclasses.
CLIPS::Environment::Defclass.subclasses(defclass)
CLIPS::Environment::Defclass.subclasses(defclass, true)
defclass.subclasses
defclass.subclasses(true)
Returns the name of a defrule as a symbol
CLIPS::Environment::Defrule.name(defrule)
defrule.name
Returns the name of the defmodule in which the defrule is defined
CLIPS::Environment::Defrule.defmodule_name(defrule)
defrule.defmodule_name
Returns the defmodule in which the defrule is defined
CLIPS::Environment::Defrule.defmodule(defrule)
defrule.defmodule
Returns a boolean whether the Defrule is deletable or not
CLIPS::Environment::Defrule.is_deletable(defrule)
defrule.is_deletable
Returns a pretty printed string representation of the Defrule
CLIPS::Environment::Defrule.pp_form(defrule)
defrule.pp_form
Returns whether or not the rule has a breakpoint set
CLIPS::Environment::Defrule.has_breakpoint(defrule)
defrule.has_breakpoint
Sets a breakpoint on a rule
CLIPS::Environment::Defrule.set_break(defrule)
defrule.set_break
Returns whether or not it was able to remove a breakpoint from a rule
CLIPS::Environment::Defrule.remove_break(defrule)
defrule.remove_break
Returns the salience of a defrule
CLIPS::Environment::Defrule.salience(defrule)
defrule.salience
Returns an array holding the number of pattern matches, partial matches,
and activations for a given rule. Pass a first argument of
:verbose
, :succinct
, or :terse
to determine the amount of information
to print to STDOUT
(:verbose
by default).
CLIPS::Environment::Defrule.matches(defrule, :succinct)
defrule.matches(:terse)
[pattern, partial, activations] = defrule.matches
Returns the name of a defmodule as a symbol
CLIPS::Environment::Defmodule.name(defmodule)
defmodule.name
Returns a pretty printed string representation of the Defmodule
CLIPS::Environment::Defmodule.pp_form(defmodule)
defmodule.pp_form
Sets the Defmodule as the current module of the environment
CLIPS::Environment::Defmodule.set_current(defmodule)
defmodule.set_current
Return an array of Facts with deftemplates in the Defmodule
CLIPS::Environment::Defmodule.get_fact_list(defmodule)
defmodule.get_fact_list
Finds an instance by name and returns a CLIPS::Environment::Instance object. Can take an optional second argument of a boolean that represents whether or not the instance should be searched for in imported modules.
CLIPS::Environment::Defmodule.find_instance(:a)
defmodule.find_instance("foo")
CLIPS::Environment::Defmodule.find_instance(:a, true)
defmodule.find_instance("foo", true)
Return an array of Defclass names as symbols in the Defmodule
CLIPS::Environment::Defmodule.get_defclass_list(defmodule)
defmodule.get_defclass_list
CLIPS::Environment::Defmodule.get_deftemplate_list
/ CLIPS::Environment::Defmodule#get_deftemplate_list
Return an array of Deftemplate names as symbols in the Defmodule
CLIPS::Environment::Defmodule.get_deftemplate_list(defmodule)
defmodule.get_deftemplate_list
Return an array of Defrule names as symbols in the Defmodule
CLIPS::Environment::Defmodule.get_defrule_list(defmodule)
defmodule.get_defrule_list
Returns the name of a deftemplate as a symbol
CLIPS::Environment::Deftemplate.name(deftemplate)
deftemplate.name
Returns a pretty printed string representation of the Deftemplate
CLIPS::Environment::Deftemplate.pp_form(deftemplate)
deftemplate.pp_form
Asserts a hash in the clips environment that a Deftemplate is defined in. Returns the Fact object that was just asserted
CLIPS::Environment::Deftemplate.assert_hash(deftemplate, foo: :bar)
deftemplate.assert_hash(foo: :bar)
Returns the name of the defmodule in which the deftemplate is defined
CLIPS::Environment::Deftemplate.defmodule_name(deftemplate)
deftemplate.defmodule_name
Returns the defmodule in which the deftemplate is defined
CLIPS::Environment::Deftemplate.defmodule(deftemplate)
deftemplate.defmodule
Returns the slot names of a deftemplate as symbols
CLIPS::Environment::Deftemplate.slot_names(deftemplate)
deftemplate.slot_names
Returns a boolean whether the Deftemplate is deletable or not
CLIPS::Environment::Deftemplate.is_deletable(deftemplate)
deftemplate.is_deletable
Returns a boolean whether the Deftemplate is implied or not. If the fact is an ordered fact, the Deftemplate is implied. Otherwise, the Deftemplate is considered explicitly defined.
CLIPS::Environment::Deftemplate.is_implied(deftemplate)
deftemplate.is_implied
CLIPS::Environment::Deftemplate.slot_allowed_values
/ CLIPS::Environment::Deftemplate#slot_allowed_values
Returns the allowed values for the slot of a deftemplate as symbols
CLIPS::Environment::Deftemplate.slot_allowed_values(deftemplate, :foo)
deftemplate.slot_allowed_values('bar')
CLIPS::Environment::Deftemplate.slot_default_value
/ CLIPS::Environment::Deftemplate#slot_default_value
Returns the default value(s) for the slot of a deftemplate as symbols
CLIPS::Environment::Deftemplate.slot_default_value(deftemplate, :foo)
deftemplate.slot_default_value('bar')
Returns the slot type(s) for the named slot of a deftemplate as symbols. Possible types are as follows:
:SYMBOL
:STRING
:LEXEME
:INTEGER
:FLOAT
:NUMBER
:"INSTANCE-NAME"
:"INSTANCE-ADDRESS"
:INSTANCE
:"EXTERNAL-ADDRESS"
:"FACT-ADDRESS"
CLIPS::Environment::Deftemplate.slot_types(deftemplate, :foo)
deftemplate.slot_types('bar')
Returns the range of a named slot of a deftemplate as integers or symbols (in case of infinity).
CLIPS::Environment::Deftemplate.slot_range(deftemplate, :foo)
deftemplate.slot_range('bar')
Returns the cardinality of a named slot of a deftemplate as integers or symbols (in case of infinity).
CLIPS::Environment::Deftemplate.slot_cardinality(deftemplate, :foo)
deftemplate.slot_cardinality('bar')
Returns a boolean for whether or not the slot with a given name exists on the Deftemplate.
CLIPS::Environment::Deftemplate.slot_existp(deftemplate, :foo)
deftemplate.slot_existp('bar')
Returns a boolean for whether or not the slot with a given name on the Deftemplate is a single slot.
CLIPS::Environment::Deftemplate.slot_singlep(deftemplate, :foo)
deftemplate.slot_singlep('bar')
Returns a boolean for whether or not the slot with a given name on the Deftemplate is a multislot.
CLIPS::Environment::Deftemplate.slot_multip(deftemplate, :foo)
deftemplate.slot_multip('bar')
Returns a symbol representing the type of default value a slot has on the Deftemplate. Possible return values are as follows:
:NO_DEFAULT
:STATIC_DEFAULT
:DYNAMIC_DEFAULT
CLIPS::Environment::Deftemplate.slot_defaultp(deftemplate, :foo)
deftemplate.slot_defaultp('bar')
Finds a deffacts by name and returns a CLIPS::Environment::Deffacts object
CLIPS::Environment.find_deffacts(:a)
env.find_deffacts("foo")
Returns the name of a deffacts as a symbol
CLIPS::Environment::Deffacts.name(deffacts)
deffacts.name
Returns a pretty printed string representation of the Deffacts
CLIPS::Environment::Deffacts.pp_form(deffacts)
deffacts.pp_form
Returns the name of a defrule that triggered this activation.
CLIPS::Environment::Activation.defrule_name
activation.defrule_name
Simply do rake compile
and then rake test
in order to run the tests.