Final Revision (CLIPS).pdf
Document Details
Uploaded by FresherVerism
2019
Tags
Related
- Two_dimensional_arrays_Advanced Computer Science.pdf
- TS ECET 2024 Computer Science Engineering Syllabus PDF
- 12th_Computer-Science_EM - www.tntextbooks.in.pdf
- Lyceum International School Grade 8 Computer Science Exam PDF - July 2023
- ICS 2601 Introduction to Computing MODULE 5 PDF
- Lecture (5) Computer Crime PDF
Full Transcript
Computer Science Level 4 Final-Revision الجزء العملي Expert Systems 2019 Clips: (C Language Integrated Production System) 1. Mathematical Operation: 1) 2*2 => ( * 2 2) 2) 4/2 =>...
Computer Science Level 4 Final-Revision الجزء العملي Expert Systems 2019 Clips: (C Language Integrated Production System) 1. Mathematical Operation: 1) 2*2 => ( * 2 2) 2) 4/2 => (/ 4 2) 3) 1+3 => (+ 1 3) 4) 4>3 => (> 4 3) 5) 2=2 => (= 2 2) 6) (eq a a) => TRUE 7) (neq a A) => FALSE 2. To define variable use bind 3. To define multi variable use create$ » Single variable (bind ?x 5) (bind ?y (read)) » Multi-Variable (bind $?x (create$ red blue green)) » Global variable » Single global variable (defglobal ?*x* = 3) » Multi-Global variable (defglobal ?*y* = (create$ 10 20 40)) 4. member$: Tell if a single field value is contained in a multi-field value. (member$ blue (create$ red green blue)) => 3 5. nth$: return specific field from a multi-field value (the value of the index) (nth$ 2 (create$ red green blue)) => green (nth$ 7 (create$ red green blue)) => nil 6. Length$: return the length of values. (length$ 2 3 4 5 7 9 ) =>6 7. To print use printout t (printout t “Hello” crlf) 1 8. Facts: » Ordered fact: (using assert & deffacts) » Non-ordered fact: (deftemplate) » To delete fact from memory use retract » Examples: 9. (assert(student(name ahmed)(age 23)(dept cs))) 10. (deffacts student (name ahmed)(age 23)(dept cs) (name aly)(age 22)(dept cs)) 11. (deftemplate student (slot name) (slot age) (slot address) (multislot degrees)) 12. (deffacts f1 (student(name Ahmed)(age 24)(address mans)(degrees 12 34 77)) (student(name Mohamed)(age 22)(address tanta)(degrees 92 34 27)) ) 13. Rules: 14. (defrule r1 => (printout t “Enter first number” crlf) (bind ?x (read)) (printout t “Enter second number ” crlf ) (bind ?y (read)) (printout t (+ ?x ?y) crlf)) (run) 2 15. Functions in Clips: Q1: Write a function to print the text "Hello", and then call it. 16. (deffunction Print() (printout t “Hello” crlf) ) 17. (Print) Q2: Write a function to print name of the student and the length of the degrees. 18. (deffunction Students ( ?n $?d) //$?d الزم يكون في النهاية فقط (printout t ?n “,” (length$ $?d) crlf) ) 19. (Students) Q3: True or False 20. (deffunction sum ($?d ?n ?s) ) ERROR 21. (deffunction sum( ?n $?d ?s)) ERROR 22. (deffunction sum( ?n ?s $?d )) TRUE Q4: Write a function to check if number is negative, positive or zero. 23. (deffunction check (?n) (if ( = ?n 0) then zero else (if ( > ?n 0) then positive else negative ))) 24. (check 5) Q5: Write a function to check if the value is number or not. 25. (deffunction test (?n) (if (numberp ?n) then number else Word)) 26. (test 4) 3 Q6: Write a function to print the name of student and the summation of degrees. 27. (deffunction sum(?n $?d) (bind ?sum 0) (prong$ (?i $?d) (bind ?sum (+ ?sum ?i)) ) (printout t ?n "," ?sum crlf) ) Q7: Write a function to calculate the factorial of any number 28. (deffunction factorial(?v) (If ( = ?v 0 ) then 1 else (* ?v ( factorial ( - ?v 1) ) ) ) Q8: Using loop-for-count function to print the text "hello" 3 times. 29. (deffunction test () (loop-for-count 3 do (printout t "hello" crlf) ) ) Q9: Write loop-for-count to print from 20 to 50. 30. (deffunction test ( ) (loop-for-count (?i 20 50) do (printout t ?i crlf) ) ) Q10: Write loop-for-count to calculate a factorial of any number. 31. (deffunction factorial ( ?f ) (bind ?result 1) (loop-for-count (?i 1 ?f) (bind ?result (* ?result ?i)) ) (printout t ?result crlf) ) 4 *********** Predicate Functions *********** 1) numberp: return true if the parameter is float or integer otherwise false. (numberp 2) (numberp -3) (numberp cairo) TRUE TRUE FALSE 2) floatp: return true if the argument is float otherwise return false. (floatp 12.3) (floatp -3.6) (floatp 3) (floatp aya) TRUE TRUE FALSE FALSE 3) Integerp: return true if the argument is integer otherwise return false (integerp 4) (integerp 60.3) TRUE FALSE 4) Lexemep: return true if the argument is string or symbol. String like: "helloworld" Symbol like: helloworld (lexemep ahmed) (lexemep "hello world") (lexemep 2) TRUE TRUE FALSE 5) stringp: return true if the argument is string. (stringp "hello") (stringp hello) TRUE FALSE 6) symbolp ( or wordp): return true if the argument is symbol (symbolp Mohamed) (symbolp "Mohamed") (wordp world) TRUE FALSE TRUE 7) evenp: return true if the argument is even. (evenp 5) (evenp 8) FALSE TRUE 8) oddp: return true if the argument is odd. (oddp 1) (oddp 6) TRUE FALSE 9) multifieldp (or sequencep): return true if the argument is multifield value. (multifieldp m) (multifieldp (create$ 2 4 5 )) (sequencep (create$ 2 4 5)) FALSE TRUE TRUE 10) Subsetp: check if one multifield value is a subset of another. (subsetp (create$ 4 3 7) (create$ 1 3 4 5 7 8 9 12)) => TRUE (subsetp (create$ mahalla luxor) (create$ cairo giza alex)) => FALSE 11) delete$: delete specified range from a multifield value. (delete$ (create$ a b c d e f ) 3 4) => a b e f 5 *********** Multi-field functions *********** 1) Create$: Create a multi-Field function.. إنشاء متغير متعدد (Create$ Red Green Blue) 2) member$: Tell if a single field value is contained in a multi-field value.. ترجع برقم قيمة داخل متغير متعدد (member$ blue (create$ red green blue)) => 3 3) nth$: return specific field from a multi-field value (the value of the index). ترجع بقيمة معينة داخل متغير متعدد عن طرق رقمها (nth$ 2 (create$ red green blue)) => green (nth$ 7 (create$ red green blue)) => nil 4) Length$: return the length of values. (length$ 2 3 4 5 7 9 ) =>6. ترجع بطول المتغير المتعدد 5) delete$: delete specified range from a multifield value.. حذف قيم من المتغير المتعدد عن طرق رقمها (delete$ (create$ a b c d e f ) 3 4) 6) explode$: function constructs a multi-field value from a string by using each field in a string as a field in a new multi-field value. symbol الىstring تقوم بالتحويل من (explode$ “Aya Mohamed Ahmed”) (Aya Mohamed Ahmed) 7) Implode$: function creates a single string from a multi-field value..string الىsymbol تقوم بالتحويل من (implode$ (create$ Mohamed Ahmed Hossam)) “ Mohamed Ahmed Hossam” 8) Subseq$: Extracts a specified range from a multi-field value.. بتقص جزء محدد من متغير متعدد (subseq$ (create$ A B C D E F G ) 2 4 ) (B C D) 9) replace$: replaces a range of field in a multi field value with a series of single field and/or multifield values.. تبدل قيم داخل متغير متعدد (replace$ (create$ a b c d e f ) 2 3 X Y Z) 6 (a X Y Z d e f) 10) insert$: inserts a series of single field and/or multi-field values at a specified location in a multi-field values.. إضافة قيم داخل متغير متعدد (insert$ (create$ a d e) 2 b c) (a b c d e) 11) first$: return the first field of multi-field value.. بترجع باخر قيمة داخل متغير متعدد (first$ (create$ a b c)) (a) (first$ (create$)) () 12) rest$: returns all values in a multi-field except fist.. بترجع بكل القيم ما عدا األول داخل المتغير المتعدد (rest$ (create$ a b c)) (bc) (rest$ (create$)) () *********** String Functions *********** 1) Str-length: return the length of string as integer. تقوم بعد الحروف والمسافات بين الكالم (str-length “Mohamed Ahmed”) 13 2) Length: return the length of string except spaces. (length “a b c d”) 4 3) Str-cat: Concatenates its arguments into a single string. واحد فقطString تقوم بالدمج في (str-cat “Micro “ Computer) “Micro Computer” 4) Sub-string: Retrieve a portion of a string from another string. قص جزء من النص عن طريق رقم اول حرف واخر حرف 7 (sub-string 10 17 “Personal Computer”) “Computer” 5) Str-index: Return the position of a string inside another string..Capital, small ترجع برقم اول حرف في الكلمة داخل جملة مع مراعات الحروف (str-index “Computer” “Personal Computer”) 10 (str-index “computer” “Personal Computer”) FALSE 6) Upcase: return a string or symbol with uppercase alphabetic characters. Capital تقوم بتحويل النص الى حروف (upcase “ hello world“) “HELLO WORLD” 7) Lowcase: return a string or symbol with lowercase alphabetic characters. Small تقوم بتحويل النص الى حروف (lowcase “ HELLO WORLD “) “hello world” 8) str-compare: compare two strings to determine their logical relationship.. في حالة عدم التشابه1- في حالة التشابه والرد بtrue مقارنة نصين والرد ب (str-compare “ab” “ab”) TRUE (str-compare “a” “b” ) -1 9) eval: evaluates the string as though it were entered at the command prompt..)،،،،، انشاء متغير متعدد، طرح، يقوم بتنفيذ الجمل داخل النص كما لو كانت امر (جمع (eval “ ( * 10 2 ) “ ) 20 (eval “ ( create$ a b c d e ) “ ) ( a b c d e) 10) build: evaluates the string as though it were entered at the command prompt. )،،،،،، تقوم بتنفيذ الجمل داخل النص كما لو كانت امر (انشاء قاعدة (build "(defrule myRule (initial-fact) => (assert (OK)))") True 8 *********** COOL *********** *********** Clips Object Oriented Language*********** Classes 1) If the class is super (defclass classname ( is-a USER) (slot variablename ) / ( multislot variablename) ) 2) If class inherit from another class. (defclass classname (is-a the classname which it is inherit from another class) ( slot variablename ) / ( multislot variablename) ) Q1. Define a A class a which it is a super class and contains (n1-n2), (defclass A (is-a USER) (slot n1) (slot n2) ) Q2. Write a rule to make instance from A class. (defrule r1 => (make-instance [x1] of A (n1 2) (n2 3)) (make-instance [x2] of A (n1 5) (n2 6)) ) Q3. Write a rule to print the value of the slot n1 – n2 in class A (defrule r3 (object (is-a A ) (n1 ?a ) (n2 ?b ) ) => (printout t “the value of class A is” ?a “and “ ?b crlf) ) Q4. Write output for the next: (defrule r3 (object (is-a A ) (n1 ?a) (n2 ?b)) => (printout t ?a "x" ?b "=" (*?a ?b) crlf) ) Output is: 5 x 6 = 30 2x3=6 9 Q5. Define a student class a which it is a super class and contains (name-age- address) , then , define another class called dept contains (deptname – group ), class dept inherit from class student. Write a rule to make instance from class student and class dept (defclass student (is-a USER) (slot name) (slot age) (slot address) ) (defclass dept (is-a student) (slot deptname) (slot group) ) (defrule r2 => (make-instance [y1] of student (name ahmed) (age 44) (address tanta)) (make-instance [y2] of dept (deptname cs) (group two)) (make-instance [y3] of dept (deptname it) (group one) (age 23)(name Mohamed)) ) Q6. Using the next: (defclass X (is -a USER) (slot a) (slot b) (slot c)) (defclass Y (is-a USER) (slot d)) (defclass z (is-a USER) (slot a)) (defrule r5 => (make-instance [x1] of X (a 20)(b 3)) (make-instance [x2] of X (a 40)(b 5)) (make-instance [x3] of X (a 10)(b 4)) (make-instance [y1] of Y (d 50)) (make-instance [y2] of Y (d 20)) (make-instance [z1] of Z (a 70)) ) 10 1) write a rule to get equivalent instances of X and Y (defrule r1 (object(is-a X)(a ?n1)) (object(is-a Y)(d ?n1)) => (printout t ?n1 crlf)) 2) Write a rule to get instances of X or Y (defrule r2 ?inst (printout t ?inst crlf)) 3) Write a rule to get instances not of X (defrule r3 ?inst (printout t ?inst crlf)) 4) Write a rule to Print all instances. (defrule r4 ?inst (printout t ?inst crlf)) 5) Write a rule to delete all instances of Y (defrule delete ?inst (unmake-instance ?inst) (printout t “this instance was deleted “ ?inst crlf)) To modify instance using » modify » send Ex: (defclass student (is-a USER) (slot name) (slot age) ) (make-instance [c1] of student (name ahmed) (age 25 ) ) 6) Edit this instance make age = 30 (send [c1] put-age 30) Or (modify-instance c1 (age 30)) 11 *************I/O system*********** 1) Open a file: (open [] Mode: » r: read access only. (default) » w: write access only. » r+: read and write access. » a: append access only. Ex: Clips> (open "d:\\test1.txt" f1 "w") True 2) Close a file: Close []) If a close called without arguments, all open file will be closed. Close function return true if file close successfully otherwise false. Ex: Clips> (open "d:\\test1.txt" f1 "w") True Clips> (;open "d:\\test2.txt" f2 "w") True Clips> (open "d:\\test3.txt" f3 "w") True Clips> (close f2) True Clips (close) True 3) Rename a file: (rename ) Ex: Clips> (rename "d:\\output.txt" "d:\\data.txt") True 4) Remove a file: 12 (remove ) Ex: Clips> (remove "d:\\output.txt") True 5) Write to a file: (printout ) Ex: Clips> (open "d:\\myTest.txt" f1 "w") True Clips>(printout f1 "my name is Ali") Clips>(printout f1 crlf) Clips>(close f1) True 6) Add data to file: Open file as append. Ex: Clips> (open "d:\\myTest.txt" f1 "a") True Clips>(printout f1 "I live in Mansoura") Clips>(printout f1 crlf) Clips>(close f1) 7) Read data from file: (read []) Ex: Clips> (open "d:\\myTest.txt" f1 "r") True Clips>(read f1) MY Clips>(read f1) name Clips>(read f1) is 13 Clips>(read f1) Ali Clips>(close f1) True (readline []) Ex: Clips> (open "d:\\myTest.txt" f1 "r") True Clips> (readline f1) My name is Ali Clips> (readline f1) EOF Clips> Q1. Write a rule to read all data line in the "d:\\text.txt" file. *************************************** Clips> (defrule Readline => (close) (open "d:\\text.txt f1 "r") (while True do (bind ?d (readline f1)) (if (eq ?d EOF) then (break)) (printout t ?d crlf) ) 8) Format Allow the user to send formatted output to a device. (format ) A logical name of nil may be used when formatted without writting to a device. 14 Ex: Clips> (format nil "Hello%nMohamed") "Hello Mohamed" Clips> (format nil "Age:%d 20") "Age: 20" Clips> (format nil "Phone:%11d 123") "Phone: 123" Clips> (format nil "Phone:%-11d 123") "Phone: 123 " Clips> (format nil "Weight:%f 70.5") "Weight: 70.500000" Clips> (format nil "Float:10%1f 70.48") "Float: 70.5" Clips> (format nil "Float:-10%1f 70.48") "Float: 70.5 " Clips> (format nil "Hexadecimal: |%x|" 15) "Hexadecimal: |f|" Clips> (format nil "Octal: |%O|" 16) "Octal: |20|" Clips> (format nil "Symbols: |%s||%s|" "Expert" "System") "Symbols: Expert System" 15