import java.util.*; class BooleanLogic { Random random = new Random(); public static void main(String[] args) { String rules = "a<-b,c;b<-d,e;c<-d,f;d;e;f"; hornReasoning(rules.split(";")); unitResolution(rules.split(";")); } public static String neg(String pTerm) { if (pTerm.startsWith("-")) return pTerm.substring(1); else return "-"+pTerm; } public static String rewrite(String rule) { StringBuffer rzRule = new StringBuffer(); String head = head(rule, "<-"); String body = tail(rule, "<-"); rzRule.append(head); if (body.length()> 0) { String[] conds = body.split(","); for (int i=0; i0) continue; // it's not unit rule. String term = rule1; String negTerm = neg(term); for (int rj=0; rj0) { String unifyExp = replace(exp, ","+negTerm+",", ","); String newRule = unifyExp.substring(1, unifyExp.length()-1); rules.add(newRule); System.out.println("unfiy("+rule1+"|"+rule2+")="+newRule); } } } } public static void hornReasoning(String[] rules) { TreeMap trueMap = new TreeMap(); int satisfyRuleCount; do { satisfyRuleCount = 0; for (int ri=0; ri 0); } public static String head(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return pStr; return pStr.substring(0,spliterPos); } public static String tail(String pStr, String pSpliter) { int spliterPos = pStr.indexOf(pSpliter); if (spliterPos < 0) return ""; return pStr.substring(spliterPos+pSpliter.length()); } public static String replace(String pStr, String fromPat, String toPat) { if (fromPat.length()==0) return pStr; if (pStr.indexOf(fromPat)<0) return pStr; StringBuffer rzStr = new StringBuffer(); int strIdx = 0, nextIdx; while ((nextIdx = pStr.indexOf(fromPat, strIdx))>=0) { rzStr.append(pStr.substring(strIdx, nextIdx)); rzStr.append(toPat); strIdx = nextIdx + fromPat.length(); } rzStr.append(pStr.substring(strIdx)); return rzStr.toString(); } }

AltStyle によって変換されたページ (->オリジナル) /