cie / rubylog

A Prolog DSL and interpreter for Ruby
MIT License
31 stars 1 forks source link

a way to save & restore assertions #3

Open wkhere opened 11 years ago

wkhere commented 11 years ago

Hi! This is very interesting stuff.

Is there any way for saving all the assertions in a current context and then restoring them? Maybe it could be exposed as some api?

cheers

cie commented 11 years ago

Hi,

First of all, the concept of "assertions in a context" does not exist since Rubylog 2.0. Assertions are not attached to the context in any way, but assertions are attached to predicates and predicates are attached to classes via closures of functor methods. For example, this call:

predicate_for Integer, ".less_than()"

creates a predicate and stores it in the closure of three new methods, Integer#less_than, Integer#less_than? and Integer#less_than! . You can get the predicate object with:

A.less_than(B).predicate

It is an instance of Rubylog::Procedure, which is a subclass of Rubylog::Predicate. A Rubylog::Procedure is a proxy object for an Array, and it contains the assertions, which are instances of Rubylog::Rule:

A.less_than(B).if {A<B}
A.less_than(B).predicate[0] # => A.less_than(B).if(#<Proc:...>)
A.less_than(B).predicate[0].class # => Rubylog::Rule
A.less_than(B).predicate[0].head # => A.less_than(B)
A.less_than(B).predicate[0].body # => #<Proc:...>

So you can introspect these objects.

A planned feature for future versions is a dependency injection for predicates, so that you can use your own class for storing assertions instead of an Array (for example, a database table).

Anyway, what do you want to achieve? Maybe I could help you more if you explain it in more detail.

wkhere commented 10 years ago

Hi! Lot of time passed... forgot to say thanks for the insightful answer :)

And yes, what I'd need is to craft my own class for storing assertions. :)

Do you plan to extend the code so it's possible?

cheers,

Wojtek

cie commented 10 years ago

Hi Wojtek! You're weclome!

What kinds of rules do you want to save? Do they contain procs? Or just structures? Are they hierarchical or just contain one structure or symbol on the body side? Do they contain variables whose identity need to be stored also? What kind of database do you use? If, for example it's a document store (like Mongodb), a solution could be to export them to a JSON-compatible structure. Or, maybe to export it to Ruby code and then eval it?

Can you give some examples?