コード例 #1
0
ファイル: dcat.py プロジェクト: zeenatcan/ckanext-canada
    def graph_from_dataset(self, dataset_dict, dataset_ref):
        g = self.g

        _add = partial(_smart_add, g, dataset_dict, dataset_ref)

        g.bind('dct', DCT)
        g.bind('dcat', DCAT)
        g.bind('vcard', VCARD)

        g.add((dataset_ref, RDF.type, DCAT.Dataset))

        _add(DCT.title, 'title')
        _add(DCT.description, 'notes')
        _add(DCT.issued, 'date_published')
        _add(DCT.modified, 'metadata_modified')
        _add(DCT.publisher, 'org_title_at_publication')
        _add(DCT.accuralPeriodicity, 'frequency', 'canada_frequency')
        _add(DCT.identifier, 'id')
        _add(DCT.spatial, 'spatial')
        # This *should* be the period (from start to finish) that the dataset
        # covers, however we don't seem to have a field for this.
        # _add(DCT.temporal, '')

        g.add((dataset_ref, DCT.language, Literal('en')))
        g.add((dataset_ref, DCT.language, Literal('fr')))

        _add(DCAT.theme, 'subject', 'canada_subject')
        _add(DCAT.keyword, 'keywords')
        g.add((dataset_ref, DCAT.landing_page,
               Literal(
                   url_for(controller='package',
                           action='read',
                           id=dataset_dict['id'],
                           qualified=True))))

        contact_details = BNode()
        g.add((contact_details, RDF.type, VCARD.organization))
        g.add((dataset_ref, DCAT.contact_point, contact_details))

        g.add((contact_details, VCARD.hasEmail,
               Literal(dataset_dict['maintainer_email'])))

        for resource_dict in dataset_dict.get('resources', []):
            resource = URIRef(
                url_for(controller='package',
                        action='resource_read',
                        id=resource_dict['id'],
                        qualified=True))

            _r_add = partial(_smart_add, g, resource_dict, resource)

            g.add((dataset_ref, DCAT.distrubtion, resource))
            g.add((resource, RDF.type, DCAT.Distribution))

            _r_add(DCT.title, 'name')
            _r_add(DCT.description, 'description')
            _r_add(DCT.issued, 'created')
            _r_add(DCT.modified, 'last_modified')
            _r_add(DCAT.media_type, 'mimetype')
            _r_add(DCT['format'], 'format')
            _r_add(DCAT.access_url, 'url')
            _smart_add(g, dataset_dict, resource, DCT.license, 'license_title')
コード例 #2
0
# lastName = URIRef("http://example.org/lastName/" + lastName)
# scopusID = URIRef("http://example.org/scopusID/" + scopusID)
# pureID = URIRef("http://example.org/pureID/" + pureID)


title = Literal(title)
firstName = Literal(firstName)
lastName = Literal(lastName)
scopusID = Literal(scopusID)
pureID = Literal(pureID)

hasScopusID = URIRef("http://example.org/hasScopusID")
hasPureID = URIRef("http://example.org/hasPureID")


linda = BNode() # a GUID is generated



# print(title)
# print(firstName)
# print(lastName)

from rdflib import Namespace

n = Namespace("http://example.org/people/")
#
# n.scopusID = rdflib.term.URIRef(u'http://example.org/people/bob')
# n.eve = rdflib.term.URIRef(u'http://example.org/people/eve')

コード例 #3
0
def checkwithactive(constructlist1):
    # owllink= rdflib.URIRef('http://www.w3.org/2002/07/owl#Ontology')
    service = URIRef('ex:service')
    hasService = URIRef('ex:hasService')
    hasArguments = URIRef('ex:hasArguments')

    g = ConjunctiveGraph()
    intentnodeString = URIRef('Intent')
    intentNode = BNode(intentnodeString)

    for q in constructlist1:
        sn = URIRef(q.service)
        print(sn)
        print(q.args)
        g.add((intentNode, hasService, Literal(sn)))
        for r in q.args:
            g.add((sn, hasArguments, Literal(r)))

    #print len(g)

    # for subj, pred, obj in g:
    # print subj, pred, obj

    print("---- Open Active Intents -------")
    filename = "../knowledgelibrary/activeintents.rdf"
    oldgraph = Graph()
    newg = g

    fileflag = 0

    try:
        f = open(filename)
        f.close()
        fileflag = 1
    except IOError as e:
        print("file not found")

    # except IOError:
    #	print ('cannot open', filename)
    # else:
    if fileflag == 1:
        oldgraphresult = oldgraph.parse(filename, format="n3")
        # print "Current active intent graph length:"
        # print len(oldgraphresult)
        # print "---- Merge Intents -------"

        for oldsubj, oldpred, oldobj in oldgraphresult:
            for newsubj, newpred, newobj in g:
                if newsubj == oldsubj and oldobj == newobj:
                    print("Repeated intent found: ")
                    print(newsubj, newobj)

        newg = oldgraphresult + g
        # break
        # finally:
        #	print "Done"
        #rdf2dot(oldgraphresult, "/Users/mkiran/try.dot")
        # draw graph using graphviz since rdf is not working
        dot = Digraph(comment='Intent')
        for subj, pred, obj in newg:
            dot.node(subj, subj)
            dot.node(obj, obj)
            dot.edge(subj, obj, pred, constraint='false')

        # print(dot.source)
        dot.format = 'png'
        # filenameput=filerenderpath+'/inputintent-dot'
        dot.render('../static/inputintent-dot', view='False')

        # print "---- Saving Active Intents -------"

        newg.serialize(filename, format='turtle')
コード例 #4
0
ファイル: gdsc_to_rdf.py プロジェクト: roy29fuku/gdsc
 def create_experiment_turtle(self, cell_id, item):
     drug_id = 'gdsc' + str(self.version) + '_' + str(item['Drug Id'])
     ic50_value = math.exp(item['IC50'])
     self.g.add((self.__cellline_ns[cell_id], self.__m2r_ns["has_assay"],
                 BNode(str(item['tmp_id']))))
     self.g.add((BNode(str(item['tmp_id'])), RDF["type"],
                 self.__ontology_ns["Assay"]))
     self.g.add((BNode(str(item['tmp_id'])), self.__bao_ns['BAO_0002854'],
                 self.__bao_ns['BAO_0020008']))
     self.g.add((BNode(str(item['tmp_id'])), self.__m2r_ns["sample"],
                 self.__cellline_ns[cell_id]))
     self.g.add((BNode(str(item['tmp_id'])), self.__m2r_ns["drug"],
                 self.__drug_ns[drug_id]))
     self.g.add(
         (BNode(str(item['tmp_id'])), self.__ontology_ns['dataset_ver'],
          Literal(str(item['Dataset version']))))
     self.g.add((BNode(str(item['tmp_id'])), DCTERMS['identifier'],
                 Literal(str(item['tmp_id']))))
     self.g.add((BNode(str(item['tmp_id'])), self.__sio_ns["SIO_000216"],
                 BNode(str(item['tmp_id']) + 'res')))
     self.g.add(
         (BNode(str(item['tmp_id']) + 'res'), self.__sio_ns['SIO_000300'],
          Literal(ic50_value, datatype=XSD.decimal)))
     self.g.add((BNode(str(item['tmp_id']) + 'res'),
                 self.__sio_ns['SIO_000221'], self.__obo_ns["UO_0000064"]))
     self.g.add((BNode(str(item['tmp_id']) + 'res'), RDF["type"],
                 self.__bao_ns["BAO_0000190"]))
     self.g.add((BNode(str(item['tmp_id'])), self.__sio_ns["SIO_000216"],
                 BNode(str(item['tmp_id']) + 'auc')))
     self.g.add(
         (BNode(str(item['tmp_id']) + 'auc'), self.__sio_ns['SIO_000300'],
          Literal(item['AUC'], datatype=XSD.decimal)))
     self.g.add((BNode(str(item['tmp_id']) + 'auc'), RDF["type"],
                 self.__bao_ns["BAO_0002120"]))
     self.g.add((BNode(str(item['tmp_id'])), self.__sio_ns["SIO_000216"],
                 BNode((str(item['tmp_id'])) + 'max_conc')))
     self.g.add((BNode(str(item['tmp_id']) + 'max_conc'),
                 self.__sio_ns['SIO_000300'],
                 Literal(item['Max conc'], datatype=XSD.decimal)))
     self.g.add((BNode(str(item['tmp_id']) + 'max_conc'), RDF["type"],
                 self.__sio_ns["SIO_001088"]))
