~keheliya-gallaba/gephi/maven-build

« back to all changes in this revision

Viewing changes to filters/src/main/java/org/gephi/filters/AbstractQueryImpl.java

  • Committer: Keheliya Gallaba
  • Date: 2011-08-01 13:01:30 UTC
  • Revision ID: keheliya.gallaba@gmail.com-20110801130130-0u2qgcufi8bvqwxv
Adding Export Plugin, Export Plugin UI, Filters Impl

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 2008-2010 Gephi
 
3
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
 
4
Website : http://www.gephi.org
 
5
 
 
6
This file is part of Gephi.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
 */
 
21
package org.gephi.filters;
 
22
 
 
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;
 
34
 
 
35
/**
 
36
 *
 
37
 * @author Mathieu Bastian
 
38
 */
 
39
public abstract class AbstractQueryImpl implements Query {
 
40
 
 
41
    protected List<AbstractQueryImpl> children;
 
42
    protected Query parent;
 
43
    protected Graph result;
 
44
 
 
45
    public AbstractQueryImpl() {
 
46
        this.children = new ArrayList<AbstractQueryImpl>();
 
47
    }
 
48
 
 
49
    public abstract int getChildrenSlotsCount();
 
50
 
 
51
    public abstract int getPropertiesCount();
 
52
 
 
53
    public abstract String getPropertyName(int index);
 
54
 
 
55
    public abstract Object getPropertyValue(int index);
 
56
 
 
57
    public abstract String getName();
 
58
 
 
59
    public abstract void setName(String name);
 
60
 
 
61
    public Query[] getChildren() {
 
62
        return children.toArray(new Query[0]);
 
63
    }
 
64
 
 
65
    public int getChildrenCount() {
 
66
        return children.size();
 
67
    }
 
68
 
 
69
    public AbstractQueryImpl getChildAt(int index) {
 
70
        return children.get(index);
 
71
    }
 
72
 
 
73
    public void addSubQuery(Query subQuery) {
 
74
        children.add((AbstractQueryImpl) subQuery);
 
75
        ((AbstractQueryImpl) subQuery).setParent(this);
 
76
    }
 
77
 
 
78
    public void removeSubQuery(Query subQuery) {
 
79
        children.remove((AbstractQueryImpl) subQuery);
 
80
    }
 
81
 
 
82
    public Query getParent() {
 
83
        return parent;
 
84
    }
 
85
 
 
86
    public void setParent(Query parent) {
 
87
        this.parent = parent;
 
88
    }
 
89
 
 
90
    public void setResult(Graph result) {
 
91
        this.result = result;
 
92
    }
 
93
 
 
94
    public Graph getResult() {
 
95
        return result;
 
96
    }
 
97
 
 
98
    public AbstractQueryImpl getRoot() {
 
99
        AbstractQueryImpl root = this;
 
100
        while (root.getParent() != null) {
 
101
            root = (AbstractQueryImpl) root.getParent();
 
102
        }
 
103
        return root;
 
104
    }
 
105
 
 
106
    public AbstractQueryImpl[] getLeaves() {
 
107
        ArrayList<AbstractQueryImpl> leaves = new ArrayList<AbstractQueryImpl>();
 
108
        Deque<Query> stack = new ArrayDeque<Query>();
 
109
        stack.add(this);
 
110
        while (!stack.isEmpty()) {
 
111
            AbstractQueryImpl query = (AbstractQueryImpl) stack.pop();
 
112
            if (query.children.size() > 0) {
 
113
                stack.addAll(query.children);
 
114
            } else {
 
115
                //Leaf
 
116
                leaves.add(query);
 
117
            }
 
118
        }
 
119
        return leaves.toArray(new AbstractQueryImpl[0]);
 
120
    }
 
121
 
 
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());
 
128
        }
 
129
 
 
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);
 
135
        }
 
136
 
 
137
        return copy;
 
138
    }
 
139
 
 
140
    public Query[] getQueries(Class<? extends Filter> filterClass) {
 
141
        List<Query> r = new LinkedList<Query>();
 
142
        LinkedList<Query> stack = new LinkedList<Query>();
 
143
        stack.add(this);
 
144
        while (!stack.isEmpty()) {
 
145
            Query q = stack.pop();
 
146
            r.add(q);
 
147
            stack.addAll(Arrays.asList(q.getChildren()));
 
148
        }
 
149
        for (Iterator<Query> itr = r.iterator(); itr.hasNext();) {
 
150
            Query q = itr.next();
 
151
            if (!q.getFilter().getClass().equals(filterClass)) {
 
152
                itr.remove();
 
153
            }
 
154
        }
 
155
        return r.toArray(new Query[0]);
 
156
    }
 
157
 
 
158
    public Query[] getDescendantsAndSelf() {
 
159
        List<Query> r = new LinkedList<Query>();
 
160
        LinkedList<Query> stack = new LinkedList<Query>();
 
161
        stack.add(this);
 
162
        while (!stack.isEmpty()) {
 
163
            Query q = stack.pop();
 
164
            r.add(q);
 
165
            stack.addAll(Arrays.asList(q.getChildren()));
 
166
        }
 
167
        return r.toArray(new Query[0]);
 
168
    }
 
169
}