package hu.webarticum.jsatbuilder.builder.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

import hu.webarticum.jsatbuilder.builder.core.AbstractConstraint;
import hu.webarticum.jsatbuilder.builder.core.CollapseException;
import hu.webarticum.jsatbuilder.builder.core.Definition;
import hu.webarticum.jsatbuilder.solver.core.Solver;

public class GeneralClauseSetConstraint extends AbstractConstraint {
    
    private final List<List<DefinitionLiteral>> clauses;
    
    private final Definition condition;
    
    public GeneralClauseSetConstraint(boolean required, Collection<? extends Collection<DefinitionLiteral>> clauses, Definition condition) {
        super(required);
        this.clauses = new ArrayList<List<DefinitionLiteral>>();
        for (Collection<DefinitionLiteral> clause: clauses) {
            this.clauses.add(new ArrayList<DefinitionLiteral>(clause));
        }
        this.condition = condition;
        for (Definition definition: getDependencies()) {
            getDependencyManager().linkDependency(definition);
        }
    }

    @Override
    public List<Definition> getDependencies() {
        Map<Definition, Object> dependencies = new IdentityHashMap<Definition, Object>();
        for (List<DefinitionLiteral> clause: clauses) {
            for (DefinitionLiteral literal: clause) {
                dependencies.put(literal.getDefinition(), null);
            }
        }
        if (condition != null) {
            dependencies.put(condition, null);
        }
        return new ArrayList<Definition>(dependencies.keySet());
    }

    @Override
    public void dependencyRemoved(Definition definition) throws CollapseException {
        // XXX
        remove();
    }

    @Override
    public void fillSolver(Solver solver) {
        for (List<DefinitionLiteral> clause: clauses) {
            Solver.Clause solverClause = new Solver.Clause();
            if (condition != null) {
                solverClause.addLiteral(new Solver.Literal(condition, false));
            }
            for (DefinitionLiteral literal: clause) {
                solverClause.addLiteral(literal.toSolverLiteral());
            }
            solver.add(solverClause);
        }
    }
    
}

@@ -68,24 +68,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -122,24 +122,24 @@
 Collection;%0A
-
 import java.
@@ -620,24 +620,1013 @@
 n condition;
+%0A%0A    public GeneralClauseSetConstraint(DefinitionLiteral%5B%5D%5B%5D clauses) %7B%0A        this(true, clauses, null);%0A    %7D%0A    %0A    public GeneralClauseSetConstraint(boolean required, DefinitionLiteral%5B%5D%5B%5D clauses) %7B%0A        this(required, clauses, null);%0A    %7D%0A    %0A    public GeneralClauseSetConstraint(boolean required, DefinitionLiteral%5B%5D%5B%5D clauses, Definition condition) %7B%0A        super(required);%0A        this.clauses = new ArrayList%3CList%3CDefinitionLiteral%3E%3E();%0A        for (DefinitionLiteral%5B%5D clause: clauses) %7B%0A            this.clauses.add(new ArrayList%3CDefinitionLiteral%3E(Arrays.asList(clause)));%0A        %7D%0A        this.condition = condition;%0A        linkDependencies();%0A    %7D%0A%0A    public GeneralClauseSetConstraint(Collection%3C? extends Collection%3CDefinitionLiteral%3E%3E clauses) %7B%0A        this(true, clauses, null);%0A    %7D%0A    %0A    public GeneralClauseSetConstraint(boolean required, Collection%3C? extends Collection%3CDefinitionLiteral%3E%3E clauses) %7B%0A        this(required, clauses, null);%0A    %7D
 %0A    %0A    pu
@@ -1986,32 +1986,32 @@
 se));%0A        %7D%0A
-
         this.con
@@ -2022,32 +2022,109 @@
 on = condition;%0A
+        linkDependencies();%0A    %7D%0A    %0A    private void linkDependencies() %7B%0A
         for (Def

Add more constructors to GeneralClauseSetConstraint