Search in sources :

Example 6 with Query

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;
}
Also used : Query(org.gephi.filters.api.Query) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 7 with Query

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);
            }
        }
    }
}
Also used : Query(org.gephi.filters.api.Query) HashMap(java.util.HashMap) Serialization(org.gephi.utils.Serialization) Graph(org.gephi.graph.api.Graph)

Example 8 with Query

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);
    }
}
Also used : ProgressTicketProvider(org.gephi.utils.progress.ProgressTicketProvider) Query(org.gephi.filters.api.Query) ProgressTicket(org.gephi.utils.progress.ProgressTicket)

Example 9 with Query

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 }));
                }
            }
        }
    }
}
Also used : Query(org.gephi.filters.api.Query) DynamicRangeFilter(org.gephi.filters.plugin.dynamic.DynamicRangeBuilder.DynamicRangeFilter) FilterBuilder(org.gephi.filters.spi.FilterBuilder) Range(org.gephi.filters.api.Range) DynamicRangeBuilder(org.gephi.filters.plugin.dynamic.DynamicRangeBuilder)

Example 10 with Query

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);
}
Also used : Query(org.gephi.filters.api.Query) FilterBuilder(org.gephi.filters.spi.FilterBuilder) FilterController(org.gephi.filters.api.FilterController)

Aggregations

Query (org.gephi.filters.api.Query)21 FilterBuilder (org.gephi.filters.spi.FilterBuilder)6 FilterController (org.gephi.filters.api.FilterController)4 LinkedList (java.util.LinkedList)3 AbstractNode (org.openide.nodes.AbstractNode)3 Node (org.openide.nodes.Node)3 Transferable (java.awt.datatransfer.Transferable)2 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)2 IOException (java.io.IOException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 FilterBuilderNode (org.gephi.desktop.filters.library.FilterBuilderNode)2 Graph (org.gephi.graph.api.Graph)2 GraphController (org.gephi.graph.api.GraphController)2 GraphModel (org.gephi.graph.api.GraphModel)2 Serialization (org.gephi.utils.Serialization)2 PasteType (org.openide.util.datatransfer.PasteType)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 PropertyEditor (java.beans.PropertyEditor)1 ArrayList (java.util.ArrayList)1