001 /* 002 * Created on Sep 18, 2004 003 * 004 */ 005 package aima.logic.fol.domain; 006 007 import java.util.ArrayList; 008 import java.util.HashSet; 009 import java.util.List; 010 import java.util.Set; 011 012 /** 013 * @author Ravi Mohan 014 * @author Ciaran O'Reilly 015 */ 016 public class FOLDomain { 017 private Set<String> constants, functions, predicates; 018 private int skolemConstantIndexical = 0; 019 private int skolemFunctionIndexical = 0; 020 private int answerLiteralIndexical = 0; 021 private List<FOLDomainListener> listeners = new ArrayList<FOLDomainListener>(); 022 023 public FOLDomain() { 024 this.constants = new HashSet<String>(); 025 this.functions = new HashSet<String>(); 026 this.predicates = new HashSet<String>(); 027 } 028 029 public FOLDomain(FOLDomain toCopy) { 030 this(toCopy.getConstants(), toCopy.getFunctions(), toCopy 031 .getPredicates()); 032 } 033 034 public FOLDomain(Set<String> constants, Set<String> functions, 035 Set<String> predicates) { 036 this.constants = new HashSet<String>(constants); 037 this.functions = new HashSet<String>(functions); 038 this.predicates = new HashSet<String>(predicates); 039 } 040 041 public Set<String> getConstants() { 042 return constants; 043 } 044 045 public Set<String> getFunctions() { 046 return functions; 047 } 048 049 public Set<String> getPredicates() { 050 return predicates; 051 } 052 053 public void addConstant(String constant) { 054 constants.add(constant); 055 } 056 057 public String addSkolemConstant() { 058 059 String sc = null; 060 do { 061 sc = "SC" + (skolemConstantIndexical++); 062 } while (constants.contains(sc) || functions.contains(sc) 063 || predicates.contains(sc)); 064 065 addConstant(sc); 066 notifyFOLDomainListeners(new FOLDomainSkolemConstantAddedEvent(this, sc)); 067 068 return sc; 069 } 070 071 public void addFunction(String function) { 072 functions.add(function); 073 } 074 075 public String addSkolemFunction() { 076 String sf = null; 077 do { 078 sf = "SF" + (skolemFunctionIndexical++); 079 } while (constants.contains(sf) || functions.contains(sf) 080 || predicates.contains(sf)); 081 082 addFunction(sf); 083 notifyFOLDomainListeners(new FOLDomainSkolemFunctionAddedEvent(this, sf)); 084 085 return sf; 086 } 087 088 public void addPredicate(String predicate) { 089 predicates.add(predicate); 090 } 091 092 public String addAnswerLiteral() { 093 String al = null; 094 do { 095 al = "Answer" + (answerLiteralIndexical++); 096 } while (constants.contains(al) || functions.contains(al) 097 || predicates.contains(al)); 098 099 addPredicate(al); 100 notifyFOLDomainListeners(new FOLDomainAnswerLiteralAddedEvent(this, al)); 101 102 return al; 103 } 104 105 public void addFOLDomainListener(FOLDomainListener listener) { 106 synchronized (listeners) { 107 if (!listeners.contains(listener)) { 108 listeners.add(listener); 109 } 110 } 111 } 112 113 public void removeFOLDomainListener(FOLDomainListener listener) { 114 synchronized (listeners) { 115 listeners.remove(listener); 116 } 117 } 118 119 // 120 // PRIVATE METHODS 121 // 122 private void notifyFOLDomainListeners(FOLDomainEvent event) { 123 synchronized (listeners) { 124 for (FOLDomainListener l : listeners) { 125 event.notifyListener(l); 126 } 127 } 128 } 129 }