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

org.ggp.base.util.reasoner.gdl.GdlSentenceSet Maven / Gradle / Ivy

There is a newer version: 0.0.15
Show newest version
package org.ggp.base.util.reasoner.gdl;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.ggp.base.util.gdl.grammar.GdlSentence;
import org.ggp.base.util.gdl.model.SentenceForm;
import org.ggp.base.util.gdl.model.assignments.AddibleFunctionInfo;
import org.ggp.base.util.gdl.model.assignments.MutableFunctionInfo;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

/**
 * Contains a set of GdlSentences arranged by SentenceForm and the
 * associated FunctionInfo for each SentenceForm. The FunctionInfos
 * are continually and automatically maintained as sentences are
 * added to the set.
 *
 * Note that this class is not thread-safe.
 */
public class GdlSentenceSet {
    private final SetMultimap sentences;
    private final Map functionInfoMap;

    private GdlSentenceSet() {
        this.sentences = HashMultimap.create();
        this.functionInfoMap = Maps.newHashMap();
    }

    private GdlSentenceSet(SetMultimap sentences,
            Map functionInfoMap) {
        this.sentences = HashMultimap.create(sentences);
        this.functionInfoMap = Maps.newHashMap(functionInfoMap);
    }

    public static GdlSentenceSet create() {
        return new GdlSentenceSet();
    }

    public static GdlSentenceSet create(Multimap sentences) {
        GdlSentenceSet result = create();
        result.putAll(sentences);
        return result;
    }

    public static GdlSentenceSet copyOf(GdlSentenceSet other) {
        return new GdlSentenceSet(other.sentences, other.functionInfoMap);
    }

    /**
     * Returns an unmodifiable view of the sentences in this set.
     * Note that this view may change if the collection is concurrently
     * modified.
     */
    public SetMultimap getSentences() {
        return Multimaps.unmodifiableSetMultimap(sentences);
    }

    /**
     * Returns true iff the given sentence is in this set of sentences.
     */
    public boolean containsSentence(SentenceForm form, GdlSentence sentence) {
        return sentences.containsEntry(form, sentence);
    }

    public void putAll(Multimap newSentences) {
        for (Entry entry : newSentences.entries()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    public void put(SentenceForm form, GdlSentence sentence) {
        if (!containsSentence(form, sentence)) {
            sentences.put(form, sentence);
            if (!functionInfoMap.containsKey(form)) {
                functionInfoMap.put(form, MutableFunctionInfo.create(form));
            }
            functionInfoMap.get(form).addSentence(sentence);
        }
    }

    /**
     * Returns an unmodifiable view of the function information
     */
    public Map getFunctionInfo() {
        return Collections.unmodifiableMap(functionInfoMap);
    }

    public boolean isEmpty() {
        return sentences.isEmpty();
    }

    public Set getSentenceForms() {
        return sentences.keySet();
    }

    @Override
    public String toString() {
        return "GdlSentenceSet [sentences=" + sentences + ", functionInfoMap="
                + functionInfoMap + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy