You are on page 1of 8

COMSATS - Lancaster Dual Degree Programme

COMSATS Institute of Information Technology Lahore


Assignment 1 SPRING 2016
Course Title:
Course
Instructor/s:
Semester:
Time Allowed:

Student
Name

Artificial Intelligence

Course Code:

Dr. Wajahat Mahmood Qazi

Programme Name: BS Computer Science

7th

Batch:

Section:
April 13

Umer Sharif

CSC475 Credit Hours: 3(2,1)

Sp13-bcs-b
Date:
Maximum Marks:

Roll No.

20

Sp11-bcs-040

Assignment 2
Pet Shop Expert System
Decision Table
Rule 1

Rule 2

If shopping cart dont


contain item=fish food
AND
Shopping cart dont
contain=fish food
sample AND item is =
gold fish
THEN
Recommended action
=add item fish food
sample to shopping
cart

If there is no suggested

tank already =fish tank


AND
Shopping cart contain
5 or more item=gold
fish
AND
There is no fish tank in
shopping cart
Then
Recommended action
=suggest a fish tank

Rule 3
If gross cost =10.00
AND
Gross cost is less
then=20.00
THEN
Recommended
action=add 5%
discount in shopping
cart

Rule 4
If gross cost greater
than or >20.00
THEN
Recommended
action=add 10%
discount in shopping
cart

Rule 1

Fish food
sample

Rule 2

Rule 3

Fish food
Gold fish

Fish tank
5 or more Gold
fish
No tank

Suggest Tank

Gross cost
=10.00
Gross cost
<20.00

Apply 5%
discount

Gross cost >20.00

Apply 10%
discount
Recommended
Action

Rule 4

add item fish


food sample to
shopping cart

suggest a fish
tank

Source Code

add 5% discount
in shopping cart

add 10% discount


in shopping cart

package umer.petstore
import org.kie.api.runtime.KieRuntime
import umer.petstore.PetStore.Order
import umer.petstore.PetStore.Purchase
import umer.petstore.PetStore.Product
import java.util.ArrayList
import javax.swing.JOptionPane;
import javax.swing.JFrame
global JFrame frame
global javax.swing.JTextArea textArea

// dialect "mvel"

// insert each item in the shopping cart into the Working Memory
rule "Explode Cart"
agenda-group "init"
auto-focus true
salience 10
dialect "java"
when
$order : Order( grossTotal == -1 )
$item : Purchase() from $order.items
then
insert( $item );
kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup( "show items" ).setFocus();
kcontext.getKnowledgeRuntime().getAgenda().getAgendaGroup( "evaluate" ).setFocus();

end

// Free Fish Food sample when we buy a Gold Fish if we haven't already bought
// Fish Food and dont already have a Fish Food Sample
rule "Free Fish Food Sample"
agenda-group "evaluate"
dialect "mvel"
when
$order : Order()
not ( $p : Product( name == "Fish Food") && Purchase( product == $p ) )
not ( $p : Product( name == "Fish Food Sample") && Purchase( product == $p ) )
exists ( $p : Product( name == "Gold Fish") && Purchase( product == $p ) )
$fishFoodSample : Product( name == "Fish Food Sample" );
then
System.out.println( "Adding free Fish Food Sample to cart" );
purchase = new Purchase($order, $fishFoodSample);
insert( purchase );
$order.addItem( purchase );
end

// Suggest a tank if we have bought more than 5 gold fish and dont already have one
rule "Suggest Tank"
agenda-group "evaluate"
dialect "java"

when
$order : Order()
not ( $p : Product( name == "Fish Tank") && Purchase( product == $p ) )
ArrayList( $total : size > 5 ) from collect( Purchase( product.name == "Gold Fish" ) )
$fishTank : Product( name == "Fish Tank" )
then
requireTank(frame, kcontext.getKieRuntime(), $order, $fishTank, $total);
end

rule "Show Items"


agenda-group "show items"
dialect "java"
when
$order : Order( )
$p : Purchase( order == $order )
then
textArea.append( $p.getProduct() + "\n");
end

rule "do checkout"


dialect "java"
when
then
doCheckout(frame, kcontext.getKieRuntime());
end

rule "Gross Total"


agenda-group "checkout"
dialect "mvel"
when
$order : Order( grossTotal == -1)
Number( total : doubleValue ) from accumulate( Purchase( $price : product.price ),
sum( $price ) )
then
modify( $order ) { grossTotal = total }
textArea.append( "\ngross total=" + total + "\n" );
end

rule "Apply 5% Discount"


agenda-group "checkout"
dialect "mvel"
when
$order : Order( grossTotal >= 10 && < 20 )
then
$order.discountedTotal = $order.grossTotal * 0.95;
textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );
end
rule "Apply 10% Discount"
agenda-group "checkout"
dialect "mvel"
when
$order : Order( grossTotal >= 20 )

then
$order.discountedTotal = $order.grossTotal * 0.90;
textArea.append( "discountedTotal total=" + $order.discountedTotal + "\n" );
end

function void doCheckout(JFrame frame, KieRuntime krt) {


Object[] options = {"Yes",
"No"};

int n = JOptionPane.showOptionDialog(frame,
"Would you like to checkout?",
"",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);

if (n == 0) {
krt.getAgenda().getAgendaGroup( "checkout" ).setFocus();
}
}

function boolean requireTank(JFrame frame, KieRuntime krt, Order order, Product fishTank, int total) {
Object[] options = {"Yes",
"No"};

int n = JOptionPane.showOptionDialog(frame,
"Would you like to buy a tank for your " + total + " fish?",
"Purchase Suggestion",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);

System.out.print( "SUGGESTION: Would you like to buy a tank for your "
+ total + " fish? - " );
if (n == 0) {
Purchase purchase = new Purchase( order, fishTank );
krt.insert( purchase );
order.addItem( purchase );
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
return true;
}

You might also like