package de.danielbasedow.prospecter.core.query.build;


import aima.core.logic.propositional.parsing.ast.ComplexSentence;
import aima.core.logic.propositional.parsing.ast.Connective;
import aima.core.logic.propositional.parsing.ast.Sentence;
import de.danielbasedow.prospecter.core.query.Condition;

import java.util.ArrayList;

public class PropositionalSentenceMapper {
    public static Sentence map(ClauseNode clause) {
        if (!clause.isLeaf()) {
            return mapAsComplexSentence((Clause) clause);
        } else {
            return mapAsAtomicSentence((Condition) clause);
        }
    }

    private static Sentence mapAsAtomicSentence(Condition clause) {
        return new PropositionSymbol(clause);
    }

    private static Sentence mapAsComplexSentence(Clause clause) {
        ArrayList<Sentence> subSentences = new ArrayList<Sentence>();
        for (ClauseNode clauseNode : clause.getSubClauses()) {
            subSentences.add(map(clauseNode));
        }

        if (subSentences.size() == 1) {
            return subSentences.get(0);
        }

        Connective connective;
        switch (clause.getType()) {
            case AND:
                connective = Connective.AND;
                break;
            case OR:
                connective = Connective.OR;
                break;
            case NOT:
                connective = Connective.NOT;
                break;
            default:
                connective = Connective.AND;
        }

        Sentence[] sentenceArray = new Sentence[subSentences.size()];
        return new ComplexSentence(connective, subSentences.toArray(sentenceArray));
    }
}

@@ -320,16 +320,39 @@
 rayList;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -1517,121 +1517,320 @@
     
-Sentence%5B%5D sentenceArray = new Sentence%5BsubSentences.size()%5D;%0A        return new ComplexSentence(connective, subS
+return bracketIfNecessary(connective, subSentences);%0A    %7D%0A%0A    private static Sentence bracketIfNecessary(Connective connective, List%3CSentence%3E sentences) %7B%0A        while (sentences.size() %3E 1) %7B%0A            ComplexSentence newComplex = new ComplexSentence(%0A                    connective,%0A                    s
 ente
@@ -1838,15 +1838,14 @@
 ces.
-toArray
+remove
 (sen
@@ -1853,14 +1853,176 @@
 ence
-Array)
+s.size() - 1),%0A                    sentences.remove(sentences.size() - 1)%0A            );%0A            sentences.add(newComplex);%0A        %7D%0A        return sentences.get(0
 );%0A 

bracket complex sentences