Example #1
0
    def __init__(self, resUri = None, **kwargs):
        """The constructor tries hard to do return you an rdfSubject

        :param resUri: the "resource uri". If `None` then create an instance with a BNode resUri.
        Can be given as one of:

           * an instance of an rdfSubject
           * an instance of a BNode or a URIRef
           * an n3 uriref string like: "<urn:isbn:1234567890>"
           * an n3 bnode string like: "_:xyz1234" 
        :param kwargs: is a set of values that will be set using the keys to find the appropriate descriptor"""

        if not resUri:  # create a bnode
            self.resUri = BNode()
            if self.rdf_type:
                self.db.add((self.resUri,RDF.type,self.rdf_type))
        elif isinstance(resUri, (BNode, URIRef)): # user the identifier passed in
            self.resUri=resUri
            if self.rdf_type and not list(self.db.triples((self.resUri,RDF.type,self.rdf_type))):
                self.db.add((self.resUri,RDF.type,self.rdf_type))
        elif isinstance(resUri, rdfSubject):      # use the resUri of the subject passed in 
            self.resUri=resUri.resUri 
            self.db=resUri.db
        elif isinstance(resUri, (str, unicode)):  # create one from a <uri> or _:bnode string
            if resUri[0]=="<" and resUri[-1]==">":
                self.resUri=URIRef(resUri[1:-1])
            elif resUri.startswith("_:"):
                self.resUri=BNode(resUri[2:])
        else:
            raise AttributeError("cannot construct rdfSubject from %s"%(str(resUri)))
        
        if kwargs:
            self._set_with_dict(kwargs)
Example #2
0
def to_rdf_triples(s, p, o, datatype=None, language=None):
    if s is not None and not isinstance(s, Identifier):
        if s.startswith("_:"):
            s = BNode(s)
        else:
            s = URIRef(s)

    if p is not None and not isinstance(p, Identifier):
        p = URIRef(p)

    if isinstance(o, basestring):
        if o.startswith("_:"):
            o = BNode(o)
        elif "://" in o or o.startswith("urn:"):
            o = URIRef(o)
        else:
            o = Literal(o, lang=language, datatype=datatype)

    if o is not None and not isinstance(o, Identifier):
        o = Literal(o, lang=language, datatype=datatype)

    return s, p, o
Example #3
0
    def test_post_multiple_bnode_obsels_w_relations(self):
        base = self.my_ktbs.create_base()
        model = base.create_model()
        otype0 = model.create_obsel_type("#MyObsel0")
        rtype0 = model.create_relation_type("#MyRel0")
        trace = base.create_stored_trace(None, model, "1970-01-01T00:00:00Z",
                                         "alice")
        graph = Graph()
        obs1 = BNode()
        obs2 = BNode()
        obs3 = BNode()
        graph.add((obs1, KTBS.hasTrace, trace.uri))
        graph.add((obs1, RDF.type, otype0.uri))
        graph.add((obs1, KTBS.hasBegin, Literal(1)))
        graph.add((obs1, rtype0.uri, obs2))
        graph.add((obs2, KTBS.hasTrace, trace.uri))
        graph.add((obs2, RDF.type, otype0.uri))
        graph.add((obs2, KTBS.hasBegin, Literal(2)))
        graph.add((obs2, rtype0.uri, obs3))
        graph.add((obs3, KTBS.hasTrace, trace.uri))
        graph.add((obs3, RDF.type, otype0.uri))
        graph.add((obs3, KTBS.hasBegin, Literal(3)))
        graph.add((obs3, rtype0.uri, obs1))

        created = trace.post_graph(graph)

        assert len(created) == 3

        obs1 = trace.get_obsel(created[0])
        obs2 = trace.get_obsel(created[1])
        obs3 = trace.get_obsel(created[2])

        assert obs1.begin == 1
        assert obs2.begin == 2
        assert obs3.begin == 3
        assert obs1.list_related_obsels(rtype0) == [obs2]
        assert obs2.list_related_obsels(rtype0) == [obs3]
        assert obs3.list_related_obsels(rtype0) == [obs1]
        assert obs1.list_relating_obsels(rtype0) == [obs3]
        assert obs2.list_relating_obsels(rtype0) == [obs1]
        assert obs3.list_relating_obsels(rtype0) == [obs2]
Example #4
0
        def jld_callback(s, p, o):
            """
            Insert extracted (s, p, o) to an rdflib graph.

            :param s: subject
            :param p: predicate
            :param o: object
            """
            #print "Entrant (s,p,o) : (", s, ",", p, ",", o, ")"

            # Subject analysis
            if s[:2] == "_:":
                s = BNode(s[2:])
            else:
                s = URIRef(s, base_uri)

            # Object analysis
            # The dictionary can be much more complex
            if isinstance(o, dict):
                if "@id" in o:
                    o = o["@id"]
                    if o[:2] == "_:":
                        o = BNode(o[2:])
                    else:
                        o = URIRef(o, base_uri)
                else:
                    assert "@value" in o
                    o = Literal(
                            o["@value"],
                            lang = o.get("@language"),
                            datatype = o.get("@type"),
                            )
            else:
                o = Literal(o)

            # Predicate analysis
            if "@type" in p:
                p = RDF.type
                # Ensure that object is really an URIRef, JSON-LD 3.3
                o = URIRef(o, base_uri)
            elif p.startswith("x-rev:"):
                p = URIRef(p[6:], base_uri)
                s, o = o, s
            else:
                p = URIRef(p, base_uri)

            #print "Sortant (s,p,o) : (", s, ",", p, ",", o, ")"
            graph.add((s, p, o))
            return (s, p, o)
Example #5
0
def main():

    # Parse Swift book to retrieve concepts and related resources
    start = "https://docs.swift.org/swift-book/"
    nextURL = start
    urls = [nextURL]

    concepts = {}

    while nextURL:
        url = nextURL
        page = urlopen(url)
        soup = BeautifulSoup(page, 'html.parser')

        #title = soup.find('title').string

        article = soup.select_one('article.page')
        headings = article.find_all(re.compile('^h[1-6]$'))

        for heading in headings:
            heading_text = str(heading.contents[0]).lower()
            permalink = url + heading.contents[1].get('href')

            doc = nlp(heading_text)

            noun_phrases = [chunk for chunk in doc.noun_chunks]

            if len(noun_phrases) > 0:
                new_concepts = [lemmatize(lstrip_stopwords(chunk)).strip() for chunk in noun_phrases]
            else:
                # if no noun-phrases, take as verbatim (e.g. break, continue)
                new_concepts = [heading_text]

            for c in new_concepts:
                if c not in concepts:
                    concepts[c] = []
                if permalink not in concepts[c]:
                    # optionally: don't add if permalink (apart from fragment) is already contained (to avoid reindexing the same page multiple times, as a concept like "Function" might appear many times on its dedicated page in different headers)
                    if not page_included(permalink, concepts[c]):
                        concepts[c].append(permalink)

        # continue to next page (if any)
        nextLink = soup.select_one("p.next a")

        if nextLink:
            parts = urlsplit(nextURL)
            base_path, _ = split(parts.path)
            base_url = urlunsplit((parts.scheme, parts.netloc, join(base_path, ""), parts.query, parts.fragment))
            nextURL = urljoin(base_url, nextLink.get('href'))
            urls.append(nextURL)
        else:
            nextURL = None

    # RDF Graph creation
    g = Graph()

    # Namespace bindings
    NS = Namespace(ALMA_NS + SCHEME_NAME + "#")
    DBPEDIA = Namespace('http://dbpedia.org/page/')
    g.namespace_manager.bind('owl', OWL)
    g.namespace_manager.bind('skos', SKOS)
    g.namespace_manager.bind('dct', DCTERMS)
    g.namespace_manager.bind('foaf', FOAF)
    g.namespace_manager.bind('dbr', DBPEDIA)
    g.namespace_manager.bind(SCHEME_NAME, NS)

    # Ontology Metadata
    ontology = URIRef(ALMA_NS + SCHEME_NAME)
    g.add((ontology, RDF.type, OWL.term("Ontology")))
    g.add((ontology, DCTERMS.term("title"), Literal("{} Ontology".format(SCHEME_NAME.title()))))
    g.add((ontology, DCTERMS.term("description"), Literal("This is an SKOS-based lightweight ontology about the Swift programming language.")))
    g.add((ontology, DCTERMS.term("subject"), URIRef(quote("http://dbpedia.org/page/Swift_(programming_language)"))))
    g.add((ontology, DCTERMS.term("license"), URIRef("https://creativecommons.org/licenses/by-sa/4.0/")))
    g.add((ontology, DCTERMS.term("created"), Literal(DATE_CREATED)))
    g.add((ontology, DCTERMS.term("modified"), Literal(DATE_MODIFIED)))
    g.add((ontology, RDFS.term("seeAlso"), URIRef("https://coast.uni.lu/alma/")))
    g.add((ontology, OWL.term("versionIRI"), URIRef("http://purl.org/lu/uni/alma/{}/{}".format(SCHEME_NAME, LANGUAGE_VERSION))))
    g.add((ontology, OWL.term("versionInfo"), Literal("{}/{}".format(LANGUAGE_VERSION, ONTOLOGY_VERSION))))
    g.add((ontology, OWL.term("imports"), URIRef("http://www.w3.org/2004/02/skos/core")))
    creator = BNode()
    g.add((ontology, DCTERMS.term("creator"), creator))
    g.add((creator, RDF.type, FOAF.term("Person")))
    g.add((creator, FOAF.term("name"), Literal(AUTHOR_NAME)))
    g.add((creator, FOAF.term("mbox"), URIRef(AUTHOR_EMAIL)))

    # Concept Scheme
    schemeURI = NS.term("Scheme")
    g.add((schemeURI, RDF.type, SKOS.term("ConceptScheme")))
    g.add((schemeURI, DCTERMS.term("title"), Literal(SCHEME_NAME.title())))

    # Concepts
    for (concept, urls) in concepts.items():
        conceptURI = NS.term(cleanse(concept))
        prefLabel = concept.title()
        g.add((conceptURI, RDF.type, SKOS.term("Concept")))
        g.add((conceptURI, RDF.type, OWL.term("NamedIndividual")))
        g.add((conceptURI, SKOS.term("inScheme"), schemeURI))
        g.add((conceptURI, SKOS.term("prefLabel"), Literal(prefLabel, lang='en')))

        # Resources from Swift book
        for url in urls:
            g.add((conceptURI, SKOS.term("definition"), URIRef(url)))

    # Serialization
    for (format, file_extension) in SERIALIZATION_FORMATS.items():
        file_name = "{}_{}_{}.{}".format(SCHEME_NAME, LANGUAGE_VERSION, ONTOLOGY_VERSION, file_extension)
        g.serialize(format=format, destination=file_name)
        print("Saved under {}".format(file_name))

    print("# triples:", len(g))
Example #6
0
def invokeRule(priorAnswers,
               bodyLiteralIterator,
               sip,
               otherargs,
               priorBooleanGoalSuccess=False,
               step=None,
               debug=False,
               buildProof=False):
    """
    Continue invokation of rule using (given) prior answers and list of
    remaining body literals (& rule sip).  If prior answers is a list,
    computation is split disjunctively

    [..] By combining the answers to all these subqueries, we generate
    answers for the original query involving the rule head

    Can also takes a PML step and updates it as it navigates the
    top-down proof tree (passing it on and updating it where necessary)

    """
    assert not buildProof or step is not None

    proofLevel, memoizeMemory, sipCollection, \
        factGraph, derivedPreds, processedRules = otherargs

    remainingBodyList = [i for i in bodyLiteralIterator]
    lazyGenerator = lazyGeneratorPeek(priorAnswers, 2)
    if lazyGenerator.successful:
        # There are multiple answers in this step, we need to call invokeRule
        # recursively for each answer, returning the first positive attempt
        success = False
        rt = None
        _step = None
        ansNo = 0
        for priorAns in lazyGenerator:
            ansNo += 1
            try:
                if buildProof:
                    newStep = InferenceStep(step.parent,
                                            step.rule,
                                            source=step.source)
                    newStep.antecedents = [ant for ant in step.antecedents]
                else:
                    newStep = None
                for rt, _step in\
                    invokeRule([priorAns],
                               iter([i for i in remainingBodyList]),
                               sip,
                               otherargs,
                               priorBooleanGoalSuccess,
                               newStep,
                               debug=debug,
                               buildProof=buildProof):
                    if rt:
                        yield rt, _step
            except RuleFailure:
                pass
        if not success:
            # None of prior answers were successful
            # indicate termination of rule processing
            raise RuleFailure(
                "Unable to solve either of %s against remainder of rule: %s" %
                (ansNo, remainingBodyList))
            # yield False, _InferenceStep(step.parent, step.rule,
            # source=step.source)
    else:
        lazyGenerator = lazyGeneratorPeek(lazyGenerator)
        projectedBindings = lazyGenerator.successful and first(
            lazyGenerator) or {}

        # First we check if we can combine a large group of subsequent body literals
        # into a single query
        # if we have a template map then we use it to further
        # distinguish which builtins can be solved via
        # cumulative SPARQl query - else we solve
        # builtins one at a time
        def sparqlResolvable(literal):
            if isinstance(literal, Uniterm):
                return not literal.naf and GetOp(literal) not in derivedPreds
            else:
                return isinstance(literal, N3Builtin) and \
                    literal.uri in factGraph.templateMap

        def sparqlResolvableNoTemplates(literal):

            if isinstance(literal, Uniterm):
                return not literal.naf and GetOp(literal) not in derivedPreds
            else:
                return False

        conjGroundLiterals = list(
            itertools.takewhile(
                hasattr(factGraph, 'templateMap') and sparqlResolvable
                or sparqlResolvableNoTemplates, remainingBodyList))

        bodyLiteralIterator = iter(remainingBodyList)

        if len(conjGroundLiterals) > 1:
            # If there are literals to combine *and* a mapping from rule
            # builtins to SPARQL FILTER templates ..
            basePredicateVars = set(
                reduce(lambda x, y: x + y, [
                    list(GetVariables(arg, secondOrder=True))
                    for arg in conjGroundLiterals
                ]))
            if projectedBindings:
                openVars = basePredicateVars.intersection(projectedBindings)
            else:
                # We don't have any given bindings, so we need to treat
                # the body as an open query
                openVars = basePredicateVars

            queryConj = EDBQuery(
                [copy.deepcopy(lit) for lit in conjGroundLiterals], factGraph,
                openVars, projectedBindings)

            query, answers = queryConj.evaluate(debug)

            if isinstance(answers, bool):
                combinedAnswers = {}
                rtCheck = answers
            else:
                if projectedBindings:
                    combinedAnswers = (mergeMappings1To2(ans,
                                                         projectedBindings,
                                                         makeImmutable=True)
                                       for ans in answers)
                else:
                    combinedAnswers = (MakeImmutableDict(ans)
                                       for ans in answers)
                combinedAnsLazyGenerator = lazyGeneratorPeek(combinedAnswers)
                rtCheck = combinedAnsLazyGenerator.successful

            if not rtCheck:
                raise RuleFailure("No answers for combined SPARQL query: %s" %
                                  query)
            else:
                # We have solved the previous N body literals with a single
                # conjunctive query, now we need to make each of the literals
                # an antecedent to a 'query' step.
                if buildProof:
                    queryStep = InferenceStep(None, source='some RDF graph')
                    # FIXME: subquery undefined
                    queryStep.groundQuery = subquery
                    queryStep.bindings = {}  # combinedAnswers[-1]
                    # FIXME: subquery undefined
                    queryHash = URIRef(
                        "tag:[email protected]:Queries#" +
                        makeMD5Digest(subquery))
                    queryStep.identifier = queryHash
                    for subGoal in conjGroundLiterals:
                        subNs = NodeSet(subGoal.toRDFTuple(),
                                        identifier=BNode())
                        subNs.steps.append(queryStep)
                        step.antecedents.append(subNs)
                        queryStep.parent = subNs
                for rt, _step in invokeRule(
                        isinstance(answers, bool) and [projectedBindings]
                        or combinedAnsLazyGenerator,
                        iter(remainingBodyList[len(conjGroundLiterals):]),
                        sip,
                        otherargs,
                        isinstance(answers, bool),
                        step,
                        debug=debug,
                        buildProof=buildProof):
                    yield rt, _step

        else:
            # Continue processing rule body condition
            # one literal at a time
            try:
                bodyLiteral = next(
                    bodyLiteralIterator
                ) if py3compat.PY3 else bodyLiteralIterator.next()
                # if a N3 builtin, execute it using given bindings for boolean answer
                # builtins are moved to end of rule when evaluating rules via
                # sip
                if isinstance(bodyLiteral, N3Builtin):
                    lhs = bodyLiteral.argument
                    rhs = bodyLiteral.result
                    lhs = isinstance(
                        lhs, Variable) and projectedBindings[lhs] or lhs
                    rhs = isinstance(
                        rhs, Variable) and projectedBindings[rhs] or rhs
                    assert lhs is not None and rhs is not None
                    if bodyLiteral.func(lhs, rhs):
                        if debug:
                            print("Invoked %s(%s, %s) -> True" %
                                  (bodyLiteral.uri, lhs, rhs))
                        # positive answer means we can continue processing the
                        # rule body
                        if buildProof:
                            ns = NodeSet(bodyLiteral.toRDFTuple(),
                                         identifier=BNode())
                            step.antecedents.append(ns)
                        for rt, _step in invokeRule([projectedBindings],
                                                    bodyLiteralIterator,
                                                    sip,
                                                    otherargs,
                                                    step,
                                                    priorBooleanGoalSuccess,
                                                    debug=debug,
                                                    buildProof=buildProof):
                            yield rt, _step
                    else:
                        if debug:
                            print("Successfully invoked %s(%s, %s) -> False" %
                                  (bodyLiteral.uri, lhs, rhs))
                        raise RuleFailure(
                            "Failed builtin invokation %s(%s, %s)" %
                            (bodyLiteral.uri, lhs, rhs))
                else:
                    # For every body literal, subqueries are generated according
                    # to the sip
                    sipArcPred = URIRef(
                        GetOp(bodyLiteral) + '_' +
                        '_'.join(GetArgs(bodyLiteral)))
                    assert len(list(IncomingSIPArcs(sip, sipArcPred))) < 2
                    subquery = copy.deepcopy(bodyLiteral)
                    subquery.ground(projectedBindings)

                    for N, x in IncomingSIPArcs(sip, sipArcPred):
                        # That is, each subquery contains values for the bound arguments
                        # that are passed through the sip arcs entering the node
                        # corresponding to that literal

                        # Create query out of body literal and apply
                        # sip-provided bindings
                        subquery = copy.deepcopy(bodyLiteral)
                        subquery.ground(projectedBindings)
                    if literalIsGround(subquery):
                        # subquery is ground, so there will only be boolean answers
                        # we return the conjunction of the answers for the current
                        # subquery

                        answer = False
                        ns = None

                        answers = first(
                            itertools.dropwhile(
                                lambda item: not item[0],
                                SipStrategy(
                                    subquery.toRDFTuple(),
                                    sipCollection,
                                    factGraph,
                                    derivedPreds,
                                    MakeImmutableDict(projectedBindings),
                                    processedRules,
                                    network=step is not None
                                    and step.parent.network or None,
                                    debug=debug,
                                    buildProof=buildProof,
                                    memoizeMemory=memoizeMemory,
                                    proofLevel=proofLevel)))
                        if answers:
                            answer, ns = answers
                        if not answer and not bodyLiteral.naf or \
                                (answer and bodyLiteral.naf):
                            # negative answer means the invokation of the rule fails
                            # either because we have a positive literal and there
                            # is no answer for the subgoal or the literal is
                            # negative and there is an answer for the subgoal
                            raise RuleFailure(
                                "No solutions solving ground query %s" %
                                subquery)
                        else:
                            if buildProof:
                                if not answer and bodyLiteral.naf:
                                    ns.naf = True
                                step.antecedents.append(ns)
                            # positive answer means we can continue processing the rule body
                            # either because we have a positive literal and answers
                            # for subgoal or a negative literal and no answers for the
                            # the goal
                            for rt, _step in invokeRule([projectedBindings],
                                                        bodyLiteralIterator,
                                                        sip,
                                                        otherargs,
                                                        True,
                                                        step,
                                                        debug=debug):
                                yield rt, _step
                    else:
                        _answers = \
                            SipStrategy(
                                subquery.toRDFTuple(),
                                sipCollection,
                                factGraph,
                                derivedPreds,
                                MakeImmutableDict(
                                    projectedBindings),
                                processedRules,
                                network=step is not None and
                                step.parent.network or None,
                                debug=debug,
                                buildProof=buildProof,
                                memoizeMemory=memoizeMemory,
                                proofLevel=proofLevel)

                        # solve (non-ground) subgoal
                        def collectAnswers(_ans):
                            for ans, ns in _ans:
                                if isinstance(ans, dict):
                                    try:
                                        map = mergeMappings1To2(
                                            ans,
                                            projectedBindings,
                                            makeImmutable=True)
                                        yield map
                                    except:
                                        pass

                        combinedAnswers = collectAnswers(_answers)
                        answers = lazyGeneratorPeek(combinedAnswers)
                        if not answers.successful \
                                and not bodyLiteral.naf \
                                or (bodyLiteral.naf and answers.successful):
                            raise RuleFailure(
                                "No solutions solving ground query %s" %
                                subquery)
                        else:
                            # Either we have a positive subgoal and answers
                            # or a negative subgoal and no answers
                            if buildProof:
                                if answers.successful:
                                    goals = set([g for a, g in answers])
                                    assert len(goals) == 1
                                    step.antecedents.append(goals.pop())
                                else:
                                    newNs = NodeSet(
                                        bodyLiteral.toRDFTuple(),
                                        network=step.parent.network,
                                        identifier=BNode(),
                                        naf=True)
                                    step.antecedents.append(newNs)
                            for rt, _step in invokeRule(
                                    answers,
                                    bodyLiteralIterator,
                                    sip,
                                    otherargs,
                                    priorBooleanGoalSuccess,
                                    step,
                                    debug=debug,
                                    buildProof=buildProof):
                                yield rt, _step
            except StopIteration:
                # Finished processing rule
                if priorBooleanGoalSuccess:
                    yield projectedBindings and projectedBindings or True, step
                elif projectedBindings:
                    # Return the most recent (cumulative) answers and the given
                    # step
                    yield projectedBindings, step
                else:
                    raise RuleFailure(
                        "Finished processing rule unsuccessfully")
Example #7
0
def StratifiedSPARQL(rule, nsMapping={EX_NS: 'ex'}):
    """
    The SPARQL specification indicates that it is possible to test if a graph
    pattern does not match a dataset, via a combination of optional patterns
    and filter conditions (like negation as failure in logic programming)([9]
    Sec. 11.4.1).
    In this section we analyze in depth the scope and limitations of this
    approach. We will introduce a syntax for the “difference” of two graph
    patterns P1 and P2, denoted (P1 MINUS P2), with the intended informal
    meaning: “the set of mappings that match P1 and does not match P2”.

    Uses telescope to construct the SPARQL MINUS BGP expressions for body
    conditions with default negation formulae
    """
    from FuXi.Rete.SidewaysInformationPassing import (GetArgs, findFullSip,
                                                      iterCondition)
    # Find a sip order of the horn rule
    if isinstance(rule.formula.body, And):
        sipOrder = first(
            findFullSip(([rule.formula.head], None), rule.formula.body))
    else:
        sipOrder = [rule.formula.head] + [rule.formula.body]
    from telescope import optional, op
    from telescope.sparql.queryforms import Select
    # from telescope.sparql.expressions import Expression
    from telescope.sparql.compiler import SelectCompiler
    from telescope.sparql.patterns import GroupGraphPattern
    toDo = []
    negativeVars = set()
    positiveLiterals = False
    for atom in sipOrder[1:]:
        if atom.naf:
            toDo.append(atom)
            negativeVars.update(GetVars(atom))
        else:
            positiveLiterals = True
    #The negative literas are moved to the back of the body conjunct
    #Intuitively, they should not be disconnected from the rest of rule
    #Due to the correlation between DL and guarded FOL
    [sipOrder.remove(toRemove) for toRemove in toDo]

    #posLiterals are all the positive literals leading up to the negated
    #literals (in left-to-right order)  There may be none, see below
    posLiterals = sipOrder[1:]

    posVarIgnore = []
    if not positiveLiterals:
        from FuXi.Horn.PositiveConditions import Uniterm
        #If there are no lead, positive literals (i.e. the LP is of the form:
        #   H :- not B1, not B2, ...
        #Then a 'phantom' triple pattern is needed as the left operand to the OPTIONAL
        #in order to properly implement P0 MINUS P where P0 is an empty pattern
        keyVar = GetVars(rule.formula.head)[0]
        newVar1 = Variable(BNode())
        newVar2 = Variable(BNode())
        posVarIgnore.extend([newVar1, newVar2])
        phantomLiteral = Uniterm(newVar1, [keyVar, newVar2])
        posLiterals.insert(0, phantomLiteral)

    #The positive variables are collected
    positiveVars = set(
        reduce(lambda x, y: x + y, [GetVars(atom) for atom in posLiterals]))

    # vars = {}
    # varExprs = {}
    # copyPatterns = []
    print("%s =: { %s MINUS %s} " % (rule.formula.head, posLiterals, toDo))

    def collapseMINUS(left, right):
        negVars = set()
        for pred in iterCondition(right):
            negVars.update(
                [term for term in GetArgs(pred) if isinstance(term, Variable)])
        innerCopyPatternNeeded = not negVars.difference(positiveVars)
        #A copy pattern is needed if the negative literals don't introduce new vars
        if innerCopyPatternNeeded:
            innerCopyPatterns, innerVars, innerVarExprs = createCopyPattern(
                [right])
            #We use an arbitrary new variable as for the outer FILTER(!BOUND(..))
            outerFilterVariable = list(innerVars.values())[0]
            optionalPatterns = [right] + innerCopyPatterns
            negatedBGP = optional(
                *[formula.toRDFTuple() for formula in optionalPatterns])
            negatedBGP.filter(
                *[k == v for k, v in list(innerVarExprs.items())])
            positiveVars.update(
                [Variable(k.value[0:]) for k in list(innerVarExprs.keys())])
            positiveVars.update(list(innerVarExprs.values()))
        else:
            #We use an arbitrary, 'independent' variable for the outer FILTER(!BOUND(..))
            outerFilterVariable = negVars.difference(positiveVars).pop()
            optionalPatterns = [right]
            negatedBGP = optional(
                *[formula.toRDFTuple() for formula in optionalPatterns])
            positiveVars.update(negVars)
        left = left.where(*[negatedBGP])
        left = left.filter(~op.bound(outerFilterVariable))
        return left

    topLevelQuery = Select(GetArgs(rule.formula.head)).where(
        GroupGraphPattern.from_obj(
            [formula.toRDFTuple() for formula in posLiterals]))
    rt = reduce(collapseMINUS, [topLevelQuery] + toDo)
    return rt, SelectCompiler(nsMapping)
Example #8
0
def test_issue494_collapsing_bnodes():
    """Test for https://github.com/RDFLib/rdflib/issues/494 collapsing BNodes"""
    g = Graph()
    g += [
        (BNode("Na1a8fbcf755f41c1b5728f326be50994"), RDF["object"],
         URIRef("source")),
        (BNode("Na1a8fbcf755f41c1b5728f326be50994"), RDF["predicate"],
         BNode("vcb3")),
        (BNode("Na1a8fbcf755f41c1b5728f326be50994"), RDF["subject"],
         BNode("vcb2")),
        (BNode("Na1a8fbcf755f41c1b5728f326be50994"), RDF["type"],
         RDF["Statement"]),
        (BNode("Na713b02f320d409c806ff0190db324f4"), RDF["object"],
         URIRef("target")),
        (BNode("Na713b02f320d409c806ff0190db324f4"), RDF["predicate"],
         BNode("vcb0")),
        (BNode("Na713b02f320d409c806ff0190db324f4"), RDF["subject"],
         URIRef("source")),
        (BNode("Na713b02f320d409c806ff0190db324f4"), RDF["type"],
         RDF["Statement"]),
        (BNode("Ndb804ba690a64b3dbb9063c68d5e3550"), RDF["object"],
         BNode("vr0KcS4")),
        (
            BNode("Ndb804ba690a64b3dbb9063c68d5e3550"),
            RDF["predicate"],
            BNode("vrby3JV"),
        ),
        (BNode("Ndb804ba690a64b3dbb9063c68d5e3550"), RDF["subject"],
         URIRef("source")),
        (BNode("Ndb804ba690a64b3dbb9063c68d5e3550"), RDF["type"],
         RDF["Statement"]),
        (BNode("Ndfc47fb1cd2d4382bcb8d5eb7835a636"), RDF["object"],
         URIRef("source")),
        (BNode("Ndfc47fb1cd2d4382bcb8d5eb7835a636"), RDF["predicate"],
         BNode("vcb5")),
        (BNode("Ndfc47fb1cd2d4382bcb8d5eb7835a636"), RDF["subject"],
         URIRef("target")),
        (BNode("Ndfc47fb1cd2d4382bcb8d5eb7835a636"), RDF["type"],
         RDF["Statement"]),
        (BNode("Nec6864ef180843838aa9805bac835c98"), RDF["object"],
         URIRef("source")),
        (BNode("Nec6864ef180843838aa9805bac835c98"), RDF["predicate"],
         BNode("vcb4")),
        (BNode("Nec6864ef180843838aa9805bac835c98"), RDF["subject"],
         URIRef("source")),
        (BNode("Nec6864ef180843838aa9805bac835c98"), RDF["type"],
         RDF["Statement"]),
    ]

    # print('graph length: %d, nodes: %d' % (len(g), len(g.all_nodes())))
    # print('triple_bnode degrees:')
    # for triple_bnode in g.subjects(RDF['type'], RDF['Statement']):
    #     print(len(list(g.triples([triple_bnode, None, None]))))
    # print('all node degrees:')
    g_node_degs = sorted(
        [len(list(g.triples([node, None, None]))) for node in g.all_nodes()],
        reverse=True,
    )
    # print(g_node_degs)

    cg = to_canonical_graph(g)
    # print('graph length: %d, nodes: %d' % (len(cg), len(cg.all_nodes())))
    # print('triple_bnode degrees:')
    # for triple_bnode in cg.subjects(RDF['type'], RDF['Statement']):
    #     print(len(list(cg.triples([triple_bnode, None, None]))))
    # print('all node degrees:')
    cg_node_degs = sorted(
        [len(list(cg.triples([node, None, None]))) for node in cg.all_nodes()],
        reverse=True,
    )
    # print(cg_node_degs)

    assert len(g) == len(
        cg), "canonicalization changed number of triples in graph"
    assert len(g.all_nodes()) == len(
        cg.all_nodes()), "canonicalization changed number of nodes in graph"
    assert len(list(g.subjects(RDF["type"], RDF["Statement"]))) == len(
        list(cg.subjects(RDF["type"], RDF["Statement"]))
    ), "canonicalization changed number of statements"
    assert g_node_degs == cg_node_degs, "canonicalization changed node degrees"

    # counter for subject, predicate and object nodes
    g_pos_counts = Counter(), Counter(), Counter()
    for t in g:
        for i, node in enumerate(t):
            g_pos_counts[i][t] += 1
    g_count_signature = [sorted(c.values()) for c in g_pos_counts]

    cg = to_canonical_graph(g)
    cg_pos_counts = Counter(), Counter(), Counter()
    for t in cg:
        for i, node in enumerate(t):
            cg_pos_counts[i][t] += 1
    cg_count_signature = [sorted(c.values()) for c in cg_pos_counts]

    assert (g_count_signature == cg_count_signature
            ), "canonicalization changed node position counts"
