Search in sources :

Example 1 with PrologMachineCopy

use of com.googlecode.prolog_cafe.lang.PrologMachineCopy in project gerrit by GerritCodeReview.

the class RulesCache method createMachine.

private PrologMachineCopy createMachine(Project.NameKey project, ObjectId rulesId) throws CompileException {
    //
    if (rulesDir != null) {
        Path jarPath = rulesDir.resolve("rules-" + rulesId.getName() + ".jar");
        if (Files.isRegularFile(jarPath)) {
            URL[] cp = new URL[] { toURL(jarPath) };
            return save(newEmptyMachine(new URLClassLoader(cp, systemLoader)));
        }
    }
    // Dynamically consult the rules into the machine's internal database.
    //
    String rules = read(project, rulesId);
    PrologMachineCopy pmc = consultRules("rules.pl", new StringReader(rules));
    if (pmc == null) {
        throw new CompileException("Cannot consult rules of " + project);
    }
    return pmc;
}
Also used : Path(java.nio.file.Path) URLClassLoader(java.net.URLClassLoader) StringReader(java.io.StringReader) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) PrologMachineCopy(com.googlecode.prolog_cafe.lang.PrologMachineCopy) URL(java.net.URL)

Example 2 with PrologMachineCopy

use of com.googlecode.prolog_cafe.lang.PrologMachineCopy in project gerrit by GerritCodeReview.

the class ProjectState method newPrologEnvironment.

/** @return Construct a new PrologEnvironment for the calling thread. */
public PrologEnvironment newPrologEnvironment() throws CompileException {
    PrologMachineCopy pmc = rulesMachine;
    if (pmc == null) {
        pmc = rulesCache.loadMachine(getProject().getNameKey(), config.getRulesId());
        rulesMachine = pmc;
    }
    return envFactory.create(pmc);
}
Also used : PrologMachineCopy(com.googlecode.prolog_cafe.lang.PrologMachineCopy)

Example 3 with PrologMachineCopy

use of com.googlecode.prolog_cafe.lang.PrologMachineCopy in project gerrit by GerritCodeReview.

the class RulesCache method loadMachine.

/**
   * Locate a cached Prolog machine state, or create one if not available.
   *
   * @return a Prolog machine, after loading the specified rules.
   * @throws CompileException the machine cannot be created.
   */
public synchronized PrologMachineCopy loadMachine(Project.NameKey project, ObjectId rulesId) throws CompileException {
    if (!enableProjectRules || project == null || rulesId == null) {
        return defaultMachine;
    }
    Reference<? extends PrologMachineCopy> ref = machineCache.get(rulesId);
    if (ref != null) {
        PrologMachineCopy pmc = ref.get();
        if (pmc != null) {
            return pmc;
        }
        machineCache.remove(rulesId);
        ref.enqueue();
    }
    gc();
    PrologMachineCopy pcm = createMachine(project, rulesId);
    MachineRef newRef = new MachineRef(rulesId, pcm, dead);
    machineCache.put(rulesId, newRef);
    return pcm;
}
Also used : PrologMachineCopy(com.googlecode.prolog_cafe.lang.PrologMachineCopy)

Aggregations

PrologMachineCopy (com.googlecode.prolog_cafe.lang.PrologMachineCopy)3 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1