Exemplo n.º 1
0
def test_triple_member():
    var_parser = make_parse("triple")
    triple = var_parser.parse("($parent ∈ <http://example.com/set1>)")

    assert triple == (
        Variable("parent"),
        IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#member"),
        IRI("http://example.com/set1"),
    )
Exemplo n.º 2
0
def test_triple():
    var_parser = make_parse("triple")
    triple = var_parser.parse("($parent a <https://schema.org/Person>)")

    assert triple == (
        Variable("parent"),
        IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
        IRI("https://schema.org/Person"),
    )
Exemplo n.º 3
0
def test_typed_literal(store):
    document = """
        <http://bnb.data.bl.uk/id/person/%C3%96zbayKaan1964-/birth> <http://purl.org/vocab/bio/0.1/date> "1964"^^<http://www.w3.org/2001/XMLSchema#gYear> .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [(
        IRI("http://bnb.data.bl.uk/id/person/%C3%96zbayKaan1964-/birth"),
        IRI("http://purl.org/vocab/bio/0.1/date"),
        TypedLiteral("1964", IRI("http://www.w3.org/2001/XMLSchema#gYear")),
    )]
Exemplo n.º 4
0
def test_turtle_example_2(store):
    document = """
        <http://example.org/#spiderman>
            <http://www.perceive.net/schemas/relationship/enemyOf>
            <http://example.org/#green-goblin> .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [(
        IRI("http://example.org/#spiderman"),
        IRI("http://www.perceive.net/schemas/relationship/enemyOf"),
        IRI("http://example.org/#green-goblin"),
    )]
Exemplo n.º 5
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
Exemplo n.º 6
0
def test_turtle_example_4(store):
    document = """
        <http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman", "Человек-паук"@ru .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (IRI("http://example.org/#spiderman"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Spiderman"),
        (
            IRI("http://example.org/#spiderman"),
            IRI("http://xmlns.com/foaf/0.1/name"),
            LangTaggedString("Человек-паук", "ru"),
        ),
    ]
Exemplo n.º 7
0
def cat(namespace, filenames, output):
    store = VersionedInMemoryHexastore()

    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)
                    store.bulk_insert(triples, 1)
                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.")
Exemplo n.º 8
0
def test_symmetric():
    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) .
        ) .
    """

    rules = generic_rule.parse(document)

    assert rules == [
        generic_rule.RecursiveRule(
            ((Variable("p"),
              IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
              SYMMETRIC_PROPERTY), ),
            tuple(),
            (generic_rule.Rule(
                ((Variable("s"), Variable("p"), Variable("o")), ),
                tuple(),
                ((Variable("o"), Variable("p"), Variable("s")), ),
            ), ),
        )
    ]
Exemplo n.º 9
0
def test_turtle_example_10(store):
    document = """
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .

        <http://example.org/#green-goblin> foaf:name "Green Goblin" .

        <http://example.org/#spiderman> foaf:name "Spiderman" .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (IRI("http://example.org/#green-goblin"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Green Goblin"),
        (IRI("http://example.org/#spiderman"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Spiderman"),
    ]
Exemplo n.º 10
0
def test_turtle_example_15(store):
    document = """
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .

        # Someone knows someone else, who has the name "Bob".
        [] foaf:knows [ foaf:name "Bob" ] .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            BlankNode(0, store.blank_node_factory),
            IRI("http://xmlns.com/foaf/0.1/knows"),
            BlankNode(1, store.blank_node_factory),
        ),
        (BlankNode(1, store.blank_node_factory),
         IRI("http://xmlns.com/foaf/0.1/name"), "Bob"),
    ]
Exemplo n.º 11
0
def test_rule_with_constraint():
    rule_parser = make_parse(
        "rule", {"schema": Namespace("schema", IRI("https://schema.org/"))})
    rule = rule_parser.parse("""
            ($child1 schema:parent $parent), ($child2 schema:parent $parent) st ($child1 is-not $child2)
                → ($child1 schema:sibling $child2) .
        """)

    assert rule == generic_rule.Rule(
        (
            (Variable("child1"), IRI("https://schema.org/parent"),
             Variable("parent")),
            (Variable("child2"), IRI("https://schema.org/parent"),
             Variable("parent")),
        ),
        ((generic_rule.ConstraintIsNot(
            (Variable("child1"), Variable("child2")))), ),
        ((Variable("child1"), IRI("https://schema.org/sibling"),
          Variable("child2")), ),
    )