コード例 #5
0
ファイル: csvw.py プロジェクト: RinkeHoekstra/COW
    def process(self, count, rows, chunksize):
        """Process the rows fed to the converter. Count and chunksize are used to determine the
        current row number (needed for default observation identifiers)"""
        obs_count = count * chunksize

        # logger.info("Row: {}".format(obs_count)) #removed for readability

        # We iterate row by row, and then column by column, as given by the CSVW mapping file.
        mult_proc_counter = 0
        iter_error_counter= 0
        for row in rows:
            # This fixes issue:10
            if row is None:
                mult_proc_counter += 1
                # logger.debug( #removed for readability
                #     "Skipping empty row caused by multiprocessing (multiple of chunksize exceeds number of rows in file)...")
                continue

            # set the '_row' value in case we need to generate 'default' URIs for each observation ()
            # logger.debug("row: {}".format(obs_count)) #removed for readability
            row[u'_row'] = obs_count
            count += 1

            # print(row)

            # The self.columns dictionary gives the mapping definition per column in the 'columns'
            # array of the CSVW tableSchema definition.

            for c in self.columns:
                c = Item(self.metadata_graph, c)
                # default about URL
                s = self.expandURL(self.aboutURLSchema, row)

                try:
                    # Can also be used to prevent the triggering of virtual
                    # columns!

                    # Get the raw value from the cell in the CSV file
                    try:
                        # Python 2
                        value = row[unicode(c.csvw_name)]
                    except NameError:
                        # Python 3
                        value = row[str(c.csvw_name)]

                    # This checks whether we should continue parsing this cell, or skip it.
                    if self.isValueNull(value, c):
                        continue

                    # If the null values are specified in an array, we need to parse it as a collection (list)
                    elif isinstance(c.csvw_null, Item):
                        nulls = Collection(self.metadata_graph, BNode(c.csvw_null))

                        if self.equal_to_null(nulls, row):
                            # Continue to next column specification in this row, if the value is equal to (one of) the null values.
                            continue
                except:
                    # No column name specified (virtual) because there clearly was no c.csvw_name key in the row.
                    # logger.debug(traceback.format_exc()) #removed for readability
                    iter_error_counter +=1
                    if isinstance(c.csvw_null, Item):
                        nulls = Collection(self.metadata_graph, BNode(c.csvw_null))
                        if self.equal_to_null(nulls, row):
                            # Continue to next column specification in this row, if the value is equal to (one of) the null values.
                            continue

                try:
                    # This overrides the subject resource 's' that has been created earlier based on the
                    # schema wide aboutURLSchema specification.

                    try:
                        csvw_virtual = unicode(c.csvw_virtual)
                        csvw_name = unicode(c.csvw_name)
                        csvw_value = unicode(c.csvw_value)
                        about_url = unicode(c.csvw_aboutUrl)
                        value_url = unicode(c.csvw_valueUrl)
                    except NameError:
                        csvw_virtual = str(c.csvw_virtual)
                        csvw_name = str(c.csvw_name)
                        csvw_value = str(c.csvw_value)
                        about_url = str(c.csvw_aboutUrl)
                        value_url = str(c.csvw_valueUrl)

                    if csvw_virtual == u'true' and c.csvw_aboutUrl is not None:
                        s = self.expandURL(c.csvw_aboutUrl, row)

                    if c.csvw_valueUrl is not None:
                        # This is an object property, because the value needs to be cast to a URL
                        p = self.expandURL(c.csvw_propertyUrl, row)
                        o = self.expandURL(c.csvw_valueUrl, row)
                        try:
                            if self.isValueNull(os.path.basename(unicode(o)), c):
                                logger.debug("skipping empty value")
                                continue
                        except NameError:
                            if self.isValueNull(os.path.basename(str(o)), c):
                                logger.debug("skipping empty value")
                                continue

                        if csvw_virtual == u'true' and c.csvw_datatype is not None and URIRef(c.csvw_datatype) == XSD.anyURI:
                            # Special case: this is a virtual column with object values that are URIs
                            # For now using a test special property
                            value = row[unicode(c.csvw_name)].encode('utf-8')
                            o = URIRef(iribaker.to_iri(value))

                        if csvw_virtual == u'true' and c.csvw_datatype is not None and URIRef(c.csvw_datatype) == XSD.linkURI:
                            about_url = about_url[about_url.find("{"):about_url.find("}")+1]
                            s = self.expandURL(about_url, row)
                            # logger.debug("s: {}".format(s))
                            value_url = value_url[value_url.find("{"):value_url.find("}")+1]
                            o = self.expandURL(value_url, row)
                            # logger.debug("o: {}".format(o))

                        # For coded properties, the collectionUrl can be used to indicate that the
                        # value URL is a concept and a member of a SKOS Collection with that URL.
                        if c.csvw_collectionUrl is not None:
                            collection = self.expandURL(c.csvw_collectionUrl, row)
                            self.g.add((collection, RDF.type, SKOS['Collection']))
                            self.g.add((o, RDF.type, SKOS['Concept']))
                            self.g.add((collection, SKOS['member'], o))

                        # For coded properties, the schemeUrl can be used to indicate that the
                        # value URL is a concept and a member of a SKOS Scheme with that URL.
                        if c.csvw_schemeUrl is not None:
                            scheme = self.expandURL(c.csvw_schemeUrl, row)
                            self.g.add((scheme, RDF.type, SKOS['Scheme']))
                            self.g.add((o, RDF.type, SKOS['Concept']))
                            self.g.add((o, SKOS['inScheme'], scheme))
                    else:
                        # This is a datatype property
                        if c.csvw_value is not None:
                            value = self.render_pattern(csvw_value, row)
                        elif c.csvw_name is not None:
                            # print s
                            # print c.csvw_name, self.encoding
                            # print row[unicode(c.csvw_name)], type(row[unicode(c.csvw_name)])
                            # print row[unicode(c.csvw_name)].encode('utf-8')
                            # print '...'
                            value = row[csvw_name].encode('utf-8')
                        else:
                            raise Exception("No 'name' or 'csvw:value' attribute found for this column specification")

                        # If propertyUrl is specified, use it, otherwise use
                        # the column name
                        if c.csvw_propertyUrl is not None:
                            p = self.expandURL(c.csvw_propertyUrl, row)
                        else:
                            if "" in self.metadata_graph.namespaces():
                                propertyUrl = self.metadata_graph.namespaces()[""][
                                    csvw_name]
                            else:
                                propertyUrl = "{}{}".format(get_namespaces()['sdv'],
                                    csvw_name)

                            p = self.expandURL(propertyUrl, row)

                        if c.csvw_datatype is not None:
                            if URIRef(c.csvw_datatype) == XSD.anyURI:
                                # The xsd:anyURI datatype will be cast to a proper IRI resource.
                                o = URIRef(iribaker.to_iri(value))
                            elif URIRef(c.csvw_datatype) == XSD.string and c.csvw_lang is not None:
                                # If it is a string datatype that has a language, we turn it into a
                                # language tagged literal
                                # We also render the lang value in case it is a
                                # pattern.
                                o = Literal(value, lang=self.render_pattern(
                                    c.csvw_lang, row))
                            else:
                                try:
                                    csvw_datatype = unicode(c.csvw_datatype)
                                except NameError:
                                    csvw_datatype = str(c.csvw_datatype).split(')')[0].split('(')[-1]
                                    # csvw_datatype = str(c.csvw_datatype)
                                # print(type(csvw_datatype))
                                # print(csvw_datatype)
                                o = Literal(value, datatype=csvw_datatype, normalize=False)
                        else:
                            # It's just a plain literal without datatype.
                            o = Literal(value)


                    # Add the triple to the assertion graph
                    self.g.add((s, p, o))

                    # Add provenance relating the propertyUrl to the column id
                    if '@id' in c:
                        self.g.add((p, PROV['wasDerivedFrom'], URIRef(c['@id'])))

                except:
                    # print row[0], value
                    traceback.print_exc()

            # We increment the observation (row number) with one
            obs_count += 1

        # for s,p,o in self.g.triples((None,None,None)):
        #     print(s.__repr__,p.__repr__,o.__repr__)

        logger.debug(
            "{} row skips caused by multiprocessing (multiple of chunksize exceeds number of rows in file)...".format(mult_proc_counter))
        logger.debug(
            "{} errors encountered while trying to iterate over a NoneType...".format(mult_proc_counter))
        logger.info("... done")
        return self.ds.serialize(format=self.output_format)
コード例 #6
0
# Regular expression object for NCNAME
ncname = re.compile("^[A-Za-z][A-Za-z0-9._-]*$")

# Regular expression object for term name
termname = re.compile("^[A-Za-z]([A-Za-z0-9._-]|/)*$")

# Regular expression object for a general XML application media type
xml_application_media_type = re.compile("application/[a-zA-Z0-9]+\+xml")

XHTML_PREFIX = "xhv"
XHTML_URI = "http://www.w3.org/1999/xhtml/vocab#"

#### Managing blank nodes for CURIE-s: mapping from local names to blank nodes.
_bnodes = {}
_empty_bnode = BNode()

####


class InitialContext:
    """
	Get the initial context values. In most cases this class has an empty content, except for the
	top level (in case of RDFa 1.1). Each L{TermOrCurie} class has one instance of this class. It provides initial
	mappings for terms, namespace prefixes, etc, that the top level L{TermOrCurie} instance uses for its own initialization.

	@ivar terms: collection of all term mappings
	@type terms: dictionary
	@ivar ns: namespace mapping
	@type ns: dictionary
	@ivar vocabulary: default vocabulary
コード例 #7
0
ファイル: ej-rdflib-08.py プロジェクト: ChrisLe7/ISSBC
Created on Thu Apr 29 09:23:25 2021
Este ejercicio es igual al de ej-rdflib-t-06.py
@author: ChrisLe
"""

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

n = Namespace("http://example.org/people/")

n.bob  # = rdflib.term.URIRef(u'http://example.org/people/bob')
n.eve  # = rdflib.term.URIRef(u'http://example.org/people/eve')

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

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

#Ver las variables creadas : ej-rdflib-t-04
print(name)
print(age)
print(height)

g = Graph()

g.add((bob, RDF.type, FOAF.Person))
g.add((bob, FOAF.name, name))
g.add((bob, FOAF.knows, linda))
コード例 #8
0
ファイル: Collection.py プロジェクト: ChunHungLiu/watchdog-1
        graph = self.graph
        while container:
            rest = graph.value(container, RDF.rest)
            graph.remove((container, RDF.first, None))
            graph.remove((container, RDF.rest, None))
            container = rest
def test():
    import doctest
    doctest.testmod()

if __name__=="__main__":
    test()

    g = Graph()

    c = Collection(g, BNode())

    assert len(c)==0

    c = Collection(g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])

    assert len(c)==4

    assert c[1]==Literal("2"), c[1]

    del c[1]

    assert list(c)==[Literal("1"), Literal("3"), Literal("4")], list(c)

    try:
        del c[500]
コード例 #9
0
ファイル: Collection.py プロジェクト: ChunHungLiu/watchdog-1
 def __init__(self, graph, uri, seq=[]):
     self.graph = graph
     self.uri = uri or BNode()
     for item in seq:
         self.append(item)
