use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.
the class Helper method getCorpusConfig.
/**
* Loads the corpus config of a specific corpus.
*
* @param corpus The name of the corpus, for which the config is fetched.
* @return A {@link CorpusConfig} object, which wraps a {@link Properties} object. This Properties
* object stores the corpus configuration as simple key-value pairs.
*/
public static CorpusConfiguration getCorpusConfig(final String corpus, final UI ui) {
if (corpus == null || corpus.isEmpty()) {
Notification.show("no corpus is selected", "please select at least one corpus and execute query again", Notification.Type.WARNING_MESSAGE);
return null;
}
CorpusConfiguration corpusConfig = new CorpusConfiguration();
final CorporaApi api = new CorporaApi(getClient(ui));
try {
corpusConfig = api.corpusConfiguration(corpus);
} catch (final ApiException ex) {
ui.access(() -> ExceptionDialog.show(ex, ERROR_MESSAGE_CORPUS_PROPS_HEADER, ui));
}
return corpusConfig;
}
use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.
the class Helper method getMetaDataDoc.
/**
* Retrieve the meta data for a given document of a corpus.
*
* @param toplevelCorpusName specifies the toplevel corpus
* @param documentName specifies the document.
* @return returns only the meta data for a single document.
*/
public static List<SMetaAnnotation> getMetaDataDoc(final String toplevelCorpusName, final String documentName, final UI ui) {
final List<SMetaAnnotation> result = new ArrayList<>();
final SearchApi api = new SearchApi(Helper.getClient(ui));
try {
// Get the corpus graph and with it the meta data on the corpus/document nodes
final File graphML = api.subgraphForQuery(toplevelCorpusName, "annis:node_type=\"corpus\" _ident_ annis:doc=/" + AQL_REGEX_VALUE_ESCAPER.escape(documentName) + "/", QueryLanguage.AQL, AnnotationComponentType.PARTOF);
final SCorpusGraph cg = CorpusGraphMapper.map(graphML);
for (final SNode n : cg.getNodes()) {
result.addAll(n.getMetaAnnotations());
}
} catch (ApiException | XMLStreamException | IOException ex) {
ui.access(() -> ExceptionDialog.show(ex, "Could not retrieve metadata for document", ui));
}
return result;
}
use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.
the class Helper method getMetaAnnotationNames.
public static Set<AnnoKey> getMetaAnnotationNames(final String corpus, final UI ui) throws ApiException {
final CorporaApi api = new CorporaApi(getClient(ui));
final SearchApi search = new SearchApi(getClient(ui));
final List<org.corpus_tools.annis.api.model.Annotation> nodeAnnos = api.nodeAnnotations(corpus, false, true).stream().filter(a -> !Objects.equals(a.getKey().getNs(), "annis") && !Objects.equals(a.getKey().getName(), "tok")).collect(Collectors.toList());
final Set<AnnoKey> metaAnnos = new HashSet<>();
// Check for each annotation if its actually a meta-annotation
for (final org.corpus_tools.annis.api.model.Annotation a : nodeAnnos) {
String annotationName = getQName(a.getKey());
if (annotationIsMetadata(corpus, annotationName, search)) {
metaAnnos.add(a.getKey());
}
}
return metaAnnos;
}
use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.
the class QueryController method executeSearch.
/**
* Executes a query.
*
* @param replaceOldTab
* @param freshQuery If true the offset and the selected matches are reset before executing the
* query.
*/
public void executeSearch(boolean replaceOldTab, boolean freshQuery) {
UI ui = UI.getCurrent();
if (freshQuery && ui instanceof AnnisUI) {
getState().getOffset().setValue(0l);
getState().getSelectedMatches().setValue(new TreeSet<Long>());
// get the value for the visible segmentation from the configured context
Collection<String> selectedCorpora = getState().getSelectedCorpora();
CorpusConfiguration config = new CorpusConfiguration();
if (selectedCorpora != null && !selectedCorpora.isEmpty()) {
config = ((AnnisUI) ui).getCorpusConfigWithCache(selectedCorpora.iterator().next());
}
if (config.getView() != null) {
String configVal = config.getView().getBaseTextSegmentation();
if ("".equals(configVal) || "tok".equals(configVal)) {
configVal = null;
}
getState().getVisibleBaseText().setValue(configVal);
}
}
// construct a query from the current properties
DisplayedResultQuery displayedQuery = getSearchQuery();
searchView.getControlPanel().getQueryPanel().setStatus("Searching...");
cancelSearch();
// cleanup resources
VaadinSession session = VaadinSession.getCurrent();
session.setAttribute(IFrameResourceMap.class, new IFrameResourceMap());
if (session.getAttribute(MediaController.class) != null) {
session.getAttribute(MediaController.class).clearMediaPlayers();
}
searchView.updateFragment(displayedQuery);
if (displayedQuery.getCorpora() == null || displayedQuery.getCorpora().isEmpty()) {
searchView.getControlPanel().getQueryPanel().setStatus("No corpus selected");
searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(false);
Notification.show("Please select a corpus", Notification.Type.WARNING_MESSAGE);
return;
}
if ("".equals(displayedQuery.getQuery())) {
searchView.getControlPanel().getQueryPanel().setStatus("Empty query");
searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(false);
Notification.show("Empty query", Notification.Type.WARNING_MESSAGE);
return;
}
checkQuirksMode(displayedQuery);
addHistoryEntry(displayedQuery);
//
// begin execute match fetching
//
ResultViewPanel oldPanel = searchView.getLastSelectedResultView();
if (replaceOldTab) {
// remove old panel from view
searchView.closeTab(oldPanel);
}
if (ui instanceof AnnisUI) {
AnnisUI annisUI = (AnnisUI) ui;
ResultViewPanel newResultView = new ResultViewPanel(annisUI, displayedQuery);
newResultView.getPaging().addCallback(new SpecificPagingCallback(annisUI, searchView, newResultView, displayedQuery));
TabSheet.Tab newTab;
List<ResultViewPanel> existingResultPanels = getResultPanels();
String caption = existingResultPanels.isEmpty() ? "Query Result" : "Query Result #" + (existingResultPanels.size() + 1);
newTab = searchView.getMainTab().addTab(newResultView, caption);
newTab.setClosable(true);
newTab.setIcon(FontAwesome.SEARCH);
searchView.getMainTab().setSelectedTab(newResultView);
searchView.notifiyQueryStarted();
Background.run(new ResultFetchJob(displayedQuery, newResultView, annisUI));
//
// end execute match fetching
//
//
// begin execute count
//
// start count query
searchView.getControlPanel().getQueryPanel().setCountIndicatorEnabled(true);
SearchApi api = new SearchApi(Helper.getClient(ui));
CountQuery countQuery = new CountQuery();
countQuery.setCorpora(new LinkedList<>(displayedQuery.getCorpora()));
countQuery.setQuery(displayedQuery.getQuery());
countQuery.setQueryLanguage(displayedQuery.getApiQueryLanguage());
try {
Call call = api.countAsync(countQuery, new CountCallback(newResultView, displayedQuery.getLimit(), annisUI));
state.getExecutedCalls().put(QueryUIState.QueryType.COUNT, call);
} catch (ApiException ex) {
ExceptionDialog.show(ex, ui);
}
}
}
use of org.corpus_tools.annis.ApiException in project ANNIS by korpling.
the class QueryController method validateQuery.
public void validateQuery() {
QueryPanel qp = searchView.getControlPanel().getQueryPanel();
// reset status
qp.setError(null);
qp.setNodes(null);
String query = state.getAql().getValue();
if (query == null || query.isEmpty()) {
qp.setStatus("Empty query");
} else {
// validate query
UI ui = UI.getCurrent();
Background.runWithCallback(() -> {
SearchApi api = new SearchApi(Helper.getClient(ui));
return api.nodeDescriptions(query, org.corpus_tools.annis.api.model.QueryLanguage.AQL);
}, new FutureCallback<List<QueryAttributeDescription>>() {
@Override
public void onSuccess(List<QueryAttributeDescription> nodes) {
qp.setNodes(nodes);
if (state.getSelectedCorpora() == null || state.getSelectedCorpora().isEmpty()) {
qp.setStatus("Please select a corpus from the list below, then click on \"Search\".");
} else {
qp.setStatus("Valid query, click on \"Search\" to start searching.");
}
}
@Override
public void onFailure(Throwable t) {
if (t instanceof ApiException) {
reportServiceException((ApiException) t, false);
}
}
});
}
}
Aggregations