Exemplo n.º 12
0
def test_parent_sibling_2():
    document = """
        @prefix schema: <https://schema.org/> .

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

    rules = generic_rule.parse(document)

    assert rules == [
        generic_rule.Rule(
            (
                (Variable("child1"), IRI("https://schema.org/parent"),
                 Variable("parent")),
                (Variable("child2"), IRI("https://schema.org/parent"),
                 Variable("parent")),
            ),
            tuple(),
            ((Variable("child1"), IRI("https://schema.org/sibling"),
              Variable("child2")), ),
        )
    ]
Exemplo n.º 13
0
def test_turtleX_example_3_serialise(store):
    document = """
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:name "Bob" .
        <<:bob foaf:age 23>> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .
    """

    parse(document, store.insert, store.blank_node_factory)

    out = io.StringIO()
    serialise(
        store,
        out,
        [
            ("", IRI("http://example.org/")),
            ("foaf", IRI("http://xmlns.com/foaf/0.1/")),
            ("dct", IRI("http://purl.org/dc/elements/1.1/")),
        ],
    )
    doc_out = out.getvalue()
    print(doc_out)
    assert (doc_out == textwrap.dedent("""
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:age 23 ;
            foaf:name "Bob" .

        << :bob foaf:age 23 >> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .

    """).lstrip())
Exemplo n.º 14
0
def test_turtle_example_11(store):
    document = """
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix show: <http://example.org/vocab/show/> .
        @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

        show:218 rdfs:label "That Seventies Show"^^xsd:string .
        show:218 rdfs:label "That Seventies Show"^^<http://www.w3.org/2001/XMLSchema#string> .
        show:218 rdfs:label "That Seventies Show" .
        show:218 show:localName "That Seventies Show"@en .
        show:218 show:localName 'Cette Série des Années Soixante-dix'@fr .
        show:218 show:localName "Cette Série des Années Septante"@fr-be .
        show:218 show:blurb '''This is a multi-line
literal with many quotes (\"\"\"\"\")
and up to two sequential apostrophes ('').''' .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert (
        IRI("http://example.org/vocab/show/218"),
        IRI("http://www.w3.org/2000/01/rdf-schema#label"),
        "That Seventies Show",
    ) in store
Exemplo n.º 15
0
def test_IRI():
    assert str(TITLE) == "http://xmlns.com/foaf/0.1/title"
    assert bytes(TITLE) == b"http://xmlns.com/foaf/0.1/title"

    assert Key(TITLE) < Key("http://xmlns.com/foaf/0.1/title")
    assert TITLE == IRI("http://xmlns.com/foaf/0.1/title")
    assert TITLE != "http://xmlns.com/foaf/0.1/title"
    assert Key(NAME) < Key(TITLE)

    with pytest.raises(TypeError):
        Key(TITLE) < Key({})

    assert Key(NAME) == Key(NAME)
    assert not (Key(NAME) < Key(NAME))
    assert not (Key(NAME) > Key(NAME))