コード例 #10
0
    def _add_text_annotation(self, a, surface):
        # Skip possible *Span elements that failed to get an end pos
        if not a.end:
            return 0

        g = self.g
        if type(a) == tei.Line:
            ann_type = SGA.LineAnnotation
        elif type(a) == tei.Delete:
            ann_type = SGA.DeletionAnnotation
        elif type(a) == tei.Add:
            ann_type = SGA.AdditionAnnotation
        elif type(a) == tei.Space:
            ann_type = SGA.SpaceAnnotation
        elif type(a) == tei.Segment:
            ann_type = SGA.SegmentAnnotation
        elif type(a) == tei.Highlight:
            ann_type = SGA.HighlightAnnotation

        # link AnnotationList to Annotation
        annotation = BNode()
        g.add((self.text_annotations, ORE.aggregates, annotation))
        g.add((annotation, RDF.type, ann_type))
        g.add((annotation, RDF.type, OAX.Highlight))

        # add rendering styles
        if a.rend:
            m = re.match('indent(\d+)', a.rend)
            if m:
                indent = int(m.group(1))
                g.add((annotation, SGA.textIndentLevel, Literal(indent)))
            else:
                g.add((annotation, SGA.textAlignment, Literal(a.rend)))
        if ann_type == SGA.SpaceAnnotation:
            g.add((annotation, SGA.spaceExt, Literal(a.ext)))

        # link LineAnnotation to SpecificResource and TEI file
        target = BNode()
        g.add((annotation, OA.hasTarget, target))
        g.add((target, RDF.type, OA.SpecificResource))
        g.add((target, OA.hasSource, URIRef(self.tei_url(surface))))

        classes = []
        if hasattr(a, 'in_work'):
            if a.in_work:  # it may be false when present
                classes.append('work-' + a.in_work)
        if a.hand:
            classes.append('hand-' + a.hand)
        if classes:
            g.add((target, SGA.hasClass, Literal(" ".join(classes))))

        # link SpecificResource and TextOffsetSelector
        selector = BNode()
        g.add((target, OA.hasSelector, selector))
        g.add((selector, RDF.type, OAX.TextOffsetSelector))
        g.add((selector, OAX.begin, Literal(a.begin)))
        g.add((selector, OAX.end, Literal(a.end)))

        # link SpecificResource to CSS as needed
        if ann_type == SGA.AdditionAnnotation and a.place:
            if a.place == "superlinear":
                text = "vertical-align: super;"
            elif a.place == "sublinear":
                text = "vertical-align: sub;"
            else:
                text = None
            if text:
                css = BNode()
                g.add((target, OA.hasStyle, css))
                g.add((css, RDF.type, CNT.ContentAsText))
                g.add((css, DC['format'], Literal("text/css")))
                g.add((css, CNT.chars, Literal(text)))

        # Add literal CSS
        if ann_type == SGA.HighlightAnnotation:
            rend_to_css = {
                # "hyphenated" : None
                "underline": "border-bottom: 1px solid; line-height: 1em;",
                "double-underline": "border-bottom: 3px double; line-height: 1em;",
                "bold": "font-weight: bold;",
                "caps": "font-variant: small-caps;",
                "italic": "font-style: italic;",
                "sup": "vertical-align: super; font-size: 80%",
                "sub": "vertical-align: sub; font-size: 80%"
            }
            value = rend_to_css.get(a.rend)
            if value:
                css = BNode()
                g.add((target, OA.hasStyle, css))
                g.add((css, RDF.type, CNT.ContentAsText))
                g.add((css, DC['format'], Literal("text/css")))
                g.add((css, CNT.chars, Literal(value)))
