database( { wine( Type:string, Producer:string, Year:integer, Price:integer, Instock:integer, SalesPreDay:integer, OrderDelay:integer ), producer( Name:string, Location:string ) } ). export wine_loc($Type, $Location, Producer). export wine_loc(Type, Location, $Producer). export wine_loc($Type, Location, Producer). export suggest_order($Type, Producer, Amount, Cost, $OrderSpeed). export suggest_order(Type, Producer, Amount, Cost, $OrderSpeed). export suggest_order($Type, Producer, Amount, Cost, OrderSpeed). export suggest_order(Type, Producer, Amount, Cost, OrderSpeed). export increment_stock($Type,$Producer,$Year,$Incr,NewInstock). export del_wine_by_location($Location,Type,Producer,Year). export del_inactive_suppliers(Producer). export list_producers(Producer,Location). export list_wine(Type,Producer,Year). export list_wine_stock(Type,Producer,Year,Instock). export generateOnHand(A,B,C,D). % wine_loc(Type, Location, ProducerName) % Type is the wine type. Location is the geographical location of the % wine producer. ProducerName is the name of the wine producer. % Note the use of named attributes for the wine relation. wine_loc(Type, Location, ProducerName) <- wine(Type, ProducerName, _, _, _, _, _), % wine(Type=Type, Producer=ProducerName), producer(ProducerName, Location). % suggest_order(Type, ProducerName, Amt, Cost, OrderSpeed) % Type is the wine type. ProducerName is the name of the producer of the % wine that should be ordered. Amt is the amount of wine Wcode that % should be ordered. Cost is the total cost of the order (not including % shipping and handling). OrderSpeed is either 'normal' or 'rush'. suggest_order(Type, ProducerName, Amt, Cost, normal) <- generateOnHand(Type, ProducerName, Price, OnHand), wine(Type, ProducerName,_,_,_, SalesPerDay, OrderDelay), OnHand > 0, OnHand <= SalesPerDay, Amt = ((SalesPerDay * OrderDelay) / 12) + 1, Cost = Price * Amt * 12. suggest_order(Type, ProducerName, Amt, Cost, rush) <- generateOnHand(Type, ProducerName, Price, OnHand), OnHand <= 0, Amt = ((1 - OnHand) / 12) + 1, Cost = Price * Amt * 12. % generateOnHand(Type, ProducerName, Price, OnHand) % Type is the wine type. ProducerName gives the name of the wine producer. % Price is the cost of a single bottle of the wine. OnHand is the number of % bottles of the wine that will be in stock when the new order is received. generateOnHand(Type, ProducerName, Price, OnHand) <- wine(Type, ProducerName, _,Price, Instock, SalesPerDay, OrderDelay), OnHand = Instock - (SalesPerDay * OrderDelay). % % increment_stock(Type,Producer,Year,Incr,NewInstock) % Given wine Type, Producer, Year, and Incr, this query increments % the value of Instock by Incr and returns the new value. % increment_stock(Type,Producer,Year,Incr,NewInstock) <- wine(Type,Producer,Year,Price,Instock,SalesPerDay,OrderDelay), NewInstock=Instock+Incr, -wine(Type,Producer,Year,_,_,_,_), +wine(Type,Producer,Year,Price,NewInstock,SalesPerDay,OrderDelay). % % del_wine_by_location(Location,Type,Producer,Year) % Given Location, this query deletes wines of producers from the given % location and returns Type, Producer, and Year of the wines deleted. % del_wine_by_producer(Producer,Type,Year) <- wine(Type,Producer,Year,Price,Instock,SalesPerDay,OrderDelay), -wine(Type,Producer,Year,_,_,_,_). del_wine_by_location(Location,Type,Producer,Year) <- producer(Producer,Location), del_wine_by_producer(Producer,Type,Year). % % del_inactive_suppliers(Producer) % This query deletes from the producer relation all producers not listed % in the wine relation and returns the names of the producers deleted. % del_inactive_suppliers(Producer) <- inactive_supplier(Producer), producer(Producer,Location), -producer(Producer,Location). inactive_supplier(P) <- producer(P,_), ~wine_supplier(P). wine_supplier(P) <- wine(_,P,_,_,_,_,_). % wine(Producer=P). % % miscellaneous queries % The following queries are provided for viewing the result of updates. % list_producers(Producer,Location) <- producer(Producer,Location). list_wine(Type,Producer,Year) <- wine(Type,Producer,Year,_,_,_,_). list_wine_stock(Type,Producer,Year,Instock) <- wine(Type,Producer,Year,_,Instock,_,_).