use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class SubmitRuleEvaluator method getPrologEnvironment.
private PrologEnvironment getPrologEnvironment(CurrentUser user) throws RuleEvalException {
ProjectState projectState = control.getProjectControl().getProjectState();
PrologEnvironment env;
try {
if (opts.rule() == null) {
env = projectState.newPrologEnvironment();
} else {
env = projectState.newPrologEnvironment("stdin", new StringReader(opts.rule()));
}
} catch (CompileException err) {
String msg;
if (opts.rule() == null && control.getProjectControl().isOwner()) {
msg = String.format("Cannot load rules.pl for %s: %s", getProjectName(), err.getMessage());
} else if (opts.rule() != null) {
msg = err.getMessage();
} else {
msg = String.format("Cannot load rules.pl for %s", getProjectName());
}
throw new RuleEvalException(msg, err);
}
env.set(StoredValues.REVIEW_DB, cd.db());
env.set(StoredValues.CHANGE_DATA, cd);
env.set(StoredValues.CHANGE_CONTROL, control);
if (user != null) {
env.set(StoredValues.CURRENT_USER, user);
}
return env;
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class Rulec method run.
@Override
public int run() throws Exception {
dbInjector = createDbInjector(SINGLE_USER);
manager.add(dbInjector);
manager.start();
dbInjector.createChildInjector(new FactoryModule() {
@Override
protected void configure() {
factory(PrologCompiler.Factory.class);
}
}).injectMembers(this);
LinkedHashSet<Project.NameKey> names = new LinkedHashSet<>();
for (String name : projectNames) {
names.add(new Project.NameKey(name));
}
if (all) {
names.addAll(gitManager.list());
}
boolean error = false;
for (Project.NameKey project : names) {
try (Repository git = gitManager.openRepository(project)) {
switch(jarFactory.create(git).call()) {
case NO_RULES:
if (!all || projectNames.contains(project.get())) {
System.err.println("error: No rules.pl in " + project.get());
error = true;
}
break;
case COMPILED:
if (!quiet) {
System.out.format("Compiled %-60s ... SUCCESS", project.get());
System.out.println();
}
break;
}
} catch (CompileException err) {
if (showStackTrace) {
err.printStackTrace();
} else {
System.err.println("fatal: " + err.getMessage());
}
error = true;
}
}
return !error ? 0 : 1;
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class PrologCompiler method call.
@Override
public Status call() throws IOException, CompileException {
ObjectId metaConfig = git.resolve(RefNames.REFS_CONFIG);
if (metaConfig == null) {
return Status.NO_RULES;
}
ObjectId rulesId = git.resolve(metaConfig.name() + ":rules.pl");
if (rulesId == null) {
return Status.NO_RULES;
}
if (ruleDir == null) {
throw new CompileException("Caching not enabled");
}
Files.createDirectories(ruleDir);
File tempDir = File.createTempFile("GerritCodeReview_", ".rulec");
if (!tempDir.delete() || !tempDir.mkdir()) {
throw new IOException("Cannot create " + tempDir);
}
try {
// Try to make the directory accessible only by this process.
// This may help to prevent leaking rule data to outsiders.
tempDir.setReadable(true, true);
tempDir.setWritable(true, true);
tempDir.setExecutable(true, true);
compileProlog(rulesId, tempDir);
compileJava(tempDir);
Path jarPath = ruleDir.resolve("rules-" + rulesId.getName() + ".jar");
List<String> classFiles = getRelativePaths(tempDir, ".class");
createJar(jarPath, classFiles, tempDir, metaConfig, rulesId);
return Status.COMPILED;
} finally {
deleteAllFiles(tempDir);
}
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class RulesCache method consultRules.
private PrologMachineCopy consultRules(String name, Reader rules) throws CompileException {
BufferingPrologControl ctl = newEmptyMachine(systemLoader);
PushbackReader in = new PushbackReader(rules, Prolog.PUSHBACK_SIZE);
try {
if (!ctl.execute(Prolog.BUILTIN, "consult_stream", SymbolTerm.intern(name), new JavaObjectTerm(in))) {
return null;
}
} catch (SyntaxException e) {
throw new CompileException(e.toString(), e);
} catch (TermException e) {
Term m = e.getMessageTerm();
if (m instanceof StructureTerm && "syntax_error".equals(m.name()) && m.arity() >= 1) {
StringBuilder msg = new StringBuilder();
if (m.arg(0) instanceof ListTerm) {
msg.append(Joiner.on(' ').join(((ListTerm) m.arg(0)).toJava()));
} else {
msg.append(m.arg(0).toString());
}
if (m.arity() == 2 && m.arg(1) instanceof StructureTerm && "at".equals(m.arg(1).name())) {
Term at = m.arg(1).arg(0).dereference();
if (at instanceof ListTerm) {
msg.append(" at: ");
msg.append(prettyProlog(at));
}
}
throw new CompileException(msg.toString(), e);
}
throw new CompileException("Error while consulting rules from " + name, e);
} catch (RuntimeException e) {
throw new CompileException("Error while consulting rules from " + name, e);
}
return save(ctl);
}
use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.
the class GerritCommonTest method reductionLimit.
@Test
public void reductionLimit() throws Exception {
PrologEnvironment env = envFactory.create(machine);
setUpEnvironment(env);
String script = "loopy :- b(5).\nb(N) :- N > 0, !, S = N - 1, b(S).\nb(_) :- true.\n";
SymbolTerm nameTerm = SymbolTerm.create("testReductionLimit");
JavaObjectTerm inTerm = new JavaObjectTerm(new PushbackReader(new StringReader(script), Prolog.PUSHBACK_SIZE));
if (!env.execute(Prolog.BUILTIN, "consult_stream", nameTerm, inTerm)) {
throw new CompileException("Cannot consult " + nameTerm);
}
exception.expect(ReductionLimitException.class);
exception.expectMessage("exceeded reduction limit of 1300");
env.once(Prolog.BUILTIN, "call", new StructureTerm(":", SymbolTerm.create("user"), SymbolTerm.create("loopy")));
}
Aggregations