コード例 #11
0
ファイル: s4.py プロジェクト: IronSenior/PracticasUNI
@author: acalvo
"""

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

if __name__ == '__main__':

    store = Graph()

    # Bind a few prefix, namespace pairs for pretty output
    store.bind("dc", DC)
    store.bind("foaf", FOAF)

    # 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"]):
コード例 #12
0
    def _add_canvases(self, page=None, skip_annos=None):
        g = self.g

        # add the list of sequences
        sequences_uri = BNode()
        g.add((self.uri, SC.hasSequences, sequences_uri))
        g.add((sequences_uri, RDF.type, RDF.List))

        # add the sequence, which itself is list of canvases
        sequence_uri = BNode()
        g.add((sequences_uri, RDF.first, sequence_uri))
        g.add((sequences_uri, RDF.rest, RDF.nil))
        g.add((sequence_uri, RDF.type, SC.Sequence))
        g.add((sequence_uri, RDF.type, RDF.List))
        g.add((sequence_uri, RDF.rest, RDF.nil))
        g.add((sequence_uri, RDFS.label, Literal("Physical sequence")))

        # add the list of ranges
        range_list_uri = BNode()
        g.add((self.uri, SC.hasRanges, range_list_uri))
        g.add((range_list_uri, RDF.type, RDF.List))

        # add each range, which itself is list of canvases
        ranges_used = {}
        for ran in self.tei.ranges:
            range_uri = BNode()
            ranges_used[ran] = range_uri
            g.add((range_list_uri, RDF.first, range_uri))
            g.add((range_uri, RDF.type, SC.Range))
            g.add((range_uri, RDF.type, RDF.List))
            g.add((range_uri, RDFS.label, Literal(ran)))
            next_range_list_uri = BNode()
            g.add((range_list_uri, RDF.rest, next_range_list_uri))
            range_list_uri = next_range_list_uri
        g.add((range_list_uri, RDF.rest, RDF.nil))

        # add the image list
        image_list_uri = BNode()
        g.add((self.uri, SC.hasImageAnnotations, image_list_uri))
        g.add((image_list_uri, RDF.type, RDF.List))

        # add the canvas list
        canvas_list_uri = BNode()
        g.add((self.uri, SC.hasCanvases, canvas_list_uri))
        g.add((canvas_list_uri, RDF.type, RDF.List))

        # now add each surface
        page_count=0

        for surface in self.tei.surfaces:

            page_count += 1
            if page is not None and page_count != page:
                continue

            # add the canvas
            canvas_uri = BNode()
            g.add((canvas_uri, RDF.type, SC.Canvas))
            g.add((canvas_uri, RDFS.label, Literal(surface.folio)))
            g.add((canvas_uri, SGA.folioLabel, Literal(surface.folio)))
            g.add((canvas_uri, SGA.shelfmarkLabel, Literal(surface.shelfmark)))
            g.add((canvas_uri, SGA.handLabel, Literal(surface.hands_label)))
            g.add((canvas_uri, EXIF.height, Literal(surface.height)))
            g.add((canvas_uri, EXIF.width, Literal(surface.width)))
            g.add((canvas_list_uri, RDF.first, canvas_uri))
            next_canvas_list_uri = BNode()
            g.add((canvas_list_uri, RDF.rest, next_canvas_list_uri))
            canvas_list_uri = next_canvas_list_uri

            # add the image
            image_uri = URIRef(surface.image)
            g.add((image_uri, DC['format'], Literal('image/jp2')))
            g.add((image_uri, EXIF.height, Literal(surface.height)))
            g.add((image_uri, EXIF.width, Literal(surface.width)))
            g.add((image_uri, SC.hasRelatedService, URIRef("https://iiif.bodleian.ox.ac.uk/iiif/image/")))

            # add the image annotation
            image_ann_uri = BNode()
            g.add((image_ann_uri, RDF.type, OA.Annotation))
            g.add((image_ann_uri, OA.hasTarget, canvas_uri))
            g.add((image_ann_uri, OA.hasBody, URIRef(surface.image)))
            g.add((image_list_uri, RDF.first, image_ann_uri))
            next_image_list_uri = BNode()
            g.add((image_list_uri, RDF.rest, next_image_list_uri))
            image_list_uri = next_image_list_uri

            # add the canvas to the sequence
            g.add((sequence_uri, RDF.first, canvas_uri))
            next_sequence_uri = BNode()
            g.add((sequence_uri, RDF.rest, next_sequence_uri))
            sequence_uri = next_sequence_uri

            # Skip lower-level annotations when requested
            if not skip_annos:
                # add the zone annotations
                self._add_zone_annotations(surface, canvas_uri)

                # add the line annotations
                self._add_text_annotations(surface)

                # add the html annotations
                self._add_html_annotations(surface, canvas_uri)

                # add the xml annotations
                self._add_xml_annotations(surface, canvas_uri)

            range_label = self.tei.section_loci_pages_only.get(surface.xmlid, None)
            if range_label:
                range_uri = ranges_used[range_label]
                g.add((range_uri, RDF.first, canvas_uri))
                new_range_uri = BNode()
                g.add((range_uri, RDF.rest, new_range_uri))
                ranges_used[range_label] = new_range_uri

        # close off the sequence list
        g.add((sequence_uri, RDF.rest, RDF.nil))
        g.add((image_list_uri, RDF.rest, RDF.nil))
        g.add((canvas_list_uri, RDF.rest, RDF.nil))

        # close off the ranges lists
        for range_uri in ranges_used.values():
            g.add((range_uri, RDF.rest, RDF.nil))
コード例 #13
0
ファイル: graphml_to_ttl.py プロジェクト: tgbugs/pyontutils
    def nodes(self):
        a = rdf.type
        for id, style_type, style_width, label, desc, url in super().nodes():
            s = None
            if desc == 'legend':
                continue
            if url:
                if url.startswith('exact'):
                    s = workflow[url]
                    yield s, a, workflow.exact
                elif url.startswith('RRIDscibot'):
                    s = workflow[url]
                    yield s, a, workflow.tagScibot
                elif any(
                        url.startswith(prefix)
                        for prefix in ('release', 'resolver')):
                    s = wf[url]
                else:
                    s = TEMP[url]
            else:
                if label == 'RRID:':
                    s = workflow['RRID']
                elif label.startswith('RRIDCUR:'):
                    s = RRIDCUR[label.split(':')[-1]]
                    self.different_tags.add(s)
                elif ' + ' in label:
                    suffixes = label.split(' + ')  # FIXME
                    s = BNode()
                    # union of is the right way to go here I think, because union
                    # implies that both must be present, not either or ...
                    yield from unionOf(*(workflow[sfx] for sfx in suffixes))(s)
                    #self.node_name_lookup[id] = s
                    #self.types[s] = wf.tag
                    #continue  # can't yield the type this node
                elif label in ('DOI', 'PMID'):
                    s = wf[label]
                    yield s, a, wf.tag
                else:
                    s = TEMP[label]

            if s is None:  # FIXME if this happens this late we don't get the error message
                raise ValueError(f'unhandled node {id} {lable}')
            else:
                self.node_name_lookup[id] = s

            #yield s, rdf.type, owl.Class
            if isinstance(s, BNode):
                #yield s, a, owl.NamedIndividual  # FIXME apparently this doesn't serializer properly ...
                yield s, a, wf.tag
            elif style_type == 'dashed':
                yield s, a, workflow.state
            elif (style_type, style_width) == ('line', '1.0'):
                pass
            elif (style_type, style_width) == ('line', '2.0'):
                pass
            elif (style_type, style_width) == ('dashed_dotted', '2.0'):
                #yield s, wf.isAttachedTo, wf.pageNote
                self.insert_object(id, wf.pageNoteInstance)
                yield wf.pageNoteInstance, wf.hasTag, s
                if s not in self.types:
                    yield s, a, wf.tagCurator
            elif (style_type, style_width) == ('dotted', '2.0'):
                # yield s, wf.isAttachedTo, wf.pageNote
                self.insert_object(id, wf.pageNoteInstance)
                yield wf.pageNoteInstance, wf.hasTag, s
                if s not in self.types:
                    yield s, a, wf.tagScibot
            else:
                msg = f'{id} {label} has unhandled type {style_type} {style_width}'
                raise ValueError(msg)
コード例 #14
0
ファイル: graphml_to_ttl.py プロジェクト: tgbugs/pyontutils
 def insert_object(self, id, s_new):
     id_new = BNode()  # just an id
     self.edge_object_shift[id] = id_new
     self.node_name_lookup[id_new] = s_new
コード例 #15
0
def get_basics_for_wduri(wikidata_uri):
    logger.info(f"Requesting data for {wikidata_uri}")
    md5 = hashlib.md5()
    md5.update(wikidata_uri.encode('utf-8'))
    filename = os.path.join("wdcache", "wd_" + md5.hexdigest())
    logger.info(f"Cache for {wikidata_uri} is {filename}")
    sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
    queryres = None
    if os.path.isfile(filename):
        with open(filename, 'rb') as cachehandle:
            queryres = pickle.load(cachehandle)
    else:
        # gör till construct senare

        sparql.setReturnFormat(JSON)

        sq = f'''
SELECT ?itemLabel ?lat ?lon ?country ?countryLabel ?imageurl ?instance ?type
?entityDescription ?citizencountry ?citizencountryLabel ?librisid ?viafid
WHERE
{{
  BIND (<{wikidata_uri}> as ?entity)
  BIND(IF(EXISTS{{?entity wdt:P31 wd:Q5}},"human", IF(EXISTS{{?entity p:P625 ?c}},"place", "other")) AS ?type)
  OPTIONAL {{
    ?entity p:P625 ?coords .
    ?coords ps:P625 ?coord.
    ?coords psv:P625 ?coordinate_node.  
    ?coordinate_node wikibase:geoLatitude ?lat .
    ?coordinate_node wikibase:geoLongitude ?lon .
    ?entity wdt:P17 ?country .
  }}

  OPTIONAL {{
    ?entity wdt:P18 ?imageurl .
  }}
  
  OPTIONAL {{
    ?entity wdt:P27 ?citizencountry .
  }}

  OPTIONAL {{
    ?entity wdt:P906 ?librisid .
  }}
  
  OPTIONAL {{
    ?entity wdt:P214 ?viafid .
  }}

  SERVICE wikibase:label {{ 
    bd:serviceParam wikibase:language "sv,en,de,da,nl,fr" .
    ?entity rdfs:label ?itemLabel .
    ?entity schema:description ?entityDescription .
    ?country rdfs:label ?countryLabel .
    ?citizencountry rdfs:label ?citizencountryLabel .
  }}
}}
LIMIT 1
        '''

        sparql.setQuery(sq)
        queryres = sparql.query().convert()

        if not os.path.exists("./wdcache"):
            os.makedirs("./wdcache")

        with open(filename, 'wb') as cachehandle:
            logger.warn(f"Storing {wikidata_uri}")
            pickle.dump(queryres, cachehandle)

    for result in queryres["results"]["bindings"]:

        #check what this is and make simpler instance info
        if "type" in result:
            logger.info("Instance of %s" % result["type"]["value"])
            if "human" in result["type"]["value"]:
                g.add((URIRef(wikidata_uri), NAMESPACES["wdt"]["P31"],
                       NAMESPACES["wd"]["Q5"]))
            if "place" in result["type"]["value"]:
                g.add((URIRef(wikidata_uri), NAMESPACES["wdt"]["P31"],
                       URIRef("http://www.wikidata.org/entity/Q2221906")))

        if "itemLabel" in result:
            logger.info(f'Got label {result["itemLabel"]["value"]}')
            g.add((URIRef(wikidata_uri), RDFS.label,
                   Literal(result["itemLabel"]["value"])))

        if "entityDescription" in result:
            g.add((URIRef(wikidata_uri), NAMESPACES["schema"]["description"],
                   Literal(result["entityDescription"]["value"],
                           lang=result["entityDescription"]["xml:lang"])))

        if "lat" in result:
            geo = BNode()
            g.add((geo, RDF.type, NAMESPACES["schema"]["GeoCoordinates"]))
            g.add((geo, NAMESPACES["schema"]["latitude"],
                   Literal(result["lat"]["value"])))
            g.add((geo, NAMESPACES["schema"]["longitude"],
                   Literal(result["lon"]["value"])))
            g.add((URIRef(wikidata_uri), NAMESPACES["schema"]["geo"], geo))

        if "citizencountry" in result:
            g.add((URIRef(wikidata_uri), NAMESPACES["schema"]["nationality"],
                   URIRef(result["citizencountry"]["value"])))
            g.add((URIRef(result["citizencountry"]["value"]), RDFS.label,
                   Literal(result["citizencountryLabel"]["value"])))

        if "imageurl" in result:
            imageurl = result["imageurl"]["value"]
            g.add((URIRef(wikidata_uri), NAMESPACES["schema"]["image"],
                   Literal(imageurl)))

            thumbnail = thumb_url(imageurl, 120)
            g.add((URIRef(wikidata_uri), NAMESPACES["schema"]["thumbnailUrl"],
                   Literal(thumbnail)))
            logger.info(f"Added thumbnail {thumbnail}")

        if "librisid" in result:
            g.add((URIRef(wikidata_uri), NAMESPACES["wdt"]["P906"],
                   Literal(result["librisid"]["value"])))

        if "viafid" in result:
            g.add((URIRef(wikidata_uri), NAMESPACES["wdt"]["P214"],
                   Literal(result["viafid"]["value"])))
コード例 #16
0
    def extract_data(self, id):
        g = self._init_graph(id)

        # Basic fields
        items = [
            ("title", DC.title, None, Literal),
            ("notes", DC.description, None, Literal),
        ]

        for triple in get_triples_from_dict(self._dataset_dict,
                                            self._dataset_ref, items):
            g.add(triple)
        g.add((self._dataset_ref, DC.landingPage, URIRef(self._dataset_url)))

        # Tags
        for tag in self._dataset_dict.get("tags", []):
            g.add((self._dataset_ref, DC.keyword, Literal(tag["name"])))

        #  Lists
        items = [
            ("creator", DC.creator, None, Literal),
            ("subject", DC.subject, None, Literal),
            ("publisher", DC.publisher, None, Literal),
            ("contributor", DC.contributer, None, Literal),
            ("publication_type", DC.publication_type, None, Literal),
            ("format", DC["format"], None, Literal),
            ("identifier", DC.identifier, None, Literal),
            ("source", DC.source, None, Literal),
            ("language", DC.languate, None, Literal),
            ("relation", DC.relations, None, Literal),
            ("rights", DC.rights, None, Literal),
        ]
        for triple in get_list_triples_from_dict(self._dataset_dict,
                                                 self._dataset_ref, items):
            g.add(triple)

        # Dates
        items = [
            ("date", DC.date, None, Literal),
        ]
        for triple in get_list_triples_from_dict(self._dataset_dict,
                                                 self._dataset_ref, items):
            g.add(triple)

        # Spatial
        spatial_uri = self._dataset_dict.get("spatial_uri")
        spatial_text = self._dataset_dict.get("spatial_text")
        spatial_geom = self._dataset_dict.get("spatial")

        if spatial_uri or spatial_text or spatial_geom:
            if spatial_uri:
                spatial_ref = CleanedURIRef(spatial_uri)
            else:
                spatial_ref = BNode()

            g.add((spatial_ref, RDF.type, DCT.Location))
            g.add((self._dataset_ref, DCT.spatial, spatial_ref))

            if spatial_text:
                g.add((spatial_ref, SKOS.prefLabel, Literal(spatial_text)))

            if spatial_geom:
                # GeoJSON
                g.add((
                    spatial_ref,
                    LOCN.geometry,
                    Literal(spatial_geom, datatype=GEOJSON_IMT),
                ))
                # WKT, because GeoDCAT-AP says so
                try:
                    g.add((
                        spatial_ref,
                        LOCN.geometry,
                        Literal(
                            wkt.dumps(json.loads(spatial_geom), decimals=4),
                            datatype=GSP.wktLiteral,
                        ),
                    ))
                except (TypeError, ValueError, InvalidGeoJSONException):
                    pass

        # Resources
        for resource_dict in self._dataset_dict.get("resources", []):

            distribution = CleanedURIRef(resource_uri(resource_dict))

            g.add((self._dataset_ref, DC.distribution, distribution))

            g.add((distribution, RDF.type, DC.Distribution))

            #  Simple values
            items = [
                ("name", DC.title, None, Literal),
                ("description", DC.description, None, Literal),
            ]

            for triple in get_triples_from_dict(resource_dict, distribution,
                                                items):
                g.add(triple)

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

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

            # URL fallback and old behavior
            url = resource_dict.get("url")
            if url:
                for triple in get_triple_from_dict(
                        resource_dict,
                        distribution,
                        DC.accessURL,
                        "url",
                        _type=URIRef,
                ):
                    g.add(triple)

            # Numbers
            if resource_dict.get("size"):
                try:
                    g.add((
                        distribution,
                        DC.byteSize,
                        Literal(
                            float(resource_dict["size"]),
                            datatype=XSD.decimal,
                        ),
                    ))
                except (ValueError, TypeError):
                    g.add((
                        distribution,
                        DC.byteSize,
                        Literal(resource_dict["size"]),
                    ))

        return {"data": g.serialize(format="pretty-xml")}
コード例 #17
0
ファイル: __init__.py プロジェクト: ChunHungLiu/watchdog-1
    #test_multiple.slowtest=True # not really slow?


def _assert_equal_graphs(g1, g2):
    assert len(g1) == len(
        g2), "Serialized graph not same size as source graph."
    g1copy = _mangled_copy(g1)
    g2copy = _mangled_copy(g2)
    g1copy -= _mangled_copy(g2)
    g2copy -= _mangled_copy(g1)
    assert len(g1copy) == 0, "Source graph larger than serialized graph."
    assert len(g2copy) == 0, "Serialized graph larger than source graph."


_blank = BNode()


def _mangled_copy(g):
    "Makes a copy of the graph, replacing all bnodes with the bnode ``_blank``."
    gcopy = ConjunctiveGraph()
    isbnode = lambda v: isinstance(v, BNode)
    for s, p, o in g:
        if isbnode(s): s = _blank
        if isbnode(p): p = _blank
        if isbnode(o): o = _blank
        gcopy.add((s, p, o))
    return gcopy


def serialize(sourceGraph, makeSerializer, getValue=True):
コード例 #18
0
adress = URIRef('http://dbpedia.org/ontology/adress')
code = URIRef('http://dbpedia.org/ontology/code')
parent = URIRef('http://dbpedia.org/ontology/parent')
name = URIRef('http://dbpedia.org/ontology/name')
order = URIRef('http://dbpedia.org/ontology/order')
tipo = URIRef('http://dbpedia.org/ontology/type')
isHandicappedAccessible = URIRef(
    'http://dbpedia.org/ontology/isHandicappedAccessible')
location = URIRef('http://dbpedia.org/ontology/location')
timezone = URIRef('http://dbpedia.org/ontology/timezone')
line = URIRef('http://www.daml.org/ontologies/396#line')
homepage = URIRef('http://www.daml.org/ontologies/396#homepage')
lat = URIRef('http://www.w3.org/2003/01/geo/wgs84_pos#lat')
long = URIRef('http://www.w3.org/2003/01/geo/wgs84_pos#long')

for row in csvReader:
    g.add((BNode(row[3]), tipo, Literal(row[0])))
    g.add((BNode(row[3]), line, Literal(row[1])))
    g.add((BNode(row[3]), order, Literal(row[2])))
    g.add((BNode(row[3]), code, Literal(row[4])))
    g.add((BNode(row[3]), name, Literal(row[5])))
    g.add((BNode(row[3]), adress, Literal(row[6])))
    g.add((BNode(row[3]), lat, Literal(row[7])))
    g.add((BNode(row[3]), long, Literal(row[8])))
    g.add((BNode(row[3]), location, Literal(row[9])))
    g.add((BNode(row[3]), homepage, URIRef(row[10])))
    g.add((BNode(row[3]), parent, Literal(row[12])))
    g.add((BNode(row[3]), timezone, Literal(row[13])))
    g.add((BNode(row[3]), isHandicappedAccessible, Literal(row[14])))

g.serialize(destination="Transporte.xml", format="xml")
コード例 #19
0
    def CURIE_to_URI(self, val):
        """CURIE to URI mapping.

		This method does I{not} take care of the last step of CURIE processing, ie, the fact that if
		it does not have a CURIE then the value is used a URI. This is done on the caller's side, because this has
		to be combined with base, for example. The method I{does} take care of BNode processing, though, ie,
		CURIE-s of the form "_:XXX".

		@param val: the full CURIE
		@type val: string
		@return: URIRef of a URI or None.
		"""
        # Just to be on the safe side:
        if val == "":
            return None
        elif val == ":":
            if self.default_curie_uri:
                return URIRef(self.default_curie_uri)
            else:
                return None

        # See if this is indeed a valid CURIE, ie, it can be split by a colon
        curie_split = val.split(':', 1)
        if len(curie_split) == 1:
            # there is no ':' character in the string, ie, it is not a valid CURIE
            return None
        else:
            if self.state.rdfa_version >= "1.1":
                prefix = curie_split[0].lower()
            else:
                prefix = curie_split[0]
            reference = curie_split[1]

            #if len(reference) > 0 :
            #	if self.state.rdfa_version >= "1.1" and (len(prefix) == 0 or prefix in self.ns) and reference.startswith('//') :
            #		# This has been defined as illegal in RDFa 1.1
            #		self.state.options.add_warning(err_absolute_reference % (reference, val), UnresolvableReference, node=self.state.node.nodeName)
            #		return None
            #	if reference[0] == ":" :
            #		return None

            # first possibility: empty prefix
            if len(prefix) == 0:
                if self.default_curie_uri and self._check_reference(reference):
                    return self.default_curie_uri[reference]
                else:
                    return None
            else:
                # prefix is non-empty; can be a bnode
                if prefix == "_":
                    # yep, BNode processing. There is a difference whether the reference is empty or not...
                    if len(reference) == 0:
                        return _empty_bnode
                    else:
                        # see if this variable has been used before for a BNode
                        if reference in _bnodes:
                            return _bnodes[reference]
                        else:
                            # a new bnode...
                            retval = BNode()
                            _bnodes[reference] = retval
                            return retval
                # check if the prefix is a valid NCNAME
                elif ncname.match(prefix):
                    # see if there is a binding for this:
                    if prefix in self.ns and self._check_reference(reference):
                        # yep, a binding has been defined!
                        if len(reference) == 0:
                            return URIRef(str(self.ns[prefix]))
                        else:
                            return self.ns[prefix][reference]
                    elif prefix in self.default_prefixes and self._check_reference(
                            reference):
                        # this has been defined through the default context
                        if len(reference) == 0:
                            return URIRef(str(
                                self.default_prefixes[prefix][0]))
                        else:
                            (ns, used) = self.default_prefixes[prefix]
                            # lazy binding of prefixes (to avoid unnecessary prefix definitions in the serializations at the end...)
                            if not used:
                                self.graph.bind(prefix, ns)
                                self.default_prefixes[prefix] = (ns, True)
                            return ns[reference]
                    else:
                        # no definition for this thing...
                        return None
                else:
                    return None
コード例 #20
0
def main():
    outfile = sys.argv[1]

    if not os.path.exists('CONTRIBUTING.md') and os.path.exists(
            'tests/ecmascript'):
        sys.stderr.write(
            'Invalid CWD, must be in Duktape root with dist/ built')
        sys.exit(1)
    os.chdir('dist')
    if not os.path.exists('Makefile.cmdline'):
        sys.stderr.write(
            'Invalid CWD, must be in Duktape root with dist/ built')
        sys.exit(1)

    duktape_version = getDuktapeVersion()
    duktape_pkgname = 'duktape-' + duktape_version + '.tar.xz'
    now = datetime.datetime.utcnow()
    now = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute,
                            now.second)
    creation_date = Literal(now.isoformat() + 'Z', datatype=XSD.dateTime)
    duktape_org = Literal('Organization: duktape.org')
    mit_license = URIRef('http://spdx.org/licenses/MIT')
    duktape_copyright = Literal(
        'Copyright 2013-2017 Duktape authors (see AUTHORS.rst in the Duktape distributable)'
    )

    g = rdflib.Graph()

    crea_node = BNode()
    g.add((crea_node, RDF.type, SPDX.CreationInfo))
    g.add((crea_node, RDFS.comment, Literal('')))
    g.add((crea_node, SPDX.creator, duktape_org))
    g.add((crea_node, SPDX.created, creation_date))
    g.add((crea_node, SPDX.licenseListVersion,
           Literal('1.20')))  # http://spdx.org/licenses/

    # 'name' should not include a version number (see best practices)
    pkg_node = BNode()
    g.add((pkg_node, RDF.type, SPDX.Package))
    g.add((pkg_node, SPDX.name, Literal('Duktape')))
    g.add((pkg_node, SPDX.versionInfo, Literal(duktape_version)))
    g.add((pkg_node, SPDX.packageFileName, Literal(duktape_pkgname)))
    g.add((pkg_node, SPDX.supplier, duktape_org))
    g.add((pkg_node, SPDX.originator, duktape_org))
    g.add((pkg_node, SPDX.downloadLocation,
           Literal('http://duktape.org/' + duktape_pkgname,
                   datatype=XSD.anyURI)))
    g.add((pkg_node, SPDX.homePage,
           Literal('http://duktape.org/', datatype=XSD.anyURI)))
    verify_node = computePackageVerification(g, '.', ['./license.spdx'])
    g.add((pkg_node, SPDX.packageVerificationCode, verify_node))
    # SPDX.checksum: omitted because license is inside the package
    g.add((
        pkg_node, SPDX.sourceInfo,
        Literal(
            'Official duktape.org release built from GitHub repo https://github.com/svaarala/duktape.'
        )))

    # NOTE: MIT license alone is sufficient for now, because Duktape, Lua,
    # Murmurhash2, and CommonJS (though probably not even relevant for
    # licensing) are all MIT.
    g.add((pkg_node, SPDX.licenseConcluded, mit_license))
    g.add((pkg_node, SPDX.licenseInfoFromFiles, mit_license))
    g.add((pkg_node, SPDX.licenseDeclared, mit_license))
    g.add((
        pkg_node, SPDX.licenseComments,
        Literal(
            'Duktape is copyrighted by its authors and licensed under the MIT license.  MurmurHash2 is used internally, it is also under the MIT license. Duktape module loader is based on the CommonJS module loading specification (without sharing any code), CommonJS is under the MIT license.'
        )))
    g.add((pkg_node, SPDX.copyrightText, duktape_copyright))
    g.add((pkg_node, SPDX.summary, Literal('Duktape ECMAScript interpreter')))
    g.add((
        pkg_node, SPDX.description,
        Literal(
            'Duktape is an embeddable Javascript engine, with a focus on portability and compact footprint'
        )))
    # hasFile properties added separately below

    #reviewed_node = BNode()
    #g.add((reviewed_node, RDF.type, SPDX.Review))
    #g.add((reviewed_node, SPDX.reviewer, XXX))
    #g.add((reviewed_node, SPDX.reviewDate, XXX))
    #g.add((reviewed_node, RDFS.comment, ''))

    spdx_doc = BNode()
    g.add((spdx_doc, RDF.type, SPDX.SpdxDocument))
    g.add((spdx_doc, SPDX.specVersion, Literal('SPDX-1.2')))
    g.add((spdx_doc, SPDX.dataLicense,
           URIRef('http://spdx.org/licenses/CC0-1.0')))
    g.add((spdx_doc, RDFS.comment,
           Literal('SPDX license for Duktape ' + duktape_version)))
    g.add((spdx_doc, SPDX.creationInfo, crea_node))
    g.add((spdx_doc, SPDX.describesPackage, pkg_node))
    # SPDX.hasExtractedLicensingInfo
    # SPDX.reviewed
    # SPDX.referencesFile: added below

    for dirpath, dirnames, filenames in os.walk('.'):
        for fn in filenames:
            full_fn = os.path.join(dirpath, fn)
            #print('# file: ' + full_fn)

            file_node = BNode()
            g.add((file_node, RDF.type, SPDX.File))
            g.add((file_node, SPDX.fileName, Literal(full_fn)))
            g.add((file_node, SPDX.fileType, fileType(full_fn)))
            g.add((file_node, SPDX.checksum, checksumFile(g, full_fn)))

            # Here we assume that LICENSE.txt provides the actual "in file"
            # licensing information, and everything else is implicitly under
            # MIT license.
            g.add((file_node, SPDX.licenseConcluded, mit_license))
            if full_fn == './LICENSE.txt':
                g.add((file_node, SPDX.licenseInfoInFile, mit_license))
            else:
                g.add((file_node, SPDX.licenseInfoInFile, URIRef(SPDX.none)))

            # SPDX.licenseComments
            g.add((file_node, SPDX.copyrightText, duktape_copyright))
            # SPDX.noticeText
            # SPDX.artifactOf
            # SPDX.fileDependency
            # SPDX.fileContributor

            # XXX: should referencesFile include all files?
            g.add((spdx_doc, SPDX.referencesFile, file_node))

            g.add((pkg_node, SPDX.hasFile, file_node))

    # Serialize into RDF/XML directly.  We could also serialize into
    # N-Triples and use external tools (like 'rapper') to get cleaner,
    # abbreviated output.

    #print('# Duktape SPDX license file (autogenerated)')
    #print(g.serialize(format='turtle'))
    #print(g.serialize(format='nt'))
    f = open(outfile, 'wb')
    #f.write(g.serialize(format='rdf/xml'))
    f.write(g.serialize(format='xml'))
    f.close()
コード例 #21
0
def sem_graph(triples):
    count =0
    for res in triples:
        news = URIRef("http://news.com/new"+str(count))
        if res[0]  == 0:    #0 is returned if entity not found in dbpedia
            pred = BNode()   #predicate = blank node
        else:
            pred = URIRef(res[0]) #predicate = dbpedia resource
        if res[1] == 0:   #0 is returned if entity not found in dbpedia
            sub = BNode()   #subject = blank node
        else:
            sub = URIRef(res[1]) #subject = dbpedia resource
        if res[0] == 0:    #0 is returned if entity not found in dbpedia
            obj = BNode()  #object = blank node
        else:
            obj = URIRef(res[2]) #object = dbpedia resource
       
        #graph generation
        g.add((sub,pred,obj))
        g.add((news,RDF.subject,sub))
        g.add((news,RDF.predicate,pred))
        g.add((news,RDF.object,obj))
        g.add((sub,RDF.type,news))
        g.add((pred,RDF.type,news))
        g.add((obj,RDF.type,news))
        #g.add((news.news1,dbp.sentiment,Literal("0.34")))
        # g.add((URIRef(res[1]),RDFS.label,Literal(res[4])))
        # g.add((URIRef(res[0]),RDFS.label,Literal(res[3])))
        # g.add((URIRef(res[2]),RDFS.label,Literal(res[5])))
        count +=1;
    g.serialize(destination='output.rdf', format='xml') #graph output and format












# from rdflib import Graph
# from rdflib import resRef, BNode, Literal
# from rdflib.namespace import RDF, FOAF,RDFS,OWL
# g = Graph()
# def insert(pred,sub,obj):
#     subject=sub
#     predicate=pred
#     object = obj
#     res = resRef("http://www.bbc.com")
#     g.add((res,RDF.type,FOAF.person))
#     g.add( (res,RDF.type,Literal(sub)) )
#     g.serialize(destination='output2.rdf', format='xml')

# from rdflib import Graph
# from rdflib import resRef, BNode, Literal
# from rdflib.namespace import RDF, FOAF,RDFS,OWL
# from rdflib import Namespace
# dpb = Namespace("http://www.dbpedia.org/ontology/")
# dc = Namespace("http://purl.org/dc/elements/1.1/")
# ex = Namespace("http://www.exmp.com/ontology/")
# g = Graph()
#
#    # res = resRef("http://www.bbc.com")
# g.add((ex.Cricket,RDF.resource,ex.Gryllidae))
# g.add ((ex.Gryllidae,RDFS.subClassOf,dpb.insect))
# g.add ((ex.Gryllidae,ex.has_body_part,Literal("head")))
# g.add ((ex.Gryllidae,ex.has_body_part,Literal("Antennae")))
# g.serialize(destination='output3.rdf', format='xml')

# from rdflib import Graph
# from rdflib import resRef, BNode, Literal
# from rdflib.namespace import RDF, FOAF,RDFS,OWL
# from rdflib import Namespace
# 
# dbpedia = Namespace("http://dbpedia.org/ontology/")
# dbp = Namespace ("http://dbpedia.org/resource/")
# news = Namespace ("http://news.com/")
# g = Graph()
# 
# g.add((URIRef(res[1]),URIRef(res[0]),URIRef(res[2])))
# g.add((news.news1,RDF.subject,URIRef(res[1])))
# g.add((news.news1,RDF.predicate,URIRef(res[0])))
# g.add((news.news1,RDF.object,URIRef(res[2])))
# g.add((URIRef(res[1]),RDF.type,news.news1))
# g.add((URIRef(res[0]),RDF.type,news.news1))
# g.add((URIRef(res[2]),RDF.type,news.news1))
# g.add((news.news1,dbp.sentiment,Literal("0.34")))
# g.add((URIRef(res[1]),RDFS.label,Literal("Police")))
# g.add((URIRef(res[0]),RDFS.label,Literal("has arrested")))
# g.add((URIRef(res[2]),RDFS.label,Literal("terrorist")))
# g.add((news.news1,URIRef(res[1]),Literal("Police")))
# g.add((news.news1,URIRef(res[0]),Literal("has arrested")))
# g.add((news.news1,URIRef(res[2]),Literal("terrorist")))
# g.add((news.news1,RDF.subject,Literal("Police")))
# g.add((news.news1,RDF.predicate,Literal("has arrested")))
# g.add((news.news1,RDF.object,Literal("terrorist")))
# g.serialize(destination='output4.rdf', format='xml')
コード例 #22
0
    def __init__(self,
                 leftNode,
                 rightNode,
                 aPassThru=False,
                 leftVariables=None,
                 rightVariables=None,
                 executeActions=None,
                 ReteMemoryKind=ReteMemory):
        self.ReteMemoryKind = ReteMemoryKind
        self.instanciatingTokens = set()
        self.aPassThru = aPassThru
        self.name = BNode()
        self.network = None

        # used by terminal nodes only
        self.consequent = set()  # List of tuples in RHS
        self.rules = set()
        self.antecedent = None
        self.headAtoms = set()
        self.leftNode = leftNode
        # The incoming right input of a BetaNode is always an AlphaNode
        self.rightNode = rightNode
        self.memories = {}
        self.descendentMemory = []
        self.descendentBetaNodes = set()
        self.leftUnlinkedNodes = set()
        self.unlinkedMemory = None
        self.fedByBuiltin = None
        if isinstance(leftNode, BuiltInAlphaNode):
            self.fedByBuiltin = LEFT_MEMORY
            assert not isinstance(
                rightNode, BuiltInAlphaNode
            ), "Both %s and %s are builtins feeding a beta node." % (leftNode,
                                                                     rightNode)
            self.memories[RIGHT_MEMORY] = self.ReteMemoryKind(
                (self, RIGHT_MEMORY, leftNode.n3builtin))
        else:
            self.memories[RIGHT_MEMORY] = self.ReteMemoryKind(
                self, RIGHT_MEMORY)

        assert not (
            self.fedByBuiltin
        ), "No support for 'built-ins', function symbols, or non-equality tests."
        if isinstance(rightNode, BuiltInAlphaNode):
            self.fedByBuiltin = RIGHT_MEMORY
            assert not isinstance(
                leftNode, BuiltInAlphaNode
            ), "Both %s and %s are builtins feeding a beta node." % (leftNode,
                                                                     rightNode)
            self.memories[LEFT_MEMORY] = self.ReteMemoryKind(
                self, LEFT_MEMORY, rightNode.n3builtin)
        else:
            self.memories[LEFT_MEMORY] = self.ReteMemoryKind(self, LEFT_MEMORY)
        if aPassThru:
            if rightNode:
                self.leftVariables = set(
                ) if leftVariables is None else leftVariables
                self.rightVariables = collectVariables(
                    self.rightNode
                ) if rightVariables is None else rightVariables
                self.commonVariables = list(self.rightVariables)
            else:
                self.leftVariables = self.rightVariables = set()
                self.commonVariables = []
        else:
            self.leftVariables = collectVariables(
                self.leftNode) if leftVariables is None else leftVariables
            self.rightVariables = collectVariables(
                self.rightNode) if rightVariables is None else rightVariables
            self.commonVariables = [
                leftVar for leftVar in self.leftVariables
                if leftVar in self.rightVariables
            ]
        self.leftIndex = {}
        self.rightIndex = {}
        self.executeActions = executeActions if executeActions is not None else {}
コード例 #23
0
ファイル: csvw.py プロジェクト: RinkeHoekstra/COW
    def __init__(self, file_name, delimiter=',', quotechar='\"', encoding='utf-8', processes=4, chunksize=5000, output_format='nquads'):
        logger.info("Initializing converter for {}".format(file_name))
        self.file_name = file_name
        self.output_format = output_format
        self.target_file = self.file_name + '.' + extensions[self.output_format]
        schema_file_name = file_name + '-metadata.json'

        if not os.path.exists(schema_file_name) or not os.path.exists(file_name):
            raise Exception(
                "Could not find source or metadata file in path; make sure you called with a .csv file")

        self._processes = processes
        self._chunksize = chunksize
        logger.info("Processes: {}".format(self._processes))
        logger.info("Chunksize: {}".format(self._chunksize))

        self.np = Nanopublication(file_name)
        # self.metadata = json.load(open(schema_file_name, 'r'))
        self.metadata_graph = Graph()
        with open(schema_file_name, 'rb') as f:
            try:
                self.metadata_graph.load(f, format='json-ld')
            except ValueError as err:
                err.message = err.message + " ; please check the syntax of your JSON-LD schema file"
                raise

        # from pprint import pprint
        # pprint([term for term in sorted(self.metadata_graph)])

        # Get the URI of the schema specification by looking for the subject
        # with a csvw:url property.
        try:
            # Python 2
            (self.metadata_uri, _) = self.metadata_graph.subject_objects(CSVW.url).next()
        except AttributeError:
            # Python 3
            (self.metadata_uri, _) = next(self.metadata_graph.subject_objects(CSVW.url))


        self.metadata = Item(self.metadata_graph, self.metadata_uri)

        # Add a prov:wasDerivedFrom between the nanopublication assertion graph
        # and the metadata_uri
        self.np.pg.add((self.np.ag.identifier, PROV[
                       'wasDerivedFrom'], self.metadata_uri))
        # Add an attribution relation and dc:creator relation between the
        # nanopublication, the assertion graph and the authors of the schema
        for o in self.metadata_graph.objects(self.metadata_uri, DC['creator']):
            self.np.pg.add((self.np.ag.identifier, PROV['wasAttributedTo'], o))
            self.np.add((self.np.uri, PROV['wasAttributedTo'], o))
            self.np.pig.add((self.np.ag.identifier, DC['creator'], o))

        self.schema = self.metadata.csvw_tableSchema

        # Taking defaults from init arguments
        self.delimiter = delimiter
        self.quotechar = quotechar
        self.encoding = encoding

        # Read csv-specific dialiect specification from JSON structure
        if self.metadata.csvw_dialect is not None:
            if self.metadata.csvw_dialect.csvw_delimiter is not None:
                self.delimiter = str(self.metadata.csvw_dialect.csvw_delimiter)

            if self.metadata.csvw_dialect.csvw_quotechar is not None:
                self.quotechar = str(self.metadata.csvw_dialect.csvw_quoteChar)

            if self.metadata.csvw_dialect.csvw_encoding is not None:
                self.encoding = str(self.metadata.csvw_dialect.csvw_encoding)

        logger.info("Quotechar: {}".format(self.quotechar.__repr__()))
        logger.info("Delimiter: {}".format(self.delimiter.__repr__()))
        logger.info("Encoding : {}".format(self.encoding.__repr__()))
        logger.warning(
            "Taking encoding, quotechar and delimiter specifications into account...")

        # The metadata schema overrides the default namespace values
        # (NB: this does not affect the predefined Namespace objects!)
        # DEPRECATED
        # namespaces.update({ns: url for ns, url in self.metadata['@context'][1].items() if not ns.startswith('@')})

        # Cast the CSVW column rdf:List into an RDF collection
        #print(self.schema.csvw_column)
        # print(len(self.metadata_graph))

        self.columns = Collection(self.metadata_graph, BNode(self.schema.csvw_column))
        # Python 3 can't work out Item so we'll just SPARQL the graph

        if not self.columns:
            self.columns = [o for s,p,o in self.metadata_graph.triples((None, URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#first"), None))]
コード例 #24
0
def testN3Store(store="default", configString=None):
    storetest = True
    g = ConjunctiveGraph(store=store)
    if configString:
        # 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
コード例 #25
0
ファイル: cgat2rdf.py プロジェクト: santayana/cgat
    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 = map(lambda x: x.strip(), 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 = map(lambda x: int(x), 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 = map(lambda x: float(x), 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 = map(lambda x: Literal(x), 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'])
コード例 #26
0
for link in links["links"]:
    #print link
    linkIRI = URIRef(link)
    g.add((linkIRI, RDF.type, ON.WebResource))
    #link = urllib.unquote(link).decode('utf8')

    #adding the scheme
    scheme = link[:link.index(":")]
    g.add((linkIRI, ON.scheme, Literal(scheme)))

    #getting the host
    link = link.replace(scheme + "://", "")
    host = link
    if "/" in link:
        host = link[:link.index("/")]
    hostNode = BNode()
    g.add((linkIRI, ON.host, hostNode))
    g.add((hostNode, RDF.type, ON.Host))
    g.add((hostNode, ON.name, Literal(host)))

    #adding the host parts
    hostParts = host.split(".")
    g.add((hostNode, ON.nbHostParts, Literal(len(hostParts))))
    numHostPart = 0
    for hostPart in hostParts:
        hostPartNode = BNode()
        numHostPart = numHostPart + 1
        g.add((hostPartNode, RDF.type, ON.HostPart))
        g.add((hostPartNode, ON.name, Literal(hostPart)))
        g.add((hostPartNode, ON.position, Literal(numHostPart)))
        g.add((hostNode, ON.hostPart, hostPartNode))
コード例 #27
0
def constructnewgraph(constructlist1, user):
    service = URIRef('ex:service')
    hasService = URIRef('ex:hasService')
    hasArguments = URIRef('ex:hasArguments')

    g = ConjunctiveGraph()
    intentnodeString = Literal(user)
    intentNode = BNode(intentnodeString)

    for q in constructlist1:
        sn = URIRef(q.service)
        if sn.upper() != "FOR":
            g.add((intentNode, hasService, Literal(sn)))
            for r in q.args:
                g.add((sn, hasArguments, Literal(r)))
    #print "Graph constructed for intent input!"

    # for subj, pred, obj in g:
    # print subj, pred, obj

    dot = Digraph(comment='Input Intent')
    for subj, pred, obj in g:
        dot.node(subj, subj)
        dot.node(obj, obj)
        dot.edge(subj, obj, pred, constraint='false')

    # print(dot.source)
    dot.format = 'png'
    # filenameput=filerenderpath+'/inputintent-dot'
    dot.render('../static/inputintent-dot', view=False)

    print("Creating intent graph in html output....")
    try:
        fname = '../templates/userinput.html'
        file = open(fname, 'w')
        firstpart = """<!DOCTYPE html>
		<html>
		<head>
		<title>First graph</title>
		<meta name="description" content="A concept map diagram ." />
		<meta charset="UTF-8">
		<script src="go.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.6.7/go-debug.js"></script>

		<link href="../assets/css/goSamples.css" rel="stylesheet" type="text/css" />  
		<script src="goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
		<script id="code">
		function init() {
		if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this
		var $ = go.GraphObject.make;  // for conciseness in defining templates
		myDiagram =
		$(go.Diagram, "myDiagramDiv",  // must name or refer to the DIV HTML element
			{\n
			initialAutoScale: go.Diagram.Uniform,  // an initial automatic zoom-to-fit\n
			contentAlignment: go.Spot.Center,  // align document to the center of the viewport\n
			layout:\n
			$(go.ForceDirectedLayout,  // automatically spread nodes apart\n
				{ defaultSpringLength: 30, defaultElectricalCharge: 100 })\n
			});\n
		// define each Node's appearance\n
		myDiagram.nodeTemplate =
		$(go.Node, "Auto",  // the whole node panel
			{ locationSpot: go.Spot.Center },
			// define the node's outer shape, which will surround the TextBlock\n
			$(go.Shape, "Rectangle",
				{ fill: $(go.Brush, "Linear", { 0: "rgb(254, 201, 0)", 1: "rgb(254, 162, 0)" }), stroke: "black" }),
				$(go.TextBlock, 
				{ font: "bold 10pt helvetica, bold arial, sans-serif", margin: 4 },
				new go.Binding("text", "text"))
			);
			// replace the default Link template in the linkTemplateMap
			myDiagram.linkTemplate =
			$(go.Link,  // the whole link panel
				$(go.Shape,  // the link shape
					{ stroke: "black" }),
					$(go.Shape,  // the arrowhead
						{ toArrow: "standard", stroke: null }),
						$(go.Panel, "Auto",
							$(go.Shape,  // the label background, which becomes transparent around the edges
								{ fill: $(go.Brush, "Radial", { 0: "rgb(240, 240, 240)", 0.3: "rgb(240, 240, 240)", 1: "rgba(240, 240, 240, 0)" }),
								stroke: null }),
						$(go.TextBlock,  // the label text
							{ textAlign: "center",
							font: "10pt helvetica, arial, sans-serif",
							stroke: "#555555", margin: 4 },
							new go.Binding("text", "text"))
								));
						// create the model for the concept map\n"""
        file.write(firstpart)
        cnode = 0

        # create a list of ids for plotting the js graph
        uniqueidlist = []
        for subj, pred, obj in g:
            flagnodefound = 0
            for j in uniqueidlist:
                if Literal(subj) == j:
                    flagnodefound = 1
            if flagnodefound == 0:
                uniqueidlist.append(Literal(subj))

        for subj, pred, obj in g:
            flagnodefound = 0
            for j in uniqueidlist:
                if Literal(obj) == j:
                    flagnodefound = 1
                    # print "found name " + j
            if flagnodefound == 0:
                # print obj
                uniqueidlist.append(Literal(obj))
        # adding links

        nodeDAstring = ''
        linkDAstring = ''
        tempstr = ""
        for j in uniqueidlist:
            # print nodeDAstring
            # print uniqueidlist.index(j)
            checkcommas = j.replace("'", "")

            tempstr = "{ key:" + str(uniqueidlist.index(j)) + \
                ", text: '" + checkcommas + "' },"
            # print tempstr
            nodeDAstring += tempstr

        tempstr = ""
        for subj, pred, obj in g:
            # print uniqueidlist.index(Literal(subj)),
            # uniqueidlist.index(Literal(obj)), pred
            tempstr = "{ from:" + str(uniqueidlist.index(
                Literal(subj))) + ", to:" + str(
                    uniqueidlist.index(
                        Literal(obj))) + ", text: '" + Literal(pred) + "'},"
            linkDAstring += tempstr

        file.write("""   var nodeDataArray = [""")

        file.write(nodeDAstring)

        file.write("{} ];")
        file.write("    var linkDataArray = [")
        file.write(linkDAstring)
        file.write("{} ];")
        secondpart = """    
				myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
				}
				</script>
				</head>
				<body onload="init()">
				<div id="sample">
				<h3>User Intent</h3>
				<div id="myDiagramDiv" style="background-color: whitesmoke; border: solid 1px black; width: 100%; height: 700px"></div>
				<p>
				The actual user intent received by INDIRA. 
				See also the <a href="interactiveForce.html">Interactive Force</a> sample that uses the exact same data
				but a different node template and a customized <a>ForceDirectedLayout</a>.
				</p>
				</div>
				</body>
				</html>"""
        file.write(secondpart)
        file.close()
    except:
        print("file writing error occured")
        sys.exit(0)

    return g
コード例 #28
0
    def __init__(self, graph, closure):
        """
        @param graph: the graph to be modified
        """
        self.lit_to_bnode = {}
        self.bnode_to_lit = {}
        self.graph = graph

        to_be_removed = []
        to_be_added = []
        for t in self.graph:
            (subj, pred, obj) = t
            # This is supposed to be a "proper" graph, so only the obj may be a literal
            if isinstance(obj, rdflibLiteral):
                # Test the validity of the datatype
                if obj.datatype:
                    try:
                        converter = AltXSDToPYTHON[obj.datatype]
                    except KeyError:
                        converter = lambda v: v
                    try:
                        converter(text_type(obj))
                    except ValueError:
                        closure.add_error(
                            "Lexical value of the literal '%s' does not match its datatype (%s)"
                            % (text_type(obj), obj.datatype))

                # In any case, this should be removed:
                if t not in to_be_removed:
                    to_be_removed.append(t)
                # Check if a BNode has already been associated with that literal
                obj_st = _LiteralStructure(obj)
                found = False
                for l in list(self.lit_to_bnode.keys()):
                    if obj_st.lex == l.lex and obj_st.dt == l.dt and obj_st.lang == l.lang:
                        t1 = (subj, pred, self.lit_to_bnode[l])
                        to_be_added.append(t1)
                        found = True
                        break
                if not found:
                    # the bnode has to be created
                    bn = BNode()
                    # store this in the internal administration
                    self.lit_to_bnode[obj_st] = bn
                    self.bnode_to_lit[bn] = obj_st
                    # modify the graph
                    to_be_added.append((subj, pred, bn))
                    to_be_added.append((bn, type, Literal))
                    # Furthermore: a plain literal should be identified with a corresponding xsd:string and vice versa,
                    # cf, RDFS Semantics document
                    if obj_st.dt is None and obj_st.lang is None:
                        newLit = rdflibLiteral(obj_st.lex,
                                               datatype=ns_xsd["string"])
                        new_obj_st = _LiteralStructure(newLit)
                        new_obj_st.dt = ns_xsd["string"]
                        bn2 = BNode()
                        self.lit_to_bnode[new_obj_st] = bn2
                        self.bnode_to_lit[bn2] = new_obj_st
                        to_be_added.append((subj, pred, bn2))
                        to_be_added.append((bn2, type, Literal))
                    elif obj_st.dt == ns_xsd["string"]:
                        newLit = rdflibLiteral(obj_st.lex, datatype=None)
                        new_obj_st = _LiteralStructure(newLit)
                        # new_obj_st = _LiteralStructure(obj) # Was this the correct one, or was this an old bug?
                        new_obj_st.dt = None
                        bn2 = BNode()
                        self.lit_to_bnode[new_obj_st] = bn2
                        self.bnode_to_lit[bn2] = new_obj_st
                        to_be_added.append((subj, pred, bn2))
                        to_be_added.append((bn2, type, Literal))

        # Do the real modifications
        self._massageGraph(to_be_removed, to_be_added)
コード例 #29
0
ファイル: serialize.py プロジェクト: tobby2002/django-rdf-io
def build_rdf( gr,obj, oml, includemembers ) :  

    # would be nice to add some comments : as metadata on the graph? '# Turtle generated by django-rdf-io configurable serializer\n'  
    for om in oml :
        # check filter
        objfilter = getattr(om,'filter') 
        if objfilter and not apply_pathfilter(obj, objfilter ) :
            continue
        try:
            tgt_id = getattr_path(obj,om.id_attr)[0]
        except ValueError as e:
            raise ValueError("target id attribute {} not found".format( (om.id_attr ,)))
        if om.target_uri_expr[0] == '"' :   
            uribase = om.target_uri_expr[1:-1]
        else:
            uribase = getattr_path(obj,om.target_uri_expr)[0]
            
        tgt_id = str(tgt_id).replace(uribase,"")
        # strip uri base if present in tgt_id
        uribase = expand_curie(uribase)
        
 
        if not tgt_id:
            uri = uribase
        elif uribase[-1] == '/' or uribase[-1] == '#' :
            uri = "".join((uribase,tgt_id))
        else :
            uri = "/".join((uribase,tgt_id))
        
        subject = URIRef(uri)
        
        for omt in om.obj_type.all() :
            gr.add( (subject, RDF.type , _as_resource(gr,omt.uri)) )
  
        # now get all the attribute mappings and add these in
        for am in AttributeMapping.objects.filter(scope=om) :
            _add_vals(gr, obj, subject, am.predicate, am.attr , am.is_resource)
        for em in EmbeddedMapping.objects.filter(scope=om) :
            try:
                # three options - scalar value in which case attributes relative to basic obj, a mulitvalue obj or we have to look for related objects
                try:
                    valuelist = getattr_path(obj,em.attr)
                except:
                    valuelist = [obj,] 

                for value in valuelist :
                    newnode = None
 
                    for element in em.struct.split(";") :
                        try:
                            (predicate,expr) = element.split()
                        except:
                            predicate = None
                            expr = element
                            
                        # resolve any internal template parameters {x}
                        expr = expr.replace("{$URI}", uri )

                        is_resource = False
                        if expr.startswith("<") :
                            is_resource = True
                            expr = expr[1:-1].join(('"','"'))
                        elif expr.startswith("/") :
                            #value relativeto root obj  - retrieve and use as literal
                            try:
                                expr = iter(getattr_path(obj,expr[1:])).next()
                                if type(expr) == str :
                                    expr = expr.join( ('"','"'))
                            except:
                                raise ValueError( "Could not access value of %s from mapped object %s (/ is relative to the object being mapped" % (expr,obj) )
                        else:
                            is_resource = False
                        
                        for (lit,var,x,y) in Formatter().parse(expr) :
                            if var :
                                try:
                                    if var.startswith("^"):
                                        val = iter(getattr_path(obj,var[1:])).next()
                                    else:
                                        val = iter(getattr_path(value,var)).next()
                                    
                                    if is_resource:
                                        try:
                                            val = u.urlencode({ 'v' : val.encode('utf-8')})[2:]
                                        except:
                                            #not a string, just pass
                                            pass
                                    val = str(val)
                                except:
                                    val="{!variable not found : %s}" % var
                                expr = expr.replace(var.join(("{","}")), val )
                        if predicate :
                            # an internal struct has been found so add a new node if not ye done
                            if not newnode:
                                newnode = BNode()
                                gr.add( (subject, _as_resource(gr,em.predicate) , newnode) )
                            _add_vals(gr, value, newnode, predicate, expr , is_resource)
                        else:
                            # add to parent
                            _add_vals(gr, value, subject, em.predicate, expr , is_resource)
            except Exception as e:
                import traceback; import sys; traceback.print_exc()
                print "Could not evaluate extended mapping %s : %s " % (e,em.attr), sys.exc_info()
                raise ValueError("Could not evaluate extended mapping %s : %s " % (e,em.attr))
    # do this after looping through all object mappings!
    return gr
コード例 #30
0
 def __init__(self, uri=None):
     if uri == None:
         OREResource.__init__(self, BNode())
     else:
         OREResource.__init__(self, uri)