Using slot-insert$ in Jess Tab /Protege - protege

The following code in the Jess Tab continuously inserts the same instance into a multivalued slot.
(defrule satisfactibleEstudio
(object (is-a Estudio)
(OBJECT ?user)
(nombre ?name)
(preferencias_minimas ?pref))
(object (is-a Chalet)
(OBJECT ?viv)
(precio ?p&: (and
(>= ?p (slot-get ?pref precio_minimo))
(<= ?p (slot-get ?pref precio_maximo))))
(tamanno ?t&: (and
(>= ?t (slot-get ?pref tamanno_minimo))
(<= ?t (slot-get ?pref tamanno_maximo))))
(componentes $?comp&: (>= (get-dorms $?comp) (slot-get ?pref dormitorios))))
=>
(slot-insert$ ?user satisfactibles 1 ?viv))
However, if I replace the slot-insert$ function by a printout it works as intended. What am I doing wrong?
Update:
So apparently it's continuously inserting the same instance into the slot, however with a printout it only prints once per match.

See the Jess manual about modifying a fact on the right hand side:
If a rule includes the declaration (declare (no-loop TRUE)), then nothing that a rule does while firing can cause the immediate reactivation of the same rule; i.e., if a no-loop rule matches a fact, and the rule modifies that same fact such that the fact still matches, the rule will not be put back on the agenda, avoiding an infinite loop.

Related

Core Data predicate for any row of the associated entity

In the following I will describe a simplification of my Core Data schema that I am pretty sure to be equivalent to my real situation.
I have two entity First and Second linked by a many-to-many relationship r.
Second has got two Boolean attribute, let us call it take and you_should_not_take.
I want to make a query that select a row of First iff it exits an associated row of Second for with take = true.
I have tried this predicate:
NSPredicate(format: "ANY (%K == YES && %K == NO)", "r.take","r.you_should_not_take")
but Core Data gives me the following error: "[General] Unable to parse the format string".
Maybe I have to use this:
NSPredicate(format: "((ANY %K == YES) && (ANY %K == NO))", "r.take","r.you_should_not_take")
but I am afraid that this last query will select a row of First iff it exists a row x of Second for which x.take == YES && it exists a row y if Second for which y.you_should_not_take == NO, with no guarantee that x == y.
An SQL query would be very simple to be made, but I am not so experienced with Core Data, so while I will try some more queries (and I will test if the second query does what I think it does) I have also asked here hoping the answer would be as simple as in SQL.
To select all First objects which are related to (at least one) Second object for which take==true and you_should_not_take==false you have
to use a SUBQUERY. Something like (untested):
SUBQUERY(r, $x, $x.take == YES AND $x.you_should_not_take == NO).#count > 0

How to call javascript in a node.js module from clojurescript?

I'm hoping to use the Coinbase Bitcoin Exchange node.js API from inside ClojureScript.
The goal is to replicate the first javascript example on the page:
var CoinbaseExchange = require('coinbase-exchange');
var publicClient = new CoinbaseExchange.PublicClient();
But in my following code I've started by trying to access PublicClient at all:
(ns plutus.core
(:require
[cljs.nodejs :as nodejs]))
(def coinb (nodejs/require "coinbase-exchange"))
(nodejs/enable-util-print!)
(defn -main [& args]
(def pc (js/PublicClient.)))
(println pc))
(set! *main-cli-fn* -main)
This throws a ReferenceError: PublicClient is not defined, though I've also tried (along with similar variations):
(def pc (. coinb (PublicClient)))
(def pc (.PublicClient coinb))
(def pc (:PublicClient coinb))
All of which have failed by printing nil. I've pretty closely studied this article for any relevant examples, but I'm confused on how using node.js affects the naming of things, if at all.
Really its not a naming issue, its more about how to use new with nested properties in an object. Unfortunately, you can't get a reference to PublicClient and then new that after the fact.
(new (.-PublicClient coinb))
It does not work even though (.-PublicClient coinb) returns a function.
You need to point to the location of the function, by using it's dot notation within ClojureScript:
(new coinb.PublicClient)
;; OR, more idiomatic
(coinb.PublicClient.)
That should give you what you want.
Anybody else seeing this?
> ((.-getProducts (cb.PublicClient.)) (fn [_ _ _] nil))
#object[TypeError TypeError: self.makeRelativeURI is not a function]

What are restas render-objects?

