Instances and Slots in CLIPS - ontology

I am currently building a CLips program for cell phones. I have different cell phones and thier specs. I am trying to figure out how to make defrules to sort out features such as Color. I will show what I mean below:
([phones_Class85] of Moto+X
(Battery+Life "240")
(Build+Material "Metal")
(Camera "13")
(Card+Slot "Yes")
(Color "Cream")
(FingerPrint+Scanner "No")
(Memory "16")
(Operating+System "Android")
(Price "119")
(RAM "1")
(Screen+Size "5.2")
(Water+Resistant "Yes")
(Weight "144"))
([phones_Class86] of IPhone+6
(Battery+Life "250")
(Build+Material "Metal")
(Camera "8")
(Card+Slot "No")
(Color "Gold")
(FingerPrint+Scanner "Yes")
(Memory "16")
(Operating+System "IOS")
(Price "199")
(RAM "1")
(Screen+Size "4.7")
(Water+Resistant "No")
(Weight "129"))
([phones_Class93] of IPhone+5s
(Battery+Life "250")
(Build+Material "Metal")
(Camera "8")
(Card+Slot "No")
(Color "Gold")
(FingerPrint+Scanner "Yes")
(Memory "16")
(Operating+System "IOS")
(Price "99")
(RAM "1")
(Screen+Size "4")
(Water+Resistant "No")
(Weight "112"))
As you can see I have different cell phones that have instances of different specs. There are many more but I jsut gave a few. I am trying to do something with a defrule that will allow me to print out all the phones whos Color is Gold. I am unsure of how to move through this when I have instances. Is there a way to access the different slots (Color) within the instances then go through each one checking it?

CLIPS>
(defclass Phone
(is-a USER)
(slot Battery+Life)
(slot Build+Material)
(slot Camera)
(slot Card+Slot)
(slot Color)
(slot FingerPrint+Scanner)
(slot Memory)
(slot Operating+System)
(slot Price)
(slot RAM)
(slot Screen+Size)
(slot Water+Resistant)
(slot Weight))
CLIPS>
(defclass Moto+X
(is-a Phone))
CLIPS>
(defclass IPhone
(is-a Phone))
CLIPS>
(defclass IPhone+6
(is-a IPhone))
CLIPS>
(defclass IPhone+5s
(is-a IPhone))
CLIPS>
(definstances Phones
([phones_Class85] of Moto+X
(Battery+Life "240")
(Build+Material "Metal")
(Camera "13")
(Card+Slot "Yes")
(Color "Cream")
(FingerPrint+Scanner "No")
(Memory "16")
(Operating+System "Android")
(Price "119")
(RAM "1")
(Screen+Size "5.2")
(Water+Resistant "Yes")
(Weight "144"))
([phones_Class86] of IPhone+6
(Battery+Life "250")
(Build+Material "Metal")
(Camera "8")
(Card+Slot "No")
(Color "Gold")
(FingerPrint+Scanner "Yes")
(Memory "16")
(Operating+System "IOS")
(Price "199")
(RAM "1")
(Screen+Size "4.7")
(Water+Resistant "No")
(Weight "129"))
([phones_Class93] of IPhone+5s
(Battery+Life "250")
(Build+Material "Metal")
(Camera "8")
(Card+Slot "No")
(Color "Gold")
(FingerPrint+Scanner "Yes")
(Memory "16")
(Operating+System "IOS")
(Price "99")
(RAM "1")
(Screen+Size "4")
(Water+Resistant "No")
(Weight "112")))
CLIPS>
(defrule find-gold-phones
(object (is-a Phone)
(name ?name)
(Color "Gold"))
=>
(printout t ?name " is a gold phone." crlf))
CLIPS>
(defrule find-gold-iPhones
(object (is-a IPhone)
(name ?name)
(Color "Gold"))
=>
(printout t ?name " is a gold iPhone." crlf))
CLIPS> (reset)
CLIPS> (run)
[phones_Class93] is a gold phone.
[phones_Class93] is a gold iPhone.
[phones_Class86] is a gold phone.
[phones_Class86] is a gold iPhone.
CLIPS>

Related

Can I COUNTIF across multiple columns in an array formula?

Seems simple to me..
I want to do something along the lines of =Countif(A2:X , "Yes")
I would wrap this in an arrayformula with a simple IF(A2:A = "" ,"" .....
I want it to bring back the number of "Yes" per row
So if row 2 has 12 yes' then bring back 12, row 3 has 7 so 7 etc
This has worked to an extent but brings back the count across the entire range rather than split by row
Try:
=ArrayFormula(IF(A2:A="",,MMult(N(A2:X="Yes"),Sequence(Columns(A2:X))^0)))
Or
=ArrayFormula(IF(A2:A="",,Len(RegexReplace(RegexReplace(Transpose(Query(Transpose(A2:X),,9^9)),"(?i)\bYes\b","~"),"[^~]",))))
Or
=ArrayFormula(IF(A2:A="",,Len(Substitute(Transpose(Query(Transpose(IF(A2:X<>"Yes",,"~")),,9^9))," ",))))
Update
If I wanted to ammend this formula to do if A2:X = No AND A1:X1 contains, for instance (AG) how would I do this?
=ArrayFormula(IF(A2:A="",,MMult((A2:X="No")*RegexMatch(A1:X1,"AG"),Sequence(Columns(A2:X))^0)))
Though it is a bit tedious, couldn't you do this:
=COUNTIF(A2:A, "Yes") + COUNTIF(B2:B, "Yes") + COUNTIF(C2:C, "Yes") + COUNTIF(D2:D, "Yes") + COUNTIF(E2:E, "Yes") + COUNTIF(F2:F, "Yes") + COUNTIF(G2:G, "Yes") + COUNTIF(H2:H, "Yes") + COUNTIF(I2:I, "Yes") + COUNTIF(J2:J, "Yes") + COUNTIF(K2:K, "Yes") + COUNTIF(L2:L, "Yes") + COUNTIF(M2:M, "Yes") + COUNTIF(N2:N, "Yes") + COUNTIF(O2:O, "Yes") + COUNTIF(P2:P, "Yes") + COUNTIF(Q2:Q, "Yes") + COUNTIF(R2:R, "Yes") + COUNTIF(S2:S, "Yes") + COUNTIF(T2:T, "Yes") + COUNTIF(U2:U, "Yes") + COUNTIF(V2:V, "Yes") + COUNTIF(W2:W, "Yes") + COUNTIF(X2:X, "Yes")

In Tableau dashboard I have a metric which i should display in kilos, Millions and Billions automatically

I have a Measure in Tableau which has to be display dynamically in Kilos, Millions , Billions and also formatted as 200k, 2726M.
You can try with this formula which you can expand on your needs:
str(
if SUM([Sales]) > 1000000000 THEN ROUND(SUM([Sales])/1000000000,1)
elseif SUM([Sales]) > 1000000 THEN ROUND(SUM([Sales])/1000000,1)
elseif SUM([Sales]) > 1000 THEN ROUND(SUM([Sales])/1000,1)
else SUM([Sales])
end )
+
if SUM([Sales]) > 1000000000 THEN 'B'
elseif SUM([Sales]) > 1000000 THEN 'M'
elseif SUM([Sales]) > 1000 THEN 'K'
else ''
end
See screenshot as quick example on Superstore:

Ruby : Loop, Case and iterations problems ( Rock , Paper, Scissors )

I´m ruby student since 1 month and i´m stuck with one part of my code. The project is based on a game ( rock, paper, scissor) but i´m facing of a problem that i cannot solve for moment. I would like to add one parameters to this game especially when the user enter a wrong input by displaying a message but with the condition i added it´s not working.
elsif player_choice != 'r' || player_choice != 'p' || player_choice != 's' || player_choice != 'q' || player_choice != 'x'
puts "wrong input"
So if you have some advice or some hint to share with me it will be great ! ( see below the entire code ).
Thank you very much.
#intro
puts "***** WELCOME TO PAPER SCISSORS ROCKS GAME *****"
puts "Input p = Paper, r = Rocks, s = Scissors, x = Display your score , q = Quit the game. "
25.times { print "-" }
puts
#scores
playerScore = 0
cpuScore = 0
CHOICES = {'p' => 'Paper', 'r' => 'Rock', 's' => 'Scissors', 'x' => 'score','q' => 'quit' }
CHOICE_CPU = {'p' => 'Paper', 'r' => 'Rock', 's' => 'Scissors'}
loop do
# player picks
begin
puts "Select your pick: (p/r/s/x/q)"
player_choice = gets.chomp.downcase
end until CHOICES.keys.include?(player_choice)
# computer picks
cpu_choice = CHOICE_CPU.keys.sample
def throw_message(winning_choice)
case winning_choice
when 'p'
puts "Paper wraps Rock!"
when 'r'
puts "Rock smashes Scissors!"
when 's'
puts "Scissors cuts Paper!"
when 'x'
puts "Live score"
when 'q'
puts "you decide to quit the game"
end
end
#display scores
if player_choice == 'x'
throw_message(player_choice)
puts "PLAYER : #{playerScore} CPU : #{cpuScore}"
#quit the game
elsif player_choice == 'q'
throw_message(player_choice)
break
# tie result
elsif player_choice == cpu_choice
puts "It's a Tie ! "
#player win
elsif (player_choice == 'p' && cpu_choice == 'r') || (player_choice == 'r' && cpu_choice == 's') || (player_choice == 's' && cpu_choice == 'p')
throw_message(playe·r_choice)
puts "You Win"
playerScore +=1
#display invalid input
elsif player_choice != 'r' || player_choice != 'p' || player_choice != 's' || player_choice != 'q' ||
player_choice != 'x'
puts "wrong input"
#cpu win
else throw_message(cpu_choice)
puts "Computer Win"
cpuScore +=1
end
end
Move the CHOICES.keys.include?(player_choice) check to the top of the main if/else logic. If you validate your input as early as possible, the rest of the code can assume the input is good; there's no need to spell out all the possible choices again.
I'm using a case/when because it's easier to read than if/elsif. throw_message is defined outside the loop, inside the loop its being redefined repeatedly. And I've removed choices from throw_message which don't have to do with the game; this avoids repeating the full set of choices.
def throw_message(winning_choice)
case winning_choice
when 'p'
puts "Paper wraps Rock!"
when 'r'
puts "Rock smashes Scissors!"
when 's'
puts "Scissors cuts Paper!"
end
end
def player_wins?(player_choice, cpu_choice)
return player_choice == 'p' && cpu_choice == 'r') ||
player_choice == 'r' && cpu_choice == 's') ||
player_choice == 's' && cpu_choice == 'p')
end
loop do
# player picks
puts "Select your pick: (p/r/s/x/q)"
player_choice = gets.chomp.downcase
# cpu picks
cpu_choice = CHOICE_CPU.keys.sample
case
when !CHOICES.keys.include?(player_choice)
puts "wrong input"
when player_choice == 'x'
puts "Live score"
puts "PLAYER : #{playerScore} CPU : #{cpuScore}"
when player_choice == 'q'
puts "you decide to quit the game"
break
when player_choice == cpu_choice
puts "It's a tie!"
when player_wins?(player_choice, cpu_choice)
throw_message(player_choice)
puts "You Win"
playerScore +=1
else
throw_message(cpu_choice)
puts "Computer Win"
cpuScore +=1
end
end