Example #9
0
with the parse() function.
Triples can also be added with the add() function:
Graph.add((s, p, o))[source]
Add a triple with self as context
"""

from rdflib import Graph
from rdflib import Namespace
from rdflib import BNode, Literal
from rdflib.namespace import RDF, FOAF

g = Graph()

n = Namespace("http://example.org/people/")
bob = n.bob
linda = BNode()
name = Literal('Bob')  # passing a string
age = Literal(24)  # passing a python int
height = Literal(76.5)  # passing a python float

g.add((bob, RDF.type, FOAF.Person))
g.add((bob, FOAF.name, name))
g.add((bob, FOAF.know, linda))
g.add((linda, RDF.type, FOAF.Person))
g.add((linda, FOAF.name, Literal('Linda')))

print("turtle:", g.serialize(format='turtle'))

# For some properties, only one value per resource makes sense (i.e they are
# functional properties, or have max-cardinality of 1). The set() method is useful for this:
g.add((bob, FOAF.age, Literal(42)))
Example #10
0
_hdlr.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
_logger.addHandler(_hdlr)

from rdflib import Graph, URIRef, Literal, BNode, Namespace, RDF

store = Graph()

# Bind a few prefix, namespace pairs.
store.bind("dc", "http://http://purl.org/dc/elements/1.1/")
store.bind("foaf", "http://xmlns.com/foaf/0.1/")

# Create a namespace object for the Friend of a friend namespace.
FOAF = Namespace("http://xmlns.com/foaf/0.1/")

# Create an identifier to use as the subject for Donna.
donna = BNode()

# Add triples using store's add method.
store.add((donna, RDF.type, FOAF["Person"]))
store.add((donna, FOAF["nick"], Literal("donna", lang="foo")))
store.add((donna, FOAF["name"], Literal("Donna Fales")))

# Iterate over triples in store and print them out.
print "--- printing raw triples ---"
for s, p, o in store:
    print s, p, o

# For each foaf:Person in the store print out its mbox property.
print "--- printing mboxes ---"
for person in store.subjects(RDF.type, FOAF["Person"]):
    for mbox in store.objects(person, FOAF["mbox"]):
Example #11
0
def _get_identifier(prefixes: dict,
                    value: object,
                    instance: str = None,
                    datatype: str = None,
                    language: str = None) -> Identifier:
    """
    Takes value extracted from the index, column or cell and returns
    an instance of Identifier (Literal, URIRef or BNode) using correct 
    datatype and language.

    Parameters
    ----------
    prefixes : dict
        Prefixes to use to normalize URIs
    value : object
        Value of index, column or cell
    instance : str
        Name of the rdfLib Identifier class to use
    datatype : str
        Datatype of rdfLib Literal to use 
        (see https://rdflib.readthedocs.io/en/stable/rdf_terms.html#python-support)
    language : str
        Language of rdfLib Literal to use 

    Returns
    -------
    rdflib.term.Identifier
        rdflib.term.Identifier instance - either URIRef or Literal.

    """

    if not instance:
        if language:
            return Literal(value, lang=language)
        elif datatype:
            return Literal(value, datatype=URIRef(datatype))
        elif _is_uri(value):
            return URIRef(value)
        elif _is_curie(value):
            return _get_uriref_for_curie(prefixes, value)
        else:
            return Literal(value)
    elif instance == Literal.__name__:
        if language:
            return Literal(value, lang=language)
        elif datatype:
            if _is_uri(datatype):
                datatype_uriref = URIRef(datatype)
            elif _is_curie(datatype):
                datatype_uriref = _get_uriref_for_curie(prefixes, datatype)
            else:
                ValueError(f'Not a valid URI for datatype {datatype}')
            return Literal(value, datatype=datatype_uriref)
        else:
            return Literal(value)
    elif instance == URIRef.__name__:
        if _is_uri(value):
            return URIRef(value)
        elif _is_curie(value):
            return _get_uriref_for_curie(prefixes, value)
        else:
            ValueError(f'Not a valid URI {value}')
    elif instance == BNode.__name__:
        return BNode(value)

    raise ValueError(
        f'Can only create Literal, URIRef or BNode but was {instance}')
Example #12
0
    def prep_annotation_file(self, basefile):
        goldstandard = self.eval_get_goldstandard(basefile)
        baseline_set = self.eval_get_ranked_set_baseline(basefile)
        baseline_map = self.eval_calc_map(
            self.eval_calc_aps(baseline_set, goldstandard))
        print("Baseline MAP %f" % baseline_map)
        self.log.info("Calculating ranked set (pagerank, unrestricted)")
        pagerank_set = self.eval_get_ranked_set(basefile,
                                                "pagerank",
                                                age_compensation=False,
                                                restrict_cited=False)
        pagerank_map = self.eval_calc_map(
            self.eval_calc_aps(pagerank_set, goldstandard))
        print("Pagerank MAP %f" % pagerank_map)
        sets = [{
            'label': 'Baseline',
            'data': baseline_set
        }, {
            'label': 'Gold standard',
            'data': goldstandard
        }, {
            'label': 'PageRank',
            'data': pagerank_set
        }]

        g = Graph()
        g.bind('dcterms', self.ns['dcterms'])
        g.bind('rinfoex', self.ns['rinfoex'])

        XHT_NS = "{http://www.w3.org/1999/xhtml}"
        tree = ET.parse(self.parsed_path(basefile))
        els = tree.findall("//" + XHT_NS + "div")
        articles = []
        for el in els:
            if 'typeof' in el.attrib and el.attrib[
                    'typeof'] == "eurlex:Article":
                article = str(el.attrib['id'][1:])
                articles.append(article)
        for article in articles:
            self.log.info("Results for article %s" % article)
            articlenode = URIRef("http://lagen.nu/ext/celex/12008E%03d" %
                                 int(article))
            resultsetcollectionnode = BNode()
            g.add((resultsetcollectionnode, RDF.type, RDF.List))
            rc = Collection(g, resultsetcollectionnode)
            g.add((articlenode, DCTERMS["relation"], resultsetcollectionnode))
            for s in sets:
                resultsetnode = BNode()
                listnode = BNode()
                rc.append(resultsetnode)
                g.add((resultsetnode, RDF.type,
                       RINFOEX["RelatedContentCollection"]))
                g.add((resultsetnode, DCTERMS["title"], Literal(s["label"])))
                g.add((resultsetnode, DCTERMS["hasPart"], listnode))
                c = Collection(g, listnode)
                g.add((listnode, RDF.type, RDF.List))
                if article in s['data']:
                    print(("    Set %s" % s['label']))
                    for result in s['data'][article]:
                        resnode = BNode()
                        g.add((resnode, DCTERMS["references"],
                               Literal(result[0])))
                        g.add((resnode, DCTERMS["title"], Literal(result[1])))
                        c.append(resnode)
                        print(("        %s" % result[1]))

        return self.graph_to_annotation_file(g, basefile)
Example #13
0
    def graph_from_dataset(self, dataset_dict, dataset_ref):

        log.debug("ODMDCATBasicProfileDataset graph_from_dataset")

        g = self.g

        namespaces = odm_rdf_helper.get_namespaces_by_dataset_type(
            dataset_dict['type'])

        for prefix, namespace in namespaces.iteritems():
            g.bind(prefix, namespace)

        g.add((dataset_ref, DCT.identifier, Literal(dataset_dict.get('id'))))
        g.add((dataset_ref, DCT.type,
               Literal(dataset_dict.get('type', 'dataset'))))
        g.add((dataset_ref, RDF.type, DCAT.Dataset))

        items = [(dataset_ref, DCT.title, dataset_dict.get('title_translated')
                  or dataset_dict.get('title')),
                 (dataset_ref, DCT.description,
                  dataset_dict.get('notes_translated')
                  or dataset_dict.get('notes'))]

        raw_triples = odm_rdf_helper.get_triples_by_dataset_type(
            dataset_ref, dataset_dict, dataset_dict['type'])
        raw_triples.extend(items)

        for raw_triple in raw_triples:
            triples = odm_rdf_helper.split_multilingual_object_into_triples(
                raw_triple)
            for triple in triples:
                g.add(triple)

        #Organization
        organization = dataset_dict.get('organization')
        g.add((dataset_ref, FOAF.organization,
               URIRef(
                   config.get('ckan.site_url') + "organization/" +
                   organization['name'])))

        #license
        license = URIRef(dataset_dict.get('license_url'))
        g.add((license, DCT.title, Literal(dataset_dict.get('license_title'))))
        g.add((dataset_ref, DCT.license, license))

        # odm_spatial_range
        for item in dataset_dict.get('odm_spatial_range', []):
            iso3_code = odm_rdf_helper.map_country_code_iso2_iso3(item.upper())
            g.add((dataset_ref, GN.countrycode,
                   URIRef("http://data.landportal.info/geo/" + iso3_code)))

        #taxonomy
        for term in dataset_dict.get('taxonomy', []):
            matches = odm_rdf_helper.map_internal_to_standard_taxonomic_term(
                term)

            if isinstance(matches, basestring):
                g.add((dataset_ref, FOAF.topic, Literal(matches)))
            else:
                node = BNode()
                if 'exact_match' in matches:
                    node = URIRef(matches['exact_match'])
                if 'broad_matches' in matches:
                    for broad_match in matches['broad_matches']:
                        g.add((node, SKOS.broadMatch, URIRef(broad_match)))
                        g.add((node, DCT.title, Literal(term)))

                g.add((dataset_ref, FOAF.topic, node))

        #  Language
        for item in dataset_dict.get('odm_language', []):
            g.add((dataset_ref, DC.language, Literal(item.upper())))

        # Dates
        try:
            items = odm_rdf_helper.get_date_fields_by_dataset_type(
                dataset_dict['type'])
            self._add_date_triples_from_dict(dataset_dict, dataset_ref, items)
        except ValueError:
            log.debug("Error adding date triples for dataset " +
                      dataset_dict['id'])

        # Resources
        for resource_dict in dataset_dict.get('resources', []):

            distribution = URIRef(resource_uri(resource_dict))
            g.add((dataset_ref, DCAT.Distribution, distribution))
            g.add((distribution, RDF.type, DCAT.Distribution))

            items = [(distribution, DCT.title,
                      resource_dict.get('name_translated')
                      or resource_dict.get('name')),
                     (distribution, DCT.description,
                      resource_dict.get('description_translated')
                      or resource_dict.get('description'))]
            for item in items:
                triples = odm_rdf_helper.split_multilingual_object_into_triples(
                    item)
                for triple in triples:
                    g.add(triple)

            try:
                self._add_triples_from_dict(resource_dict, distribution, items)
            except ValueError:
                log.debug("Error adding triples for dataset " +
                          dataset_dict['id'])

            #  Language
            for item in resource_dict.get('odm_language', []):
                g.add((distribution, DC.language, Literal(item.upper())))

            # Format
            if '/' in resource_dict.get('format', ''):
                g.add((distribution, DCAT.mediaType,
                       Literal(resource_dict['format'])))
            else:
                if resource_dict.get('format'):
                    g.add((distribution, DCT['format'],
                           Literal(resource_dict['format'])))

                if resource_dict.get('mimetype'):
                    g.add((distribution, DCAT.mediaType,
                           Literal(resource_dict['mimetype'])))

            # URL
            url = resource_dict.get('url')
            download_url = resource_dict.get('download_url')
            if download_url:
                g.add((distribution, DCAT.downloadURL, Literal(download_url)))
            if (url and not download_url) or (url and url != download_url):
                g.add((distribution, DCAT.downloadURL, URIRef(url)))
Example #14
0
oa = Namespace("http://www.w3.org/ns/oa#")
jw = Namespace("http://localhost:8000/jabberwocky/")


def StrLiteral(s):
    """String literal convenience method."""
    return Literal(s, datatype=XSD.string)


# Build RDF
g = Graph()
g.add((jw.manifest, RDF.type, sc.Manifest))
# Simple descriptive information
g.add((jw.manifest, RDFS.label, StrLiteral("Jabberwocky")))
g.add((jw.manifest, DC.description,
       StrLiteral("A bad edition of wonderful nonsense.")))
# Label/value pairs
author = BNode()
g.add((jw.manifest, sc.metadataLabels, author))
g.add((author, RDFS.label, StrLiteral("Author")))
g.add((author, RDF.value, StrLiteral("Lewis Carroll")))

# Get JSON-LD object in PyLD form
jld = pyld_json_from_rdflib_graph(g)

# Frame and compact...
framed = jsonld.compact(
    jsonld.frame(jld, "http://iiif.io/api/presentation/2/manifest_frame.json"),
    "http://iiif.io/api/presentation/2/context.json")
print(json.dumps(framed, indent=2, sort_keys=True))
Example #15
0
 def __init__(self, sensor_description, observable_property_uri):
     self.sensorid = BNode()
     self.sensor_description = Literal(sensor_description)
     obsgraph.add((self.sensorid, RDF.type, sosa.Sensor))
     obsgraph.add((self.sensorid, sosa.Observes, observable_property_uri))
     obsgraph.add((self.sensorid, RDFS.comment, self.sensor_description))
Example #16
0
 def __init__(self, comment, label):
     self.comment = Literal(comment)
     self.observation_id = BNode()
 def make_v_result(
     self,
     datagraph: GraphLike,
     focus_node: 'Identifier',
     value_node: Optional['Identifier'] = None,
     result_path: Optional['Identifier'] = None,
     constraint_component: Optional['Identifier'] = None,
     source_constraint: Optional['Identifier'] = None,
     extra_messages: Optional[Iterable] = None,
     bound_vars=None,
 ):
     """
     :param datagraph:
     :type datagraph: rdflib.Graph | rdflib.ConjunctiveGraph | rdflib.Dataset
     :param focus_node:
     :type focus_node: Identifier
     :param value_node:
     :type value_node: Identifier | None
     :param result_path:
     :type result_path: Identifier | None
     :param constraint_component:
     :param source_constraint:
     :param extra_messages:
     :type extra_messages: collections.abc.Iterable | None
     :param bound_vars:
     :return:
     """
     constraint_component = constraint_component or self.shacl_constraint_component
     severity = self.shape.severity
     sg = self.shape.sg.graph
     r_triples = list()
     r_node = BNode()
     r_triples.append((r_node, RDF_type, SH_ValidationResult))
     r_triples.append((r_node, SH_sourceConstraintComponent, (sg, constraint_component)))
     r_triples.append((r_node, SH_sourceShape, (sg, self.shape.node)))
     r_triples.append((r_node, SH_resultSeverity, severity))
     r_triples.append((r_node, SH_focusNode, (datagraph or sg, focus_node)))
     if value_node is not None:
         r_triples.append((r_node, SH_value, (datagraph, value_node)))
     if result_path is None and self.shape.is_property_shape:
         result_path = self.shape.path()
     if result_path is not None:
         r_triples.append((r_node, SH_resultPath, (sg, result_path)))
     if source_constraint is not None:
         r_triples.append((r_node, SH_sourceConstraint, (sg, source_constraint)))
     messages = list(self.shape.message)
     if extra_messages:
         for m in iter(extra_messages):
             if m in messages:
                 continue
             if isinstance(m, Literal):
                 msg = str(m.value)
                 if bound_vars is not None:
                     msg = self._format_sparql_based_result_message(msg, bound_vars)
                     m = Literal(msg)
             r_triples.append((r_node, SH_resultMessage, m))
     elif not messages:
         messages = self.make_generic_messages(datagraph, focus_node, value_node) or messages
     for m in messages:
         if isinstance(m, Literal):
             msg = str(m.value)
             if bound_vars is not None:
                 msg = self._format_sparql_based_result_message(msg, bound_vars)
                 m = Literal(msg)
         r_triples.append((r_node, SH_resultMessage, m))
     desc = self.make_v_result_description(
         datagraph,
         focus_node,
         severity,
         value_node,
         messages,
         result_path=result_path,
         constraint_component=constraint_component,
         source_constraint=source_constraint,
         extra_messages=extra_messages,
         bound_vars=bound_vars,
     )
     self.shape.logger.debug(desc)
     return desc, r_node, r_triples
Example #18
0
 def _generate_bnode():
     return BNode()
Example #19
0
 def provenance(self, context, g, subj):
     pnode = BNode()
     g.add((subj, PROV['wasDerivedFrom'], pnode))
     g.add((pnode, RDFS['label'], Literal(context.getInitialProvenance())))
     return g
Example #20
0
 def BNode(self):
     return BNode(-self.store.world.new_blank_node())
Example #21
0
def build_graph(ac_descriptors, uri=None):

    g = Graph()

    audioFile = BNode()

    if uri is None:
        availableItemOf = BNode()
    else:
        availableItemOf = URIRef(uri)
    g.add((availableItemOf, RDF['type'], AC['AudioClip']))
    g.add((audioFile, AC['availableItemOf'], availableItemOf))

    g.add((audioFile, RDF['type'], AC['AudioFile']))
    g.add((audioFile, AC['singalSamplerate'],
           Literal(ac_descriptors['samplerate'])))
    g.add((audioFile, AC['signalChannels'],
           Literal(int(ac_descriptors['channels']))))
    g.add(
        (audioFile, AC['signalDuration'], Literal(ac_descriptors['duration'])))
    g.add((audioFile, EBU['bitrate'], Literal(ac_descriptors['bitrate'])))
    g.add((audioFile, EBU['filesize'], Literal(ac_descriptors['filesize'])))
    g.add((audioFile, AC['audioMd5'], Literal(ac_descriptors['audio_md5'])))
    #g.add((audioFile, AC['singleEvent'], Literal(ac_descriptors['single_event'])))

    audioCodec = BNode()
    g.add((audioCodec, RDF['type'], EBU['AudioCodec']))
    g.add((audioCodec, EBU['codecId'], Literal(ac_descriptors['codec'])))
    g.add((audioFile, EBU['hasCodec'], audioCodec))

    if ac_descriptors['lossless']:
        g.add((audioFile, NFO['compressionType'],
               Literal('nfo:losslessCompressionType')))
    else:
        g.add((audioFile, NFO['compressionType'],
               Literal('nfo:lossyCompressionType')))

    for type_name, value_field, confidence_field in [
        ('Tempo', 'tempo', 'tempo_confidence'),
        ('Loop', 'loop', None),
        ('Key', 'tonality', 'tonality_confidence'),
        ('Loudness', 'loudness', None),
        ('TemporalCentroid', 'temporal_centroid', None),
        ('LogAttackTime', 'log_attack_time', None),
        ('MIDINote', 'note_midi', 'note_confidence'),
        ('Note', 'note_name', 'note_confidence'),
        ('Pitch', 'note_frequency', 'note_confidence'),
            #('TimbreBrightness', 'brightness', 'note_confidence'),
            #('TimbreDepth', 'depth', None),
            #('TimbreHardness', 'hardness', None),
            #('TimbreMetallic', 'metallic', None),
            #('TimbreReverb', 'reverb', None),
            #('TimbreRoughness', 'roughness', None),
            #('TimbreBoominess', 'booming', None),
            #('TimbreWarmth', 'warmth', None),
            #('TimbreSharpness', 'sharpness', None),
    ]:
        if value_field in ac_descriptors:
            # Only include descriptors if present in analysis
            signalFeature = BNode()
            g.add((signalFeature, RDF['type'], AFV[type_name]))
            g.add((signalFeature, AFO['value'],
                   Literal(ac_descriptors[value_field])))
            if confidence_field is not None:
                g.add((signalFeature, AFO['confidence'],
                       Literal(ac_descriptors[confidence_field])))
            g.add((audioFile, AC['signalAudioFeature'], signalFeature))

    return g
start = 11
end = 16
uri_wikidata = 'https://www.wikidata.org/wiki/Q686822'

###### end ######

NEE = Namespace('http://www.ics.forth.gr/isl/oae/core#')

store = Graph()

# Bind a few prefix, namespace pairs for pretty output
store.bind("NEE", NEE)

# Create an identifier
# todo: non usare BNODE, ma resource, chiedere la uri
act = BNode()

# Add triples using store's add method.
store.add((act, RDF.type, NEE.Entity))

# todo: check the following fields, one by one:
# reference from: https://www.ics.forth.gr/isl/oae/specification/index.html

# Relates the annotation process to a configuration.
# store.add((act, NEE.usingConfiguration,

# Relates an entity to a literal representing the string in the document that was detected and considered an entity.
store.add((act, NEE.detectedAs, Literal(act_text[start:end])))

# Relates an entity to a literal representing the actual entity name that exists in a gazetteer of the NEE system.
# store.add((act, NEE.regardsEntityName, # using Wikidata italian label?
Example #23
0
def assert_CDTInstant_value(g, cdtinst, yr=0, mo=0, da=0, hr=0, min=0, sec=0, ut=time.unitYear) :
	# if not a CDInstant then return
	if not (cdtinst, RDF.type, cot.CDateTimeInstant) in g :
		logging.warning('assert_CDTInstant_value: Not a CDTInstant [%s %s] ', str(g), str(cdtinst))
		return(False)

	# create temporary DTD for asserted value
	new_dtd = DateTimeDescription.DateTimeDescription(g, None, yr, mo, da, hr, min, sec, ut)

	# reset min but check if new value is less than it
	cdtimin_i = g.value(cdtinst, cot.hasMin, any=False)
	if not cdtimin_i :
		cdtimin_i = BNode()
		g.add((cdtimin_i, RDF.type, time.Instant))
		g.add((cdtinst, cot.hasMin, cdtimin_i))
	cdtimin = g.value(cdtimin_i, time.inDateTime, any=False)

	if not cdtimin:
		logging.warning('assert_CDTInstant_value: No hasMin cdti [%s %s] ', str(g), str(cdtinst))
		cdtimin = BNode()
		g.add((cdtimin_i, time.inDateTime, cdtimin))
		g.add((cdtimin, RDF.type, time.DateTimeDescription))
		g.add((cdtimin, time.year, Literal(yr)))
		g.add((cdtimin, time.month, Literal(mo)))
		g.add((cdtimin, time.day, Literal(da)))
		g.add((cdtimin, time.hour, Literal(hr)))
		g.add((cdtimin, time.minute, Literal(min)))
		g.add((cdtimin, time.second, Literal(sec)))
		g.add((cdtimin, time.unitType, ut))
	else :
		# if DTD exists, check its bound
		dtd = DateTimeDescription.DateTimeDescription.dtd_dtdc_map.get((g, cdtimin), None)
		if dtd :
			if dtd.greaterthan(new_dtd) :
				logging.warning('assert_CDTInstant_value: new dtd less than hasMin [%s %s] ', str(g), str(cdtinst))
		else :
			# if DTD does not exist, create it
			dtd = DateTimeDescription.DateTimeDescription(g, cdtimin)
		new_dtd.copy_into(dtd)
		dtd.update()

	# reset max but check if new value is greater than it
	cdtimax_i = g.value(cdtinst, cot.hasMax, any=False)
	if not cdtimax_i :
		cdtimax_i = BNode()
		g.add((cdtimax_i, RDF.type, time.Instant))
		g.add((cdtinst, cot.hasMax, cdtimax_i))
	cdtimax = g.value(cdtimax_i, time.inDateTime, any=False)
	if not cdtimax :
		logging.warning('assert_CDTInstant_value: No hasMax cdti [%s %s] ', str(g), str(cdtinst))
		cdtimax = BNode()
		g.add((cdtimax_i, time.inDateTime, cdtimax))
		g.add((cdtimax, RDF.type, time.DateTimeDescription))
		g.add((cdtimax, time.year, Literal(yr)))
		g.add((cdtimax, time.month, Literal(mo)))
		g.add((cdtimax, time.day, Literal(da)))
		g.add((cdtimax, time.hour, Literal(hr)))
		g.add((cdtimax, time.minute, Literal(min)))
		g.add((cdtimax, time.second, Literal(sec)))
		g.add((cdtimax, time.unitType, ut))
	else :
		# if DTD exists, check its bound
		dtd = DateTimeDescription.DateTimeDescription.dtd_dtdc_map.get((g, cdtimax), None)
		if dtd :
			if dtd.lessthan(new_dtd) :
				logging.warning('assert_CDTInstant_value: new dtd greater than hasMax [%s %s] ', str(g), str(cdtinst))
		else :
			# if DTD does not exist, create it
			dtd = DateTimeDescription.DateTimeDescription(g, cdtimax)
		new_dtd.copy_into(dtd)
		dtd.update()

	ctime.changed_DCTInstants.add(cdtinst)
Example #24
0
from rdflib.namespace import FOAF, RDF
from rdflib import Graph
from rdflib import URIRef, BNode, Literal
from rdflib import Namespace



bob = URIRef("http://example.org/people/Bob")
linda = BNode()  # a GUID is generated

amlo = URIRef("http://example.org/people/López_Obrador")
bety = URIRef("http://example.org/people/Beatriz_Müller")
merry = URIRef("http://example.org/status/esta_casado_con")

name = Literal('Bob')  # passing a string
# age = Literal(24)  # passing a python int
# height = Literal(76.5)  # passing a python float

g = Graph()

g.bind("foaf", FOAF)

# g.add((bob, RDF.type, FOAF.Person))
# g.add((bob, FOAF.name, name))
# g.add((bob, FOAF.knows, linda))
# g.add((linda, RDF.type, FOAF.Person))
# g.add((linda, FOAF.name, Literal("Linda")))

g.add((amlo, RDF.type, FOAF.Person))
g.add((amlo, FOAF.name, Literal('López_Obrador')))
g.add((amlo, merry, bety))
Example #25
0
 def node(self, model):
     if self.uri:
         return URIRef(reverse(self.uri, args=[model.id]))
     return BNode()
Example #26
0
    def _addDict(self, data):
        '''convert a dictionary to an RDF graph.'''

        t_node = BNode(_e(data['name']))
        self._addTriple(t_node, RDF.type,
                        CLP['CommandLineProgramComponentType'])
        self._addTriple(t_node, DCTERMS['label'], data['name'])
        self._addTriple(t_node, DCTERMS['title'], data['binary'])
        self._addTriple(t_node, DCTERMS['description'], data['description'])
        self._addTriple(t_node, Component['hasVersion'], data['version'])
        self._addTriple(t_node, DCTERMS['comment'], data['help'])
        self._generateStatements(t_node, data['property_bag'])

        r_node = BNode()
        self._addTriple(t_node, Component['hasExecutionRequirements'], r_node)
        self._addTriple(r_node, RDF.type, Component['ExecutionRequirements'])
        self._addTriple(r_node, Component['requiresOperationSystem'],
                        Component['Linux'])  # TODO
        if data['interpreter'] != '(binary)':
            self._addTriple(r_node, Component['requiresSoftware'],
                            Component[data['interpreter']])
        if data['grid_access_type'] != '-':
            self._addTriple(r_node, CLP['gridAccessType'],
                            data['grid_access_type'])
            self._addTriple(r_node, Component['gridID'],
                            data['grid_access_location'])
        for req in data['requirements']:
            req_node = BNode()
            self._addTriple(r_node, CLP['requiresSoftware'], req_node)
            self._addTriple(req_node, RDF.type, CLP['Software'])
            self._addTriple(req_node, DCTERMS['title'], req['req_name'])
            self._addTriple(req_node, CLP['gridID'], req['req_location'])
            self._addTriple(req_node, CLP['softwareType'], req['req_type'])

        argument_list = BNode('argument_list')
        self._addTriple(t_node, Component['hasArguments'], argument_list)
        # self._addTriple(argument_list, RDF.type,
        # Component['argumentAndPrefixList'])
        argument_nodes = Collection(self.graph, argument_list)

        input_list = BNode('input_list')
        self._addTriple(t_node, Component['hasInputs'], input_list)
        # self._addTriple(input_list, RDF.type,
        # Component['FileOrCollectionList'])
        input_nodes = Collection(self.graph, input_list)

        output_list = BNode('output_list')
        self._addTriple(t_node, Component['hasOutputs'], output_list)
        # self._addTriple(output_list, RDF.type,
        # Component['FileOrCollectionList'])
        output_nodes = Collection(self.graph, output_list)

        for p in data['parameters']:
            ap_node = BNode(_e(p['name']))
            argument_nodes.append(ap_node)
            self._addTriple(ap_node, RDF.type, Component['ArgumentAndPrefix'])

            a_node = BNode(_e(p['name']) + '_arg')
            self._addTriple(ap_node, Component['hasArgument'], a_node)

            choices = []
            if 'choices' in p and p['choices']:
                choices = [x.strip() for x in p['choices'].split(',')]

            p_type = p['type']
            if p_type == 'integer':
                self._addTriple(a_node, RDF.type, FO['Int'])
                try:
                    self._addTriple(a_node, FO['hasIntValue'], int(p['value']))
                    choices = [int(x) for x in choices]
                except ValueError:
                    pass  # do nothing if value is not an integer
            elif p_type == 'float':
                self._addTriple(a_node, RDF.type, FO['Float'])
                try:
                    self._addTriple(a_node, FO['hasFloatValue'],
                                    float(p['value']))
                    choices = [float(x) for x in choices]
                except ValueError:
                    pass  # do nothing if value is not a float
            elif p_type in ['string', 'select']:
                self._addTriple(a_node, RDF.type, FO['String'])
                self._addTriple(a_node, FO['hasStringValue'], p['value'])
            elif p_type in ['input', 'stdin']:
                self._addTriple(a_node, RDF.type, FO['File'])
                self._addTriple(a_node, DCTERMS['format'], p['format'])
                self._addTriple(a_node, Component['hasValue'], p['value'])
                input_nodes.append(a_node)
            elif p_type in ['output', 'stdout', 'stderr']:
                self._addTriple(a_node, RDF.type, FO['File'])
                self._addTriple(a_node, DCTERMS['format'], p['format'])
                self._addTriple(a_node, Component['hasValue'], p['value'])
                output_nodes.append(a_node)
            else:
                self._addTriple(a_node, Component['hasValue'], p['value'])

            if choices:
                choices = [Literal(x) for x in choices]
                choice_list = BNode(_e(p['name'] + '_choice_list'))
                choice_nodes = Collection(self.graph, choice_list, choices)
                self._addTriple(a_node, CLP['hasValueChoices'], choice_list)

            self._addTriple(ap_node, DCTERMS['title'], p['name'])
            self._addTriple(ap_node, DCTERMS['description'], p['description'])
            self._addTriple(ap_node, RDFS.label, p['label'])
            self._addTriple(ap_node, Component['hasPrefix'], p['arg'])
            self._addTriple(ap_node, CLP['hasAlternativePrefix'],
                            p['arg_long'])
            self._addTriple(ap_node, CLP['order'], int(p['rank']))
            self._addTriple(ap_node, CLP['display'], p['display'])
            self._addTriple(ap_node, CLP['minOccurrence'], p['min_occurrence'])
            self._addTriple(ap_node, CLP['maxOccurrence'], p['max_occurrence'])

            self._generateStatements(ap_node, p['property_bag'])
            self._generateDependencies(ap_node, p['dependencies'])
Example #27
0
from persistent import Persistent
from persistent.dict import PersistentDict
import transaction
import logging

import contextlib
import itertools
from operator import itemgetter
from random import randrange

import BTrees
from BTrees.Length import Length
from BTrees.OOBTree import intersection as oo_intersection

ANY = Any = None
DEFAULT = BNode('ZODBStore:DEFAULT')
L = logging.getLogger(__name__)
# TODO:
#   * is zope.intids id search faster? (maybe with large dataset and actual
#     disk access?)

ID_SERIES = {'default': 0}
ID_MAX = 2**48 - 1
ID_MASK = ID_MAX
SERIES_MAX = 2**16 - 1


def register_id_series(name):
    if name in ID_SERIES:
        raise ValueError('The given name has already been registered')
    series = max(ID_SERIES.values()) + 1
Example #28
0
def test_issue725_collapsing_bnodes_2():
    g = Graph()
    g += [
        (
            BNode("N0a76d42406b84fe4b8029d0a7fa04244"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"),
            BNode("v2"),
        ),
        (
            BNode("N0a76d42406b84fe4b8029d0a7fa04244"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"),
            BNode("v0"),
        ),
        (
            BNode("N0a76d42406b84fe4b8029d0a7fa04244"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"),
            URIRef("urn:gp_learner:fixed_var:target"),
        ),
        (
            BNode("N0a76d42406b84fe4b8029d0a7fa04244"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"),
        ),
        (
            BNode("N2f62af5936b94a8eb4b1e4bfa8e11d95"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"),
            BNode("v1"),
        ),
        (
            BNode("N2f62af5936b94a8eb4b1e4bfa8e11d95"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"),
            BNode("v0"),
        ),
        (
            BNode("N2f62af5936b94a8eb4b1e4bfa8e11d95"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"),
            URIRef("urn:gp_learner:fixed_var:target"),
        ),
        (
            BNode("N2f62af5936b94a8eb4b1e4bfa8e11d95"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"),
        ),
        (
            BNode("N5ae541f93e1d4e5880450b1bdceb6404"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"),
            BNode("v5"),
        ),
        (
            BNode("N5ae541f93e1d4e5880450b1bdceb6404"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"),
            BNode("v4"),
        ),
        (
            BNode("N5ae541f93e1d4e5880450b1bdceb6404"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"),
            URIRef("urn:gp_learner:fixed_var:target"),
        ),
        (
            BNode("N5ae541f93e1d4e5880450b1bdceb6404"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"),
        ),
        (
            BNode("N86ac7ca781f546ae939b8963895f672e"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"),
            URIRef("urn:gp_learner:fixed_var:source"),
        ),
        (
            BNode("N86ac7ca781f546ae939b8963895f672e"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"),
            BNode("v0"),
        ),
        (
            BNode("N86ac7ca781f546ae939b8963895f672e"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"),
            URIRef("urn:gp_learner:fixed_var:target"),
        ),
        (
            BNode("N86ac7ca781f546ae939b8963895f672e"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"),
        ),
        (
            BNode("Nac82b883ca3849b5ab6820b7ac15e490"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#object"),
            BNode("v1"),
        ),
        (
            BNode("Nac82b883ca3849b5ab6820b7ac15e490"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate"),
            BNode("v3"),
        ),
        (
            BNode("Nac82b883ca3849b5ab6820b7ac15e490"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject"),
            URIRef("urn:gp_learner:fixed_var:target"),
        ),
        (
            BNode("Nac82b883ca3849b5ab6820b7ac15e490"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
            URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement"),
        ),
    ]

    turtle = """
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

    [] a rdf:Statement ;
        rdf:object [ ] ;
        rdf:predicate _:v0 ;
        rdf:subject <urn:gp_learner:fixed_var:target> .

    [] a rdf:Statement ;
        rdf:object _:v1 ;
        rdf:predicate _:v0 ;
        rdf:subject <urn:gp_learner:fixed_var:target> .

    [] a rdf:Statement ;
        rdf:object [ ] ;
        rdf:predicate [ ] ;
        rdf:subject <urn:gp_learner:fixed_var:target> .

    [] a rdf:Statement ;
        rdf:object <urn:gp_learner:fixed_var:source> ;
        rdf:predicate _:v0 ;
        rdf:subject <urn:gp_learner:fixed_var:target> .

    [] a rdf:Statement ;
        rdf:object _:v1 ;
        rdf:predicate [ ] ;
        rdf:subject <urn:gp_learner:fixed_var:target> ."""

    # g = Graph()
    # g.parse(data=turtle, format='turtle')

    stats = {}
    cg = rdflib.compare.to_canonical_graph(g, stats=stats)

    # print ('graph g length: %d, nodes: %d' % (len(g), len(g.all_nodes())))
    # print ('triple_bnode degrees:')
    # for triple_bnode in g.subjects(rdflib.RDF['type'], rdflib.RDF['Statement']):
    #     print (len(list(g.triples([triple_bnode, None, None]))))
    # print ('all node out-degrees:')
    # print (sorted(
    #     [len(list(g.triples([node, None, None]))) for node in g.all_nodes()]))
    # print ('all node in-degrees:')
    # print (sorted(
    #     [len(list(g.triples([None, None, node]))) for node in g.all_nodes()]))
    # print(g.serialize(format='n3'))
    #
    # print ('graph cg length: %d, nodes: %d' % (len(cg), len(cg.all_nodes())))
    # print ('triple_bnode degrees:')
    # for triple_bnode in cg.subjects(rdflib.RDF['type'],
    #                                 rdflib.RDF['Statement']):
    #     print (len(list(cg.triples([triple_bnode, None, None]))))
    # print ('all node out-degrees:')
    # print (sorted(
    #     [len(list(cg.triples([node, None, None]))) for node in cg.all_nodes()]))
    # print ('all node in-degrees:')
    # print (sorted(
    #     [len(list(cg.triples([None, None, node]))) for node in cg.all_nodes()]))
    # print(cg.serialize(format='n3'))

    assert len(g.all_nodes()) == len(cg.all_nodes())

    cg = to_canonical_graph(g)
    assert len(g) == len(
        cg), "canonicalization changed number of triples in graph"
    assert len(g.all_nodes()) == len(
        cg.all_nodes()), "canonicalization changed number of nodes in graph"
    assert len(list(g.subjects(RDF["type"], RDF["Statement"]))) == len(
        list(cg.subjects(RDF["type"], RDF["Statement"]))
    ), "canonicalization changed number of statements"

    # counter for subject, predicate and object nodes
    g_pos_counts = Counter(), Counter(), Counter()
    for t in g:
        for i, node in enumerate(t):
            g_pos_counts[i][t] += 1
    g_count_signature = [sorted(c.values()) for c in g_pos_counts]

    cg_pos_counts = Counter(), Counter(), Counter()
    for t in cg:
        for i, node in enumerate(t):
            cg_pos_counts[i][t] += 1
    cg_count_signature = [sorted(c.values()) for c in cg_pos_counts]

    assert (g_count_signature == cg_count_signature
            ), "canonicalization changed node position counts"
Example #29
0
    def test_post_multiple_obsels(self):
        base = self.my_ktbs.create_base()
        model = base.create_model()
        otype0 = model.create_obsel_type("#MyObsel0")
        otype1 = model.create_obsel_type("#MyObsel1")
        otype2 = model.create_obsel_type("#MyObsel2")
        otype3 = model.create_obsel_type("#MyObsel3")
        otypeN = model.create_obsel_type("#MyObselN")
        trace = base.create_stored_trace(None, model, "1970-01-01T00:00:00Z",
                                         "alice")
        # purposefully mix obsel order,
        # to check whether batch post is enforcing the monotonic order
        graph = Graph()
        obsN = BNode()
        graph.add((obsN, KTBS.hasTrace, trace.uri))
        graph.add((obsN, RDF.type, otypeN.uri))
        obs1 = BNode()
        graph.add((obs1, KTBS.hasTrace, trace.uri))
        graph.add((obs1, RDF.type, otype1.uri))
        graph.add((obs1, KTBS.hasBegin, Literal(1)))
        graph.add((obs1, RDF.value, Literal("obs1")))
        obs3 = BNode()
        graph.add((obs3, KTBS.hasTrace, trace.uri))
        graph.add((obs3, RDF.type, otype3.uri))
        graph.add((obs3, KTBS.hasBegin, Literal(3)))
        graph.add((obs3, KTBS.hasSubject, Literal("bob")))
        obs2 = BNode()
        graph.add((obs2, KTBS.hasTrace, trace.uri))
        graph.add((obs2, RDF.type, otype2.uri))
        graph.add((obs2, KTBS.hasBegin, Literal(2)))
        graph.add((obs2, KTBS.hasEnd, Literal(3)))
        graph.add((obs2, RDF.value, Literal("obs2")))
        obs0 = BNode()
        graph.add((obs0, KTBS.hasTrace, trace.uri))
        graph.add((obs0, RDF.type, otype0.uri))
        graph.add((obs0, KTBS.hasBegin, Literal(0)))

        old_tag = trace.obsel_collection.str_mon_tag
        created = trace.post_graph(graph)
        new_tag = trace.obsel_collection.str_mon_tag

        assert len(created) == 5
        assert old_tag == new_tag

        obs0 = trace.get_obsel(created[0])
        assert obs0.begin == 0
        assert obs0.end == 0
        assert obs0.subject == Literal("alice")
        assert obs0.obsel_type == otype0

        obs1 = trace.get_obsel(created[1])
        assert obs1.begin == 1
        assert obs1.end == 1
        assert obs1.subject == Literal("alice")
        assert obs1.obsel_type == otype1
        assert obs1.get_attribute_value(RDF.value) == "obs1"

        obs2 = trace.get_obsel(created[2])
        assert obs2.begin == 2
        assert obs2.end == 3
        assert obs2.subject == Literal("alice")
        assert obs2.obsel_type == otype2
        assert obs2.get_attribute_value(RDF.value) == "obs2"

        obs3 = trace.get_obsel(created[3])
        assert obs3.begin == 3
        assert obs3.end == 3
        assert obs3.subject == Literal("bob")
        assert obs3.obsel_type == otype3

        obsN = trace.get_obsel(created[4])
        assert obsN.begin > 4 # set to current date, which is *much* higher
        assert obsN.end == obsN.begin
        assert obsN.subject == Literal("alice")
        assert obsN.obsel_type == otypeN
Example #30
0
    print url
    filename = 'cache/'+urllib2.quote(url, safe='')
    try:
        return open(filename).read()
    except IOError:
        page = urllib2.urlopen(url).read()
        open(filename, 'w').write(page)
        return page

mainpage = get_page('http://projects.propublica.org/docdollars/')
majorsoup = BeautifulSoup(mainpage)
States = []
for td in majorsoup.find_all("td",class_="label",style=False)[1:-1]:
    States.append(td.string)
g = Graph()
state = BNode()
clinic = BNode()
company = BNode()
transaction = BNode()
payment = BNode()

for state in States:
    try:
        page = get_page('http://projects.propublica.org/docdollars/states/'+state.replace(' ','-'))
        soup = BeautifulSoup(page)
        state_txns = soup.find_all('tr') #find all transactions
    except urllib2.HTTPError:
        print state+' not available'
        continue

    def track(tag): #get payment information
Example #31
0
class rdfSubject(object):
    db = ConjunctiveGraph()
    """Default graph for access to instances of this type"""
    rdf_type = None
    """rdf:type of instances of this class"""

    def __init__(self, resUri=None, **kwargs):
        """The constructor tries hard to do return you an rdfSubject

        :param resUri: the "resource uri". If `None` then create an instance
        with a BNode resUri. Can be given as one of:

           * an instance of an rdfSubject
           * an instance of a BNode or a URIRef
           * an n3 uriref string like: "<urn:isbn:1234567890>"
           * an n3 bnode string like: "_:xyz1234"
        :param kwargs: is a set of values that will be set using the keys to
        find the appropriate descriptor"""

        if not resUri:  # create a bnode
            self.resUri = BNode()
            if self.rdf_type:
                self.db.add((self.resUri, RDF.type, self.rdf_type))

        elif isinstance(resUri, (BNode, URIRef)):  # use the identifier passed
            self.resUri = resUri
            if self.rdf_type \
                and not list(self.db.triples(
                    (self.resUri, RDF.type, self.rdf_type))):
                self.db.add((self.resUri, RDF.type, self.rdf_type))

        elif isinstance(resUri, rdfSubject):  # use the resUri of the subject
            self.resUri = resUri.resUri
            self.db = resUri.db

        elif isinstance(resUri, (str, unicode)):   # create one from a <uri> or
            if resUri[0] == "<" and resUri[-1] == ">":  # _:bnode string
                self.resUri = URIRef(resUri[1:-1])
            elif resUri.startswith("_:"):
                self.resUri = BNode(resUri[2:])

            if self.rdf_type:
                self.db.add((self.resUri, RDF.type, self.rdf_type))

        else:
            raise AttributeError("cannot construct rdfSubject from %s" % (
                str(resUri)))

        if kwargs:
            self._set_with_dict(kwargs)

    def n3(self):
        """n3 repr of this node"""
        return self.resUri.n3()

    @classmethod
    def _getdescriptor(cls, key):
        """__get_descriptor returns the descriptor for the key.
        It essentially cls.__dict__[key] with recursive calls to super"""
        # NOT SURE if mro is the way to do this or if we should call
        # super() or bases?
        for kls in cls.mro():
            if key in kls.__dict__:
                return kls.__dict__[key]
        raise AttributeError(
            "descriptor %s not found for class %s" % (key, cls))

    # short term hack.  Need to go to a sqlalchemy 0.4 style query method
    # obj.query.get_by should map to obj.get_by  ..same for fetch_by
    @classmethod
    def query(cls):
        return cls

    @classmethod
    def get_by(cls, **kwargs):
        """Class Method, returns a single instance of the class
        by a single kwarg.  the keyword must be a descriptor of the
        class.
        example:

        .. code-block:: python

            bigBlue = Company.get_by(symbol='IBM')

        :Note:
            the keyword should map to an rdf predicate
            that is of type owl:InverseFunctional"""
        if len(kwargs) != 1:
            raise ValueError(
                "get_by wanted exactly 1 but got  %i args\n" +
                "Maybe you wanted filter_by" % (len(kwargs)))
        key, value = kwargs.items()[0]
        if isinstance(value, (URIRef, BNode, Literal)):
            o = value
        else:
            o = Literal(value)
        pred = cls._getdescriptor(key).pred
        uri = cls.db.value(None, pred, o)
        if uri:
            return cls(uri)
        else:
            raise LookupError("%s = %s not found" % (key, value))

    @classmethod
    def filter_by(cls, **kwargs):
        """Class method returns a generator over classs instances
        meeting the kwargs conditions.

        Each keyword must be a class descriptor

        filter by RDF.type == cls.rdf_type is implicit

        Order helps, the first keyword should be the most restrictive
        """
        filters = []
        for key, value in kwargs.items():
            pred = cls._getdescriptor(key).pred
            # try to make the value be OK for the triple query as an object
            if isinstance(value, Identifier):
                obj = value
            else:
                obj = Literal(value)
            filters.append((pred, obj))
        # make sure we filter by type
        if not (RDF.type, cls.rdf_type) in filters:
            filters.append((RDF.type, cls.rdf_type))
        pred, obj = filters[0]
        log.debug("Checking %s, %s" % (pred, obj))
        for sub in cls.db.subjects(pred, obj):
            log.debug("maybe %s" % sub)
            for pred, obj in filters[1:]:
                log.debug("Checking %s, %s" % (pred, obj))
                try:
                    cls.db.triples((sub, pred, obj)).next()
                except:
                    log.warn("No %s" % sub)
                    break
            else:
                yield cls(sub)

    @classmethod
    def ClassInstances(cls):
        """return a generator for instances of this rdf:type
        you can look in MyClass.rdf_type to see the predicate being used"""
        beenthere = set([])
        for i in cls.db.subjects(RDF.type, cls.rdf_type):
            if not i in beenthere:
                yield cls(i)
                beenthere.add(i)

    @classmethod
    def GetRandom(cls):
        """for develoment just returns a random instance of this class"""
        from random import choice
        xii = list(cls.ClassInstances())
        return choice(xii)

    def __hash__(self):
        return hash("ranD0Mi$h_" + self.n3())

    def __cmp__(self, other):
        if other is None:
            return False
        else:
            return cmp(self.n3(), other.n3())

    def __repr__(self):
        return """%s('%s')""" % (
            self.__class__.__name__, self.n3().encode('utf-8'))

    if rdflibversion.startswith('2'):
        def __str__(self):
            return str(self.resUri)

    def __getitem__(self, pred):
        log.debug("Getting with __getitem__ %s for %s" % (pred, self.n3()))
        val = self.db.value(self.resUri, pred)
        if isinstance(val, Literal):
            val = val.toPython()
        elif isinstance(val, (BNode, URIRef)):
            val = rdfSubject(val)
        return val

    def __delitem__(self, pred):
        log.debug("Deleting with __delitem__ %s for %s" % (pred, self))
        for s, p, o in self.db.triples((self.resUri, pred, None)):
            self.db.remove((s, p, o))
            # finally if the object in the triple was a bnode
            # cascade delete the thing it referenced
            # ?? FIXME Do we really want to cascade if it's an rdfSubject??
            if isinstance(o, (BNode, rdfSubject)):
                rdfSubject(o)._remove(db=self.db, cascade='bnode')

    def _set_with_dict(self, kv):
        """
        :param kv: a dict

          for each key,value pair in dict kv
               set self.key = value

        """
        for key, value in kv.items():
            descriptor = self.__class__._getdescriptor(key)
            descriptor.__set__(self, value)

    def _remove(
            self, db=None, cascade='bnode',
            bnodeCheck=True, objectCascade=False):
        """
        Remove all triples where this rdfSubject is the subject of the triple

        :param db: limit the remove operation to this graph
        :param cascade: must be one of:

            * none --  remove none
            * bnode -- (default) remove all unreferenced bnodes
            * all -- remove all unreferenced bnode(s) AND uri(s)

        :param bnodeCheck: boolean

            * True -- (default) check bnodes and raise exception if there are
              still references to this node
            * False --  do not check.  This can leave orphaned object reference
              in triples.  Use only if you are resetting the value in
              the same transaction
        :param objectCascade: boolean
            * False -- (default) do nothing
            * True -- delete also all triples where this refSubject is the
            object of the triple.
        """
        noderef = self.resUri
        log.debug("Called remove on %s" % self)
        if not db:
            db = self.db

        # we cannot delete a bnode if it is still referenced,
        # i.e. if it is the o of a s,p,o
        if bnodeCheck and isinstance(noderef, BNode):
            for s, p, o in db.triples((None, None, noderef)):
                raise RDFAlchemyError(
                    "Cannot delete BNode %s because %s still references it" % (
                    noderef.n3(), s.n3()))

        # determine an appropriate test for cascade decisions
        if cascade == 'bnode':
            # we cannot delete a bnode if there are still references to it
            def test(node):
                if isinstance(node, (URIRef, Literal)):
                    return False
                for s, p, o in db.triples((None, None, node)):
                    return False
                return True
        elif cascade == 'none':

            def f1(node):
                return False
            test = f1
        elif cascade == 'all':

            def f2(node):
                if isinstance(node, Literal):
                    return False
                for s, p, o in db.triples((None, None, node)):
                    return False
                return True
            test = f2
        else:
            raise AttributeError("unknown cascade argument")

        for s, p, o in db.triples((noderef, None, None)):
            db.remove((s, p, o))
            if test(o):
                rdfSubject(o)._remove(db=db, cascade=cascade)

        if objectCascade:
            for s, p, o in db.triples((None, None, noderef)):
                db.remove((s, p, o))

    def _rename(self, name, db=None):
        """rename a node """
        if not db:
            db = self.db
        if not (isinstance(name, (BNode, URIRef))):
            raise AttributeError("cannot rename to %s" % name)
        for s, p, o in db.triples((self.resUri, None, None)):
            db.remove((s, p, o))
            db.add((name, p, o))
        for s, p, o in db.triples((None, None, self.resUri)):
            db.set((s, p, name))
        self.resUri = name

    def _ppo(self, db=None):
        """Like pretty print...
        Return a 'pretty predicate,object' of self
        returning all predicate object pairs with qnames"""
        db = db or self.db
        for p, o in db.predicate_objects(self.resUri):
            print "%20s = %s" % (db.qname(p), str(o))
        print " "

    def md5_term_hash(self):
        """Not sure what good this method is but it's defined for
        rdflib.Identifiers so it's here for now"""
        return self.resUri.md5_term_hash()
Example #32
0
def exportRDFGraph(mi):
    g = ConjunctiveGraph()
    bnodes = {}
    for NSName, NSuriStr in mi.namespaceBindings.iteritems():
        g.namespace_manager.bind(NSName, URIRef(NSuriStr))

    modelAttrs = [model.__dict__[c] for c in model.__dict__.keys()]
    knownTypes = dict([(c.classURI, c) for c in modelAttrs
                       if hasattr(c, "classURI")])
    knownInstances = dict([(i.URI, i) for i in modelAttrs
                           if hasattr(i, "URI")])

    # Assign blind nodes :
    for s in mi.MainIdx.values():
        if s.URI == None or isBlind(s):
            snode = BNode()
            bnodes[s.URI] = snode
        for propName, propSet in s._props.iteritems():
            for v in propSet:
                if type(v) not in propSet.Lits and isBlind(v):
                    if not bnodes.has_key(v.URI):
                        vnode = BNode()
                        bnodes[v.URI] = vnode

    for s in mi.MainIdx.values():
        if not hasattr(s, "classURI") or s.classURI not in knownTypes.keys():
            raise ExportException(
                "Object " + str(s) +
                " has no classURI, or classURI is not known in the MO model.")
            # FIXME : Maybe use a Resource ?

        if s.URI == None or isBlind(s):
            snode = bnodes[s.URI]
        else:
            snode = URIRef(s.URI)

        g.add((snode, RDF.type, URIRef(s.classURI)))

        for propName, propSet in s._props.iteritems():
            for v in propSet:
                if not hasattr(propSet, "propertyURI"):
                    raise ExportException("Property " + str(propName) +
                                          " on object " + str(s) +
                                          " has no propertyURI !")

                if type(v) not in propSet.Lits:
                    if not hasattr(v, "URI"):
                        raise ExportException(
                            "Property value " + str(v) +
                            " is not a Literal, but has no URI !")
                    if isBlind(v):
                        g.add((snode, URIRef(propSet.propertyURI),
                               bnodes[v.URI]))
                    else:
                        g.add((snode, URIRef(propSet.propertyURI),
                               URIRef(v.URI)))
                else:
                    g.add((snode, URIRef(propSet.propertyURI), Literal(v)))

        info("Added " + str(type(s)) + " @ " + str(snode))

    return g
Example #33
0
def SipStrategy(query,
                sipCollection,
                factGraph,
                derivedPreds,
                bindings={},
                processedRules=None,
                network=None,
                debug=False,
                buildProof=False,
                memoizeMemory=None,
                proofLevel=1):
    """
    Accordingly, we define a sip-strategy for computing the answers to a query
    expressed using a set of Datalog rules, and a set of sips, one for each
    adornment of a rule head, as follows...

    Each evaluation uses memoization (via Python decorators) but also relies on well-formed
    rewrites for using semi-naive bottom up method over large SPARQL data.

    """
    memoizeMemory = memoizeMemory and memoizeMemory or {}
    queryLiteral = BuildUnitermFromTuple(query)
    processedRules = processedRules and processedRules or set()
    if bindings:
        # There are bindings.  Apply them to the terms in the query
        queryLiteral.ground(bindings)

    if debug:
        print("%sSolving" % ('\t' * proofLevel), queryLiteral, bindings)
    # Only consider ground triple pattern isomorphism with matching bindings
    goalRDFStatement = queryLiteral.toRDFTuple()

    if queryLiteral in memoizeMemory:
        if debug:
            print(
                "%sReturning previously calculated results for " %
                ('\t' * proofLevel), queryLiteral)
        for answers in memoizeMemory[queryLiteral]:
            yield answers
    elif AlphaNode(goalRDFStatement).alphaNetworkHash(
        True,
        skolemTerms=list(bindings.values())) in \
        [AlphaNode(r.toRDFTuple()).alphaNetworkHash(True,
                                                    skolemTerms=list(bindings.values()))
            for r in processedRules
         if AdornLiteral(goalRDFStatement).adornment ==
         r.adornment]:
        if debug:
            print("%s Goal already processed..." % ('\t' * proofLevel))
    else:
        isGround = literalIsGround(queryLiteral)
        if buildProof:
            ns = NodeSet(goalRDFStatement, network=network, identifier=BNode())
        else:
            ns = None
        # adornedProgram = factGraph.adornedProgram
        queryPred = GetOp(queryLiteral)
        if sipCollection is None:
            rules = []
        else:
            # For every rule head matching the query, we invoke the rule,
            # thus determining an adornment, and selecting a sip to follow
            rules = sipCollection.headToRule.get(queryPred, set())
            if None in sipCollection.headToRule:
                # If there are second order rules, we add them
                # since they are a 'wildcard'
                rules.update(sipCollection.headToRule[None])

        # maintained list of rules that haven't been processed before and
        # match the query
        validRules = []

        # each subquery contains values for the bound arguments that are passed
        # through the sip arcs entering the node corresponding to that literal. For
        # each subquery generated, there is a set of answers.
        answers = []

        # variableMapping = {}

        # Some TBox queries can be 'joined' together into SPARQL queries against
        # 'base' predicates via an RDF dataset
        # These atomic concept inclusion axioms can be evaluated together
        # using a disjunctive operator at the body of a horn clause
        # where each item is a query of the form uniPredicate(?X):
        # Or( uniPredicate1(?X1), uniPredicate2(?X), uniPredicate3(?X), ..)
        # In this way massive, conjunctive joins can be 'mediated'
        # between the stated facts and the top-down solver
        @parameterizedPredicate([i for i in derivedPreds])
        def IsAtomicInclusionAxiomRHS(rule, dPreds):
            """
            This is an atomic inclusion axiom with
            a variable (or bound) RHS:  uniPred(?ENTITY)
            """
            bodyList = list(iterCondition(rule.formula.body))
            body = first(bodyList)
            return GetOp(body) not in dPreds and \
                len(bodyList) == 1 and \
                body.op == RDF.type

        atomicInclusionAxioms = list(filter(IsAtomicInclusionAxiomRHS, rules))
        if atomicInclusionAxioms and len(atomicInclusionAxioms) > 1:
            if debug:
                print("\tCombining atomic inclusion axioms: ")
                pprint(atomicInclusionAxioms, sys.stderr)
            if buildProof:
                factStep = InferenceStep(ns, source='some RDF graph')
                ns.steps.append(factStep)

            axioms = [rule.formula.body for rule in atomicInclusionAxioms]

            # attempt to exaustively apply any available substitutions
            # and determine if query if fully ground
            vars = [
                v for v in GetArgs(queryLiteral, secondOrder=True)
                if isinstance(v, Variable)
            ]
            openVars, axioms, _bindings = \
                normalizeBindingsAndQuery(vars,
                                          bindings,
                                          axioms)
            if openVars:
                # mappings = {}
                # See if we need to do any variable mappings from the query literals
                # to the literals in the applicable rules
                query, rt = EDBQuery(axioms, factGraph, openVars,
                                     _bindings).evaluate(
                                         debug, symmAtomicInclusion=True)
                if buildProof:
                    # FIXME: subquery undefined
                    factStep.groundQuery = subquery
                for ans in rt:
                    if buildProof:
                        factStep.bindings.update(ans)
                    memoizeMemory.setdefault(queryLiteral, set()).add(
                        (prepMemiozedAns(ans), ns))
                    yield ans, ns
            else:
                # All the relevant derivations have been explored and the result
                # is a ground query we can directly execute against the facts
                if buildProof:
                    factStep.bindings.update(bindings)
                query, rt = EDBQuery(axioms, factGraph, _bindings).evaluate(
                    debug, symmAtomicInclusion=True)
                if buildProof:
                    # FIXME: subquery undefined
                    factStep.groundQuery = subquery
                memoizeMemory.setdefault(queryLiteral, set()).add(
                    (prepMemiozedAns(rt), ns))
                yield rt, ns
            rules = filter(lambda i: not IsAtomicInclusionAxiomRHS(i), rules)
        for rule in rules:
            # An exception is the special predicate ph; it is treated as a base
            # predicate and the tuples in it are those supplied for qb by
            # unification.
            headBindings = getBindingsFromLiteral(goalRDFStatement,
                                                  rule.formula.head)
            # comboBindings = dict([(k, v) for k, v in itertools.chain(
            #                                           bindings.items(),
            #                                           headBindings.items())])
            varMap = rule.formula.head.getVarMapping(queryLiteral)
            if headBindings and\
                [term for term in rule.formula.head.getDistinguishedVariables(True)
                 if varMap.get(term, term) not in headBindings]:
                continue
            # subQueryAnswers = []
            # dontStop = True
            # projectedBindings = comboBindings.copy()
            if debug:
                print("%sProcessing rule" % ('\t' * proofLevel), rule.formula)
                if debug and sipCollection:
                    print("Sideways Information Passing (sip) graph for %s: " %
                          queryLiteral)
                    print(sipCollection.serialize(format='n3'))
                    for sip in SIPRepresentation(sipCollection):
                        print(sip)
            try:
                # Invoke the rule
                if buildProof:
                    step = InferenceStep(ns, rule.formula)
                else:
                    step = None
                for rt, step in\
                    invokeRule([headBindings],
                               iter(iterCondition(rule.formula.body)),
                               rule.sip,
                               (proofLevel + 1,
                                memoizeMemory,
                                sipCollection,
                                factGraph,
                                derivedPreds,
                                processedRules.union([
                                    AdornLiteral(query)])),
                               step=step,
                               debug=debug):
                    if rt:
                        if isinstance(rt, dict):
                            # We received a mapping and must rewrite it via
                            # correlation between the variables in the rule head
                            # and the variables in the original query (after applying
                            # bindings)
                            varMap = rule.formula.head.getVarMapping(
                                queryLiteral)
                            if varMap:
                                rt = MakeImmutableDict(
                                    refactorMapping(varMap, rt))
                            if buildProof:
                                step.bindings = rt
                        else:
                            if buildProof:
                                step.bindings = headBindings
                        validRules.append(rule)
                        if buildProof:
                            ns.steps.append(step)
                        if isGround:
                            yield True, ns
                        else:
                            memoizeMemory.setdefault(queryLiteral, set()).add(
                                (prepMemiozedAns(rt), ns))
                            yield rt, ns

            except RuleFailure:
                # Clean up failed antecedents
                if buildProof:
                    if ns in step.antecedents:
                        step.antecedents.remove(ns)
        if not validRules:
            # No rules matching, query factGraph for answers
            successful = False
            if buildProof:
                factStep = InferenceStep(ns, source='some RDF graph')
                ns.steps.append(factStep)
            if not isGround:
                subquery, rt = EDBQuery([queryLiteral], factGraph, [
                    v for v in GetArgs(queryLiteral, secondOrder=True)
                    if isinstance(v, Variable)
                ], bindings).evaluate(debug)
                if buildProof:
                    factStep.groundQuery = subquery
                for ans in rt:
                    successful = True
                    if buildProof:
                        factStep.bindings.update(ans)
                    memoizeMemory.setdefault(queryLiteral, set()).add(
                        (prepMemiozedAns(ans), ns))
                    yield ans, ns
                if not successful and queryPred not in derivedPreds:
                    # Open query didn't return any results and the predicate
                    # is ostensibly marked as derived predicate, so we have
                    # failed
                    memoizeMemory.setdefault(queryLiteral, set()).add(
                        (False, ns))
                    yield False, ns
            else:
                # All the relevant derivations have been explored and the result
                # is a ground query we can directly execute against the facts
                if buildProof:
                    factStep.bindings.update(bindings)

                subquery, rt = EDBQuery([queryLiteral], factGraph,
                                        bindings).evaluate(debug)
                if buildProof:
                    factStep.groundQuery = subquery
                memoizeMemory.setdefault(queryLiteral, set()).add(
                    (prepMemiozedAns(rt), ns))
                yield rt, ns
Example #34
0
    def test_n3_store(self):
        # Thorough test suite for formula-aware store

        implies = URIRef("http://www.w3.org/2000/10/swap/log#implies")
        testN3 = """
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://test/> .
{:a :b :c;a :foo} => {:a :d :c,?y}.
_:foo a rdfs:Class.
:a :d :c."""

        g = self.open_graph()
        g.parse(data=testN3, format="n3")

        formulaA = BNode()
        formulaB = BNode()
        for s,o in g.subject_objects(predicate=implies):
            formulaA = s
            formulaB = o
        assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph

        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')
        
        universe = ConjunctiveGraph(g.store)
        
        # test formula as terms
        assert len(list(universe.triples((formulaA, implies, formulaB)))) == 1
        
        # test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None,None,v)))) == 1
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                assert isinstance(o, Variable)
                assert o == v

        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s, BNode)
        assert len(list(universe.triples((None,implies,None)))) == 1
        assert len(list(universe.triples((None,RDF.type,None)))) == 1

        assert len(list(formulaA.triples((None,RDF.type,None)))) == 1
        assert len(list(formulaA.triples((None,None,None)))) == 2

        assert len(list(formulaB.triples((None,None,None)))) == 2
        assert len(list(formulaB.triples((None,d,None)))) == 2

        assert len(list(universe.triples((None,None,None)))) == 3 
        assert len(list(universe.triples((None,d,None)))) == 1
        
        # context tests
        # test contexts with triple argument
        assert len(list(universe.contexts((a,d,c))))==1
        
        # remove test cases
        universe.remove((None,implies,None))
        assert len(list(universe.triples((None,implies,None)))) == 0
        assert len(list(formulaA.triples((None,None,None)))) == 2
        assert len(list(formulaB.triples((None,None,None)))) == 2
        
        formulaA.remove((None,b,None))
        assert len(list(formulaA.triples((None,None,None)))) == 1

        formulaA.remove((None,RDF.type,None))
        assert len(list(formulaA.triples((None,None,None)))) == 0
        
        universe.remove((None,RDF.type,RDFS.Class))
        
        # remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None,RDF.type,None)))) == 0
        assert len(universe) == 1
        assert len(formulaB) == 0
        
        universe.remove((None,None,None))
        assert len(universe) == 0
