Search in sources :

Example 6 with Match

use of annis.service.objects.Match in project ANNIS by korpling.

the class SaltBasedExporter method convertText.

@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
    CacheManager cacheManager = CacheManager.create();
    try {
        Cache cache = cacheManager.getCache("saltProjectsCache");
        if (keys == null || keys.isEmpty()) {
            // auto set
            keys = new LinkedList<>();
            keys.add("tok");
            List<AnnisAttribute> attributes = new LinkedList<>();
            for (String corpus : corpora) {
                attributes.addAll(annisResource.path("corpora").path(urlPathEscape.escape(corpus)).path("annotations").queryParam("fetchvalues", "false").queryParam("onlymostfrequentvalues", "false").get(new AnnisAttributeListType()));
            }
            for (AnnisAttribute a : attributes) {
                if (a.getName() != null) {
                    String[] namespaceAndName = a.getName().split(":", 2);
                    if (namespaceAndName.length > 1) {
                        keys.add(namespaceAndName[1]);
                    } else {
                        keys.add(namespaceAndName[0]);
                    }
                }
            }
        }
        Map<String, String> args = new HashMap<>();
        for (String s : argsAsString.split("&|;")) {
            String[] splitted = s.split("=", 2);
            String key = splitted[0];
            String val = "";
            if (splitted.length > 1) {
                val = splitted[1];
            }
            args.put(key, val);
        }
        int stepSize = 10;
        int pCounter = 1;
        Map<Integer, Integer> offsets = new HashMap<Integer, Integer>();
        // 1. Get all the matches as Salt ID
        InputStream matchStream = annisResource.path("search/find/").queryParam("q", Helper.encodeJersey(queryAnnisQL)).queryParam("corpora", StringUtils.join(corpora, ",")).accept(MediaType.TEXT_PLAIN_TYPE).get(InputStream.class);
        // get node count for the query
        WebResource resource = Helper.getAnnisWebResource();
        List<QueryNode> nodes = resource.path("query/parse/nodes").queryParam("q", Helper.encodeJersey(queryAnnisQL)).get(new GenericType<List<QueryNode>>() {
        });
        Integer nodeCount = nodes.size();
        try (BufferedReader inReader = new BufferedReader(new InputStreamReader(matchStream, "UTF-8"))) {
            WebResource subgraphRes = annisResource.path("search/subgraph");
            MatchGroup currentMatches = new MatchGroup();
            String currentLine;
            int offset = 1;
            // 2. iterate over all matches and get the sub-graph for a group of matches
            while (!Thread.currentThread().isInterrupted() && (currentLine = inReader.readLine()) != null) {
                Match match = Match.parseFromString(currentLine);
                currentMatches.getMatches().add(match);
                if (currentMatches.getMatches().size() >= stepSize) {
                    WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                    if (args.containsKey("segmentation")) {
                        res = res.queryParam("segmentation", args.get("segmentation"));
                    }
                    SubgraphFilter filter = getSubgraphFilter();
                    if (filter != null) {
                        res = res.queryParam("filter", filter.name());
                    }
                    Stopwatch stopwatch = Stopwatch.createStarted();
                    SaltProject p = res.post(SaltProject.class, currentMatches);
                    stopwatch.stop();
                    // export was fast enough
                    if (stopwatch.elapsed(TimeUnit.MILLISECONDS) < 500 && stepSize < 50) {
                        stepSize += 10;
                    }
                    convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size(), corpusConfigs, out, nodeCount);
                    offsets.put(pCounter, offset - currentMatches.getMatches().size());
                    cache.put(new Element(pCounter++, p));
                    currentMatches.getMatches().clear();
                    if (eventBus != null) {
                        eventBus.post(offset + 1);
                    }
                }
                offset++;
            }
            if (Thread.interrupted()) {
                return new InterruptedException("Exporter job was interrupted");
            }
            // query the left over matches
            if (!currentMatches.getMatches().isEmpty()) {
                WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                if (args.containsKey("segmentation")) {
                    res = res.queryParam("segmentation", args.get("segmentation"));
                }
                SubgraphFilter filter = getSubgraphFilter();
                if (filter != null) {
                    res = res.queryParam("filter", filter.name());
                }
                SaltProject p = res.post(SaltProject.class, currentMatches);
                convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size() - 1, corpusConfigs, out, nodeCount);
                offsets.put(pCounter, offset - currentMatches.getMatches().size() - 1);
                cache.put(new Element(pCounter++, p));
            }
            offset = 1;
        }
        // build the list of ordered match numbers (ordering by occurrence in text)
        getOrderedMatchNumbers();
        @SuppressWarnings("unchecked") List<Integer> cacheKeys = cache.getKeys();
        List<Integer> listOfKeys = new ArrayList<Integer>();
        for (Integer key : cacheKeys) {
            listOfKeys.add(key);
        }
        Collections.sort(listOfKeys);
        for (Integer key : listOfKeys) {
            SaltProject p = (SaltProject) cache.get(key).getObjectValue();
            convertSaltProject(p, keys, args, alignmc, offsets.get(key), corpusConfigs, out, null);
        }
        out.append(System.lineSeparator());
        return null;
    } catch (AnnisQLSemanticsException | AnnisQLSyntaxException | AnnisCorpusAccessException | UniformInterfaceException | IOException | CacheException | IllegalStateException | ClassCastException ex) {
        return ex;
    } finally {
        cacheManager.removalAll();
        cacheManager.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) CacheException(net.sf.ehcache.CacheException) AnnisAttribute(annis.service.objects.AnnisAttribute) Element(net.sf.ehcache.Element) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) Match(annis.service.objects.Match) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) CacheManager(net.sf.ehcache.CacheManager) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) SubgraphFilter(annis.service.objects.SubgraphFilter) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisCorpusAccessException(annis.exceptions.AnnisCorpusAccessException) QueryNode(annis.model.QueryNode) MatchGroup(annis.service.objects.MatchGroup) BufferedReader(java.io.BufferedReader) Cache(net.sf.ehcache.Cache)

