public Object visitConnectedSentence(ConnectedSentence sentence, Object arg) {
// Distribute V over ^:
// This will cause flattening out of nested ^s and Vs
Sentence alpha = (Sentence) sentence.getFirst().accept(this, arg);
Sentence beta = (Sentence) sentence.getSecond().accept(this, arg);
// (alpha V (beta ^ gamma)) equivalent to
// ((alpha V beta) ^ (alpha V gamma))
if (Connectors.isOR(sentence.getConnector())
&& ConnectedSentence.class.isInstance(beta)) {
ConnectedSentence betaAndGamma = (ConnectedSentence) beta;
if (Connectors.isAND(betaAndGamma.getConnector())) {
beta = betaAndGamma.getFirst();
Sentence gamma = betaAndGamma.getSecond();
return new ConnectedSentence(Connectors.AND,
(Sentence) (new ConnectedSentence(Connectors.OR, alpha,
beta)).accept(this, arg),
(Sentence) (new ConnectedSentence(Connectors.OR, alpha,
gamma)).accept(this, arg));
}
}
// ((alpha ^ gamma) V beta) equivalent to
// ((alpha V beta) ^ (gamma V beta))
if (Connectors.isOR(sentence.getConnector())
&& ConnectedSentence.class.isInstance(alpha)) {
ConnectedSentence alphaAndGamma = (ConnectedSentence) alpha;
if (Connectors.isAND(alphaAndGamma.getConnector())) {
alpha = alphaAndGamma.getFirst();
Sentence gamma = alphaAndGamma.getSecond();
return new ConnectedSentence(Connectors.AND,
(Sentence) (new ConnectedSentence(Connectors.OR, alpha,
beta)).accept(this, arg),
(Sentence) (new ConnectedSentence(Connectors.OR, gamma,
beta)).accept(this, arg));