问题描述
我正在使用 pyswip 从 Python 3 运行我的 Prolog 查询,但对查询的响应不是我所期望的。而不是:X = us,D = [fact2,ipGeolocation(us)] ?
我得到了 {'X': 'us','D': [Atom('712197'),Atom('711429'),Functor(15831181,1,us)]}
。我如何去掉 Atom 和 Functor 关键字并获得实际值,就像在第一个实例中一样?看看我附上的两个截图。
这些分别是我的 prolog 和 python 代码:
序言
:- compile('/PATH_TO_GORGIAS/gorgias-src-0.6d/lib/gorgias.pl').
:- compile('/PATH_TO_GORGIAS/gorgias-src-0.6d/ext/lpwnf.pl').
% Rules
rule(notGuiltyByDefault(X),(neg(isCulprit(X)),[]).
rule(ipGeolocation(X),isCulprit(X),[ipGeoloc(X,IP)]).
rule(spoofedIp(X),neg(isCulprit(X)),IP),spoofedIP(IP)]).
% Facts
rule(fact1,ipGeoloc(china,ip1),[]).
rule(fact2,ipGeoloc(us,ip2),[]).
rule(fact3,spoofedIP(ip1),[]).
%Priority/Preference
rule(p1(X),prefer(spoofedIp(X),ipGeolocation(X)),[]).
rule(p2(X),prefer(ipGeolocation(X),notGuiltyByDefault(X)),[]).
蟒蛇
def get_argument():
parser = argparse.ArgumentParser(description=display_banner())
parser.add_argument("-q","--query",dest= "query",help="Query")
option = parser.parse_args()
if not option.query:
parser.error(f"{Red}[-] You need to specify query,enter -h for help")
raise SystemExit
return option.query
def query_kNowledge_base(query_term):
prolog = Prolog()
prolog.consult('code.pl')
return list(prolog.query(query_term))
def print_query_result(query_term,query_result_list):
print(f'[+] The query submitted is: {Green}%s{Reset}' % (query_term))
if not query_result_list:
print(f'\n[-] The result of your submitted query: {Red}%s{Reset}' % ('False'))
raise SystemExit
for query_result in query_result_list:
if len(query_result) == 0:
print(f'\n[+] The result of your submitted query: {Green}%s{Reset}' % ('True'))
raise SystemExit
str(query_result)
print(f'\n[+] The result of your submitted query: {Green}{query_result}{Reset}')
query_term = get_argument()
query_result_list = query_kNowledge_base(query_term)
print_query_result(query_term,query_result_list)
而不是使用这些 Atom 和 Functor 关键字输出:
我想要这样的输出,没有 Atom 和 Functor 关键字,而是实际值:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)