Re: [F-Script-talk] Methods in F-Script.
Brought to you by:
pmougin
From: Todd B. <tbl...@ma...> - 2005-07-14 05:15:00
|
Huh, seems like a theme. I've been working on an app based on integrating the following components: CoreData - basic object modeling and persistence CLIPS - see http://www.ghg.net/clips/CLIPS.html - constraints/ intelligence/rules FScript - actions and predicates from CLIPS. (Yes Philippe - this is based on the thing I showed you at the Paris Squeaknic a couple years ago). CLIPS is pretty pluggable - you can install new functions into the interpreter by handing it a C function pointer and registering it. You can also register a custom parser to parse the rest of the function line. I registered a function named '[' and in the parser I just scan to the matching ']' and then pick out the variables in the expression and bind them in the FScript symbol table. With this you can write predicates in FScript. So you could write the clips rule: (defrule event-is-active ?event<-(Event (startDate ?start&:([NSDate date > ?start])) (endDate ? end&:([NSDate date < endDate])) (status ~active)) => ([?event setStatus: 'active'])) Which says - match any Event with a startDate later than now, an endDate earlier than now, and status not set to active => (then) set the status to active. The CoreData/CLIPS integration allows you to assert CoreData objects as 'facts' in the CLIPS knowledge base so the rules can match them. This is why the above rule even works. Each core data object has a 'fact' that shadows the values. KVO allows me to keep the fact sync'd with the object. Lately I've been wishing I could have derived attributes that are calculated based on other attributes. This is useful for binding table columns and such. To do this, I override valueForKey: in KBManagedObject (my NSManagedObject subclass) to look up the userInfo on the attribute description and look for a key called "value". This should be an FScript expression that I toss into a block and evaluate. I'm using this to model a Week object where I just have one real persistent attribute 'sunday'. The 'monday', 'tuesday' etc are transient attributes that have FScript expressions like 'self sunday dateByAddingDays: 1'. I slap this into a template that looks like '[:self | %@ ]' and evaluate it as a block with the managed object as an argument. I've also notice that the latest FScript has a special case for NSManagedObject's and "fakes" zero argument message sends that are the same name as an attribute and calls valueForKey: I've extended it to include mutator support as well. So even if you are using vanilla managed objects, you can still do setFoo: val instead of setValue: val forKey: 'Foo' This little combination has proven really powerful. I've build a working project management app with just clips rules, IB bindings, and a CoreData model. It works OK but its pretty vanilla. I've been working on sexing up the UI and this is rapidly turning into a black hole. But for quickie apps based on calculations and constraints, this little framework is da bomb. I added the following to ExecEngine.m static NSString* keyFromMutator(NSString* key) { if([key hasPrefix: @"set"]) { key = [[[key substringWithRange: NSMakeRange(3,1)] lowercaseString] stringByAppendingString: [key substringWithRange: NSMakeRange(4,[key length]-5)]]; } return key; } and changed the special case for NSMangedObject in sendMsgNoPattern to read: else if (![receiver isProxy] && [receiver isKindOfClass:NSClassFromString(@"NSManagedObject")] && [[[[receiver entity] propertiesByName] allKeys] containsObject:keyFromMutator (selectorStr)]) // We don't support proxy here because it will crash when the isKindOfClass:NSClassFromString(@"NSManagedObject") message is executed on Mac OS X < 10.4 { if([selectorStr hasPrefix: @"set"] && [selectorStr hasSuffix: @":"] && numbersOfArguments == 3) { return [receiver performSelector:@selector(setValue:forKey:) withObject: args[2] withObject: keyFromMutator(selectorStr)]; } else { return [receiver performSelector:@selector(valueForKey:) withObject:selectorStr]; } } On Jul 13, 2005, at 4:55 AM, Marcel Weiher wrote: > > On 13 Jul 2005, at 12:36, Massimiliano Gubinelli wrote: > > >> recently I wrote some code which allows to override an arbitrary >> objc methods by sending the corresponding NSInvocation to a >> delegate for handling. >> > > Cool. I wrote something very similar for Objective-Smalltalk, > which I haven't been able to work much on recently. > > >> My purpose is to use this technique to extend objc classes from an >> external language (Io, in my case). However I think that such a >> mechanism can be also useful in F-Script to replace native methods >> with scripts (and ultimately to be able to define ObjC classes in >> F-Script). >> > > Yup. > > > -- > > Marcel Weiher Metaobject Software Technologies > ma...@me... www.metaobject.com > Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc. > 1d480c25f397c4786386135f8e8938e4 > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by the 'Do More With Dual!' webinar > happening > July 14 at 8am PDT/11am EDT. We invite you to explore the latest in > dual > core and dual graphics technology at this free one hour event > hosted by HP,AMD, and NVIDIA. To register visit http://www.hp.com/ > go/dualwebinar > _______________________________________________ > F-Script-talk mailing list > F-S...@li... > https://lists.sourceforge.net/lists/listinfo/f-script-talk > |