Example #35
0
    def visit_class(self, cls: ClassDefinition) -> bool:
        cls_uri = self._class_uri(cls.name)
        self.graph.add((cls_uri, RDF.type, OWL.Class))
        self.graph.add((cls_uri, RDF.type,
                        self.metamodel.namespaces[METAMODEL_NAMESPACE_NAME][
                            camelcase('class definition')]))
        self._add_element_properties(cls_uri, cls)

        # Parent classes
        # TODO: reintroduce this
        # if not cls.defining_slots:
        if True:
            if cls.is_a:
                self.graph.add(
                    (cls_uri, RDFS.subClassOf, self._class_uri(cls.is_a)))
            if cls.mixin:
                self.graph.add(
                    (cls_uri, RDFS.subClassOf, METAMODEL_NAMESPACE.mixin))
            for mixin in sorted(cls.mixins):
                self.graph.add(
                    (cls_uri, RDFS.subClassOf, self._class_uri(mixin)))
            if cls.name in self.synopsis.applytorefs:
                for appl in sorted(
                        self.synopsis.applytorefs[cls.name].classrefs):
                    self.graph.add(
                        (cls_uri, RDFS.subClassOf, self._class_uri(appl)))
        else:
            raise NotImplementedError("Defining slots need to be implemented")
            # If defining slots, we generate an equivalentClass entry
            # equ_node = BNode()
            # self.graph.add((cls_uri, OWL.equivalentClass, equ_node))
            # self.graph.add((equ_node, RDF.type, OWL.Class))
            #
            # elts = []
            # if cls.is_a:
            #     elts.append(self._class_uri(cls.is_a))
            # if cls.mixin:
            #     self.graph.add((cls_uri, RDFS.subClassOf, META_NS.mixin))
            # for mixin in cls.mixins:
            #     self.graph.add((cls_uri, RDFS.subClassOf, self._class_uri(mixin)))
            # if cls.name in self.synopsis.applytorefs:
            #     for appl in self.synopsis.applytorefs[cls.name].classrefs:
            #         self.graph.add((cls_uri, RDFS.subClassOf, self._class_uri(appl)))
            #
            # for slotname in cls.defining_slots:
            #     restr_node = BNode()
            #     slot = self.schema.slots[slotname]
            #
            #     self.graph.add((restr_node, RDF.type, OWL.Restriction))
            #     self.graph.add((restr_node, OWL.onProperty, self._prop_uri(slotname)))
            #     self._add_cardinality(restr_node, slot)
            #     # TODO: fix this
            #     # self.graph.add((restr_node, OWL.someValuesFrom, self._build_range(slot)))
            #     elts.append(restr_node)
            #
            # coll_bnode = BNode()
            # Collection(self.graph, coll_bnode, elts)
            # self.graph.add((equ_node, OWL.intersectionOf, coll_bnode))

        # TODO: see whether unions belong
        # if cls.union_of:
        #     union_node = BNode()
        #     Collection(self.graph, union_coll, [self.class_uri(union_node) for union_node in cls.union_of])
        #     self.graph.add((union_node, OWL.unionOf, union_coll))
        #     self.graph.add((cls_uri, RDFS.subClassOf, union_node))

        for sn in sorted(self.own_slot_names(cls)):
            # Defining_slots are covered above
            if sn not in cls.defining_slots:
                slot = self.schema.slots[sn]
                # Non-inherited slots are annotation properties
                if self.is_slot_object_property(slot):
                    slot_node = BNode()
                    self.graph.add((cls_uri, RDFS.subClassOf, slot_node))

                    #         required multivalued
                    if slot.required:
                        if slot.multivalued:
                            #    y         y     intersectionOf(restriction(slot only type) restriction(slot some type)
                            restr1 = BNode()
                            self.graph.add((restr1, RDF.type, OWL.Restriction))
                            self.graph.add((restr1, OWL.allValuesFrom,
                                            self._range_uri(slot)))
                            self.graph.add(
                                (restr1, OWL.onProperty,
                                 self._prop_uri(self.aliased_slot_name(slot))))

                            restr2 = BNode()
                            self.graph.add((restr2, RDF.type, OWL.Restriction))
                            self.graph.add((restr2, OWL.someValuesFrom,
                                            self._range_uri(slot)))
                            self.graph.add(
                                (restr2, OWL.onProperty,
                                 self._prop_uri(self.aliased_slot_name(slot))))

                            coll_bnode = BNode()
                            Collection(self.graph, coll_bnode,
                                       [restr1, restr2])
                            self.graph.add(
                                (slot_node, OWL.intersectionOf, coll_bnode))
                            self.graph.add((slot_node, RDF.type, OWL.Class))
                        else:
                            #    y         n      restriction(slot exactly 1 type)
                            self.graph.add(
                                (slot_node, RDF.type, OWL.Restriction))
                            self.graph.add(
                                (slot_node, OWL.qualifiedCardinality,
                                 Literal(1)))
                            self.graph.add(
                                (slot_node, OWL.onProperty,
                                 self._prop_uri(self.aliased_slot_name(slot))))
                            self.graph.add((slot_node, OWL.onClass,
                                            self._range_uri(slot)))
                    else:
                        if slot.multivalued:
                            #    n         y      restriction(slot only type)
                            self.graph.add(
                                (slot_node, RDF.type, OWL.Restriction))
                            self.graph.add((slot_node, OWL.allValuesFrom,
                                            self._range_uri(slot)))
                            self.graph.add(
                                (slot_node, OWL.onProperty,
                                 self._prop_uri(self.aliased_slot_name(slot))))
                        else:
                            #    n         n      intersectionOf(restriction(slot only type) restriction(slot max 1 type))
                            self.graph.add(
                                (slot_node, RDF.type, OWL.Restriction))
                            self.graph.add((slot_node, OWL.onClass,
                                            self._range_uri(slot)))
                            self.graph.add(
                                (slot_node, OWL.maxQualifiedCardinality,
                                 Literal(1)))
                            self.graph.add(
                                (slot_node, OWL.onProperty,
                                 self._prop_uri(self.aliased_slot_name(slot))))

        return True