What's wrong with this C++ code lottery guessing game?

I am making a simple lottery game application, where three random numbers between 0 and 10 are generated, if the user gets all three in the right order, they get 1 million. If they get one right then they win 10 dollars, and if they get all three but not in order, they win a thousand, if two are matching then they get $1,000. and if they get none right then they get nothing.
Here's my code here.
int main()
{
cout << "Hello, this is the lottery! Three random numbers between 0 and 10 will be generated. Guess what they are and the order!" << endl;
char answer;
cout << "Do you want to play? (y or n): " << endl;
cin >> answer;
while (answer == 'y' || 'Y')
{
srand((unsigned)time(NULL));
int ran1 = rand() % 10;
int ran2 = rand() % 10;
int ran3 = rand() % 10;
int guess1, guess2, guess3;
cout << "Enter your first number guess: " << endl;
cin >> guess1;
cout << "Enter your second number guess: " << endl;
cin >> guess2;
cout << "Enter your third number guess: " << endl;
cin >> guess3;
if ((guess1 != ran1 || ran2 || ran3) && (guess2 != ran1 || ran2 || ran3) && (guess3 != ran1 || ran2 || ran3))
cout << "You won no money. Sucks for you." << endl;
else
if ((guess1 == ran1 || ran2 || ran3) || (guess2 == ran1 || ran2 || ran3) || (guess3 == ran1 || ran2) || ran3)
cout << "You won 10 dollars!" << endl;
else
if ((guess1 && guess2 == ran1 && ran2) || (guess1 && guess3 == ran1 && ran3) || (guess2 && guess3 == ran2 && ran3))
cout << "You won 100 dollars!" << endl;
else
if ((guess1 == ran1 || ran2 || ran3) && (guess2 == ran1 || ran2 || ran3) && (guess3 == ran1 || ran2 || ran3))
cout << "You won 1 thousand dollars! good job!" << endl;
else
if ((guess1 == ran1) && (guess2 == ran2) && (guess3 == ran3))
cout << "You won 1 million dollars! jackpot!" << endl;
cout << "The numbers were " << ran1 << "," << ran2 << "," << ran3 << endl;
cout << "Play again?(y or n): " << endl;
cin >> answer;
if (answer == 'y')
continue;
else
break;
}
cout << "Game Over" << endl;
system("pause");
return 0;
}
when i run this code, things don't go right with the decisions. All it says is "You won no money. sucks for you". Idk what's wrong maybe its something simple but can someone help? Thanks.
if ((guess1 != ran1 || ran2 || ran3) && (guess2 != ran1 || ran2 || ran3) && (guess3 != ran1 || ran2 || ran3))
should be
if ((guess1 != ran1 || guess1 != ran2 || guess1 != ran3) && (guess2 != ran1 || guess2 != ran2 || guess2 != ran3) && (guess3 != ran1 || guess3 != ran2 || guess3 != ran3))
It is not the order of the conditionals, even though there are flaws there as well, but is the way in which you have written the conditionals.
(guess1 == ran1 || ran2) is different than (guess1 == ran1 || guess1 == ran2)
This is because in c++ a number other than 0 evaluates to true. This is why it is evaluating to true every time.
For example, say guess1 = 1, ran1 = 2, and ran2 = 3, then
(guess1 == ran1 || guess1 == ran2) will evaluate to false, but
(guess1 == ran1 || ran2) will evaluate to true.

