/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fasp;
import fasp.datatypes.*;
import fasp.parser.*;
import fasp.parser.parsers.ProgramParser;
import fasp.parser.tokenizer.TokenState;
import fuzzysat.FuzzyLiteral;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
//import javax.management.ImmutableDescriptor;
/**
*
* @author Jeroen Janssen <Jeroen.Janssen@vub.ac.be>
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws ParseException {
//String[] newArgs = new String[2];
//args = newArgs;
//args[0] = "-g";
//args[1] = "faspprograms/seating.fasp";
if(args.length >= 2) {
if(args[0].equals("-g")) {
readFile(args[1],new FileAction() {
public void act(String str) {
try {
TokenState st = new TokenState(str);
NonGroundProgram ngProg = new ProgramParser().parse(st);
GroundProgram groundProg = ngProg.ground();
System.out.println(groundProg);
//System.out.println(groundProg.filterGrounding());
} catch (ParseException e) {
System.out.println(e.getParseError());
}
}
});
} else if(args[0].equals("-c") && args.length == 3) {
Double lowerBound;
try {
lowerBound = Double.parseDouble(args[1]);
if(lowerBound >= 0 && lowerBound <= 1)
readFile(args[2],new CompletionAction(lowerBound));
else
System.out.println("The y-argument should be in [0,1]");
} catch (NumberFormatException e) {
System.out.println("ERROR: the second argument of -c was not a Double.");
printUsage();
}
} else if(args[0].equals("-s")) {
System.out.println("Not implemented yet!");
} else if(args[0].equals("-im")) {
readFile(args[1],new FileAction() {
// TODO: remove duplication of code in this action and the
// grounding action (i.e. have a separate groundingaction that
// can be called that returns a GroundProgram).
public void act(String str) {
try {
TokenState st = new TokenState(str);
NonGroundProgram ngProg = new ProgramParser().parse(st);
GroundProgram groundProg = ngProg.ground();
ConsequenceOp imcons = new ConsequenceOp(groundProg);
FaspInterpretation model = imcons.computeLFP();
System.out.println("LFP Model:");
System.out.println(model);
} catch (ParseException e) {
System.out.println(e.getParseError());
}
}
});
} else {
System.out.println("Unrecognized option: "+args[0]);
printUsage();
}
} else {
printUsage();
}
/*TokenState st = new TokenState("a :- TW(~b). b :- TW(~a).");
NonGroundProgram ngProg = new ProgramParser().parse(st);
GroundProgram gProg = ngProg.ground();
Map<GroundLiteral,Double> intMap = new HashMap<GroundLiteral,Double>();
intMap.put(new GroundLiteral(new GroundPredicate("a"),false,false),0.5);
intMap.put(new GroundLiteral(new GroundPredicate("b"),false,false),0.5);
FaspInterpretation interp = new FaspInterpretation(intMap);
GroundProgram reduct = gProg.computeReduct(interp);
//FaspInterpretation ansSet = new ConsequenceOp (gProg).computeLFP();
System.out.println("Reduct:");
System.out.println(reduct);
System.out.println("AnsSet reduct:");
FaspInterpretation ansSet = new ConsequenceOp (reduct).computeLFP();
System.out.println(ansSet);*/
/*TokenState st = new TokenState("a :- TW(a).");
NonGroundProgram ngProg = new ProgramParser().parse(st);
GroundProgram gProg = ngProg.ground();
Map<GroundLiteral,Double> intMap = new HashMap<GroundLiteral,Double>();
intMap.put(new GroundLiteral(new GroundPredicate("a"),false,false),0.5);
FaspInterpretation interp = new FaspInterpretation(intMap);
System.out.println("Is Answer Set?"+gProg.isAnswerSet(interp));
GroundProgram reduct = gProg.computeReduct(interp);
System.out.println("Answer Set Reduct: "+new ConsequenceOp(reduct).computeLFP());
System.out.println("Cycle: "+gProg.findCycle(interp));
Set<GroundLiteral> loop = gProg.findCycle(interp);
System.out.println("DepGraph: "+gProg.getDependencyGraph());
System.out.println(gProg.computeLoopFormula(loop));*/
/*TokenState st = new TokenState("a :- TW(~b). b :- TW(~a).");
NonGroundRule r1 = new RuleParser().parse(st);
NonGroundRule r2 = new RuleParser().parse(st);
new TokenParser(Token.Type.EOF);
ArrayList<NonGroundRule> rules = new ArrayList<NonGroundRule>();
rules.add(r1);
rules.add(r2);
NonGroundProgram p = new NonGroundProgram(rules);
System.out.println(p);
GroundProgram gp = p.ground();
System.out.println(gp);*/
/*ArrayList<FaspFuzzyExpression> body1 = new ArrayList<FaspFuzzyExpression>();
body1.add(new GroundLiteral(new GroundPredicate("a"),false,true));
body1.add(new GroundLiteral(new GroundPredicate("c"),false,false));
ArrayList<FaspFuzzyExpression> body2 = new ArrayList<FaspFuzzyExpression>();
body2.add(new GroundLiteral(new GroundPredicate("b"),false,true));
GroundRule r1 = new GroundRegularRule(new GroundLiteral(new GroundPredicate("b"),false,false),body1,new TW());
GroundRule r2 = new GroundRegularRule(new GroundLiteral(new GroundPredicate("a"),false,false),body2,new TW());
ArrayList<GroundRule> rules = new ArrayList<GroundRule>();
rules.add(r1);
rules.add(r2);
GroundProgram prog = new GroundProgram(rules);
for(FuzzyLiteral l : prog.createCompletion())
System.out.println(l.toString());*/
//try {
//String str = "predicate(X,const,Y) :- TM(1.0,pred(X),pred2(X,Y)).";
//TokenState ts = new TokenState(str);
//ts.initialize();
//FaspPredicate pred = new PredicateParser().parse(ts);
/*Set<FaspVariable> varSet = new TreeSet<FaspVariable>();
varSet.add(new FaspVariable("X"));
varSet.add(new FaspVariable("Y"));
varSet.add(new FaspVariable("Z"));
Set<FaspConstant> constSet = new TreeSet<FaspConstant>();
constSet.add(new FaspConstant("a"));
constSet.add(new FaspConstant("b"));
constSet.add(new FaspConstant("c"));
Set<Map<FaspVariable,FaspConstant>> bindings = new FaspProgram(null).generateBindings(varSet, constSet);
boolean ok = true;
for(Map<FaspVariable,FaspConstant> binding : bindings) {
for(Map<FaspVariable,FaspConstant> binding2 : bindings) {
if(!binding.equals(binding2))
ok = !printBinding(binding).equals(printBinding(binding2));
}
System.out.println(printBinding(binding));
}
if(ok)
System.out.println("Correct bindings");
else
System.out.println("Wrong bindings");*/
//FaspPredicate groundedPred = pred.ground(bindings);
//FaspConstant groundedVar = (FaspConstant) new FaspVariable("X").ground(bindings);
//System.out.println(groundedVar);
//System.out.println(pred);
//System.out.println(groundedPred);
//String rule1 = "pred(X) :- TM(somePred(X,const,Y),0.4,X /= Y,diffPred(Y)).";
//TokenState st = new TokenState(rule1);
//FaspRule r = FaspParser.parseRule(st);
//} catch (ParseException e) {
// System.out.println(e.getParseError());
//}
}
private static String printBinding(Map<FaspVariable,FaspConstant> binding) {
StringBuffer buff = new StringBuffer();
buff.append("binding: [");
for(FaspVariable v : binding.keySet())
buff.append("("+v.toString()+","+binding.get(v).toString()+")");
buff.append("]");
return buff.toString();
}
private static void printUsage() {
System.out.println("USAGE: ./JFaspSolver [-g|-c|-y] [ARGS]");
System.out.println("For example:");
System.out.println("\t ./JFaspSolver -g FILE: prints the grounding of the program in FILE");
System.out.println("\t ./JFaspSolver -c y FILE: prints the completion of the program in FILE (for y-models)");
System.out.println("\t ./JFaspSolver -y y FILE: finds a y-model of the program in FILE");
System.out.println("\t Where the argument 'y' should be a Double in [0,1]");
}
private static void readFile(String fileName, FileAction action) {
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
StringBuffer buff = new StringBuffer();
while ((str = in.readLine()) != null) {
// BufferedReader adds "" on two \n\ns
// have to watch out for this, so that it does not influence
// the error reporting of the parser ...
buff.append(str);
buff.append(System.getProperty("line.separator"));
}
action.act(buff.toString());
in.close();
} catch (IOException e) {
}
}
private interface FileAction {
public void act(String str);
};
private static class CompletionAction implements FileAction {
private Double lowerBound;
public CompletionAction(Double d) {
this.lowerBound = d;
}
public void act(String str) {
try {
TokenState st = new TokenState(str);
NonGroundProgram ngProg = new ProgramParser().parse(st);
GroundProgram groundProg = ngProg.ground();
List<FuzzyLiteral> completion = groundProg.createCompletion(lowerBound);
System.out.println(completion);
} catch (ParseException e) {
System.out.println(e.getParseError());
}
}
}
}