Example #36
0
def _parse_1_0(node, graph, parent_object, incoming_state, parent_incomplete_triples) :
	"""The (recursive) step of handling a single node. See the
	U{RDFa 1.0 syntax document<http://www.w3.org/TR/rdfa-syntax>} for further details.
	
	This is the RDFa 1.0 version.

	@param node: the DOM node to handle
	@param graph: the RDF graph
	@type graph: RDFLib's Graph object instance
	@param parent_object: the parent's object, as an RDFLib URIRef
	@param incoming_state: the inherited state (namespaces, lang, etc.)
	@type incoming_state: L{state.ExecutionContext}
	@param parent_incomplete_triples: list of hanging triples (the missing resource set to None) to be handled (or not)
	by the current node.
	@return: whether the caller has to complete it's parent's incomplete triples
	@rtype: Boolean
	"""

	# Update the state. This means, for example, the possible local settings of
	# namespaces and lang
	state = None
	state = ExecutionContext(node, graph, inherited_state=incoming_state)

	#---------------------------------------------------------------------------------
	# Handling the role attribute is pretty much orthogonal to everything else...
	handle_role_attribute(node, graph, state)

	#---------------------------------------------------------------------------------
	# Handle the special case for embedded RDF, eg, in SVG1.2. 
	# This may add some triples to the target graph that does not originate from RDFa parsing
	# If the function return TRUE, that means that an rdf:RDF has been found. No
	# RDFa parsing should be done on that subtree, so we simply return...
	if state.options.embedded_rdf and node.nodeType == node.ELEMENT_NODE and handle_embeddedRDF(node, graph, state) : 
		return	

	#---------------------------------------------------------------------------------
	# calling the host language specific massaging of the DOM
	if state.options.host_language in host_dom_transforms and node.nodeType == node.ELEMENT_NODE :
		for func in host_dom_transforms[state.options.host_language] : func(node, state)

	#---------------------------------------------------------------------------------
	# First, let us check whether there is anything to do at all. Ie,
	# whether there is any relevant RDFa specific attribute on the element
	#
	if not has_one_of_attributes(node, "href", "resource", "about", "property", "rel", "rev", "typeof", "src") :
		# nop, there is nothing to do here, just go down the tree and return...
		for n in node.childNodes :
			if n.nodeType == node.ELEMENT_NODE : parse_one_node(n, graph, parent_object, state, parent_incomplete_triples)
		return

	#-----------------------------------------------------------------
	# The goal is to establish the subject and object for local processing
	# The behaviour is slightly different depending on the presense or not
	# of the @rel/@rev attributes
	current_subject = None
	current_object  = None
	prop_object		= None

	if has_one_of_attributes(node, "rel", "rev")  :
		# in this case there is the notion of 'left' and 'right' of @rel/@rev
		# in establishing the new Subject and the objectResource
		current_subject = state.getResource("about","src")
			
		# get_URI may return None in case of an illegal CURIE, so
		# we have to be careful here, not use only an 'else'
		if current_subject == None :
			if node.hasAttribute("typeof") :
				current_subject = BNode()
			else :
				current_subject = parent_object
		else :
			state.reset_list_mapping(origin = current_subject)
		
		# set the object resource
		current_object = state.getResource("resource", "href")
		
	else :
		# in this case all the various 'resource' setting attributes
		# behave identically, though they also have their own priority
		current_subject = state.getResource("about", "src", "resource", "href")
		
		# get_URI_ref may return None in case of an illegal CURIE, so
		# we have to be careful here, not use only an 'else'
		if current_subject == None :
			if node.hasAttribute("typeof") :
				current_subject = BNode()
			else :
				current_subject = parent_object
			current_subject = parent_object
		else :
			state.reset_list_mapping(origin = current_subject)

		# in this case no non-literal triples will be generated, so the
		# only role of the current_object Resource is to be transferred to
		# the children node
		current_object = current_subject
		
	# ---------------------------------------------------------------------
	## The possible typeof indicates a number of type statements on the new Subject
	for defined_type in state.getURI("typeof") :
		graph.add((current_subject, ns_rdf["type"], defined_type))

	# ---------------------------------------------------------------------
	# In case of @rel/@rev, either triples or incomplete triples are generated
	# the (possible) incomplete triples are collected, to be forwarded to the children
	incomplete_triples  = []
	for prop in state.getURI("rel") :
		if not isinstance(prop,BNode) :
			theTriple = (current_subject, prop, current_object)
			if current_object != None :
				graph.add(theTriple)
			else :
				incomplete_triples.append(theTriple)
		else :
			state.options.add_warning(err_no_blank_node % "rel", warning_type=IncorrectBlankNodeUsage, node=node.nodeName)

	for prop in state.getURI("rev") :
		if not isinstance(prop,BNode) :
			theTriple = (current_object,prop,current_subject)
			if current_object != None :
				graph.add(theTriple)
			else :
				incomplete_triples.append(theTriple)
		else :
			state.options.add_warning(err_no_blank_node % "rev", warning_type=IncorrectBlankNodeUsage, node=node.nodeName)

	# ----------------------------------------------------------------------
	# Generation of the literal values. The newSubject is the subject
	# A particularity of property is that it stops the parsing down the DOM tree if an XML Literal is generated,
	# because everything down there is part of the generated literal. 
	if node.hasAttribute("property") :
		ProcessProperty(node, graph, current_subject, state).generate_1_0()

	# ----------------------------------------------------------------------
	# Setting the current object to a bnode is setting up a possible resource
	# for the incomplete triples downwards
	if current_object == None :
		object_to_children = BNode()
	else :
		object_to_children = current_object

	#-----------------------------------------------------------------------
	# Here is the recursion step for all the children
	for n in node.childNodes :
		if n.nodeType == node.ELEMENT_NODE : 
			_parse_1_0(n, graph, object_to_children, state, incomplete_triples)

	# ---------------------------------------------------------------------
	# At this point, the parent's incomplete triples may be completed
	for (s,p,o) in parent_incomplete_triples :
		if s == None and o == None :
			# This is an encoded version of a hanging rel for a collection:
			incoming_state.add_to_list_mapping( p, current_subject )
		else :
			if s == None : s = current_subject
			if o == None : o = current_subject
			graph.add((s,p,o))

	# -------------------------------------------------------------------
	# This should be it...
	# -------------------------------------------------------------------
	return
Example #37
0
def _parse_1_1(node, graph, parent_object, incoming_state, parent_incomplete_triples) :
	"""The (recursive) step of handling a single node. See the
	U{RDFa 1.1 Core document<http://www.w3.org/TR/rdfa-core/>} for further details.
	
	This is the RDFa 1.1 version.

	@param node: the DOM node to handle
	@param graph: the RDF graph
	@type graph: RDFLib's Graph object instance
	@param parent_object: the parent's object, as an RDFLib URIRef
	@param incoming_state: the inherited state (namespaces, lang, etc.)
	@type incoming_state: L{state.ExecutionContext}
	@param parent_incomplete_triples: list of hanging triples (the missing resource set to None) to be handled (or not)
	by the current node.
	@return: whether the caller has to complete it's parent's incomplete triples
	@rtype: Boolean
	"""
	def header_check(p_obj) :
		"""Special disposition for the HTML <head> and <body> elements..."""
		if state.options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ] :
			if node.nodeName == "head" or node.nodeName == "body" :
				if not has_one_of_attributes(node, "about", "resource", "src", "href") :
					return p_obj
		else :
			return None

	def lite_check() :
		if state.options.check_lite and state.options.host_language in [ HostLanguage.html5, HostLanguage.xhtml5, HostLanguage.xhtml ] :
			if node.tagName == "link" and node.hasAttribute("rel") and state.term_or_curie.CURIE_to_URI(node.getAttribute("rel")) != None :
				state.options.add_warning("In RDFa Lite, attribute @rel in <link> is only used in non-RDFa way (consider using @property)", node=node)

	# Update the state. This means, for example, the possible local settings of
	# namespaces and lang
	state = None
	state = ExecutionContext(node, graph, inherited_state=incoming_state)

	#---------------------------------------------------------------------------------
	# Extra warning check on RDFa Lite
	lite_check()
	
	#---------------------------------------------------------------------------------
	# Handling the role attribute is pretty much orthogonal to everything else...
	handle_role_attribute(node, graph, state)

	#---------------------------------------------------------------------------------
	# Handle the special case for embedded RDF, eg, in SVG1.2. 
	# This may add some triples to the target graph that does not originate from RDFa parsing
	# If the function return TRUE, that means that an rdf:RDF has been found. No
	# RDFa parsing should be done on that subtree, so we simply return...
	if state.options.embedded_rdf and node.nodeType == node.ELEMENT_NODE and handle_embeddedRDF(node, graph, state) : 
		return	

	#---------------------------------------------------------------------------------
	# calling the host language specific massaging of the DOM
	if state.options.host_language in host_dom_transforms and node.nodeType == node.ELEMENT_NODE :
		for func in host_dom_transforms[state.options.host_language] : func(node, state)

	#---------------------------------------------------------------------------------
	# First, let us check whether there is anything to do at all. Ie,
	# whether there is any relevant RDFa specific attribute on the element
	#
	if not has_one_of_attributes(node, "href", "resource", "about", "property", "rel", "rev", "typeof", "src", "vocab", "prefix") :
		# nop, there is nothing to do here, just go down the tree and return...
		for n in node.childNodes :
			if n.nodeType == node.ELEMENT_NODE : parse_one_node(n, graph, parent_object, state, parent_incomplete_triples)
		return

	#-----------------------------------------------------------------
	# The goal is to establish the subject and object for local processing
	# The behaviour is slightly different depending on the presense or not
	# of the @rel/@rev attributes
	current_subject = None
	current_object  = None
	typed_resource	= None
	
	if has_one_of_attributes(node, "rel", "rev")  :
		# in this case there is the notion of 'left' and 'right' of @rel/@rev
		# in establishing the new Subject and the objectResource
		current_subject = header_check(parent_object)

		# set first the subject
		if node.hasAttribute("about") :
			current_subject = state.getURI("about")
			if node.hasAttribute("typeof") : typed_resource = current_subject
			
		# get_URI may return None in case of an illegal CURIE, so
		# we have to be careful here, not use only an 'else'
		if current_subject == None :
			current_subject = parent_object
		else :
			state.reset_list_mapping(origin = current_subject)
		
		# set the object resource
		current_object = state.getResource("resource", "href", "src")
			
		if node.hasAttribute("typeof") and not node.hasAttribute("about") :
			if current_object == None :
				current_object = BNode()
			typed_resource = current_object
		
		if not node.hasAttribute("inlist") and current_object != None :
			# In this case the newly defined object is, in fact, the head of the list
			# just reset the whole thing.
			state.reset_list_mapping(origin = current_object)

	elif  node.hasAttribute("property") and not has_one_of_attributes(node, "content", "datatype") :
		current_subject = header_check(parent_object)

		# this is the case when the property may take hold of @src and friends...
		if node.hasAttribute("about") :
			current_subject = state.getURI("about")
			if node.hasAttribute("typeof") : typed_resource = current_subject

		# getURI may return None in case of an illegal CURIE, so
		# we have to be careful here, not use only an 'else'
		if current_subject == None :
			current_subject = parent_object
		else :
			state.reset_list_mapping(origin = current_subject)

		if typed_resource == None and node.hasAttribute("typeof") :
			typed_resource = state.getResource("resource", "href", "src")
			if typed_resource == None :
				typed_resource = BNode()
			current_object = typed_resource
		else :
			current_object = current_subject
			
	else :
		current_subject = header_check(parent_object)

		# in this case all the various 'resource' setting attributes
		# behave identically, though they also have their own priority
		if current_subject == None :
			current_subject = state.getResource("about", "resource", "href", "src")
			
		# get_URI_ref may return None in case of an illegal CURIE, so
		# we have to be careful here, not use only an 'else'
		if current_subject == None :
			if node.hasAttribute("typeof") :
				current_subject = BNode()
				state.reset_list_mapping(origin = current_subject)
			else :
				current_subject = parent_object
		else :
			state.reset_list_mapping(origin = current_subject)

		# in this case no non-literal triples will be generated, so the
		# only role of the current_object Resource is to be transferred to
		# the children node
		current_object = current_subject
		if node.hasAttribute("typeof") : typed_resource = current_subject
		
	# ---------------------------------------------------------------------
	## The possible typeof indicates a number of type statements on the typed resource
	for defined_type in state.getURI("typeof") :
		if typed_resource :
			graph.add((typed_resource, ns_rdf["type"], defined_type))

	# ---------------------------------------------------------------------
	# In case of @rel/@rev, either triples or incomplete triples are generated
	# the (possible) incomplete triples are collected, to be forwarded to the children
	incomplete_triples  = []
	for prop in state.getURI("rel") :
		if not isinstance(prop,BNode) :
			if node.hasAttribute("inlist") :
				if current_object != None :
					# Add the content to the list. Note that if the same list
					# was initialized, at some point, by a None, it will be
					# overwritten by this real content
					state.add_to_list_mapping(prop, current_object)
				else :
					# Add a dummy entry to the list... Note that
					# if that list was initialized already with a real content
					# this call will have no effect
					state.add_to_list_mapping(prop, None)
					
					# Add a placeholder into the hanging rels
					incomplete_triples.append( (None, prop, None) )
			else :
				theTriple = (current_subject, prop, current_object)
				if current_object != None :
					graph.add(theTriple)
				else :
					incomplete_triples.append(theTriple)
		else :
			state.options.add_warning(err_no_blank_node % "rel", warning_type=IncorrectBlankNodeUsage, node=node.nodeName)

	for prop in state.getURI("rev") :
		if not isinstance(prop,BNode) :
			theTriple = (current_object,prop,current_subject)
			if current_object != None :
				graph.add(theTriple)
			else :
				incomplete_triples.append(theTriple)
		else :
			state.options.add_warning(err_no_blank_node % "rev", warning_type=IncorrectBlankNodeUsage, node=node.nodeName)

	# ----------------------------------------------------------------------
	# Generation of the @property values, including literals. The newSubject is the subject
	# A particularity of property is that it stops the parsing down the DOM tree if an XML Literal is generated,
	# because everything down there is part of the generated literal. 
	if node.hasAttribute("property") :
		ProcessProperty(node, graph, current_subject, state, typed_resource).generate_1_1()

	# ----------------------------------------------------------------------
	# Setting the current object to a bnode is setting up a possible resource
	# for the incomplete triples downwards
	if current_object == None :
		object_to_children = BNode()
	else :
		object_to_children = current_object

	#-----------------------------------------------------------------------
	# Here is the recursion step for all the children
	for n in node.childNodes :
		if n.nodeType == node.ELEMENT_NODE : 
			_parse_1_1(n, graph, object_to_children, state, incomplete_triples)

	# ---------------------------------------------------------------------
	# At this point, the parent's incomplete triples may be completed
	for (s,p,o) in parent_incomplete_triples :
		if s == None and o == None :
			# This is an encoded version of a hanging rel for a collection:
			incoming_state.add_to_list_mapping( p, current_subject )
		else :
			if s == None : s = current_subject
			if o == None : o = current_subject
			graph.add((s,p,o))

	# Generate the lists, if any and if this is the level where a new list was originally created	
	if state.new_list and not state.list_empty() :
		for prop in state.get_list_props() :
			vals  = state.get_list_value(prop)
			if vals == None :
				# This was an empty list, in fact, ie, the list has been initiated by a <xxx rel="prop" inlist>
				# but no list content has ever been added
				graph.add( (state.get_list_origin(), prop, ns_rdf["nil"]) )
			else :
				heads = [ BNode() for r in vals ] + [ ns_rdf["nil"] ]
				for i in range(0, len(vals)) :
					graph.add( (heads[i], ns_rdf["first"], vals[i]) )
					graph.add( (heads[i], ns_rdf["rest"],  heads[i+1]) )
				# Anchor the list
				graph.add( (state.get_list_origin(), prop, heads[0]) )

	# -------------------------------------------------------------------
	# This should be it...
	# -------------------------------------------------------------------
	return
Example #38
0
def testN3Store(store="default", configString=None):
    storetest = True
    g = ConjunctiveGraph(store=store)
    if configString is not None:
        g.destroy(configString)
        g.open(configString, create=True)
    g.parse(data=testN3, format="n3")
    # op = g.serialize(format="n3")
    # print(op)
    formulaA = BNode()
    formulaB = BNode()
    try:
        for s,p,o in g.triples((None,implies,None)):
            formulaA = s
            formulaB = o
        
        assert type(formulaA)==QuotedGraph and type(formulaB)==QuotedGraph
        a = URIRef('http://test/a')
        b = URIRef('http://test/b')
        c = URIRef('http://test/c')
        d = URIRef('http://test/d')
        v = Variable('y')
        
        universe = ConjunctiveGraph(g.store)
        
        #test formula as terms
        assert len(list(universe.triples((formulaA,implies,formulaB))))==1
        
        #test variable as term and variable roundtrip
        assert len(list(formulaB.triples((None,None,v))))==1
        for s,p,o in formulaB.triples((None,d,None)):
            if o != c:
                assert isinstance(o,Variable)
                assert o == v
        s = list(universe.subjects(RDF.type, RDFS.Class))[0]
        assert isinstance(s,BNode)
        assert len(list(universe.triples((None,implies,None)))) == 1
        assert len(list(universe.triples((None,RDF.type,None)))) ==1
        assert len(list(formulaA.triples((None,RDF.type,None))))==1
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2
        assert len(list(universe.triples((None,None,None))))==3
        assert len(list(formulaB.triples((None,URIRef('http://test/d'),None))))==2
        assert len(list(universe.triples((None,URIRef('http://test/d'),None))))==1
        
        #context tests
        #test contexts with triple argument
        assert len(list(universe.contexts((a,d,c))))==1
        
        #Remove test cases
        universe.remove((None,implies,None))
        assert len(list(universe.triples((None,implies,None))))==0
        assert len(list(formulaA.triples((None,None,None))))==2
        assert len(list(formulaB.triples((None,None,None))))==2
        
        formulaA.remove((None,b,None))
        assert len(list(formulaA.triples((None,None,None))))==1
        formulaA.remove((None,RDF.type,None))
        assert len(list(formulaA.triples((None,None,None))))==0
        
        universe.remove((None,RDF.type,RDFS.Class))
        
        #remove_context tests
        universe.remove_context(formulaB)
        assert len(list(universe.triples((None,RDF.type,None))))==0
        assert len(universe)==1
        assert len(formulaB)==0
        
        universe.remove((None,None,None))
        assert len(universe)==0
        
        g.store.destroy(configString)
    except:
        g.store.destroy(configString)
        raise
