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