use of annis.sqlgen.model.Precedence in project ANNIS by korpling.
the class JoinListener method enterRangePrecedence.
@Override
public void enterRangePrecedence(AqlParser.RangePrecedenceContext ctx) {
QueryNode left = relationChain.get(relationIdx);
QueryNode right = relationChain.get(relationIdx + 1);
QueryNode.Range range = annisRangeFromARangeSpec(ctx.rangeSpec());
if (range.getMin() == 0 || range.getMax() == 0) {
throw new AnnisQLSemanticsException(AnnisParserAntlr.getLocation(ctx.getStart(), ctx.getStop()), "Distance can't be 0");
} else if (range.getMin() > range.getMax()) {
throw new AnnisQLSemanticsException(AnnisParserAntlr.getLocation(ctx.getStart(), ctx.getStop()), "Minimal distance can't be larger than maximal distance");
} else {
String segmentationName = getLayerName(ctx.NAMED_PRECEDENCE());
left.addOutgoingJoin(addParsedLocation(ctx, new Precedence(right, range.getMin(), range.getMax(), segmentationName)));
}
}
use of annis.sqlgen.model.Precedence in project ANNIS by korpling.
the class JoinListener method enterIndirectPrecedence.
@Override
public void enterIndirectPrecedence(AqlParser.IndirectPrecedenceContext ctx) {
QueryNode left = relationChain.get(relationIdx);
QueryNode right = relationChain.get(relationIdx + 1);
String segmentationName = getLayerName(ctx.NAMED_PRECEDENCE());
if (precedenceBound > 0) {
left.addOutgoingJoin(addParsedLocation(ctx, new Precedence(right, 1, precedenceBound, segmentationName)));
} else {
left.addOutgoingJoin(addParsedLocation(ctx, new Precedence(right, segmentationName)));
}
}
use of annis.sqlgen.model.Precedence in project ANNIS by korpling.
the class JoinListener method enterDirectPrecedence.
@Override
public void enterDirectPrecedence(AqlParser.DirectPrecedenceContext ctx) {
QueryNode left = relationChain.get(relationIdx);
QueryNode right = relationChain.get(relationIdx + 1);
String segmentationName = null;
if (ctx.NAMED_PRECEDENCE() != null) {
segmentationName = ctx.NAMED_PRECEDENCE().getText().substring(1);
}
left.addOutgoingJoin(addParsedLocation(ctx, new Precedence(right, 1, segmentationName)));
}
use of annis.sqlgen.model.Precedence 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.sqlgen.model.Precedence in project ANNIS by korpling.
the class TestDefaultWhereClauseGenerator method whereClauseForNodeAnnotation2Nodes.
@Test
public void whereClauseForNodeAnnotation2Nodes() {
node23.addNodeAnnotation(new QueryAnnotation("namespace1", "name1"));
node23.addNodeAnnotation(new QueryAnnotation("namespace2", "name2"));
node42.addNodeAnnotation(new QueryAnnotation("namespace3", "name3"));
node42.addNodeAnnotation(new QueryAnnotation("namespace4", "name4"));
node23.addOutgoingJoin(new Precedence(node42, 1));
checkWhereConditions(node23, "_annotation23_1.qannotext LIKE 'namespace1:name1:%'", "_annotation23_2.qannotext LIKE 'namespace2:name2:%'", "_node23.right_token = _node42.left_token - 1", "_node23.text_ref = _node42.text_ref");
checkWhereConditions(node42, "_annotation42_1.qannotext LIKE 'namespace3:name3:%'", "_annotation42_2.qannotext LIKE 'namespace4:name4:%'");
}
Aggregations