Search in sources :

Example 1 with SortedSetMultimap

use of com.google.common.collect.SortedSetMultimap in project error-prone by google.

the class BugPatternIndexWriter method dump.

void dump(Collection<BugPatternInstance> patterns, Writer w, Target target, Set<String> enabledChecks) throws IOException {
    // (Default, Severity) -> [Pattern...]
    SortedSetMultimap<IndexEntry, MiniDescription> sorted = TreeMultimap.create(Comparator.comparing(IndexEntry::onByDefault).reversed().thenComparing(IndexEntry::severity), Comparator.comparing(MiniDescription::name));
    for (BugPatternInstance pattern : patterns) {
        sorted.put(IndexEntry.create(enabledChecks.contains(pattern.name), pattern.severity), MiniDescription.create(pattern));
    }
    Map<String, Object> templateData = new HashMap<>();
    List<Map<String, Object>> bugpatternData = Multimaps.asMap(sorted).entrySet().stream().map(e -> ImmutableMap.of("category", e.getKey().asCategoryHeader(), "checks", e.getValue())).collect(Collectors.toCollection(ArrayList::new));
    templateData.put("bugpatterns", bugpatternData);
    if (target == Target.EXTERNAL) {
        Map<String, String> frontmatterData = ImmutableMap.<String, String>builder().put("title", "Bug Patterns").put("layout", "bugpatterns").build();
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(options);
        Writer yamlWriter = new StringWriter();
        yamlWriter.write("---\n");
        yaml.dump(frontmatterData, yamlWriter);
        yamlWriter.write("---\n");
        templateData.put("frontmatter", yamlWriter.toString());
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_external.mustache");
        mustache.execute(w, templateData);
    } else {
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_internal.mustache");
        mustache.execute(w, templateData);
    }
}
Also used : DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) SortedSetMultimap(com.google.common.collect.SortedSetMultimap) ImmutableMap(com.google.common.collect.ImmutableMap) StringWriter(java.io.StringWriter) Collection(java.util.Collection) Mustache(com.github.mustachejava.Mustache) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) DumperOptions(org.yaml.snakeyaml.DumperOptions) List(java.util.List) TreeMultimap(com.google.common.collect.TreeMultimap) Map(java.util.Map) MustacheFactory(com.github.mustachejava.MustacheFactory) AutoValue(com.google.auto.value.AutoValue) Writer(java.io.Writer) Comparator(java.util.Comparator) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) Target(com.google.errorprone.DocGenTool.Target) HashMap(java.util.HashMap) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) Mustache(com.github.mustachejava.Mustache) Yaml(org.yaml.snakeyaml.Yaml) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory) StringWriter(java.io.StringWriter) DumperOptions(org.yaml.snakeyaml.DumperOptions) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 2 with SortedSetMultimap

use of com.google.common.collect.SortedSetMultimap in project narchy by automenta.

the class DeriveTime method solveMerged.

@Nullable
Function<long[], Term> solveMerged(ArrayHashSet<Event> solutions, int dur) {
    int ss = solutions.size();
    // callee will use the only solution by default
    if (ss <= 1)
        return null;
    SortedSetMultimap<Term, LongLongPair> m = MultimapBuilder.hashKeys(ss).treeSetValues().build();
    solutions.forEach(x -> {
        long w = x.when();
        if (w != TIMELESS)
            m.put(x.id, PrimitiveTuples.pair(w, w));
    });
    int ms = m.size();
    switch(ms) {
        case 0:
            return null;
        case 1:
            Map.Entry<Term, LongLongPair> ee = m.entries().iterator().next();
            LongLongPair ww = ee.getValue();
            long s = ww.getOne();
            long e = ww.getTwo();
            return (w) -> {
                w[0] = s;
                w[1] = e;
                return ee.getKey();
            };
    }
    FasterList<Pair<Term, long[]>> choices = new FasterList(ms);
    // coalesce adjacent events
    m.asMap().forEach((t, cw) -> {
        int cws = cw.size();
        if (cws > 1) {
            long[][] ct = new long[cws][2];
            int i = 0;
            for (LongLongPair p : cw) {
                long[] cc = ct[i++];
                cc[0] = p.getOne();
                cc[1] = p.getTwo();
            }
            // TODO more complete comparison
            long[] prev = ct[0];
            for (int j = 1; j < cws; j++) {
                long[] next = ct[j];
                if (prev[0] == ETERNAL) {
                    assert (j == 1);
                    assert (ct[0][0] == ETERNAL);
                    // ignore eternal solution amongst other temporal solutions
                    ct[0] = null;
                } else if (Math.abs(prev[1] - next[0]) <= dur) {
                    // stretch
                    prev[1] = next[1];
                    ct[j] = null;
                    continue;
                }
                prev = next;
            }
            for (int j = 0; j < cws; j++) {
                long[] nn = ct[j];
                if (nn != null)
                    choices.add(pair(t, nn));
            }
        } else {
            LongLongPair f = ((SortedSet<LongLongPair>) cw).first();
            choices.add(pair(t, new long[] { f.getOne(), f.getTwo() }));
        }
    });
    if (choices.size() > 1) {
        // random fallback
        return (w) -> {
            Pair<Term, long[]> pp = choices.get(d.random);
            long[] cw = pp.getTwo();
            w[0] = cw[0];
            w[1] = cw[1];
            return pp.getOne();
        };
    } else {
        Pair<Term, long[]> c = choices.get(0);
        long[] cw = c.getTwo();
        Term cct = c.getOne();
        return (w) -> {
            w[0] = cw[0];
            w[1] = cw[1];
            return cct;
        };
    }
}
Also used : Derivation(nars.derive.Derivation) java.util(java.util) Tense(nars.time.Tense) CONJ(nars.Op.CONJ) MultimapBuilder(com.google.common.collect.MultimapBuilder) ArrayHashSet(jcog.data.ArrayHashSet) ETERNAL(nars.time.Tense.ETERNAL) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Truth(nars.truth.Truth) Tuples.pair(org.eclipse.collections.impl.tuple.Tuples.pair) EllipsisMatch(nars.derive.match.EllipsisMatch) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Bool(nars.term.atom.Bool) PrimitiveTuples(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples) NEG(nars.Op.NEG) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap) Pair(org.eclipse.collections.api.tuple.Pair) Longerval(jcog.math.Longerval) LongLongPair(org.eclipse.collections.api.tuple.primitive.LongLongPair) Term(nars.term.Term) SortedSetMultimap(com.google.common.collect.SortedSetMultimap) Predicate(java.util.function.Predicate) FasterList(jcog.list.FasterList) EviDensity(nars.task.EviDensity) Param(nars.Param) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) Termed(nars.term.Termed) VarPattern(nars.term.var.VarPattern) TIMELESS(nars.time.Tense.TIMELESS) LongLongPair(org.eclipse.collections.api.tuple.primitive.LongLongPair) FasterList(jcog.list.FasterList) Term(nars.term.Term) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap) Pair(org.eclipse.collections.api.tuple.Pair) LongLongPair(org.eclipse.collections.api.tuple.primitive.LongLongPair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

SortedSetMultimap (com.google.common.collect.SortedSetMultimap)2 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)1 Mustache (com.github.mustachejava.Mustache)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 AutoValue (com.google.auto.value.AutoValue)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 Multimaps (com.google.common.collect.Multimaps)1 TreeMultimap (com.google.common.collect.TreeMultimap)1 SeverityLevel (com.google.errorprone.BugPattern.SeverityLevel)1 Target (com.google.errorprone.DocGenTool.Target)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1