package ra;
import java.util.ArrayList;
import java.util.List;
import operators.Operator;
import core.Formulas;
import core.Relation;
/**
*
* @author Jacek Sroka
*/
public class Result1 {
private final Formulas formulas;
private final List<Relation> dummyRelations;
private final List<Relation> resultRelations;
static private List<Relation> copyArrayListOfRelations(List<Relation> input) {
ArrayList<Relation> copy = new ArrayList<>();
for (Relation r : input)
copy.add(new Relation(r));
return copy;
}
public Result1(Formulas formulas, ArrayList<Relation> dummyRelations, ArrayList<Relation> resultRelations) {
this.formulas = new Formulas(formulas);
formulas.sortColumns();
this.dummyRelations = copyArrayListOfRelations(dummyRelations);
this.resultRelations = copyArrayListOfRelations(resultRelations);
}
public Result1(Formulas formulas, List<Relation> dummyRelations, List<Relation> resultRelations) {
this(formulas, new ArrayList<>(dummyRelations), new ArrayList<>(resultRelations));
}
static private ArrayList<Relation> buildSingletonIfNotEmpty(Relation r) {
ArrayList<Relation> dummies = new ArrayList<>();
if (r.getCardinality() != 0) {
dummies.add(r);
}
return dummies;
}
static private ArrayList<Relation> buildSingleton(Relation r) {
ArrayList<Relation> dummies = new ArrayList<>();
dummies.add(r);
return dummies;
}
public Result1(Formulas formulas, Relation dummyRelation, Relation resultRelation) {
this(formulas, buildSingletonIfNotEmpty(dummyRelation), buildSingleton(resultRelation));
}
public Result1(Formulas formulas, List<Relation> dummyRelations, Relation resultRelation) {
this(formulas, dummyRelations, buildSingleton(resultRelation));
}
public Result1(Formulas formulas, Relation dummyRelation, List<Relation> resultRelations) {
this(formulas, buildSingletonIfNotEmpty(dummyRelation), resultRelations);
}
public Formulas getFormulas() {
return formulas;
}
public List<Relation> getDummyRelations() {
return dummyRelations;
}
public Relation getLastDummyRelations() {
return dummyRelations.get(dummyRelations.size()-1);
}
public List<Relation> getResultRelations() {
return resultRelations;
}
public Relation getResultRelation() {
return resultRelations.get(resultRelations.size()-1);
}
//extends this and returns it
public Result1 extend(Formulas f, Relation dummyRelation, Relation resultRelation) {
formulas.putAll(f);
formulas.sortColumns();
dummyRelations.add(new Relation(dummyRelation));
System.out.println("k6"+Utils.getPreviouslyComputedResult("tab").getResultRelation().getCardinality());
resultRelations.add(new Relation(resultRelation));
System.out.println("k7"+Utils.getPreviouslyComputedResult("tab").getResultRelation().getCardinality());
return this;
}
//extends this and returns it
public Result1 extend(Formulas f, List<Relation> dummyRelations, List<Relation> resultRelation) {
formulas.putAll(f);
formulas.sortColumns();
this.dummyRelations.addAll(copyArrayListOfRelations(dummyRelations));
this.resultRelations.addAll(copyArrayListOfRelations(resultRelations));
return this;
}
public Result1 extend(Operator oper) {
System.out.println("k2"+Utils.getPreviouslyComputedResult("tab").getResultRelation().getCardinality());
return extend(oper.getFormulas(), oper.getDummyRelation(), oper.getResultRelation());
}
public Result1 extend(Result1 other) {
return extend(other.getFormulas(), copyArrayListOfRelations(other.getDummyRelations()), copyArrayListOfRelations(other.getResultRelations()));
}
}