use of org.gephi.filters.api.Query in project gephi by gephi.
the class FilterModelPersistenceProvider method readQuery.
private Query readQuery(XMLStreamReader reader, FilterModelImpl model, Serialization serialization) throws XMLStreamException {
String builderClassName = reader.getAttributeValue(null, "builder");
String filterClassName = reader.getAttributeValue(null, "filter");
String queryName = reader.getAttributeValue(null, "name");
FilterBuilder builder = null;
for (FilterBuilder fb : model.getLibrary().getLookup().lookupAll(FilterBuilder.class)) {
if (fb.getClass().getName().equals(builderClassName)) {
if (filterClassName != null) {
if (fb.getFilter(model.getWorkspace()).getClass().getName().equals(filterClassName)) {
builder = fb;
break;
}
} else {
builder = fb;
break;
}
}
}
if (builder == null) {
for (CategoryBuilder catBuilder : Lookup.getDefault().lookupAll(CategoryBuilder.class)) {
for (FilterBuilder fb : catBuilder.getBuilders(model.getWorkspace())) {
if (fb.getClass().getName().equals(builderClassName)) {
if (filterClassName != null) {
if (fb.getFilter(model.getWorkspace()).getClass().getName().equals(filterClassName)) {
builder = fb;
break;
}
} else {
builder = fb;
break;
}
}
}
}
}
if (builder != null) {
// Create filter
Filter filter = builder.getFilter(model.getWorkspace());
Query query;
if (filter instanceof Operator) {
query = new OperatorQueryImpl((Operator) filter);
} else {
query = new FilterQueryImpl(builder, filter);
}
if (queryName != null) {
query.setName(queryName);
}
FilterProperty property = null;
boolean end = false;
while (reader.hasNext() && !end) {
Integer eventType = reader.next();
if (eventType.equals(XMLEvent.START_ELEMENT)) {
String name = reader.getLocalName();
if ("parameter".equalsIgnoreCase(name)) {
int index = Integer.parseInt(reader.getAttributeValue(null, "index"));
property = query.getFilter().getProperties()[index];
}
} else if (eventType.equals(XMLStreamReader.CHARACTERS) && property != null) {
try {
String textValue = reader.getText();
Object value = serialization.fromText(textValue, property.getValueType());
property.setValue(value);
model.updateParameters(query);
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
} else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
property = null;
if ("query".equalsIgnoreCase(reader.getLocalName())) {
end = true;
}
}
}
return query;
}
return null;
}
use of org.gephi.filters.api.Query in project gephi by gephi.
the class FilterModelPersistenceProvider method readXML.
public void readXML(XMLStreamReader reader, FilterModelImpl model) throws XMLStreamException {
Serialization serialization = new Serialization(model.getGraphModel());
Map<Integer, Query> idMap = new HashMap<>();
boolean end = false;
while (reader.hasNext() && !end) {
Integer eventType = reader.next();
if (eventType.equals(XMLEvent.START_ELEMENT)) {
String name = reader.getLocalName();
if ("autorefresh".equalsIgnoreCase(name)) {
String val = reader.getAttributeValue(null, "value");
model.setAutoRefresh(Boolean.parseBoolean(val));
} else if ("query".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
int parentId = Integer.parseInt(parent);
Query parentQuery = idMap.get(parentId);
// For example a partition filter, which depends on partitions, and partitions are not serialized
if (parentQuery != null) {
model.setSubQuery(parentQuery, query);
}
} else {
// Top query
model.addFirst(query);
}
}
} else if ("savedquery".equalsIgnoreCase(name)) {
String id = reader.getAttributeValue(null, "id");
String parent = reader.getAttributeValue(null, "parent");
Query query = readQuery(reader, model, serialization);
if (query != null) {
idMap.put(Integer.parseInt(id), query);
if (parent != null) {
int parentId = Integer.parseInt(parent);
Query parentQuery = idMap.get(parentId);
if (parentQuery != null) {
AbstractQueryImpl impl = (AbstractQueryImpl) parentQuery;
impl.addSubQuery(query);
}
} else {
model.getLibrary().saveQuery(query);
}
}
}
} else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
if ("filtermodel".equalsIgnoreCase(reader.getLocalName())) {
end = true;
}
}
}
// Init filters
Graph graph;
graph = model.getGraphModel().getGraph();
for (Query rootQuery : model.getQueries()) {
for (Query q : rootQuery.getDescendantsAndSelf()) {
Filter filter = q.getFilter();
if (filter instanceof NodeFilter || filter instanceof EdgeFilter || filter instanceof ElementFilter) {
FilterProcessor filterProcessor = new FilterProcessor();
filterProcessor.init(filter, graph);
}
}
}
}
use of org.gephi.filters.api.Query in project gephi by gephi.
the class FilterThread method run.
@Override
public void run() {
while (running) {
AbstractQueryImpl q;
while ((q = rootQuery.getAndSet(null)) == null && running) {
try {
synchronized (this.lock) {
lock.wait();
}
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}
}
if (!running) {
return;
}
Query modifiedQuery = null;
for (Iterator<PropertyModifier> itr = modifiersMap.values().iterator(); itr.hasNext(); ) {
PropertyModifier pm = itr.next();
itr.remove();
pm.callback.setValue(pm.value);
modifiedQuery = pm.query;
}
if (modifiedQuery != null) {
model.updateParameters(modifiedQuery);
}
// Progress
ProgressTicket progressTicket = null;
ProgressTicketProvider progressTicketProvider = Lookup.getDefault().lookup(ProgressTicketProvider.class);
if (progressTicketProvider != null) {
String msg = NbBundle.getMessage(FilterThread.class, "FilterThread.progress.taskName", q.getName());
progressTicket = progressTicketProvider.createTicket(msg, null);
Progress.start(progressTicket);
}
if (filtering) {
filter(q);
} else {
select(q);
}
Progress.finish(progressTicket);
/*try {
//System.out.println("filter query " + q.getName());
Thread.sleep(5000);
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
}*/
}
// clear map
Query q = null;
for (PropertyModifier pm : modifiersMap.values()) {
pm.callback.setValue(pm.value);
q = pm.query;
}
modifiersMap.clear();
if (q != null) {
model.updateParameters(q);
}
}
use of org.gephi.filters.api.Query in project gephi by gephi.
the class TimelineControllerImpl method setInterval.
@Override
public void setInterval(double from, double to) {
if (model != null) {
if (model.getIntervalStart() != from || model.getIntervalEnd() != to) {
if (from >= to) {
throw new IllegalArgumentException("from should be less than to");
}
if (!(Double.isInfinite(from) && Double.isInfinite(to))) {
if (from < model.getCustomMin() || to > model.getCustomMax()) {
throw new IllegalArgumentException("From and to should be in the bounds");
}
}
model.setInterval(from, to);
// Filter magic
Query dynamicQuery = null;
boolean selecting = false;
// Get or create Dynamic Query
if (filterModel.getCurrentQuery() != null) {
// Look if current query is dynamic - filtering must be active
Query query = filterModel.getCurrentQuery();
Query[] dynamicQueries = query.getQueries(DynamicRangeFilter.class);
if (dynamicQueries.length > 0) {
dynamicQuery = query;
selecting = filterModel.isSelecting();
}
} else if (filterModel.getQueries().length == 1) {
// Look if a dynamic query alone exists
Query query = filterModel.getQueries()[0];
Query[] dynamicQueries = query.getQueries(DynamicRangeFilter.class);
if (dynamicQueries.length > 0) {
dynamicQuery = query;
}
}
if (Double.isInfinite(from) && Double.isInfinite(to)) {
if (dynamicQuery != null) {
filterController.remove(dynamicQuery);
}
} else {
if (dynamicQuery == null) {
// Create dynamic filter
DynamicRangeBuilder rangeBuilder = filterModel.getLibrary().getLookup().lookup(DynamicRangeBuilder.class);
FilterBuilder[] fb = rangeBuilder.getBuilders(filterModel.getWorkspace());
if (fb.length > 0) {
dynamicQuery = filterController.createQuery(fb[0]);
filterController.add(dynamicQuery);
}
}
if (dynamicQuery != null) {
dynamicQuery.getFilter().getProperties()[0].setValue(new Range(from, to));
if (selecting) {
filterController.selectVisible(dynamicQuery);
} else {
filterController.filterVisible(dynamicQuery);
}
fireTimelineModelEvent(new TimelineModelEvent(TimelineModelEvent.EventType.INTERVAL, model, new double[] { from, to }));
}
}
}
}
}
use of org.gephi.filters.api.Query in project gephi by gephi.
the class FilterBuilderNodeDefaultAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent ev) {
FilterBuilderNode node = (FilterBuilderNode) ev.getSource();
FilterBuilder builder = node.getBuilder();
FilterController filterController = Lookup.getDefault().lookup(FilterController.class);
Query function = filterController.createQuery(builder);
filterController.add(function);
}
Aggregations