/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fasp;
import csp.backends.TailorSolver;
import csp.convertors.FuzzyToIntegerConvertor;
import csp.datatypes.CSPSolution;
import expressions.DoubleNegationEliminator;
import expressions.FuzzyExpression;
import fasp.datatypes.*;
import fasp.datatypes.FaspConstant;
import fasp.datatypes.GroundLiteral;
import fasp.parser.parsers.LiteralParser;
import fasp.parser.tokenizer.TokenState;
import finiteReduction.DomainFinder;
import finiteReduction.FiniteReductionConfig;
import fuzzysat.FuzzyClause;
import fuzzysat.FuzzyLiteral;
import fuzzysat.Literal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import operators.MultiOperator;
import operators.N;
import operators.NM;
import operators.TM;
import operators.TW;
import operators.UnaryOperator;
import util.Pair;
/**
*
* @author Jeroen Janssen <Jeroen.Janssen@vub.ac.be>
*/
public class TPLPProgram1 {
public static void main(String[] args) throws Exception {
// GroundLiteral tDistHead = createRegLiteral("totNear");
// ArrayList<FaspFuzzyExpression> tDistBody = new ArrayList<FaspFuzzyExpression>();
// for (FaspConstant a : atm) {
// for (FaspConstant t : town) {
// tDistBody.add(createRegLiteral("atmNear", a, t));
// }
// }
// GroundRule tDistRule = new GroundRegularRule(tDistHead, tDistBody, new TM());
// rules.add(tDistRule);
GroundLiteral aHead = createRegLiteral("a");
ArrayList<FaspFuzzyExpression> aBody = new ArrayList<FaspFuzzyExpression>();
aBody.add(createNafLiteral("b",new NM()));
GroundRule aRule = new GroundRegularRule(aHead,aBody,new TW());
GroundLiteral bHead = createRegLiteral("b");
ArrayList<FaspFuzzyExpression> bBody = new ArrayList<FaspFuzzyExpression>();
bBody.add(createNafLiteral("a",new NM()));
GroundRule bRule = new GroundRegularRule(bHead,bBody,new TW());
ArrayList<GroundRule> rules = new ArrayList<GroundRule>();
rules.add(aRule);
rules.add(bRule);
GroundProgram prog = new GroundProgram(rules);
int defaultBound = 10;
int k = 1;
Map<String, Double> solution = null;
FaspInterpretation i = null;
List<FuzzyClause> clauses = prog.createCompletion2();
for (FuzzyClause c : clauses) {
System.out.println(c);
}
FuzzyToIntegerConvertor ficonv =
new FuzzyToIntegerConvertor(defaultBound, k);
// DoubleNegationEliminator dnegElim = new DoubleNegationEliminator();
// ArrayList<FuzzyClause> newClauses = new ArrayList<FuzzyClause>();
// for (FuzzyClause c : clauses) {
// ArrayList<Literal> newLiterals = new ArrayList<Literal>();
// for (Literal l : c.getDisjuncts()) {
// FuzzyLiteral flit = (FuzzyLiteral) l;
// FuzzyExpression newExp = flit.getExpression().accept(dnegElim);
// newLiterals.add(new FuzzyLiteral(flit.getLowerBound(),
// flit.getUpperBound(), newExp));
// }
// newClauses.add(new FuzzyClause(newLiterals));
// }
// overwrite the old clauses with the simplified ones ...
// clauses = newClauses;
HashMap trivialAssignments = new HashMap();
// if (FiniteReductionConfig.optimizeClauses) {
// boolean changed = true;
// while (changed) {
// DomainFinder.simplify(clauses);
// changed = DomainFinder.eliminateTriviallySatisfiableClauses(clauses, trivialAssignments);
// }
// }
for (FuzzyClause c : clauses) {
ficonv.addFuzzyClause(c);
}
TailorSolver solv = new TailorSolver("/home/jeroen/programming/tailorV0.3.2/tailor.jar",
"/home/jeroen/programming/minion-0.10/bin/minion");
solv.read(ficonv.convertToCSPProblem());
System.out.println("Ok, read file. Now solving ...");
CSPSolution cspSolution = solv.solve();
solution = ficonv.convertCSPSolutionToFuzzyModel(cspSolution);
System.out.println("Solving ended!");
if (solution != null) {
solution.putAll(trivialAssignments);
checkModel(clauses, solution);
printSolution(solution);
Map<GroundLiteral, Double> interp = new HashMap<GroundLiteral, Double>();
for (String s : solution.keySet()) {
GroundLiteral lit = parseLiteral(s);
interp.put(lit, solution.get(s));
}
i = new FaspInterpretation(interp);
if (prog.isAnswerSet(i)) {
System.out.println("is answer set!");
} else {
System.out.println("is NOT answer set!");
}
} else {
System.out.println("No solution found!");
}
}
// Helper method
// TODO: integrate in factory or constructors
public static GroundLiteral createLiteral(String name, boolean naf, UnaryOperator nafOp, FaspConstant... args) {
ArrayList<FaspConstant> pargs = new ArrayList<FaspConstant>();
for (FaspConstant c : args) {
pargs.add(c);
}
GroundPredicate p = new GroundPredicate(name, pargs);
return new GroundLiteral(p, false, naf, nafOp);
}
public static GroundLiteral createNafLiteral(String name, UnaryOperator nafOp, FaspConstant... args) {
return createLiteral(name, true, nafOp, args);
}
public static GroundLiteral createRegLiteral(String name, FaspConstant... args) {
return createLiteral(name, false, null, args);
}
public static GroundRule createRegularRule(GroundLiteral head, MultiOperator bodyOp, FaspFuzzyExpression... body) {
ArrayList<FaspFuzzyExpression> bodyElts = new ArrayList<FaspFuzzyExpression>();
for (FaspFuzzyExpression b : body) {
bodyElts.add(b);
}
return new GroundRegularRule(head, bodyElts, bodyOp);
}
// Parse the result of the CSP solver
public static GroundLiteral parseLiteral(String str) throws Exception {
NonGroundLiteral lit = new LiteralParser().parse(new TokenState(str));
Map<FaspVariable, FaspConstant> constMap = new HashMap<FaspVariable, FaspConstant>();
return lit.ground(constMap);
}
public static void printSolution(Map<String, Double> solution) {
for (String k : solution.keySet()) {
System.out.println("(" + k + "," + solution.get(k) + ")");
}
}
public static boolean checkModel(List<FuzzyClause> problem, Map<String, Double> model) {
for (FuzzyClause cl : problem) {
if (!cl.isSatisfied(model)) {
System.out.println("WARNING: incorrect model!");
System.out.println(" model = " + model);
System.out.println(" clause = " + cl);
return false;
}
}
return true;
}
public static void printLoop(Set<GroundLiteral> loop) {
System.out.print("Loop: {");
Iterator<GroundLiteral> it = loop.iterator();
if (it.hasNext()) {
System.out.print(it.next());
}
while (it.hasNext()) {
System.out.print("," + it.next());
}
System.out.println("}");
}
}