use of annis.model.QueryNode in project ANNIS by korpling.
the class AnnotationExistenceValidator method transform.
@Override
public QueryData transform(QueryData data) {
List<Long> corpusList = data.getCorpusList();
if (queryDao != null && (corpusList != null) && !corpusList.isEmpty()) {
// get first corpus name
// List<AnnisCorpus> mycorpora = queryDao.listCorpora();
// String firstcorpusname = mycorpora.get(0).getName();
Set<String> result = new TreeSet<>();
/*get a list of all annotations in a similar way that TigerQueryBuilder gets it through
QueryServiceImpl in queryDao.listAnnotations()*/
List<AnnisAttribute> atts = queryDao.listAnnotations(corpusList, false, true);
// among them, get only node annotations
for (AnnisAttribute a : atts) {
if (a.getType() == AnnisAttribute.Type.node) {
List<String> splitted = Splitter.on(":").limit(2).omitEmptyStrings().trimResults().splitToList(a.getName());
result.add(splitted.get(splitted.size() - 1));
// result is a set of strings of available annotations
}
}
List<AqlParseError> errors = new LinkedList<>();
for (List<QueryNode> alternative : data.getAlternatives()) {
for (QueryNode n : alternative) {
Set<QueryAnnotation> m = n.getNodeAnnotations();
// always get the first one
if (!m.isEmpty()) {
// name is the node name string, ready to check if name is in the list of
// available names
String name = m.iterator().next().getName();
if (!result.contains(name)) {
errors.add(new AqlParseError(n, "\"" + name + "\"" + " is not a valid annotation name in selected corpora "));
}
}
}
}
if (!errors.isEmpty()) {
throw new AnnisQLSemanticsException("Invalid annotation names detected.", errors);
}
}
return data;
}
use of annis.model.QueryNode in project ANNIS by korpling.
the class TransitivePrecedenceOptimizer method propagateNodePrecedence.
private void propagateNodePrecedence(QueryNode initialNode, QueryNode currentNode, Set<Long> visitedNodes, Map<Long, Set<Precedence>> outJoins, Range range, String segmentation) {
visitedNodes.add(currentNode.getId());
Map<QueryNode, Range> nextNodes = new HashMap<>();
// iterator over all outgoing precedence joins
List<Join> originalJoins = new LinkedList<>(currentNode.getOutgoingJoins());
for (Join join : originalJoins) {
if (join instanceof Precedence) {
Precedence p = (Precedence) join;
if ((segmentation == null && p.getSegmentationName() == null) || (segmentation != null && segmentation.equals(p.getSegmentationName()))) {
Range newRange;
if (range == null) {
// create a new range at initial node
newRange = new Range(p.getMinDistance(), p.getMaxDistance());
} else {
// calculate the new range depending on old one
if (currentNode.isToken() == false || (range.getMin() == 0 && range.getMax() == 0) || (p.getMinDistance() == 0 && p.getMaxDistance() == 0)) {
// use unlimited range since
// a) the node could also be a
// span covering more than one token,
// b) the original constraint is an unlimited range
newRange = new Range(0, 0);
} else {
// add the new precendence values to the old one
newRange = new Range(range.getMin() + p.getMinDistance(), range.getMax() + p.getMaxDistance());
}
}
// put the target node in the list of nodes to check if not visited yet
if (!visitedNodes.contains(p.getTarget().getId())) {
nextNodes.put(p.getTarget(), newRange);
Precedence newJoin = new Precedence(p.getTarget(), newRange.getMin(), newRange.getMax());
Set<Precedence> existingJoins = outJoins.get(initialNode.getId());
// only add if this join is not already included
// (which is always true for the initial node)
// and the join is more restrictive than any previous one
boolean moreRestrictive = true;
for (Precedence oldJoin : existingJoins) {
if (oldJoin.getTarget() == newJoin.getTarget()) {
if (!joinMoreRestrictive(oldJoin, newJoin)) {
moreRestrictive = false;
break;
}
}
}
if (moreRestrictive) {
// add newly created discovered transitive precedence
initialNode.addOutgoingJoin(newJoin);
existingJoins.add(newJoin);
}
}
// end if not visited yet
}
// end if segmentation matches
}
// end if is precedence join
}
for (Map.Entry<QueryNode, Range> e : nextNodes.entrySet()) {
// call us recursivly but remember the range
propagateNodePrecedence(initialNode, e.getKey(), visitedNodes, outJoins, e.getValue(), segmentation);
}
}
use of annis.model.QueryNode in project ANNIS by korpling.
the class SampleWhereClause method whereConditions.
@Override
public Set<String> whereConditions(QueryData queryData, List<QueryNode> alternative, String indent) {
Set<String> conditions = new HashSet<>();
List<Long> corpusList = queryData.getCorpusList();
List<Long> documents = queryData.getDocuments();
for (QueryNode node : alternative) {
conditions.addAll(whereConditions(node, corpusList, documents));
}
return conditions;
}
use of annis.model.QueryNode in project ANNIS by korpling.
the class SelectedFactsFromClauseGenerator method fromClause.
@Override
public String fromClause(QueryData queryData, List<QueryNode> alternative, String indent) {
List<String> clauses = new LinkedList<>();
for (QueryNode node : alternative) {
TableAccessStrategy tas = tables(node);
String aliasName = TableAccessStrategy.aliasedTable(node, tas.getTableAliases(), TableAccessStrategy.FACTS_TABLE, 1);
clauses.add(inheritedFactTables(queryData.getCorpusList(), indent) + " AS " + aliasName);
}
return Joiner.on(",\n" + indent + AbstractSqlGenerator.TABSTOP).join(clauses);
}
use of annis.model.QueryNode in project ANNIS by korpling.
the class FindSqlGenerator method selectClause.
@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
StringBuilder sb = new StringBuilder();
String indent2 = indent + TABSTOP;
sb.append(indent2).append("solution.*,\n");
// add node annotation namespace and name for each query node
int i = 0;
Iterator<QueryNode> itNodes = alternative.iterator();
while (itNodes.hasNext()) {
i++;
QueryNode n = itNodes.next();
TableAccessStrategy tas = tables(n);
sb.append(indent2).append(annoCondition.getNodeAnnoNamespaceSQL(tas)).append(" AS node_annotation_ns").append(i).append(",\n");
sb.append(indent2).append(annoCondition.getNodeAnnoNameSQL(tas)).append(" AS node_annotation_name").append(i).append(",\n");
// corpus path is only needed once
sb.append(indent2).append("c.path_name AS path_name");
if (itNodes.hasNext()) {
sb.append(",\n");
}
}
return sb.toString();
}
Aggregations