aima.core.logic.propositional.visitors.ClauseCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.logic.propositional.visitors;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import aima.core.logic.propositional.kb.data.Clause;
import aima.core.logic.propositional.kb.data.Literal;
import aima.core.logic.propositional.parsing.ast.ComplexSentence;
import aima.core.logic.propositional.parsing.ast.PropositionSymbol;
import aima.core.logic.propositional.parsing.ast.Sentence;
/**
* Utility class for collecting clauses from CNF Sentences.
*
* @author Ravi Mohan
* @author Ciaran O'Reilly
*/
public class ClauseCollector extends BasicGatherer {
/**
* Collect a set of clauses from a list of given sentences.
*
* @param cnfSentences
* a list of CNF sentences from which to collect clauses.
* @return a set of all contained clauses.
* @throws IllegalArgumentException
* if any of the given sentences are not in CNF.
*/
public static Set getClausesFrom(Sentence... cnfSentences) {
Set result = new LinkedHashSet();
ClauseCollector clauseCollector = new ClauseCollector();
for (Sentence cnfSentence : cnfSentences) {
result = cnfSentence.accept(clauseCollector, result);
}
return result;
}
@Override
public Set visitPropositionSymbol(PropositionSymbol s, Set arg) {
// a positive unit clause
Literal positiveLiteral = new Literal(s);
arg.add(new Clause(positiveLiteral));
return arg;
}
@Override
public Set visitUnarySentence(ComplexSentence s, Set arg) {
if (!s.getSimplerSentence(0).isPropositionSymbol()) {
throw new IllegalStateException("Sentence is not in CNF: "+s);
}
// a negative unit clause
Literal negativeLiteral = new Literal((PropositionSymbol)s.getSimplerSentence(0), false);
arg.add(new Clause(negativeLiteral));
return arg;
}
@Override
public Set visitBinarySentence(ComplexSentence s, Set arg) {
if (s.isAndSentence()) {
s.getSimplerSentence(0).accept(this, arg);
s.getSimplerSentence(1).accept(this, arg);
} else if (s.isOrSentence()) {
List literals = new ArrayList(LiteralCollector.getLiterals(s));
arg.add(new Clause(literals));
} else {
throw new IllegalArgumentException("Sentence is not in CNF: "+s);
}
return arg;
}
//
// PRIVATE
//
private static class LiteralCollector extends BasicGatherer {
private static Set getLiterals(Sentence disjunctiveSentence) {
Set result = new LinkedHashSet();
LiteralCollector literalCollector = new LiteralCollector();
result = disjunctiveSentence.accept(literalCollector, result);
return result;
}
@Override
public Set visitPropositionSymbol(PropositionSymbol s, Set arg) {
// a positive literal
Literal positiveLiteral = new Literal(s);
arg.add(positiveLiteral);
return arg;
}
@Override
public Set visitUnarySentence(ComplexSentence s, Set arg) {
if (!s.getSimplerSentence(0).isPropositionSymbol()) {
throw new IllegalStateException("Sentence is not in CNF: "+s);
}
// a negative literal
Literal negativeLiteral = new Literal((PropositionSymbol)s.getSimplerSentence(0), false);
arg.add(negativeLiteral);
return arg;
}
@Override
public Set visitBinarySentence(ComplexSentence s, Set arg) {
if (s.isOrSentence()) {
s.getSimplerSentence(0).accept(this, arg);
s.getSimplerSentence(1).accept(this, arg);
} else {
throw new IllegalArgumentException("Sentence is not in CNF: "+s);
}
return arg;
}
}
}