コード例 #1
0
def test_recursive_recursive_rule_fails(store, reasoner):
    document = """
        @prefix example: <http://example.com/> .
        @prefix schema: <https://schema.org/> .

        ($s a example:Widget) → (
            ($p a example:WeirdProperty) → (
                ($s $p $o) → ($o $p $o) .
            ) .
        ) .
    """

    generic_rule.parse_and_register(document, reasoner)
コード例 #2
0
def test_sibling_symmetric_parse_and_register_1(store, reasoner):
    document = """
        @prefix schema: <https://schema.org/> .

        ($child1 schema:sibling $child2)
            → ($child2 schema:sibling $child1) .
    """

    generic_rule.parse_and_register(document, reasoner)

    reasoner.insert(A, SIBLING, B)

    assert (A, SIBLING, B) in store
    assert (B, SIBLING, A) in store
コード例 #3
0
def test_fixed_object(store, reasoner):
    document = """
        @prefix example: <http://example.com/> .
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix schema: <https://schema.org/> .

        ($s a schema:Person) → ($s a example:Person) .
    """

    generic_rule.parse_and_register(document, reasoner)

    reasoner.insert(A, TYPE, PERSON)
    reasoner.insert(B, TYPE, ORGANISATION)

    assert (A, TYPE, IRI("http://example.com/Person")) in store
    assert (B, TYPE, IRI("http://example.com/Person")) not in store
コード例 #4
0
def test_symmetric_register(store, reasoner):
    document = """
        @prefix owl: <http://www.w3.org/2002/07/owl#> .
        @prefix schema: <https://schema.org/> .

        ($p a owl:SymmetricProperty) → (
            ($s $p $o) → ($o $p $s) .
        ) .
    """

    generic_rule.parse_and_register(document, reasoner)

    reasoner.insert(SPOUSE, TYPE, SYMMETRIC_PROPERTY)
    reasoner.insert(A, SPOUSE, B)

    assert (B, SPOUSE, A) in store
コード例 #5
0
def test_sibling_symmetric_parse_and_register_many(store, reasoner):
    document = """
        @prefix schema: <https://schema.org/> .

        ($child1 schema:parent $parent), ($child2 schema:parent $parent) st ($child1 is-not $child2)
            → ($child1 schema:sibling $child2) .
    """

    generic_rule.parse_and_register(document, reasoner)

    reasoner.insert(A, PARENT, C)
    reasoner.insert(B, PARENT, C)

    assert (A, SIBLING, B) in store
    assert (B, SIBLING, A) in store
    assert (A, SIBLING, A) not in store
コード例 #6
0
def test_fixed_subject(store, reasoner):
    """This rule is arbitrary, as I can't think of a legit
    use case for this."""

    document = """
        @prefix example: <http://example.com/> .
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

        (example:A a $o) → ($o rdfs:subclassOf rdfs:Resource) .
    """

    generic_rule.parse_and_register(document, reasoner)

    reasoner.insert(A, TYPE, FOO)
    reasoner.insert(B, TYPE, BAR)

    assert (FOO, SUBCLASS_OF, RESOURCE) in store
    assert (BAR, SUBCLASS_OF, RESOURCE) not in store
コード例 #7
0
ファイル: mutant.py プロジェクト: alexchamberlain/mutant
def reason(namespace, filenames, output):
    store = InMemoryHexastore()
    reasoner = default_forward_reasoner(store)

    namespaces: Dict[str, Namespace] = {
        n: Namespace(n, IRI(i))
        for n, i in namespace
    }

    try:
        with Timer() as t:
            for filename in filenames:
                if filename.endswith("ttl") or filename.endswith("nt"):
                    triples = []
                    with open(filename) as fo:
                        new_namespaces = turtle.parse(
                            fo.read(), lambda s, p, o: triples.append(
                                (s, p, o)))

                    _update_namespaces(namespaces, new_namespaces)
                    reasoner.bulk_insert(triples)
                elif filename.endswith("mtt"):
                    with open(filename) as fo:
                        generic_rule.parse_and_register(fo.read(), reasoner)
                else:
                    raise RuntimeError(f"Unknown file {filename}")
    finally:
        logger.info(f"Parsing took {t.interval} seconds.")

    try:
        with Timer() as t:
            print(f"# Triples {len(store)}")

            with smart_open(output) as fo:
                turtle_serialiser.serialise(store, fo,
                                            [(n.name, n.prefix)
                                             for n in namespaces.values()])
    finally:
        logger.info(f"Serialisation took {t.interval} seconds.")