001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.stream; 033 034/** 035 * Allows to filter the attribute event stream. 036 * 037 * <p> 038 * The filtering is based on attribute predicates. An attribute predicate is an 039 * object that you provide and that only defines one method 040 * {@link AttributePredicate#matches(String, Object)}. If the "matches()" method 041 * return false, the attribute is discarded from the event stream, else it is 042 * passed to the listeners of this filter. 043 * </p> 044 * 045 * <p> 046 * You can setup a predicate from all attributes (graph, node and edge 047 * attributes) and specific predicates for graph, node and edge attributes. 048 * </p> 049 */ 050public class AttributePipe extends PipeBase { 051 protected AttributePredicate globalPredicate = new FalsePredicate(); 052 053 protected AttributePredicate graphPredicate = new FalsePredicate(); 054 055 protected AttributePredicate nodePredicate = new FalsePredicate(); 056 057 protected AttributePredicate edgePredicate = new FalsePredicate(); 058 059 /** 060 * Set an attribute filter for graph, node and edge attributes. If the 061 * filter is null, attributes will not be filtered globally. 062 * 063 * @param filter 064 * The filter to use, it can be null to disable global attribute 065 * filtering. 066 */ 067 public void setGlobalAttributeFilter(AttributePredicate filter) { 068 if (filter == null) 069 globalPredicate = new FalsePredicate(); 070 else 071 globalPredicate = filter; 072 } 073 074 /** 075 * Set an attribute filter for graph attributes only (node an edge 076 * attributes are not filtered by this filter). If the filter is null, graph 077 * attributes will not be filtered. 078 * 079 * @param filter 080 * The filter to use, it can be null to disable graph attribute 081 * filtering. 082 */ 083 public void setGraphAttributeFilter(AttributePredicate filter) { 084 if (filter == null) 085 graphPredicate = new FalsePredicate(); 086 else 087 graphPredicate = filter; 088 } 089 090 /** 091 * Set an attribute filter for node attributes only (graph an edge 092 * attributes are not filtered by this filter). If the filter is null, node 093 * attributes will not be filtered. 094 * 095 * @param filter 096 * The filter to use, it can be null to disable node attribute 097 * filtering. 098 */ 099 public void setNodeAttributeFilter(AttributePredicate filter) { 100 if (filter == null) 101 nodePredicate = new FalsePredicate(); 102 else 103 nodePredicate = filter; 104 } 105 106 /** 107 * Set an attribute filter for edge attributes only (graph an node 108 * attributes are not filtered by this filter). If the filter is null, edge 109 * attributes will not be filtered. 110 * 111 * @param filter 112 * The filter to use, it can be null to disable edge attribute 113 * filtering. 114 */ 115 public void setEdgeAttributeFilter(AttributePredicate filter) { 116 if (filter == null) 117 edgePredicate = new FalsePredicate(); 118 else 119 edgePredicate = filter; 120 } 121 122 /** 123 * The filter for all graph, node and edge attributes. This filter can be 124 * null. 125 * 126 * @return The global attribute filter or null if there is no global filter. 127 */ 128 public AttributePredicate getGlobalAttributeFilter() { 129 return globalPredicate; 130 } 131 132 /** 133 * The filter for all graph attributes. This filter can be null. 134 * 135 * @return The graph attribute filter or null if there is no graph filter. 136 */ 137 public AttributePredicate getGraphAttributeFilter() { 138 return graphPredicate; 139 } 140 141 /** 142 * The filter for all node attributes. This filter can be null. 143 * 144 * @return The node global attribute filter or null if there is no node 145 * filter. 146 */ 147 public AttributePredicate getNodeAttributeFilter() { 148 return nodePredicate; 149 } 150 151 /** 152 * The filter for all edge attributes. This filter can be null. 153 * 154 * @return The edge attribute filter or null of there is no edge filter. 155 */ 156 public AttributePredicate getEdgeAttributeFilter() { 157 return edgePredicate; 158 } 159 160 // GraphListener 161 162 @Override 163 public void edgeAttributeAdded(String graphId, long timeId, String edgeId, 164 String attribute, Object value) { 165 if (!edgePredicate.matches(attribute, value)) { 166 if (!globalPredicate.matches(attribute, value)) { 167 sendEdgeAttributeAdded(graphId, timeId, edgeId, attribute, 168 value); 169 } 170 } 171 } 172 173 @Override 174 public void edgeAttributeChanged(String graphId, long timeId, 175 String edgeId, String attribute, Object oldValue, Object newValue) { 176 if (!edgePredicate.matches(attribute, newValue)) { 177 if (!globalPredicate.matches(attribute, newValue)) { 178 sendEdgeAttributeChanged(graphId, timeId, edgeId, attribute, 179 oldValue, newValue); 180 } 181 } 182 } 183 184 @Override 185 public void edgeAttributeRemoved(String graphId, long timeId, 186 String edgeId, String attribute) { 187 if (!edgePredicate.matches(attribute, null)) { 188 if (!globalPredicate.matches(attribute, null)) { 189 sendEdgeAttributeRemoved(graphId, timeId, edgeId, attribute); 190 } 191 } 192 } 193 194 @Override 195 public void graphAttributeAdded(String graphId, long timeId, 196 String attribute, Object value) { 197 if (!graphPredicate.matches(attribute, value)) { 198 if (!globalPredicate.matches(attribute, value)) { 199 sendGraphAttributeAdded(graphId, timeId, attribute, value); 200 } 201 } 202 } 203 204 @Override 205 public void graphAttributeChanged(String graphId, long timeId, 206 String attribute, Object oldValue, Object newValue) { 207 if (!graphPredicate.matches(attribute, newValue)) { 208 if (!globalPredicate.matches(attribute, newValue)) { 209 sendGraphAttributeChanged(graphId, timeId, attribute, oldValue, 210 newValue); 211 } 212 } 213 } 214 215 @Override 216 public void graphAttributeRemoved(String graphId, long timeId, 217 String attribute) { 218 if (!graphPredicate.matches(attribute, null)) { 219 if (!globalPredicate.matches(attribute, null)) { 220 sendGraphAttributeRemoved(graphId, timeId, attribute); 221 } 222 } 223 } 224 225 @Override 226 public void nodeAttributeAdded(String graphId, long timeId, String nodeId, 227 String attribute, Object value) { 228 if (!nodePredicate.matches(attribute, value)) { 229 if (!globalPredicate.matches(attribute, value)) { 230 sendNodeAttributeAdded(graphId, timeId, nodeId, attribute, 231 value); 232 } 233 } 234 } 235 236 @Override 237 public void nodeAttributeChanged(String graphId, long timeId, 238 String nodeId, String attribute, Object oldValue, Object newValue) { 239 if (!nodePredicate.matches(attribute, newValue)) { 240 if (!globalPredicate.matches(attribute, newValue)) { 241 sendNodeAttributeChanged(graphId, timeId, nodeId, attribute, 242 oldValue, newValue); 243 } 244 } 245 } 246 247 @Override 248 public void nodeAttributeRemoved(String graphId, long timeId, 249 String nodeId, String attribute) { 250 if (!nodePredicate.matches(attribute, null)) { 251 if (!globalPredicate.matches(attribute, null)) { 252 sendNodeAttributeRemoved(graphId, timeId, nodeId, attribute); 253 } 254 } 255 } 256 257 protected class FalsePredicate implements AttributePredicate { 258 public boolean matches(String attributeName, Object attributeValue) { 259 return false; 260 } 261 262 } 263}