% Examples from documentation for shellOAVbc. % % These are the rules appearing as examples in the documentation. % Additional rules have been added so that the sample rules can % be used. % % To try these example issue Prolog goal: % % ?- rootGoal( CONS ). % % where CONS is in the consequent of one of the rules below. :-['shellOAVbc.pro']. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% props(a,"Is a true?",yesNo). props(b,"Is b true?",yesNo). props(c,"Is c true?",yesNo). r2:if a and b then c2. r2b:if a and b then d:.e. exampleRule:if a and b then c1. exampleRule:if a or b and c then d1. notExampleRule:if not a or b and c then d2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% props(truck,numWheels,"How many wheels does it have?",isNum(none)). compExamp1:if truck:.numWheels=L and L>18 then truck:.huge. compExamp2:if truck:.numWheels=L and H=(L>18) then truck:.huge:=H. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% props(coffeeBeans,costPerKilo,"How much does 1 kg of CS cost?",isNum(none)). props(purchase,amount,"How much would you like. Metric only, please:", isNum(none)). traceRule(r3). traceRule(r5). % Shell will indicate when rules are used. r3:if coffeeBeans:.costPerKilo=C and purchase:.amount=A and Price is C*A then purchase:.price:=Price. r4:if purchase:.price=Price and purchase:.price=P2 then twice1. r5:if coffeeBeans:.costPerKilo=C and purchase:.amount=A and Price is C*A then compPrice(Price). r6:if compPrice(P) and compPrice(P2) then twice2. %isExample:if coffeeBeans:.costPerKilo=C and purchase:.amount=A % and Price is C*A then purchase:.price=Price. % Same as r3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % The code below is all to demonstrate callVetRule. % % The demo is started using: % % ?- rootGoal(zooTest). props(zooAnimal,alive,"Is * alive?",yesNo). props(zooAnimal,sick,"Is * sick?",yesNo). props(zooAnimal,name,"What is an * name?",isString). data(zooAnimal,firstQuest,"Are there any animals?",init). data(zooAnimal,moreQuest,"Are there any more animals?",init). zoot:if instSet(zooAnimal,zoo) and vetNeeded then zooTest. % Command which asks about first instance in Class. % newInstRule1:if Class:.firstQuest=Q % Get first question. and askAndCheck(Q,Parent,yesNo,Ans) % Ask. and % (Below) If yes, create instance. ( Ans=true and makeNew1(Class,Parent) or Ans=false ) then instSet(Class,Parent). % The command. % Command which creates instance and asks about more instances. % newInstRule2:if new(Class,InstName) % Create instance. and InstName:.name=T % Find name. and Class:.moreQuest=Q % Retrieve "more" question. and askAndCheck(Q,Parent,yesNo,Ans) % Ask. and % If yes, make recursive call. ( Ans=true and makeNew1(Class,Parent) or Ans=false ) then makeNew1(Class,Parent). % The command. callVetRule:if forSome( Animal:.isa=zooAnimal, Animal:.alive=true, Animal:.sick=true ) then vetNeeded. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% props(person,name,"What is the name of *?",isString). props(person,age,"How old is *?",isNum(min(0))). newPerson:if new(person,Person) and Person:.name=_ and Person:.age=_ then newPerson(Person). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % To demonstrate, first enter goal: % % ?- rootGoal( poped() ). % % And enter a list of people. Then enter goal: % % ?- deduce( unsuspectingHero1, X ). % % or % % ?- deduce( unsuspectingHero2, X ). % % or % % ?- deduce( unsuspectingHero3, X ). % % If rootGoal is used in place of deduce the list of people will be erased. % Note that unsuspectingHero3 should result in an error because P is left % unbound. props(person,canFightBear,"Can * fight a bear?",yesNo). props(person,brave,"Is * brave?",yesNo). data(person,firstQuest,"Is there a person?",init). data(person,moreQuest,"Is there another person?",init). % The rule below is used to instantiate people. Since its consequent % is a command it can be repeatedly called. % populate:if instSet(person,_) then poped(_). wereSaved1:if thereExist( P:.isa=person, P:.canFightBear and P:.brave ) then unsuspectingHero1:=P. wereSaved2:if thereExist( P:.isa=person, P:.canFightBear ) and P:.brave then unsuspectingHero2:=P. wereSaved3:if forSome( P:.isa=person, P:.canFightBear, P:.brave ) then unsuspectingHero3:=P. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% futile:if V=false then okay(_,_,_):=V. passRule:if askAndCheck("Do you want to cross the bridge?",_,yesNo,true) and askAndCheck("What is your name?",_,isString,Name) and askAndCheck("*, what is your quest?",Name, isEnum([ grail for "Holy grail.", otherSide for "Other side of chasm.", stroll for "Out for a stroll."]), Quest) and askAndCheck("What is the airspeed of a swallow?",_, isNum(range(0,100)),Speed) and okay(Name,Quest,Speed) then canCrossBridge. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % One limit of the shell is that classes cannot be specified in the consequent, % permitting the following less-than-ideal code (which nevertheless demonstrates % the use of call): data(car,type,surface,init). props(car,passengers,"How many passengers can the car hold?",isNum(range(1,10))). data(b747,type,jet,init). props(b747,passengers,"How many passengers can the jet hold?", isNum(range(1,600))). transList([]). r1:if Transport:.type=T and Transport:.passengers=P and call( retract(transList(L)) ) and call( assert(transList( [ tp(T,P) | L ] )) ) then Transport:.processed. % The code is less-than-ideal because there is no way to be sure the right % types of objects match the consequent; instead any object with attributes % type and passengers will match.