Exemplo n.º 16
0
def test_turtle_example_12(store):
    document = """
        @prefix : <http://example.org/elements#> .

        <http://en.wikipedia.org/wiki/Helium>
            :atomicNumber 2 ;
            :atomicMass 4.002602 ;
            :specificGravity 1.663E-4 .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            IRI("http://en.wikipedia.org/wiki/Helium"),
            IRI("http://example.org/elements#atomicMass"),
            Decimal("4.002602"),
        ),
        (IRI("http://en.wikipedia.org/wiki/Helium"),
         IRI("http://example.org/elements#atomicNumber"), 2),
        (IRI("http://en.wikipedia.org/wiki/Helium"),
         IRI("http://example.org/elements#specificGravity"), 1.663e-4),
    ]
Exemplo n.º 17
0
import pytest

from hexastore.ast import IRI, Variable
from hexastore.model import Key, Solution

A = IRI("http://example.com/A")
B = IRI("http://example.com/B")
C = IRI("http://example.com/C")
D = IRI("http://example.com/D")


@pytest.mark.model
def test_Key():
    keyA = Key(A)
    keyAp = Key(A)
    keyB = Key(B)
    key42 = Key(42)

    assert keyA == keyAp
    assert keyA != keyB
    assert keyA != key42


@pytest.mark.model
def test_Solution():
    s = Solution({Variable("A"): A}, [], set())

    assert s.mutate({}) is not s
    assert s.mutate({}) == s

    sp = s.mutate({Variable("B"): B})
Exemplo n.º 18
0
import pytest

from hexastore.ast import IRI, Order, OrderCondition, Variable
from hexastore.blank_node_factory import BlankNodeFactory
from hexastore.engine import execute
from hexastore.memory import VersionedInMemoryHexastore

DAVE_SMITH = IRI("http://example.com/dave-smith")
ERIC_MILLER = IRI("http://example.com/eric-miller")
ERIC_MILLER_MBOX = IRI("mailto:e.miller123(at)example")
W3 = IRI("http://example.com/w3")

TYPE = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")

KNOWS = IRI("http://xmlns.com/foaf/0.1/knows")
MBOX = IRI("http://xmlns.com/foaf/0.1/mbox")
NAME = IRI("http://xmlns.com/foaf/0.1/name")
ORGANIZATION = IRI("https://schema.org/Organization")
PERSON = IRI("http://xmlns.com/foaf/0.1/Person")
TITLE = IRI("http://xmlns.com/foaf/0.1/title")
WORKS_FOR = IRI("https://schema.org/worksFor")


@pytest.fixture
def store():
    blank_node_factory = BlankNodeFactory()
    hexastore = VersionedInMemoryHexastore(blank_node_factory)
    hexastore.insert(DAVE_SMITH, TYPE, PERSON, 0)
    hexastore.insert(DAVE_SMITH, NAME, "Dave Smith", 1)

    hexastore.insert(ERIC_MILLER, TYPE, PERSON, 2)
Exemplo n.º 19
0
from unittest import mock

import pytest

from hexastore.ast import IRI, BlankNode, TripleStatus, TripleStatusItem, Variable
from hexastore.model import Key, Solution

DAVE_SMITH = IRI("http://example.com/dave-smith")
ERIC_MILLER = IRI("http://example.com/eric-miller")
ERIC_MILLER_MBOX = IRI("mailto:e.miller123(at)example")

TYPE = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")

KNOWS = IRI("http://xmlns.com/foaf/0.1/knows")
MBOX = IRI("http://xmlns.com/foaf/0.1/mbox")
NAME = IRI("http://xmlns.com/foaf/0.1/name")
PERSON = IRI("http://xmlns.com/foaf/0.1/Person")
TITLE = IRI("http://xmlns.com/foaf/0.1/title")


@pytest.mark.ast
def test_IRI():
    assert str(TITLE) == "http://xmlns.com/foaf/0.1/title"
    assert bytes(TITLE) == b"http://xmlns.com/foaf/0.1/title"

    assert Key(TITLE) < Key("http://xmlns.com/foaf/0.1/title")
    assert TITLE == IRI("http://xmlns.com/foaf/0.1/title")
    assert TITLE != "http://xmlns.com/foaf/0.1/title"
    assert Key(NAME) < Key(TITLE)

    with pytest.raises(TypeError):
Exemplo n.º 20
0
def _li(n: int):
    return IRI(f"http://www.w3.org/1999/02/22-rdf-syntax-ns#_{n}")
Exemplo n.º 21
0
def test_turtleX_example_3(store):
    document = """
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:name "Bob" .
        <<:bob foaf:age 23>> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .
    """
    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            (IRI("http://example.org/bob"),
             IRI("http://xmlns.com/foaf/0.1/age"), 23),
            IRI("http://purl.org/dc/elements/1.1/creator"),
            IRI("http://example.com/crawlers#c1"),
        ),
        (
            (IRI("http://example.org/bob"),
             IRI("http://xmlns.com/foaf/0.1/age"), 23),
            IRI("http://purl.org/dc/elements/1.1/source"),
            IRI("http://example.net/homepage-listing.html"),
        ),
        (IRI("http://example.org/bob"), IRI("http://xmlns.com/foaf/0.1/age"),
         23),
        (IRI("http://example.org/bob"), IRI("http://xmlns.com/foaf/0.1/name"),
         "Bob"),
    ]
Exemplo n.º 22
0
import pkgutil

import pytest
from lark import Lark

from hexastore import generic_rule
from hexastore.ast import IRI, Variable
from hexastore.blank_node_factory import BlankNodeFactory
from hexastore.forward_reasoner import ForwardReasoner
from hexastore.memory import InMemoryHexastore
from hexastore.namespace import Namespace

A = IRI("http://example.com/A")
B = IRI("http://example.com/B")
C = IRI("http://example.com/C")
D = IRI("http://example.com/D")

FOO = IRI("http://example.com/Foo")
BAR = IRI("http://example.com/Bar")

SIBLING = IRI("https://schema.org/sibling")
PARENT = IRI("https://schema.org/parent")
SPOUSE = IRI("https://schema.org/spouse")
NAME = IRI("https://schema.org/name")
PERSON = IRI("https://schema.org/Person")
ORGANISATION = IRI("https://schema.org/Organisation")

SYMMETRIC_PROPERTY = IRI("http://www.w3.org/2002/07/owl#SymmetricProperty")
TYPE = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
RESOURCE = IRI("http://www.w3.org/2000/01/rdf-schema#Resource")
SUBCLASS_OF = IRI("http://www.w3.org/2000/01/rdf-schema#subclassOf")
Exemplo n.º 23
0
import pytest

from hexastore.ast import IRI, TripleStatus, TripleStatusItem
from hexastore.blank_node_factory import BlankNodeFactory
from hexastore.bulk_inserter import BulkInserter
from hexastore.memory import TrunkPayload, VersionedInMemoryHexastore

DAVE_SMITH = IRI("http://example.com/dave-smith")
ERIC_MILLER = IRI("http://example.com/eric-miller")
ERIC_MILLER_MBOX = IRI("mailto:e.miller123(at)example")

A = IRI("http://example.com/A")
B = IRI("http://example.com/B")
C = IRI("http://example.com/C")
D = IRI("http://example.com/D")

KNOWS = IRI("http://xmlns.com/foaf/0.1/knows")
MBOX = IRI("http://xmlns.com/foaf/0.1/mbox")
NAME = IRI("http://xmlns.com/foaf/0.1/name")
PERSON = IRI("http://xmlns.com/foaf/0.1/Person")
TITLE = IRI("http://xmlns.com/foaf/0.1/title")

CHILDREN = IRI("https://schema.org/children")
PARENT = IRI("https://schema.org/parent")
SIBLING = IRI("https://schema.org/sibling")
SPOUSE = IRI("https://schema.org/spouse")

BAG = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag")
INFERRED_FROM = IRI("https://example.com/inferred_from")
TYPE = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
import pytest

from hexastore.ast import IRI, Variable
from hexastore.blank_node_factory import BlankNodeFactory
from hexastore.default_forward_reasoner import make_default_forward_reasoner
from hexastore.memory import InMemoryHexastore

A = IRI("http://example.com/A")
B = IRI("http://example.com/B")
C = IRI("http://example.com/C")
D = IRI("http://example.com/D")

CHILDREN = IRI("https://schema.org/children")
PARENT = IRI("https://schema.org/parent")
SIBLING = IRI("https://schema.org/sibling")
SPOUSE = IRI("https://schema.org/spouse")
OWL_THING = IRI("http://www.w3.org/2002/07/owl#Thing")
THING = IRI("https://schema.org/Thing")
PERSON = IRI("https://schema.org/Person")
ORGANISATION = IRI("https://schema.org/Organisation")
RELATED_TO = IRI("http://example.com/relatedTo")

BAG = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag")
INFERRED_FROM = IRI("https://example.com/inferred_from")
TYPE = IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")

SYMMETRIC_PROPERTY = IRI("http://www.w3.org/2002/07/owl#SymmetricProperty")
INVERSE_OF = IRI("http://www.w3.org/2002/07/owl#inverseOf")
DOMAIN = IRI("http://www.w3.org/2000/01/rdf-schema#domain")
RANGE = IRI("http://www.w3.org/2000/01/rdf-schema#range")
SUBCLASS_OF = IRI("http://www.w3.org/2000/01/rdf-schema#subclassOf")
Exemplo n.º 25
0
def test_turtle_example_9(store):
    document = """
        <http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .

        # @base <http://one.example/> .
        # <subject2> <predicate2> <object2> .

        # BASE <http://one.example/>
        # <subject2> <predicate2> <object2> .

        @prefix p: <http://two.example/> .
        p:subject3 p:predicate3 p:object3 .

        PREFIX p: <http://two.example/>
        p:subject3 p:predicate3 p:object3 .

        # @prefix p: <path/> .
        # p:subject4 p:predicate4 p:object4 .

        @prefix : <http://another.example/> .
        :subject5 :predicate5 :object5 .

        :subject6 a :subject7 .

        <http://伝言.example/?user=أكرم&amp;channel=R%26D> a :subject8 .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == sorted(
        [
            (
                IRI("http://one.example/subject1"),
                IRI("http://one.example/predicate1"),
                IRI("http://one.example/object1"),
            ),
            # (
            #     IRI("http://one.example/subject2"),
            #     IRI("http://one.example/predicate2"),
            #     IRI("http://one.example/object2"),
            # ),
            (
                IRI("http://two.example/subject3"),
                IRI("http://two.example/predicate3"),
                IRI("http://two.example/object3"),
            ),
            # (
            #     IRI("http://two.example/subject4"),
            #     IRI("http://two.example/predicate4"),
            #     IRI("http://two.example/object4"),
            # ),
            (
                IRI("http://another.example/subject5"),
                IRI("http://another.example/predicate5"),
                IRI("http://another.example/object5"),
            ),
            (
                IRI("http://another.example/subject6"),
                IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
                IRI("http://another.example/subject7"),
            ),
            (
                IRI("http://伝言.example/?user=أكرم&amp;channel=R%26D"),
                IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
                IRI("http://another.example/subject8"),
            ),
        ],
        key=Key,
    )