Example 7 with Match

use of annis.service.objects.Match in project ANNIS by korpling.

the class SingleResultFetchJob method call.

@Override
public SaltProject call() throws Exception {
    WebResource subgraphRes = Helper.getAnnisWebResource().path("query/search/subgraph");
    if (Thread.interrupted()) {
        return null;
    }
    List<Match> subList = new LinkedList<>();
    subList.add(match);
    SaltProject p = executeQuery(subgraphRes, new MatchGroup(subList), query.getLeftContext(), query.getRightContext(), query.getSegmentation(), SubgraphFilter.all);
    return p;
}
Also used : MatchGroup(annis.service.objects.MatchGroup) WebResource(com.sun.jersey.api.client.WebResource) SaltProject(org.corpus_tools.salt.common.SaltProject) LinkedList(java.util.LinkedList) Match(annis.service.objects.Match)

Example 8 with Match

use of annis.service.objects.Match in project ANNIS by korpling.

the class ResultFetchJob method run.

@Override
public void run() {
    WebResource subgraphRes = Helper.getAnnisWebResource().path("query/search/subgraph");
    // holds the ids of the matches.
    MatchGroup result;
    try {
        if (Thread.interrupted()) {
            return;
        }
        // set the the progress bar, for given the user some information about the loading process
        ui.accessSynchronously(new Runnable() {

            @Override
            public void run() {
                resultPanel.showMatchSearchInProgress(query);
            }
        });
        // get the matches
        result = futureMatches.get();
        // get the subgraph for each match, when the result is not empty
        if (result.getMatches().isEmpty()) {
            // check if thread was interrupted
            if (Thread.interrupted()) {
                return;
            }
            // nothing found, so inform the user about this.
            ui.access(new Runnable() {

                @Override
                public void run() {
                    resultPanel.showNoResult();
                }
            });
        } else {
            if (Thread.interrupted()) {
                return;
            }
            // since annis found something, inform the user that subgraphs are created
            ui.access(new Runnable() {

                @Override
                public void run() {
                    resultPanel.showSubgraphSearchInProgress(query, 0.0f);
                }
            });
            // prepare fetching subgraphs
            final BlockingQueue<SaltProject> queue = new ArrayBlockingQueue<>(result.getMatches().size());
            int current = 0;
            final ArrayList<Match> matchList = new ArrayList<>(result.getMatches());
            for (Match m : matchList) {
                if (Thread.interrupted()) {
                    return;
                }
                List<Match> subList = new LinkedList<>();
                subList.add(m);
                final SaltProject p = executeQuery(subgraphRes, new MatchGroup(subList), query.getLeftContext(), query.getRightContext(), query.getSegmentation(), SubgraphFilter.all);
                queue.put(p);
                log.debug("added match {} to queue", current + 1);
                if (current == 0) {
                    ui.access(new Runnable() {

                        @Override
                        public void run() {
                            resultPanel.setQueryResultQueue(queue, query, matchList);
                        }
                    });
                }
                if (Thread.interrupted()) {
                    return;
                }
                current++;
            }
        }
    // end if no results
    } catch (InterruptedException ex) {
    // just return
    } catch (final ExecutionException root) {
        ui.accessSynchronously(new Runnable() {

            @Override
            public void run() {
                if (resultPanel != null && resultPanel.getPaging() != null) {
                    PagingComponent paging = resultPanel.getPaging();
                    Throwable cause = root.getCause();
                    if (cause instanceof UniformInterfaceException) {
                        UniformInterfaceException ex = (UniformInterfaceException) cause;
                        if (ex.getResponse().getStatus() == 400) {
                            List<AqlParseError> errors = ex.getResponse().getEntity(new GenericType<List<AqlParseError>>() {
                            });
                            String errMsg = Joiner.on(" | ").join(errors);
                            paging.setInfo("parsing error: " + errMsg);
                        } else if (ex.getResponse().getStatus() == 504) {
                            paging.setInfo("Timeout: query execution took too long");
                        } else if (ex.getResponse().getStatus() == 403) {
                            paging.setInfo("Not authorized to query this corpus.");
                        } else {
                            paging.setInfo("unknown error: " + ex);
                        }
                    } else {
                        log.error("Unexcepted ExecutionException cause", root);
                    }
                    resultPanel.showFinishedSubgraphSearch();
                }
            }
        });
    }
// end catch
}
Also used : ArrayList(java.util.ArrayList) AqlParseError(annis.model.AqlParseError) AsyncWebResource(com.sun.jersey.api.client.AsyncWebResource) WebResource(com.sun.jersey.api.client.WebResource) SaltProject(org.corpus_tools.salt.common.SaltProject) LinkedList(java.util.LinkedList) Match(annis.service.objects.Match) PagingComponent(annis.gui.paging.PagingComponent) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MatchGroup(annis.service.objects.MatchGroup) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with Match

