2
Copyright 2008-2010 Gephi
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
4
Website : http://www.gephi.org
6
This file is part of Gephi.
8
Gephi is free software: you can redistribute it and/or modify
9
it under the terms of the GNU Affero General Public License as
10
published by the Free Software Foundation, either version 3 of the
11
License, or (at your option) any later version.
13
Gephi is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Affero General Public License for more details.
18
You should have received a copy of the GNU Affero General Public License
19
along with Gephi. If not, see <http://www.gnu.org/licenses/>.
21
package org.gephi.filters;
23
import java.util.ArrayDeque;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.Deque;
27
import java.util.Iterator;
28
import java.util.LinkedList;
29
import java.util.List;
30
import org.gephi.filters.api.Query;
31
import org.gephi.filters.spi.Filter;
32
import org.gephi.filters.spi.Operator;
33
import org.gephi.graph.api.Graph;
37
* @author Mathieu Bastian
39
public abstract class AbstractQueryImpl implements Query {
41
protected List<AbstractQueryImpl> children;
42
protected Query parent;
43
protected Graph result;
45
public AbstractQueryImpl() {
46
this.children = new ArrayList<AbstractQueryImpl>();
49
public abstract int getChildrenSlotsCount();
51
public abstract int getPropertiesCount();
53
public abstract String getPropertyName(int index);
55
public abstract Object getPropertyValue(int index);
57
public abstract String getName();
59
public abstract void setName(String name);
61
public Query[] getChildren() {
62
return children.toArray(new Query[0]);
65
public int getChildrenCount() {
66
return children.size();
69
public AbstractQueryImpl getChildAt(int index) {
70
return children.get(index);
73
public void addSubQuery(Query subQuery) {
74
children.add((AbstractQueryImpl) subQuery);
75
((AbstractQueryImpl) subQuery).setParent(this);
78
public void removeSubQuery(Query subQuery) {
79
children.remove((AbstractQueryImpl) subQuery);
82
public Query getParent() {
86
public void setParent(Query parent) {
90
public void setResult(Graph result) {
94
public Graph getResult() {
98
public AbstractQueryImpl getRoot() {
99
AbstractQueryImpl root = this;
100
while (root.getParent() != null) {
101
root = (AbstractQueryImpl) root.getParent();
106
public AbstractQueryImpl[] getLeaves() {
107
ArrayList<AbstractQueryImpl> leaves = new ArrayList<AbstractQueryImpl>();
108
Deque<Query> stack = new ArrayDeque<Query>();
110
while (!stack.isEmpty()) {
111
AbstractQueryImpl query = (AbstractQueryImpl) stack.pop();
112
if (query.children.size() > 0) {
113
stack.addAll(query.children);
119
return leaves.toArray(new AbstractQueryImpl[0]);
122
public AbstractQueryImpl copy() {
123
AbstractQueryImpl copy = null;
124
if (this instanceof FilterQueryImpl) {
125
copy = new FilterQueryImpl(this.getFilter());
126
} else if (this instanceof OperatorQueryImpl) {
127
copy = new OperatorQueryImpl((Operator) this.getFilter());
130
for (int i = 0; i < children.size(); i++) {
131
AbstractQueryImpl child = (AbstractQueryImpl) children.get(i);
132
AbstractQueryImpl childCopy = child.copy();
133
childCopy.parent = copy;
134
copy.children.add(childCopy);
140
public Query[] getQueries(Class<? extends Filter> filterClass) {
141
List<Query> r = new LinkedList<Query>();
142
LinkedList<Query> stack = new LinkedList<Query>();
144
while (!stack.isEmpty()) {
145
Query q = stack.pop();
147
stack.addAll(Arrays.asList(q.getChildren()));
149
for (Iterator<Query> itr = r.iterator(); itr.hasNext();) {
150
Query q = itr.next();
151
if (!q.getFilter().getClass().equals(filterClass)) {
155
return r.toArray(new Query[0]);
158
public Query[] getDescendantsAndSelf() {
159
List<Query> r = new LinkedList<Query>();
160
LinkedList<Query> stack = new LinkedList<Query>();
162
while (!stack.isEmpty()) {
163
Query q = stack.pop();
165
stack.addAll(Arrays.asList(q.getChildren()));
167
return r.toArray(new Query[0]);