All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ggp.base.util.prover.aima.cache.ProverCache Maven / Gradle / Ivy

The newest version!
package org.ggp.base.util.prover.aima.cache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.ggp.base.util.gdl.grammar.GdlSentence;
import org.ggp.base.util.prover.aima.substituter.Substituter;
import org.ggp.base.util.prover.aima.substitution.Substitution;
import org.ggp.base.util.prover.aima.unifier.Unifier;


public final class ProverCache
{

    private final Map> contents;

    private ProverCache(Map> mapForContents) {
        this.contents = mapForContents;
    }

    public static ProverCache createSingleThreadedCache() {
        return new ProverCache(new HashMap>());
    }

    public static ProverCache createMultiThreadedCache() {
        return new ProverCache(new ConcurrentHashMap>());
    }

    /**
     * NOTE: The given sentence must have been renamed with a VariableRenamer.
     */
    public boolean contains(GdlSentence renamedSentence)
    {
        return contents.containsKey(renamedSentence);
    }

    public List get(GdlSentence sentence, GdlSentence varRenamedSentence)
    {
        Set cacheContents = contents.get(varRenamedSentence);
        if (cacheContents == null) {
            return null;
        }
        Set results = new HashSet();
        for (GdlSentence answer : cacheContents)
        {
            results.add(Unifier.unify(sentence, answer));
        }

        return new ArrayList(results);
    }

    public void put(GdlSentence sentence, GdlSentence renamedSentence,
            Set answers)
    {
        Set results = new HashSet();
        for (Substitution answer : answers)
        {
            results.add(Substituter.substitute(sentence, answer));
        }

        contents.put(renamedSentence, results);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy