pyshacl/rdfutil/clone.py
def mix_datasets(
    base_ds: ConjunctiveLike, extra_ds: GraphLike, target_ds: Optional[Union[ConjunctiveLike, str]] = None
):
    """
    Make a clone of base_ds (dataset) and add in the triples from extra_ds (dataset)
    :param base_ds:
    :type base_ds: rdflib.Dataset
    :param extra_ds:
    :type extra_ds: rdflib.Dataset
    :param target_ds:
    :type target_ds: rdflib.Dataset|str|NoneType
    :return: The cloned Dataset with mixed in triples from extra_ds
    :rtype: rdflib.Dataset
    """
    default_union = base_ds.default_union
    base_named_graphs = list(base_ds.contexts())
    if target_ds is None:
        target_ds = rdflib.Dataset(default_union=default_union)
    elif isinstance(target_ds, rdflib.ConjunctiveGraph):
        raise RuntimeError("Cannot mix new graphs into a ConjunctiveGraph, use Dataset instead.")
    elif target_ds == "inplace":
        pass  # do nothing here
    elif not isinstance(target_ds, rdflib.Dataset):
        raise RuntimeError("Cannot mix datasets if target_ds passed in is not a Dataset itself.")
    if isinstance(extra_ds, (rdflib.Dataset, rdflib.ConjunctiveGraph)):
        mixin_graphs = list(extra_ds.contexts())
    else:
        mixin_graphs = [extra_ds]
    if target_ds == "inplace":
        target_ds = base_ds
        for mg in mixin_graphs:
            mod_named_graphs = {g.identifier: mix_graphs(g, mg, target_graph="inplace") for g in base_named_graphs}
    elif isinstance(target_ds, str):
        raise RuntimeError("target_ds cannot be a string (unless it is 'inplace')")
    else:

        mixed_graphs = {}
        for mg in mixin_graphs:
            mod_named_graphs = {
                g.identifier: mix_graphs(
                    g, mg, target_graph=rdflib.Graph(store=target_ds.store, identifier=g.identifier)
                )
                for g in base_named_graphs
            }
            mixed_graphs.update(mod_named_graphs)
        default_context_id = target_ds.default_context.identifier
        for i, m in mixed_graphs.items():
            if i == default_context_id:
                target_ds.store.remove_graph(target_ds.default_context)
                target_ds.default_context = m
            target_ds.add_graph(m)
    return target_ds

    Make a clone of base_ds (dataset) and add in the triples from extra_ds (dataset)
    :param base_ds:
    :type base_ds: rdflib.Dataset
    :param extra_ds:
    :type extra_ds: rdflib.Dataset
    :param target_ds:
    :type target_ds: rdflib.Dataset|str|NoneType
    :return: The cloned Dataset with mixed in triples from extra_ds
    :rtype: rdflib.Dataset
    
Make a clone of base_ds (dataset) and add in the triples from extra_ds (dataset)
Make a clone of base_ds ( dataset ) and add in the triples from extra_ds ( dataset )
def mix_datasets(
    base_ds: ConjunctiveLike, extra_ds: GraphLike, target_ds: Optional[Union[ConjunctiveLike, str]] = None
):
    default_union = base_ds.default_union
    base_named_graphs = list(base_ds.contexts())
    if target_ds is None:
        target_ds = rdflib.Dataset(default_union=default_union)
    elif isinstance(target_ds, rdflib.ConjunctiveGraph):
        raise RuntimeError("Cannot mix new graphs into a ConjunctiveGraph, use Dataset instead.")
    elif target_ds == "inplace":
        pass  
    elif not isinstance(target_ds, rdflib.Dataset):
        raise RuntimeError("Cannot mix datasets if target_ds passed in is not a Dataset itself.")
    if isinstance(extra_ds, (rdflib.Dataset, rdflib.ConjunctiveGraph)):
        mixin_graphs = list(extra_ds.contexts())
    else:
        mixin_graphs = [extra_ds]
    if target_ds == "inplace":
        target_ds = base_ds
        for mg in mixin_graphs:
            mod_named_graphs = {g.identifier: mix_graphs(g, mg, target_graph="inplace") for g in base_named_graphs}
    elif isinstance(target_ds, str):
        raise RuntimeError("target_ds cannot be a string (unless it is 'inplace')")
    else:
        mixed_graphs = {}
        for mg in mixin_graphs:
            mod_named_graphs = {
                g.identifier: mix_graphs(
                    g, mg, target_graph=rdflib.Graph(store=target_ds.store, identifier=g.identifier)
                )
                for g in base_named_graphs
            }
            mixed_graphs.update(mod_named_graphs)
        default_context_id = target_ds.default_context.identifier
        for i, m in mixed_graphs.items():
            if i == default_context_id:
                target_ds.store.remove_graph(target_ds.default_context)
                target_ds.default_context = m
            target_ds.add_graph(m)
    return target_ds
def mix_datasets ( base_ds : ConjunctiveLike , extra_ds : GraphLike , target_ds : Optional [ Union [ ConjunctiveLike , str ] ] = None ) : default_union = base_ds . default_union base_named_graphs = list ( base_ds . contexts ( ) ) if target_ds is None : target_ds = rdflib . Dataset ( default_union = default_union ) elif isinstance ( target_ds , rdflib . ConjunctiveGraph ) : raise RuntimeError ( "Cannot mix new graphs into a ConjunctiveGraph, use Dataset instead." ) elif target_ds == "inplace" : pass elif not isinstance ( target_ds , rdflib . Dataset ) : raise RuntimeError ( "Cannot mix datasets if target_ds passed in is not a Dataset itself." ) if isinstance ( extra_ds , ( rdflib . Dataset , rdflib . ConjunctiveGraph ) ) : mixin_graphs = list ( extra_ds . contexts ( ) ) else : mixin_graphs = [ extra_ds ] if target_ds == "inplace" : target_ds = base_ds for mg in mixin_graphs : mod_named_graphs = { g . identifier : mix_graphs ( g , mg , target_graph = "inplace" ) for g in base_named_graphs } elif isinstance ( target_ds , str ) : raise RuntimeError ( "target_ds cannot be a string (unless it is 'inplace')" ) else : mixed_graphs = { } for mg in mixin_graphs : mod_named_graphs = { g . identifier : mix_graphs ( g , mg , target_graph = rdflib . Graph ( store = target_ds . store , identifier = g . identifier ) ) for g in base_named_graphs } mixed_graphs . update ( mod_named_graphs ) default_context_id = target_ds . default_context . identifier for i , m in mixed_graphs . items ( ) : if i == default_context_id : target_ds . store . remove_graph ( target_ds . default_context ) target_ds . default_context = m target_ds . add_graph ( m ) return target_ds
Make a clone of base_ds (dataset) and add in the triples from extra_ds (dataset)
Make a clone of base_ds ( dataset ) and add in the triples from extra_ds ( dataset )
"""
    Make a clone of base_ds (dataset) and add in the triples from extra_ds (dataset)
    :param base_ds:
    :type base_ds: rdflib.Dataset
    :param extra_ds:
    :type extra_ds: rdflib.Dataset
    :param target_ds:
    :type target_ds: rdflib.Dataset|str|NoneType
    :return: The cloned Dataset with mixed in triples from extra_ds
    :rtype: rdflib.Dataset
    """ # do nothing here
The cloned Dataset with mixed in triples from extra_ds
The cloned Dataset with mixed in triples from extra_ds
rdflib.Dataset
base_ds
ConjunctiveLike
None
extra_ds
GraphLike
None
target_ds
Optional[Union[ConjunctiveLike, str]]
None