Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am running my Prolog query from Python 3 using pyswip, but the response to the query is not what I am expecting. Instead of: X = us, D = [fact2,ipGeolocation(us)] ? I am getting {'X': 'us', 'D': [Atom('712197'), Atom('711429'), Functor(15831181,1,us)]}. How do I do away with the Atom and Functor keywords and get the actual values, as in the first instance? Look at the two screenshots I have attached.

These are my prolog and python codes respectively:

Prolog

:- 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)), [ipGeoloc(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)), []).

Python

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'
[-] 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'
[+] The result of your submitted query: {Green}%s{Reset}' % ('True'))
            raise SystemExit

        str(query_result)
        print(f'
[+] 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)

Instead of having the output with these Atom and Functor keywords:

enter image description here

I want the output like this, without the Atom and Functor keywords, but the actual values:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
265 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...