IVR Script in lua

I've created an ivr script in lua. In this script goto statement is not working. Please someone tell me what is the problem in this code. If I erase ::chandu:: and elseif (digits == "0") then goto chandu then it is running fine. but adding goto statement call is not connecting or I can say script is not running.
.
i = 0
j = 0
while (session:ready() == true) do
session:setAutoHangup(false);
repeat
::chandu::
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "\\d+");
if (digits == "1") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/819#192.168.22.1");
session:hangup();
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/826#192.168.22.1");
session:hangup();
elseif (digits == "9") then
i = i + 1
elseif (digits == "0") then
goto chandu
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.22.1");
session:hangup();
end
until i == 3
elseif(digits == "0") then
j = j + 1
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
end
until j == 2
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
end
Finally I've created a script having completely working now.
i = 0
j = 0
while (session:ready() == true) do
session:setAutoHangup(false);
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/coral.wav", "shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\d+");
if (digits == "1") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/corporate_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/819#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/826#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0); --Destroys the session and releases resources.
--This is done for you when your script ends,
--but if your script contains an infinite loop
--you can use this to terminate the session.
end
until i == 3
elseif (digits == "2") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/support_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/825#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/830#192.168.1.240");
session:hangup();
destroy(0);
elseif(digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "3") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/808#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/878#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "4") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/890#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/891#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif (digits == "5") then
repeat
digits = session:playAndGetDigits(1, 3, 3, 3000, "#", "/var/sipxdata/mediaserver/data/ivrscript/sampleLua/prompts/SPG_greet_long.wav","shout://translate.google.com/translate_tts?tl=en&q=This+option+is+not+valid", "\\d+");
if (digits == "1") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/851#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "2") then
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/872#192.168.1.240");
session:hangup();
destroy(0);
elseif (digits == "9") then
i = i + 1
elseif(digits == "0") then
i = 0
j = 0
break
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until i == 3
elseif(digits == "0") then
j = j + 1
else
language = "de"
session:execute("set","transfer_ringback=$${us-ring}");
session:execute("bridge","sofia/coraltele.com/898#192.168.1.240");
session:hangup();
destroy(0);
end
until j == 2
end

Resources