Rules for varying length of PYKE satements

81 views
Skip to first unread message

sam helsen

unread,
May 5, 2016, 9:03:21 AM5/5/16
to PyKE
Hi,

I am very new to PYKE,

I would like to use PYKE to simplify my work of 700 lines of code and 4 json files. I have a json file with key and value where the value is a list of tuples with id and score.

For example,

{1 : [ (2, 0.9),(3, 0.8) ],  2 : [ (1, 0.8),(3, 0.9),(4, 0.5) ],  3: [ (4, 0.85) ],  4: [ ]  }

The Keys and Values of the json file will have a relationship of parent and child. I would like to structure the json key_val pair it into PYKE statements. Is there a good way to do it.

The following structure can be taken, However I am not sure if proper rules can be written upon them.

parent_child(1, 2, 90, 3, 80)
parent_child(2, 1, 80, 3, 90, 4, 50)
parent_child(3, 4, 85)
parent_child(4)

I'd really appreciate any help.

Thank you

Bruce Frederiksen

unread,
May 5, 2016, 9:47:52 AM5/5/16
to py...@googlegroups.com
Two things.

First, if you structure the facts as you suggest, it will be difficult to match them.  A rule trying to match:

parent_child($parent, $child, $score)

would only match your third example, and not the other 3 because of the different number of arguments.

You might consider making each parent-child relationship a separate fact.  Thus your examples would become:

parent_child(1, 2, 90)
parent_child(1, 3, 80)
parent_child(2, 1, 80)
parent_child(2, 3, 90)
parent_child(2, 4, 50)
parent_child(3, 4, 85)

Then the above rule would be able to match all of the facts.

But note the omission of parent 4.  If you want to know what all of the parents are, you could also have parent facts that simply identify the parents (or whatever these things are):

parent(1)
parent(2)
parent(3)
parent(4)


Second, you have two options about how to get this information into pyke.

The easiest would be write a function to read the json files and asserting the information in a fact base with either add_case_specific_fact or add_universal_fact.

Use add_case_specific_fact if you want to possibly assert different facts for each run of the rule engine.  In that case, you would have to run your function each time before running the run engine.  This might be useful, for example, if different runs of the rule engine should use different json files.

On the other hand, if you use add_universal_fact, you would only need to run your function once, and then those facts would be available to all runs of the rule engine.

The other way to get the json information into pyke would be to develop your own knowledge base.  Pyke has been written to allow you to develop your own kind of knowledge bases, as is mentioned in the third bullet here.  This would be more work, and may not perform as well since pyke does a good job indexing the facts in a fact base.  You would need to consult the pyke source code to see how to do this.

-Bruce


--
You received this message because you are subscribed to the Google Groups "PyKE" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyke+uns...@googlegroups.com.
To post to this group, send email to py...@googlegroups.com.
Visit this group at https://groups.google.com/group/pyke.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages