2015-10-06 56 views
0

我想指定一個人對某件事的評論,如果是肯定或否定,但不能同時出現。如何在prolog中指定一個獨特的事實?

我希望把我的文件一般規則和這些事實:

:-comment(Person, Thing, B), comment(Person, Thing, OtherB), B \=OtherB. 
comment(person, book, positive). 
comment(person, book, negative). 

,當我嘗試做一個查詢,我會收到一個錯誤,或者說告訴我的東西是矛盾的。

當然是有效的如下事實:

comment(person, book, positive). 
comment(person, icecream, negativa). 
+2

*當*你想執行規則? '評論(P,O,C),評論(P,O,D),C \ = D,拋出(invalid_comment).' – CapelliC

+0

@CapelliC它不起作用。我試圖把它放在一個文件中,所以在我將它加載到控制檯後它會相互矛盾。但事實並非如此。 – augu

回答

1

您應該通過以下方式添加到您的文件checkContradictory(至少在gnuprolog):

yourfile.lp

comment(person, book, positive). 
comment(person, book, negative). 

checkContradictory:- checkCommentContradiction, ... others checking predicates 

checkCommentContradiction:-comment(Person, Thing, positive), 
      comment(Person, Thing, negative),throw(contradiction_with_comments). 

所以,如果你想查詢前檢查您的文件,只需執行你的checkContradictory或者如果你有一個主謂,只是添加checkContradictory樣的要求。

重要,如果你需要有一個是沒有錯誤的和異常的時候有你需要添加的findall一個矛盾:

yourfile.lp

comment(person, book, positive). 
comment(person, book, negative). 

checkFreeOfContradictory:- checkAllCommentsContradictions. 

checkAllCommentsContradictions:-findall(X,checkCommentContradiction,R). 

checkCommentContradiction:-comment(Person, Thing, B1), 
      comment(Person, Thing, B2), 
      (B1==B2->true;throw(contradiction_with_comments)). 

主要是因爲同一事實將與評論(人,東西,B1)評論(人,東西,B2)統一。

+0

非常感謝!我不知道如何寫入我的知識庫。 :)是的,我意識到這一點;) – augu

0

如果重構謂詞,會更容易嗎? 以這樣的方式更換一個謂語由兩個:

positive_comment(Person,Book). 
negative_comment(Person,Book). 

然後使用一些像

positive_comment(Person,Book):- 
negative_comment(Person,Book), 
throw_some_error, 
false. 
negative_comment(Person,Book):- 
positive_comment(Person,Book), 
throw_some_error, 
false. 

或更好地利用不同的檢查:

check_comments_consistency(Person,Book):- 
    positive_comment(Person,Book), 
    negative_comment(Person,Book), 
    throw_some_error. 

...或者類似的東西。

你明白了嗎?

+0

是的,我這樣做。但是如果我想提出第三種意見(例如:優柔寡斷)呢?如果我試圖對其他謂詞做同樣的事情,它將是NumberOfPredicates * PossiblesValues – augu

+0

您將不會超過log2(N)表示複雜謂詞的簡單謂詞(N是不同意見的數量)。把它看作是某些決策樹中路徑的編碼。 – 4KCheshireCat

+0

https://en.wikipedia.org/wiki/Venn_diagram任何多值複雜邏輯都可以用布爾邏輯手動編碼。並在此之後正式簡化。 – 4KCheshireCat