use of annis.service.objects.Match in project ANNIS by korpling.

the class EmbeddedVisUI method generateVisFromRemoteURL.

private void generateVisFromRemoteURL(final String visName, final String rawUri, Map<String, String[]> args) {
    try {
        // find the matching visualizer
        final VisualizerPlugin visPlugin = this.getVisualizer(visName);
        if (visPlugin == null) {
            displayMessage("Unknown visualizer \"" + visName + "\"", "This ANNIS instance does not know the given visualizer.");
            return;
        }
        URI uri = new URI(rawUri);
        // fetch content of the URI
        Client client = null;
        AnnisUser user = Helper.getUser();
        if (user != null) {
            client = user.getClient();
        }
        if (client == null) {
            client = Helper.createRESTClient();
        }
        final WebResource saltRes = client.resource(uri);
        displayLoadingIndicator();
        // copy the arguments for using them later in the callback
        final Map<String, String[]> argsCopy = new LinkedHashMap<>(args);
        Background.runWithCallback(new Callable<SaltProject>() {

            @Override
            public SaltProject call() throws Exception {
                return saltRes.get(SaltProject.class);
            }
        }, new FutureCallback<SaltProject>() {

            @Override
            public void onFailure(Throwable t) {
                displayMessage("Could not query the result.", t.getMessage());
            }

            @Override
            public void onSuccess(SaltProject p) {
                // TODO: allow to display several visualizers when there is more than one document
                SCorpusGraph firstCorpusGraph = null;
                SDocument doc = null;
                if (p.getCorpusGraphs() != null && !p.getCorpusGraphs().isEmpty()) {
                    firstCorpusGraph = p.getCorpusGraphs().get(0);
                    if (firstCorpusGraph.getDocuments() != null && !firstCorpusGraph.getDocuments().isEmpty()) {
                        doc = firstCorpusGraph.getDocuments().get(0);
                    }
                }
                if (doc == null) {
                    displayMessage("No documents found in provided URL.", "");
                    return;
                }
                if (argsCopy.containsKey(KEY_INSTANCE)) {
                    Map<String, InstanceConfig> allConfigs = loadInstanceConfig();
                    InstanceConfig newConfig = allConfigs.get(argsCopy.get(KEY_INSTANCE)[0]);
                    if (newConfig != null) {
                        setInstanceConfig(newConfig);
                    }
                }
                // now it is time to load the actual defined instance fonts
                loadInstanceFonts();
                // generate the visualizer
                VisualizerInput visInput = new VisualizerInput();
                visInput.setDocument(doc);
                if (getInstanceConfig() != null && getInstanceConfig().getFont() != null) {
                    visInput.setFont(getInstanceFont());
                }
                Properties mappings = new Properties();
                for (Map.Entry<String, String[]> e : argsCopy.entrySet()) {
                    if (!KEY_SALT.equals(e.getKey()) && e.getValue().length > 0) {
                        mappings.put(e.getKey(), e.getValue()[0]);
                    }
                }
                visInput.setMappings(mappings);
                String[] namespace = argsCopy.get(KEY_NAMESPACE);
                if (namespace != null && namespace.length > 0) {
                    visInput.setNamespace(namespace[0]);
                } else {
                    visInput.setNamespace(null);
                }
                String baseText = null;
                if (argsCopy.containsKey(KEY_BASE_TEXT)) {
                    String[] value = argsCopy.get(KEY_BASE_TEXT);
                    if (value.length > 0) {
                        baseText = value[0];
                    }
                }
                List<SNode> segNodes = CommonHelper.getSortedSegmentationNodes(baseText, doc.getDocumentGraph());
                if (argsCopy.containsKey(KEY_MATCH)) {
                    String[] rawMatch = argsCopy.get(KEY_MATCH);
                    if (rawMatch.length > 0) {
                        // enhance the graph with match information from the arguments
                        Match match = Match.parseFromString(rawMatch[0]);
                        Helper.addMatchToDocumentGraph(match, doc);
                    }
                }
                Map<String, String> markedColorMap = new HashMap<>();
                Map<String, String> exactMarkedMap = Helper.calculateColorsForMarkedExact(doc);
                Map<String, Long> markedAndCovered = Helper.calculateMarkedAndCoveredIDs(doc, segNodes, baseText);
                Helper.calulcateColorsForMarkedAndCovered(doc, markedAndCovered, markedColorMap);
                visInput.setMarkedAndCovered(markedAndCovered);
                visInput.setMarkableMap(markedColorMap);
                visInput.setMarkableExactMap(exactMarkedMap);
                visInput.setContextPath(Helper.getContext());
                String template = Helper.getContext() + "/Resource/" + visName + "/%s";
                visInput.setResourcePathTemplate(template);
                visInput.setSegmentationName(baseText);
                // TODO: which other thing do we have to provide?
                Component c = visPlugin.createComponent(visInput, null);
                // add the styles
                c.addStyleName("corpus-font");
                c.addStyleName("vis-content");
                Link link = new Link();
                link.setCaption("Show in ANNIS search interface");
                link.setIcon(ANNISFontIcon.LOGO);
                link.setVisible(false);
                link.addStyleName("dontprint");
                link.setTargetName("_blank");
                if (argsCopy.containsKey(KEY_SEARCH_INTERFACE)) {
                    String[] interfaceLink = argsCopy.get(KEY_SEARCH_INTERFACE);
                    if (interfaceLink.length > 0) {
                        link.setResource(new ExternalResource(interfaceLink[0]));
                        link.setVisible(true);
                    }
                }
                VerticalLayout layout = new VerticalLayout(link, c);
                layout.setComponentAlignment(link, Alignment.TOP_LEFT);
                layout.setSpacing(true);
                layout.setMargin(true);
                setContent(layout);
                IDGenerator.assignID(link);
            }
        });
    } catch (URISyntaxException ex) {
        displayMessage("Invalid URL", "The provided URL is malformed:<br />" + ex.getMessage());
    } catch (LoginDataLostException ex) {
        displayMessage("LoginData Lost", "No login data available any longer in the session:<br /> " + ex.getMessage());
    } catch (UniformInterfaceException ex) {
        if (ex.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()) {
            displayMessage("Corpus access forbidden", "You are not allowed to access this corpus. " + "Please login at the <a target=\"_blank\" href=\"" + Helper.getContext() + "\">main application</a> first and then reload this page.");
        } else {
            displayMessage("Service error", ex.getMessage());
        }
    } catch (ClientHandlerException ex) {
        displayMessage("Could not generate the visualization because the ANNIS service reported an error.", ex.getMessage());
    } catch (Throwable ex) {
        displayMessage("Could not generate the visualization.", ex.getMessage() == null ? ("An unknown error of type " + ex.getClass().getSimpleName()) + " occured." : ex.getMessage());
    }
}
Also used : VisualizerPlugin(annis.libgui.visualizers.VisualizerPlugin) WebResource(com.sun.jersey.api.client.WebResource) URISyntaxException(java.net.URISyntaxException) Properties(java.util.Properties) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) Match(annis.service.objects.Match) InstanceConfig(annis.libgui.InstanceConfig) VerticalLayout(com.vaadin.ui.VerticalLayout) List(java.util.List) LinkedList(java.util.LinkedList) Client(com.sun.jersey.api.client.Client) Component(com.vaadin.ui.Component) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) ExternalResource(com.vaadin.server.ExternalResource) AnnisUser(annis.libgui.AnnisUser) URISyntaxException(java.net.URISyntaxException) LoginDataLostException(annis.libgui.LoginDataLostException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) VisualizerInput(annis.libgui.visualizers.VisualizerInput) LoginDataLostException(annis.libgui.LoginDataLostException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Link(com.vaadin.ui.Link)

Example 10 with Match

use of annis.service.objects.Match in project ANNIS by korpling.

the class LegacyGraphConverter method convertToAnnotationGraph.

public static AnnotationGraph convertToAnnotationGraph(SDocument document) {
    SDocumentGraph docGraph = document.getDocumentGraph();
    SFeature featMatchedIDs = docGraph.getFeature(ANNIS_NS, FEAT_MATCHEDIDS);
    Match match = new Match();
    if (featMatchedIDs != null && featMatchedIDs.getValue_STEXT() != null) {
        match = Match.parseFromString(featMatchedIDs.getValue_STEXT(), ',');
    }
    // get matched node names by using the IDs
    List<Long> matchedNodeIDs = new ArrayList<>();
    for (URI u : match.getSaltIDs()) {
        SNode node = docGraph.getNode(u.toASCIIString());
        if (node == null) {
            // that's weird, fallback to the id
            log.warn("Could not get matched node from id {}", u.toASCIIString());
            matchedNodeIDs.add(-1l);
        } else {
            RelannisNodeFeature relANNISFeat = (RelannisNodeFeature) node.getFeature(SaltUtil.createQName(ANNIS_NS, FEAT_RELANNIS_NODE)).getValue();
            matchedNodeIDs.add(relANNISFeat.getInternalID());
        }
    }
    AnnotationGraph result = convertToAnnotationGraph(docGraph, matchedNodeIDs);
    return result;
}
Also used : AnnotationGraph(annis.model.AnnotationGraph) RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) ArrayList(java.util.ArrayList) URI(java.net.URI) SFeature(org.corpus_tools.salt.core.SFeature) Match(annis.service.objects.Match)

Aggregations

Match (annis.service.objects.Match)18 MatchGroup (annis.service.objects.MatchGroup)9 LinkedList (java.util.LinkedList)8 SaltProject (org.corpus_tools.salt.common.SaltProject)8 WebResource (com.sun.jersey.api.client.WebResource)5 ArrayList (java.util.ArrayList)5 QueryData (annis.ql.parser.QueryData)4 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)4 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)4 URI (java.net.URI)4 HashMap (java.util.HashMap)4 SubgraphFilter (annis.service.objects.SubgraphFilter)3 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)3 AnnisCorpusAccessException (annis.exceptions.AnnisCorpusAccessException)2 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)2 AnnisQLSyntaxException (annis.exceptions.AnnisQLSyntaxException)2 AnnotationGraph (annis.model.AnnotationGraph)2 QueryNode (annis.model.QueryNode)2 AnnisAttribute (annis.service.objects.AnnisAttribute)2 MatrixQueryData (annis.sqlgen.MatrixQueryData)2