Example #39
0
    def dcterms(self, context, g):
        """Return a set of tuples covering DC metadata"""

        curl = context.absolute_url()
        vh_root = context.REQUEST.environ.get('VH_ROOT')
        if vh_root:
            curl = curl.replace(vh_root, '')
        subj = URIRef(curl)

        # Title, Description, and Modification Date
        g.add((subj, DCTERMS['title'], Literal(context.Title())))
        g.add((subj, DCTERMS['description'], Literal(context.Description())))
        g.add((subj, DCTERMS['modified'], Literal(context.ModificationDate())))

        # Tags
        for tag in context.Subject():
            g.add((subj, DCTERMS['subject'], Literal(tag)))

        orig_url = str(subj).replace('https://', 'http://')
        if orig_url and orig_url != str(subj):
            g.add((subj, OWL['sameAs'], URIRef(orig_url)))

        # Authors
        creators, contributors = principals(context)

        for principal in creators:
            p = user_info(context, principal)
            url = p.get('url')
            opnode = None
            if not url:
                if principal in self.authority:
                    username, url = self.authority.get(principal)
                    if username and not url:
                        url = "https://pleiades.stoa.org/author/" + username
            if url:
                old_url = url.replace('https://', 'http://')
                pnode = URIRef(url)
                opnode = URIRef(old_url)
            else:
                pnode = BNode()
                opnode = None
            g.add((subj, DCTERMS['creator'], pnode))
            if not url and p.get('fullname'):
                g.add((pnode, RDF.type, FOAF['Person']))
                g.add((pnode, FOAF['name'], Literal(p.get('fullname'))))
                if opnode:
                    g.add((pnode, OWL['sameAs'], opnode))

        for principal in contributors:
            p = user_info(context, principal)
            url = p.get('url')
            opnode = None
            if not url:
                if principal in self.authority:
                    username, url = self.authority.get(principal)
                    if username and not url:
                        url = "https://pleiades.stoa.org/author/" + username
            if url:
                old_url = url.replace('https://', 'http://')
                pnode = URIRef(url)
                opnode = URIRef(old_url)
            else:
                pnode = BNode()
                opnode = None
            g.add((subj, DCTERMS['contributor'], pnode))
            if not url and p.get('fullname'):
                g.add((pnode, RDF.type, FOAF['Person']))
                g.add((pnode, FOAF['name'], Literal(p.get('fullname'))))
                if opnode:
                    g.add((pnode, OWL['sameAs'], opnode))

        return g