001package org.clafer.ontology; 002 003import java.util.Collection; 004import java.util.Collections; 005import java.util.HashMap; 006import java.util.HashSet; 007import java.util.Iterator; 008import java.util.List; 009import java.util.Map; 010import java.util.Map.Entry; 011import java.util.Objects; 012import java.util.Set; 013import java.util.function.BiConsumer; 014import org.clafer.collection.Pair; 015import org.clafer.graph.GraphUtil; 016import org.clafer.graph.KeyGraph; 017 018/** 019 * A binary relation. 020 * 021 * @param <T> the type 022 * @author jimmy 023 */ 024public class Relation<T> implements Iterable<Pair<T, T>> { 025 026 /** 027 * Contains the forward joins. 028 */ 029 final Map<T, Set<T>> forwards; 030 /** 031 * Contains the backward joins. 032 */ 033 final Map<T, Set<T>> backwards; 034 035 public Relation() { 036 this(new HashMap<>(), new HashMap<>()); 037 } 038 039 public Relation(Relation<T> copy) { 040 this(new HashMap<>(copy.forwards), new HashMap<>(copy.backwards)); 041 } 042 043 Relation(Map<T, Set<T>> forwards, Map<T, Set<T>> backwards) { 044 this.forwards = forwards; 045 this.backwards = backwards; 046 } 047 048 public void add(T a, T b) { 049 Set<T> bs = forwards.get(a); 050 if (bs == null) { 051 bs = new HashSet<>(); 052 forwards.put(a, bs); 053 } 054 bs.add(b); 055 056 Set<T> as = backwards.get(b); 057 if (as == null) { 058 as = new HashSet<>(); 059 backwards.put(b, as); 060 } 061 as.add(a); 062 } 063 064 public void addAll(T a, Collection<T> b) { 065 Set<T> bs = forwards.get(a); 066 if (bs == null) { 067 bs = new HashSet<>(); 068 forwards.put(a, bs); 069 } 070 bs.addAll(b); 071 072 for (T t : b) { 073 Set<T> as = backwards.get(t); 074 if (as == null) { 075 as = new HashSet<>(); 076 backwards.put(t, as); 077 } 078 as.add(a); 079 } 080 } 081 082 public void set(Relation<T> relation) { 083 forwards.clear(); 084 forwards.putAll(relation.forwards); 085 backwards.clear(); 086 backwards.putAll(relation.backwards); 087 } 088 089 public boolean has(T a, T b) { 090 Set<T> bs = forwards.get(a); 091 return bs != null && bs.contains(b); 092 } 093 094 public Set<T> from(T a) { 095 Set<T> bs = forwards.get(a); 096 return bs == null ? Collections.emptySet() : bs; 097 } 098 099 public Set<T> to(T b) { 100 Set<T> as = backwards.get(b); 101 return as == null ? Collections.emptySet() : as; 102 } 103 104 public Relation<T> compose(Relation<T> with) { 105 Relation<T> composition = new Relation<>(); 106 for (Entry<T, Set<T>> entry : entrySet()) { 107 T sub = entry.getKey(); 108 Set<T> sups = entry.getValue(); 109 for (T sup : sups) { 110 composition.addAll(sub, with.from(sup)); 111 } 112 } 113 return composition; 114 } 115 116 public Relation<T> inverse() { 117 return new Relation<>(new HashMap<>(backwards), new HashMap<>(forwards)); 118 } 119 120 public void closeTransitively() { 121 // Naive algorithm. 122 for (Entry<T, Set<T>> forward : forwards.entrySet()) { 123 Set<T> reachable = new HashSet<>(forward.getValue().size()); 124 reachable(forward.getKey(), reachable); 125 forward.setValue(reachable); 126 } 127 } 128 129 private void reachable(T from, Set<T> reachable) { 130 Set<T> tos = forwards.get(from); 131 if (tos != null) { 132 for (T to : tos) { 133 if (reachable.add(to)) { 134 reachable(to, reachable); 135 } 136 } 137 } 138 } 139 140 public Relation<T> transitiveClosureWithoutCycles() { 141 KeyGraph<T> graph = new KeyGraph<>(); 142 forEach(graph::addEdge); 143 List<Set<T>> items = GraphUtil.computeStronglyConnectedComponents(graph); 144 for (Set<T> cycle : items) { 145 if (cycle.size() > 1) { 146 throw new IllegalStateException("Found a cycle in the relationship: " + cycle); 147 } 148 } 149 Map<T, Set<T>> newForwards = new HashMap<>(forwards); 150 Map<T, Set<T>> newBackwards = new HashMap<>(backwards); 151 for (int i = 0; i < 2; i++) { 152 Map<T, Set<T>> map = i == 0 ? newForwards : newBackwards; 153 for (Set<T> item : items) { 154 for (T from : item) { 155 Set<T> newTos = new HashSet<>(); 156 newTos.add(from); 157 Set<T> tos = map.get(from); 158 if (tos != null) { 159 newTos.addAll(tos); 160 tos.stream().map(map::get) 161 .filter(Objects::nonNull) 162 .forEach(newTos::addAll); 163 } 164 map.put(from, newTos); 165 } 166 } 167 } 168 return new Relation<>(newForwards, newBackwards); 169 } 170 171 public void removeSymmetry() { 172 for (Entry<T, Set<T>> forward : forwards.entrySet()) { 173 forward.getValue().remove(forward.getKey()); 174 } 175 for (Entry<T, Set<T>> backward : backwards.entrySet()) { 176 backward.getValue().remove(backward.getKey()); 177 } 178 } 179 180 public Set<Entry<T, Set<T>>> entrySet() { 181 return forwards.entrySet(); 182 } 183 184 public void forEach(BiConsumer<T, T> action) { 185 forwards.forEach((x, y) -> y.forEach(z -> action.accept(x, z))); 186 } 187 188 @Override 189 public Iterator<Pair<T, T>> iterator() { 190 return forwards.entrySet().stream() 191 .flatMap(x -> x.getValue().stream().map(y -> new Pair<>(x.getKey(), y))) 192 .iterator(); 193 } 194}