When i try to compile the render-object method shown here in the documentation http://restas.lisper.ru/en/manual/special-pages.html,
(defmethod restas:render-object ((designer mydrawer)
(code (eql hunchentoot:+http-internal-server-error+)))
(setf (hunchentoot:content-type*) "text/plain")
"Oh, all very bad")
it gives
There is no class named RESTAURANT::MYDRAWER SIMPLE-ERROR
How do these render-object thingies work ?
render-object is a generic function that takes a rendering object, which is either the object passed to :render-method for define-route or the value of *default-render-method*, and the object to render. It then renders that object (usually as text, although you could probably render it to an octet array as well).
The example assumes that you have a class called mydrawer. To get this working you would need to do something like the following:
(defclass mydrawer () ())
(defmethod restas:render-object ((designer mydrawer)
(code (eql hunchentoot:+http-internal-server-error+)))
(setf (hunchentoot:content-type*) "text/plain")
"Oh, all very bad")
(defmethod restas:render-object ((designer mydrawer) obj)
;; Default rendering of objects goes here,
;; this will just call the default render method
(restas:render-object nil obj))
And then use an instance of mydrawer as the render method either for individual routes, or for a restas module.

find relation in CLIPS expert systems

How can I find the relation between two or more family members in a family tree using CLIPS. I tried this rule but it's not working. I have a syntax Error.
Is there any hints to avoid the Error.
(defrule Family
(FamilyTree ?L-name ?F-name)
=>
(assert(FamilyTree ?L-name ?F-name(read))
(printout t ?L-name "is parent of" ?F-name crlf)))
There appears to be some critical information missing from your question. The code snippet you posted loads correctly. It's only when I add a FamilyTree deftemplate that I get the error you described. If you use deftemplate facts in your rules, you have to use the syntax for deftemplate facts which requires specifying the slot name.
CLIPS> (clear)
CLIPS>
(defrule Family
(FamilyTree ?L-name ?F-name)
=>
(assert(FamilyTree ?L-name ?F-name(read))
(printout t ?L-name "is parent of" ?F-name crlf)))
CLIPS> (clear)
CLIPS> (deftemplate FamilyTree (slot last-name) (slot first-name))
CLIPS>
(defrule Family
(FamilyTree ?L-name ?F-name)
=>
(assert(FamilyTree ?L-name ?F-name(read))
(printout t ?L-name "is parent of" ?F-name crlf)))
[PRNTUTIL2] Syntax Error: Check appropriate syntax for deftemplate patterns.
ERROR:
(defrule MAIN::Family
(FamilyTree ?L-name
CLIPS>

Digestive Functors with a variable number of subforms (Snap/Heist)

I'm working on porting a site from PHP to Snap w/ Heist. I've ported some of the simpler forms to using Digestive Functors successfully, but now I have to do the tricky ones that require the use of subforms.
This application manages producing flyers for retail stores, so one of the tasks that need to be done is adding an ad size and defining its physical dimensions on the printed flyer. Sizes will vary depending on the type of page (configurable by the flyer owner) and its orientation (which can only be controlled by the administrators).
This form is guaranteed to have a minimum of 3 cells, most likely going to have 9 cells (as pictured above from the PHP version), but could theoretically have an unlimited number.
Here's what I've got so far for the dimensions subform:
data AdDimensions = AdDimensions
{ sizeId :: Int64
, layoutId :: Int64
, dimensions :: Maybe String
}
adDimensionsForm :: Monad m => AdDimensions -> Form Text m AdDimensions
adDimensionsForm d = AdDimensions
<$> "size_id" .: stringRead "Must be a number" (Just $ sizeId d)
<*> "layout_id" .: stringRead "Must be a number" (Just $ layoutId d)
<*> "dimensions" .: opionalString (dimensions d)
The form definition doesn't feel quite right (maybe I have completely the wrong idea here?). AdDimensions.dimensions should be a Maybe String, since it will be null when coming back from the database when running the query to get a list of all of the possible combinations of size_id/layout_id for a new ad size, but it will be not null from a similar query that will be run when creating the edit form. The field itself is required (ad_dimensions.dimensions is set to not null in the database).
From here, I have no idea where to go to tell the parent form that it has a list of subforms or how I might render them using Heist.
I wrote a special combinator for this quite some time ago for digestive-functors-0.2. It was a very full featured solution that included javascript code allowing fields to be dynamically added and removed. That code was based on a much earlier implementation Chris and I did for the formlets package which digestive-functors eventually superceded. This function was never ported to work with the new API that digestive-functors got in 0.3.
The problem is tricky and has some subtle corner cases, so I would recommend that you spend some time looking at the code. I think Jasper would probably accept a good port of the code into the current version of digestive-functors. It's just that nobody has done the work yet.
Edit: This has been done now for the latest digestive-functors. See the listOf function.
Using the listOf functionality (which wasn't around when the question was originally asked/answered), this is how one would go about about it. This requires 2 forms where the form representing your list's type is a formlet:
data Thing = Thing { name: Text, properties: [(Text, Text)] }
thingForm :: Monad m => Maybe Thing -> Form Text m Thing
thingForm p = Thing
<$> "name" .: text (name <$> p)
<*> "properties" .: listOf propertyForm (properties <$> p)
propertyForm :: Monad m => Maybe (Text, Text) -> Form Text m (Text, Text)
propertyForm p = ( , )
<$> "name" .: text (fst <$> p)
<*> "value" .: text (snd <$> p)
Simple forms
If you have a simple list of items, digestive-functors-heist defines some splices for this, but you might find that you'll end up with invalid markup, especially if your form is in a table.
<label>Name <dfInputText ref="formname" /></label>
<fieldset>
<legend>Properties</legend>
<dfInputList ref="codes"><ul>
<dfListItem><li itemAttrs><dfLabel ref="name">Name <dfInputText ref="name" /></dfLabel>
<dfLabel ref="code">Value <dfInputText ref="value" required /></dfLabel>
<input type="button" name="remove" value="Remove" /></li></dfListItem>
</ul>
<input type="button" name="add" value="Add another property" /></dfInputList>
</fieldset>
There is JavaScript provided by digestiveFunctors to control adding and removing elements from the form that has a jQuery dependency. I ended up writing my own to avoid the jQuery dependency, which is why I'm not using the provided addControl or removeControl splices (attributes for button type elements).
Complex Forms
The form in the OP can't make use of the splices provided by digestive-functors-heist because the labels are dynamic (eg. they come from the database) and because we want it in a complex table layout. This means we have to perform 2 additional tasks:
Generate the markup manually
If you haven't looked at the markup that are generated by the digestive-functors-heist splices, you might want to do that first so that you get an idea of exactly what you have to generate so that your Form can be processed correctly.
For dynamic forms (eg. forms where the users are allowed to add or remove new items on the fly), you will need a hidden indices field:
<input type='hidden' name='formname.fieldname.indices' value='0,1,2,3' />
formname = whatever you named your form when you ran it via runForm
fieldname = the name of the list field in your main form (adjust as necessary if you're using subforms), in this example it would be named "properties"
value = a comma delimited list of numbers representing the indices of the subforms that should be processed when the form is submitted
When one of the items from your list is removed or a new one is added, this list will need to be adjusted otherwise new items will be completely ignored and removed items will still exist in your list. This step is unnecessary for static forms like the one in the OP.
Generating the rest of the form should be pretty straight forward if you already know how to write splices. Chunk up the data as appropriate (groupBy, chunksOf, etc.) and send it through your splices.
In case you can't already tell by looking at markup generated by digestive-splices-heist, you'll need to insert the index value of your subform as part of the fields for each subform. Tthis is what the output HTML should look like for the first field of our list of subforms:
<input type='text' name='formname.properties.0.name' value='Foo' />
<input type='text' name='formname.properties.0.value' value='Bar' />
(Hint: zip your list together with an infinite list starting from 0)
Pull the data back out of your form when handling errors
(I apologize in advance if none of this code is actually able to compile as written, but hopefully it illustrates the process)
This part is less straight forward than the other part, you'll have to dig through the innards of digestive-functors for this. Basically, we're going to use the same functions digestive-functors-heist does to get the data back out and populate our Thing with it. The function we're needing is listSubViews:
-- where `v` is the view returned by `runForm`
-- the return type will be `[View v]`, in our example `v` will be `Text`
viewList = listSubViews "properties" v
For a static form, this can be as simple as zipping this list together with your list of data.
let x = zipWith (curry updatePropertyData) xs viewList
And then your updatePropertyData function will need to update your records by pulling the information out of the view using the fileInputRead function:
updatePropertyData :: (Text, Text) -> View Text -> (Text, Text)
updatePropertyData x v =
let
-- pull the field information we want out of the subview
-- this is a `Maybe Text
val = fieldInputRead "value" v
in
-- update the tuple
maybe x ((fst x, )) val

Resources