这是我的程序:
是一个根据用户输入提出最适合推荐哪种漏洞扫描工具的问题的程序。
我尽力尝试添加检查代码哪些部分检查正确的方法,但发现无法真正存储用户偏好并打印适当的扫描仪推荐
无论我尝试什么输入,都只得到“无扫描仪推荐”。
相信这可能是因为它的规则是基于进度而扫描仪建议没有正确存储?
(deftemplate user-preference
(slot scan-type (type SYMBOL))
(slot environment (type SYMBOL)))
(deftemplate scanner-recommendation
(slot scanner-name (type STRING))
(slot description (type STRING)))
(deftemplate question-progress
(slot stage (type SYMBOL)))
;; Initialize question stage
(defrule initialize-question-stage
(not (question-progress))
=>
(assert (question-progress (stage next)))
(printout t "Initialization complete. Stage set to 'next'." crlf))
;; Rule to ask initial scan type
(defrule ask-scan-type
(not (user-preference (scan-type ?)))
?f <- (question-progress (stage next))
=>
(printout t "What type of scan are you interested in? (webapp/network/cloud/infrastructure): " crlf)
(bind ?input (read))
(if (or (eq ?input webapp) (eq ?input network) (eq ?input cloud) (eq ?input infrastructure)) then
(assert (user-preference (scan-type ?input)))
(printout t "Scan type set to: " ?input crlf)
(retract ?f)
(assert (question-progress (stage follow-up)))
else
(printout t "Invalid input. Please enter 'webapp', 'network', 'cloud', or 'infrastructure'." crlf)
(retract ?f)
(assert (question-progress (stage next)))))
;; Ask environment for webapp scans
(defrule ask-webapp-environment
(user-preference (scan-type webapp))
?f <- (question-progress (stage follow-up))
=>
(printout t "Are you looking for a comprehensive or entry-level webapp scanner? " crlf)
(bind ?input (read))
(if (or (eq ?input comprehensive) (eq ?input entry-level)) then
(assert (user-preference (environment ?input)))
(printout t "Environment set to: " ?input crlf)
(retract ?f)
(assert (question-progress (stage complete)))
else
(printout t "Invalid input. Please enter 'comprehensive' or 'entry-level'." crlf)))
;; Ask environment for network scans
(defrule ask-network-environment
(user-preference (scan-type network))
?f <- (question-progress (stage follow-up))
=>
(printout t "Do you prefer deep or fast network scanning? " crlf)
(bind ?input (read))
(if (or (eq ?input deep) (eq ?input fast)) then
(assert (user-preference (environment ?input)))
(printout t "Environment set to: " ?input crlf)
(retract ?f)
(assert (question-progress (stage complete)))
else
(printout t "Invalid input. Please enter 'deep' or 'fast'." crlf)))
;; Ask environment for cloud scans
(defrule ask-cloud-environment
(user-preference (scan-type cloud))
?f <- (question-progress (stage follow-up))
=>
(printout t "Are you focused on cloud security or container security? " crlf)
(bind ?input (read))
(if (or (eq ?input cloud) (eq ?input container)) then
(assert (user-preference (environment ?input)))
(printout t "Environment set to: " ?input crlf)
(retract ?f)
(assert (question-progress (stage complete)))
else
(printout t "Invalid input. Please enter 'cloud' or 'container'." crlf)))
;; Ask environment for infrastructure scans
(defrule ask-infrastructure-environment
(user-preference (scan-type infrastructure))
?f <- (question-progress (stage follow-up))
=>
(printout t "Is this for IT infrastructure or endpoint security? " crlf)
(bind ?input (read))
(if (or (eq ?input infrastructure) (eq ?input endpoint)) then
(assert (user-preference (environment ?input)))
(printout t "Environment set to: " ?input crlf)
(retract ?f)
(assert (question-progress (stage complete)))
else
(printout t "Invalid input. Please enter 'infrastructure' or 'endpoint'." crlf)))
;; Recommendations based on user input
;; Webapp scan recommendations
(defrule recommend-invicti
(user-preference (scan-type webapp) (environment comprehensive))
=>
(assert (scanner-recommendation (scanner-name "Invicti")
(description "Best for comprehensive webapp scanning.")))
(printout t "Recommendation rule triggered for Invicti." crlf))
(defrule recommend-stackhawk
(user-preference (scan-type webapp) (environment entry-level))
=>
(assert (scanner-recommendation (scanner-name "StackHawk")
(description "Best entry-level scanner for webapp security.")))
(printout t "Recommendation rule triggered for StackHawk." crlf))
;;; Other scan recommendations with same / similar code to Webapp scan reccommendations
;;; e.g. network, cloud and infrastructure
;; No solution if no scanner-recommendation is found
(defrule no-solution
(question-progress (stage complete))
(not (scanner-recommendation (scanner-name ?)))
=>
(printout t "No scanner recommendations based on your selections." crlf))
;; Output best solution if scanner recommendation exists
(defrule best-solution
(question-progress (stage complete))
(scanner-recommendation (scanner-name ?name) (description ?desc))
=>
(printout t "Recommended Scanner: " ?name crlf)
(printout t "Description: " ?desc crlf))
当您的程序运行时,您将创建两个不同的用户偏好事实,每个事实只有一个所需的值:
您的 recommend-invicti 规则仅期望一个具有两个所需值的事实:
修改规则,以便只为一个事实分配适当的值: