Ejemplo n.º 1
0
def get_nations(regionname):
    """Execute the equivalent of the following XQuery statement and convert the XML into a clan:
        for $x in doc("regions.xml")/regions/region[name="MIDDLE EAST"]/nation
            return <nation>{$x/nationkey}<nationname>{data($x/name)}</nationname></nation>
    """
    timer = FunctionTimer()
    short_prints = True

    # Load the XML document. (Don't use multiplicity or sequence; our data doesn't require this.)
    regions = xml.import_xml("regions.xml", convert_numerics=True)
    timer.lap("regions", short=short_prints)

    # Get a clan where each region is a row.
    regions_clan = regions("regions")["region"]
    timer.lap("regions_clan", short=short_prints)

    # Filter this clan down to the region of interest (name is `regionname`).
    target_region = clans.superstrict(regions_clan, clans.from_dict({"name": regionname}))
    timer.lap("target_region", short=short_prints)

    # Get all 'nation' lefts out of this clan and create a clan where every row is a nation's data.
    nations_clan = target_region["nation"]
    timer.lap("nations_clan", short=short_prints)

    # Rename 'name' to 'nationname' and project 'nationkey' and 'nationname' (removing 'comment').
    nations = clans.compose(nations_clan, clans.from_dict({"nationkey": "nationkey", "nationname": "name"}))
    timer.end("nations", short=short_prints)

    return nations
Ejemplo n.º 2
0
def get_nations(regionname):
    """Execute the equivalent of the following XQuery statement and convert the XML into a clan:
        for $x in doc("regions.xml")/regions/region[name="MIDDLE EAST"]/nation
            return <nation>{$x/nationkey}<nationname>{data($x/name)}</nationname></nation>
    """
    timer = FunctionTimer()
    short_prints = True

    # Load the XML document. (Don't use multiplicity or sequence; our data doesn't require this.)
    regions = xml.import_xml('regions.xml', convert_numerics=True)
    timer.lap('regions', short=short_prints)

    # Get a clan where each region is a row.
    regions_clan = regions('regions')['region']
    timer.lap('regions_clan', short=short_prints)

    # Filter this clan down to the region of interest (name is `regionname`).
    target_region = clans.superstrict(regions_clan,
                                      clans.from_dict({'name': regionname}))
    timer.lap('target_region', short=short_prints)

    # Get all 'nation' lefts out of this clan and create a clan where every row is a nation's data.
    nations_clan = target_region['nation']
    timer.lap('nations_clan', short=short_prints)

    # Rename 'name' to 'nationname' and project 'nationkey' and 'nationname' (removing 'comment').
    nations = clans.compose(
        nations_clan,
        clans.from_dict({
            'nationkey': 'nationkey',
            'nationname': 'name'
        }))
    timer.end('nations', short=short_prints)

    return nations
Ejemplo n.º 3
0
def get_supplier_solutions():
    """Execute the equivalent of the following SPARQL query, querying the Turtle file supplier.ttl:
        SELECT
            ?suppkey, ?nationkey
        FROM
            supplier
        WHERE {
            ?supplier <tpch:suppkey> ?suppkey .
            ?supplier <tpch:nationkey> ?nationkey .
        }
    """
    timer = FunctionTimer()
    short_prints = True

    suppliers = rdf.import_graph("supplier.ttl")
    timer.lap("suppliers", short=short_prints)

    # Find all triples that define a 'suppkey' (as predicate).
    bgp_suppkey_matches = clans.superstrict(suppliers, clans.from_dict({"p": rdflib.URIRef("tpch:suppkey")}))
    # Give the subject a name for later joining and object the name we need in the output.
    bgp_suppkey = clans.compose(bgp_suppkey_matches, clans.from_dict({"supplier": "s", "suppkey": "o"}))

    # Find all triples that define a 'nationkey' (as predicate) and give the subject a name for
    # later joining and object the name we need in the output.
    bgp_nationkey = clans.compose(
        clans.superstrict(suppliers, clans.from_dict({"p": rdflib.URIRef("tpch:nationkey")})),
        clans.from_dict({"supplier": "s", "nationkey": "o"}),
    )

    # Join the previous results on 'supplier' and project the columns we need.
    supplier_solutions = clans.project(clans.functional_cross_union(bgp_suppkey, bgp_nationkey), "nationkey", "suppkey")
    timer.end("supplier_solutions", short=short_prints)

    return supplier_solutions
Ejemplo n.º 4
0
def get_supplier_solutions():
    """Execute the equivalent of the following SPARQL query, querying the Turtle file supplier.ttl:
        SELECT
            ?suppkey, ?nationkey
        FROM
            supplier
        WHERE {
            ?supplier <tpch:suppkey> ?suppkey .
            ?supplier <tpch:nationkey> ?nationkey .
        }
    """
    timer = FunctionTimer()
    short_prints = True

    suppliers = rdf.import_graph('supplier.ttl')
    timer.lap('suppliers', short=short_prints)

    # Find all triples that define a 'suppkey' (as predicate).
    bgp_suppkey_matches = clans.superstrict(
        suppliers, clans.from_dict({'p': rdflib.URIRef('tpch:suppkey')}))
    # Give the subject a name for later joining and object the name we need in the output.
    bgp_suppkey = clans.compose(
        bgp_suppkey_matches, clans.from_dict({
            'supplier': 's',
            'suppkey': 'o'
        }))

    # Find all triples that define a 'nationkey' (as predicate) and give the subject a name for
    # later joining and object the name we need in the output.
    bgp_nationkey = clans.compose(
        clans.superstrict(
            suppliers, clans.from_dict({'p':
                                        rdflib.URIRef('tpch:nationkey')})),
        clans.from_dict({
            'supplier': 's',
            'nationkey': 'o'
        }))

    # Join the previous results on 'supplier' and project the columns we need.
    supplier_solutions = clans.project(
        clans.cross_functional_union(bgp_suppkey, bgp_nationkey), 'nationkey',
        'suppkey')
    timer.end('supplier_solutions', short=short_prints)

    return supplier_solutions
Ejemplo n.º 5
0
def pattern_match(graph: 'PP( AxA )', pattern: dict):
    r"""Return all relations in ``graph`` that contain all members of ``pattern``.

    :param graph: An absolute :term:`clan`.
    :param pattern: A dictionary where the keys are the :term:`left`\s and the values the
        :term:`right`\s that will be matched.
    """
    assert _clans.is_member(graph)
    match_predicate = _clans.from_dict(pattern)
    return _clans.superstrict(graph, match_predicate, _checked=False)
Ejemplo n.º 6
0
def get_missing_rowcols(block_clan):
    band, stack = by_clan_keys('band', 'stack', block_clan)

    # Get block defined by band, stack
    full_block_clan = clans.superstrict(BANDS_STACKS,
                                        clans.from_dict({'band': band, 'stack': stack}))
    # Get missing rows/cols from the block
    target_rowcols = sets.minus(project(full_block_clan, 'row', 'col'),
                                project(block_clan, 'row', 'col'))
    return target_rowcols
Ejemplo n.º 7
0
def pattern_match(graph: 'PP( AxA )', pattern: dict):
    """Return all relations in ``graph`` that contain all members of ``pattern``.

    :param graph: An absolute clan.
    :param pattern: A dictionary where the keys are the lefts and the values the rights that
        will be matched.
    """
    assert(_clans.is_member(graph))
    match_predicate = _clans.from_dict(pattern)
    return _clans.superstrict(graph, match_predicate, _checked=False)
Ejemplo n.º 8
0
def get_missing_rowcols(block_clan):
    band, stack = by_clan_keys('band', 'stack', block_clan)

    # Get block defined by band, stack
    full_block_clan = clans.superstrict(BANDS_STACKS,
                                        clans.from_dict({'band': band, 'stack': stack}))
    # Get missing rows/cols from the block
    target_rowcols = sets.minus(project(full_block_clan, 'row', 'col'),
                                project(block_clan, 'row', 'col'))
    return target_rowcols
Ejemplo n.º 9
0
def check_cols(_board, try_harder=0):
    """Check the columns the same way rows are checked"""
    if VERBOSE:
        print("* check_cols")

    # Rotate the board by swapping row and col then call check_rows
    swaps = clans.from_dict({'row': 'col', 'band': 'stack'})
    rotated = extension.binary_extend(
        _board, swaps, partial(relations.swap, _checked=False)).cache_clan(
            CacheStatus.IS).cache_functional(CacheStatus.IS)

    new_board = check_rows(rotated, try_harder)
    if rotated is not new_board:
        _board = extension.binary_extend(
            new_board, swaps, partial(relations.swap, _checked=False)
        ).cache_clan(CacheStatus.IS).cache_functional(CacheStatus.IS)
    return _board
Ejemplo n.º 10
0
def check_cols(_board, try_harder=0):
    """Check the columns the same way rows are checked"""
    if VERBOSE:
        print("* check_cols")

    # Rotate the board by swapping row and col then call check_rows
    swaps = clans.from_dict({'row': 'col', 'band': 'stack'})
    rotated = extension.binary_extend(
        _board, swaps, partial(relations.swap, _checked=False)).cache_clan(
            CacheStatus.IS).cache_functional(CacheStatus.IS)

    new_board = check_rows(rotated, try_harder)
    if rotated is not new_board:
        _board = extension.binary_extend(
            new_board, swaps, partial(relations.swap, _checked=False)
        ).cache_clan(CacheStatus.IS).cache_functional(CacheStatus.IS)
    return _board
Ejemplo n.º 11
0
def match_and_project(graph: 'PP( AxA )', pattern: dict=None, projection: dict=None):
    """Return all relations in ``graph`` that contain all members of ``pattern``. Rename their lefts
    according to the members of ``projection``.

    :param graph: An absolute clan.
    :param pattern: A dictionary where the keys are the lefts and the values the rights that
        will be matched.
    :param projection: A dictionary where the values are the new names and the keys the existing
        names of the lefts to be renamed.
    """
    assert(_clans.is_member(graph))
    if pattern is None:
        pattern = {}
    if projection is None:
        projection = {}

    matches = pattern_match(graph, pattern)
    compose_ctrl_set = _clans.transpose(_clans.from_dict(projection))
    return _clans.compose(matches, compose_ctrl_set, _checked=False)
Ejemplo n.º 12
0
def check_cols(_board):
    """Check the columns the same way rows are checked"""
    if verbose:
        print("* check_cols")

    # Rotate the board by swapping row and col then call check_rows
    swaps = clans.from_dict({'row': 'col', 'band': 'stack'})
    rotated = extension.binary_extend(_board, swaps, partial(relations.swap, _checked=False)
                                     ).cache_is_clan(True).cache_is_left_functional(True)
    for rel in rotated:
        rel.cache_is_left_functional(True)

    new_board = check_rows(rotated)
    if rotated != new_board:
        _board = extension.binary_extend(new_board, swaps, partial(relations.swap, _checked=False)
                                         ).cache_is_clan(True).cache_is_left_functional(True)
        for rel in _board:
            rel.cache_is_left_functional(True)

    return _board
Ejemplo n.º 13
0
def match_and_project(graph: 'PP(A x A)',
                      pattern: dict = None,
                      projection: dict = None):
    r"""Return all relations in ``graph`` that contain all members of ``pattern``. Rename their
    lefts according to the members of ``projection``.

    :param graph: An absolute :term:`clan`.
    :param pattern: A dictionary where the keys are the :term:`left`\s and the values the
        :term:`right`\s that will be matched.
    :param projection: A dictionary where the values are the new names and the keys the existing
        names of the :term:`left`\s to be renamed.
    """
    assert _clans.is_member(graph)
    if pattern is None:
        pattern = {}
    if projection is None:
        projection = {}

    matches = pattern_match(graph, pattern)
    compose_ctrl_set = _clans.transpose(_clans.from_dict(projection))
    return _clans.compose(matches, compose_ctrl_set, _checked=False)
Ejemplo n.º 14
0
# /regions/region/regionkey
region_keys = regions['regionkey']
print('region_keys:\n' + mo_to_str(region_keys))

# Get all nation names
# /regions/region/nation/name
nations = regions['nation']
print('regions[\'nation\']:\n' + mo_to_str(nations))

nation_names = nations['name']
print('nation_names:\n' + mo_to_str(nation_names))

# Get all nation names of a given region
# for $x in doc("regions.xml")/regions/region where $x/name='AMERICA' return $x/nation/name
from algebraixlib.algebras.clans import superstrict, from_dict
america_region_name = from_dict({'name': 'AMERICA'})
print('america_region_name:\n' + mo_to_str(america_region_name))

america_region = superstrict(regions, america_region_name)
print('america_region:\n' + mo_to_str(america_region))

america_nations_names = america_region['nation']['name']
print('america_nations_names:\n' + mo_to_str(america_nations_names))

# Get all region key and nation name pairs
# for $x in doc("regions.xml")/regions/region/nation/name return <pair>{$x/../../regionkey}{$x}</pair>
from algebraixlib.mathobjects import Set
from algebraixlib.algebras.clans import project, cross_union
from algebraixlib.algebras.sets import union
region_key_nation_name_pairs_accumulator = Set()
for region in regions:
Ejemplo n.º 15
0
# /regions/region/regionkey
region_keys = regions['regionkey']
print('region_keys:\n' + mo_to_str(region_keys))

# Get all nation names
# /regions/region/nation/name
nations = regions['nation']
print('regions[\'nation\']:\n' + mo_to_str(nations))

nation_names = nations['name']
print('nation_names:\n' + mo_to_str(nation_names))

# Get all nation names of a given region
# for $x in doc("regions.xml")/regions/region where $x/name='AMERICA' return $x/nation/name
from algebraixlib.algebras.clans import superstrict, from_dict
america_region_name = from_dict({'name': 'AMERICA'})
print('america_region_name:\n' + mo_to_str(america_region_name))

america_region = superstrict(regions, america_region_name)
print('america_region:\n' + mo_to_str(america_region))

america_nations_names = america_region['nation']['name']
print('america_nations_names:\n' + mo_to_str(america_nations_names))

# Get all region key and nation name pairs
# for $x in doc("regions.xml")/regions/region/nation/name return <pair>{$x/../../regionkey}{$x}</pair>
from algebraixlib.mathobjects import Set
from algebraixlib.algebras.clans import project, cross_union
from algebraixlib.algebras.sets import union
region_key_nation_name_pairs_accumulator = Set()
for region in regions: