def test_evaluate():
    gp = GraphPattern((
        (SOURCE_VAR, wikilink, TARGET_VAR),
        (SOURCE_VAR, a, URIRef('http://dbpedia.org/ontology/PopulatedPlace')),
        (TARGET_VAR, a, URIRef('http://schema.org/Country'))
    ))
    res = evaluate(
        sparql, timeout, gtp_scores, gp)
    # (655, 0.4048, 0.4048, 0.0089, 7.5, 3, 3, 2, 0, 0.1936)
    # (remains, score, gain, f_measure, avg_reslens, gt_matches,
    #  patlen, patvars, timeout, qtime)
    update_individuals([gp], [res])
    fitness = gp.fitness.values
    matching_node_pairs = gp.matching_node_pairs
    gtp_precisions = gp.gtp_precisions
    gp.matching_node_pairs = matching_node_pairs
    logger.info(gp.matching_node_pairs)

    assert fitness.remains == len(ground_truth_pairs), 'remains wrong?'
    assert fitness.gt_matches == 3, "didn't match 3 gt pairs?"
    score = fitness.score
    assert 0 < score < 0.5, 'score not correct?'
    assert score == fitness.gain, 'score and gain should be the same here'
    assert 0 < fitness.f_measure < 0.1, \
        'f1 measure not correct?'
    assert fitness.patlen == 3, 'pattern should have 3 triples'
    assert fitness.patvars == 2, 'pattern should have 2 vars'
    if not query_time_soft_exceeded(fitness.qtime, timeout):
        assert 0 < fitness.avg_reslens < 10, \
            'avg match count should be ~7.5'
        assert fitness.timeout == 0, 'should not be a timeout'
    else:
        assert 0 < fitness.avg_reslens < 15, \
            'avg match count out of bounds for timeout'
        assert fitness.timeout > 0, 'should be a timeout'

    assert isinstance(gtp_precisions, OrderedDict)
    assert list(gtp_precisions) == matching_node_pairs
    logger.info(gtp_precisions)
    assert sum(gtp_precisions.values()) == fitness.gain, \
        'sum of precisions should be gain in this case'
def test_evaluate():
    gp = GraphPattern(
        ((SOURCE_VAR, wikilink, TARGET_VAR),
         (SOURCE_VAR, a, URIRef('http://dbpedia.org/ontology/PopulatedPlace')),
         (TARGET_VAR, a, URIRef('http://schema.org/Country'))))
    res = evaluate(sparql, timeout, gtp_scores, gp)
    # (655, 0.4048, 0.4048, 0.0089, 7.5, 3, 3, 2, 0, 0.1936)
    # (remains, score, gain, f_measure, avg_reslens, gt_matches,
    #  patlen, patvars, timeout, qtime)
    update_individuals([gp], [res])
    fitness = gp.fitness.values
    matching_node_pairs = gp.matching_node_pairs
    gtp_precisions = gp.gtp_precisions
    gp.matching_node_pairs = matching_node_pairs
    logger.info(gp.matching_node_pairs)

    assert fitness.remains == len(ground_truth_pairs), 'remains wrong?'
    assert fitness.gt_matches == 3, "didn't match 3 gt pairs?"
    score = fitness.score
    assert 0 < score < 0.5, 'score not correct?'
    assert score == fitness.gain, 'score and gain should be the same here'
    assert 0 < fitness.f_measure < 0.1, \
        'f1 measure not correct?'
    assert fitness.patlen == 3, 'pattern should have 3 triples'
    assert fitness.patvars == 2, 'pattern should have 2 vars'
    if not query_time_soft_exceeded(fitness.qtime, timeout):
        assert 0 < fitness.avg_reslens < 10, \
            'avg match count should be ~7.5'
        assert fitness.timeout == 0, 'should not be a timeout'
    else:
        assert 0 < fitness.avg_reslens < 15, \
            'avg match count out of bounds for timeout'
        assert fitness.timeout > 0, 'should be a timeout'

    assert isinstance(gtp_precisions, OrderedDict)
    assert list(gtp_precisions) == matching_node_pairs
    logger.info(gtp_precisions)
    assert sum(gtp_precisions.values()) == fitness.gain, \
        'sum of precisions should be gain in this case'
def test_timeout_pattern():
    u = URIRef('http://dbpedia.org/resource/Template:Reflist')
    wpdisambig = URIRef('http://dbpedia.org/ontology/wikiPageDisambiguates')
    gp = GraphPattern([
        (SOURCE_VAR, Variable('v1'), u),
        (SOURCE_VAR, Variable('v5'), u),
        (TARGET_VAR, Variable('v0'), u),
        (TARGET_VAR, Variable('v3'), u),
        (TARGET_VAR, Variable('v6'), Variable('v2')),
        (Variable('v4'), wpdisambig, TARGET_VAR),
    ])
    res = evaluate(sparql, timeout, gtp_scores, gp)
    update_individuals([gp], [res])
    fitness = gp.fitness.values
    matching_node_pairs = gp.matching_node_pairs
    gp.matching_node_pairs = matching_node_pairs
    logger.info(gp.matching_node_pairs)
    assert query_time_soft_exceeded(fitness.qtime, timeout)
    assert fitness.score == 0
    if query_time_hard_exceeded(fitness.qtime, timeout):
        assert fitness.f_measure == 0
    else:
        assert fitness.f_measure > 0
def test_timeout_pattern():
    u = URIRef('http://dbpedia.org/resource/Template:Reflist')
    wpdisambig = URIRef('http://dbpedia.org/ontology/wikiPageDisambiguates')
    gp = GraphPattern([
        (SOURCE_VAR, Variable('v1'), u),
        (SOURCE_VAR, Variable('v5'), u),
        (TARGET_VAR, Variable('v0'), u),
        (TARGET_VAR, Variable('v3'), u),
        (TARGET_VAR, Variable('v6'), Variable('v2')),
        (Variable('v4'), wpdisambig, TARGET_VAR),
    ])
    res = evaluate(
        sparql, timeout, gtp_scores, gp)
    update_individuals([gp], [res])
    fitness = gp.fitness.values
    matching_node_pairs = gp.matching_node_pairs
    gp.matching_node_pairs = matching_node_pairs
    logger.info(gp.matching_node_pairs)
    assert query_time_soft_exceeded(fitness.qtime, timeout)
    assert fitness.score == 0
    if query_time_hard_exceeded(fitness.qtime, timeout):
        assert fitness.f_measure == 0
    else:
        assert fitness.f_measure > 0