예제 #1
0
import unittest

from rdflib.Graph import Graph
from rdflib.exceptions import SubjectTypeError
from rdflib.exceptions import PredicateTypeError
from rdflib.exceptions import ObjectTypeError
from rdflib.URIRef import URIRef

foo = URIRef("foo")


class TypeCheckCase(unittest.TestCase):
    unstable = True  # TODO: until we decide if we want to add type checking back to rdflib
    backend = 'default'
    path = 'store'

    def setUp(self):
        self.store = Graph(backend=self.backend)
        self.store.open(self.path)

    def tearDown(self):
        self.store.close()

    def testSubjectTypeCheck(self):
        self.assertRaises(SubjectTypeError, self.store.add, (None, foo, foo))

    def testPredicateTypeCheck(self):
        self.assertRaises(PredicateTypeError, self.store.add, (foo, None, foo))

    def testObjectTypeCheck(self):
        self.assertRaises(ObjectTypeError, self.store.add, (foo, foo, None))
예제 #2
0
    def __init__(self,
                 node,
                 graph,
                 inherited_state=None,
                 base="",
                 options=None):
        """
		@param node: the current DOM Node
		@param graph: the RDFLib Graph
		@keyword inherited_state: the state as inherited
		from upper layers. This inherited_state is mixed with the state information
		retrieved from the current node.
		@type inherited_state: L{State.ExecutionContext}
		@keyword base: string denoting the base URI for the specific node. This is
		overridden by a possible C{@xml:base}, but it overrides the possible
		base inherited from the upper layers. Note: C{@xml:base} is not officially part of the
		XHTML+RDFa syntax, but this could/should handle by the DTD validation of the
		incoming document. The code itself is prepared for the C{@xml:base} usage, in 
		accordnace with the reference (in the RDFa syntax document) to other XML dialects that might use it.
		@keyword options: invocation option
		@type options: L{Options<pyRdfa.Options>}
		"""
        #-----------------------------------------------------------------
        # settling the base
        # note that, strictly speaking, it is not necessary to add the base to the
        # context, because there is only one place to set it (<base> element of the <header>).
        # It is done because it is prepared for a possible future change in direction of
        # accepting xml:base on each element.
        # At the moment, it is invoked with a 'None' at the top level of parsing, that is
        # when the <base> element is looked for.
        if inherited_state:
            self.base = inherited_state.base
            self.warning_URI_ref = inherited_state.warning_URI_ref
            self.options = inherited_state.options
        else:
            # this is the branch called from the very top
            self.base = ""
            for bases in node.getElementsByTagName("base"):
                if bases.hasAttribute("href"):
                    self.base = bases.getAttribute("href")
                    continue
            if self.base == "":
                self.base = base
            if node.hasAttribute("xml:base"):
                self.base = node.getAttribute("xml:base")
            self.warning_URI_ref = URIRef(base)
            # this is just to play safe. I believe this branch should actually not happen...
            if options == None:
                from pyRdfa import Options
                self.options = Options()
            else:
                self.options = options

            # check the the presense of the @profile and or @version attribute for the RDFa profile...
            # (Not 100% sure that is necessary...)
            html = node.ownerDocument.documentElement
            if not (html.hasAttribute("version")
                    and RDFa_VERSION == html.getAttribute("version")):
                # see if least the profile has been set

                # Find the <head> element
                head = None
                for index in range(0, html.childNodes.length - 1):
                    if html.childNodes.item(index).nodeName == "head":
                        head = html.childNodes.item(index)
                        break

                if not (head != None and head.hasAttribute("profile")
                        and RDFa_PROFILE
                        in head.getAttribute("profile").strip().split()):
                    self.add_warning(
                        "Neither an RDFa profile nor an RFDa version is set")

        #-----------------------------------------------------------------
        # Settling the language tags
        # check first the lang or xml:lang attribute
        # RDFa does not allow the lang attribute. XHTML5 relies :-( on @lang;
        # I just want to be prepared here...
        if node.hasAttribute("lang"):
            self.lang = node.getAttribute("lang")
            if len(self.lang) == 0: self.lang = None
        elif node.hasAttribute("xml:lang"):
            self.lang = node.getAttribute("xml:lang")
            if len(self.lang) == 0: self.lang = None
        elif inherited_state:
            self.lang = inherited_state.lang
        else:
            self.lang = None

        #-----------------------------------------------------------------
        # Handling namespaces
        # First get the local xmlns declarations/namespaces stuff.
        dict = {}
        for i in range(0, node.attributes.length):
            attr = node.attributes.item(i)
            if attr.name.find('xmlns:') == 0:
                # yep, there is a namespace setting
                key = attr.localName
                if key != "":
                    # exclude the top level xmlns setting...
                    uri = attr.value
                    # 1. create a new Namespace entry
                    ns = Namespace(uri)
                    # 2. 'bind' it in the current graph to
                    # get a nicer output
                    graph.bind(key, uri)
                    # 3. Add an entry to the dictionary
                    dict[key] = ns

        # See if anything has been collected at all.
        # If not, the namespaces of the incoming state is
        # taken over
        self.ns = {}
        if len(dict) == 0 and inherited_state:
            self.ns = inherited_state.ns
        else:
            if inherited_state:
                for k in inherited_state.ns:
                    self.ns[k] = inherited_state.ns[k]
                # copying the newly found namespace, possibly overwriting
                # incoming values
                for k in dict:
                    self.ns[k] = dict[k]
            else:
                self.ns = dict

        # see if the xhtml core vocabulary has been set
        self.xhtml_prefix = None
        for key in self.ns.keys():
            if XHTML_URI == str(self.ns[key]):
                self.xhtml_prefix = key
                break
        if self.xhtml_prefix == None:
            if XHTML_PREFIX not in self.ns:
                self.ns[XHTML_PREFIX] = Namespace(XHTML_URI)
                self.xhtml_prefix = XHTML_PREFIX
            else:
                # the most disagreeable thing, the user has used
                # the prefix for something else...
                self.xhtml_prefix = XHTML_PREFIX + '_' + (
                    "%d" % random.randint(1, 1000))
                self.ns[self.xhtml_prefix] = Namespace(XHTML_URI)
            graph.bind(self.xhtml_prefix, XHTML_URI)

        # extra tricks for unusual usages...
        # if the 'rdf' prefix is not used, it is artificially added...
        if "rdf" not in self.ns:
            self.ns["rdf"] = ns_rdf
        if "rdfs" not in self.ns:
            self.ns["rdfs"] = ns_rdfs

        # Final touch: setting the default namespace...
        if node.hasAttribute("xmlns"):
            self.defaultNS = node.getAttribute("xmlns")
        elif inherited_state and inherited_state.defaultNS != None:
            self.defaultNS = inherited_state.defaultNS
        else:
            self.defaultNS = None
예제 #3
0
    def open(self, path, create=True):
        homeDir = path

        if self.__identifier is None:
            self.__identifier = URIRef(pathname2url(abspath(homeDir)))

        self.db_env = db_env = self._init_db_environment(homeDir, create)
        self.__open = True

        dbname = None
        dbtype = db.DB_BTREE
        # auto-commit ensures that the open-call commits when transactions are enabled
        dbopenflags = db.DB_THREAD
        if self.transaction_aware == True:
            dbopenflags |= db.DB_AUTO_COMMIT

        dbmode = 0660
        dbsetflags = 0

        # create and open the DBs
        self.__indicies = [
            None,
        ] * 3
        self.__indicies_info = [
            None,
        ] * 3
        for i in xrange(0, 3):
            index_name = to_key_func(i)(("s", "p", "o"), "c")
            index = db.DB(db_env)
            index.set_flags(dbsetflags)
            index.open(index_name, dbname, dbtype, dbopenflags | db.DB_CREATE,
                       dbmode)
            self.__indicies[i] = index
            self.__indicies_info[i] = (index, to_key_func(i), from_key_func(i))

        lookup = {}
        for i in xrange(0, 8):
            results = []
            for start in xrange(0, 3):
                score = 1
                len = 0
                for j in xrange(start, start + 3):
                    if i & (1 << (j % 3)):
                        score = score << 1
                        len += 1
                    else:
                        break
                tie_break = 2 - start
                results.append(((score, tie_break), start, len))

            results.sort()
            score, start, len = results[-1]

            def get_prefix_func(start, end):
                def get_prefix(triple, context):
                    if context is None:
                        yield ""
                    else:
                        yield context
                    i = start
                    while i < end:
                        yield triple[i % 3]
                        i += 1
                    yield ""

                return get_prefix

            lookup[i] = (self.__indicies[start],
                         get_prefix_func(start,
                                         start + len), from_key_func(start),
                         results_from_key_func(start, self._from_string))

        self.__lookup_dict = lookup

        self.__contexts = db.DB(db_env)
        self.__contexts.set_flags(dbsetflags)
        self.__contexts.open("contexts", dbname, dbtype,
                             dbopenflags | db.DB_CREATE, dbmode)

        self.__namespace = db.DB(db_env)
        self.__namespace.set_flags(dbsetflags)
        self.__namespace.open("namespace", dbname, dbtype,
                              dbopenflags | db.DB_CREATE, dbmode)

        self.__prefix = db.DB(db_env)
        self.__prefix.set_flags(dbsetflags)
        self.__prefix.open("prefix", dbname, dbtype,
                           dbopenflags | db.DB_CREATE, dbmode)

        self.__k2i = db.DB(db_env)
        self.__k2i.set_flags(dbsetflags)
        self.__k2i.open("k2i", dbname, db.DB_HASH, dbopenflags | db.DB_CREATE,
                        dbmode)

        self.__i2k = db.DB(db_env)
        self.__i2k.set_flags(dbsetflags)
        self.__i2k.open("i2k", dbname, db.DB_RECNO, dbopenflags | db.DB_CREATE,
                        dbmode)

        self.__needs_sync = False
        t = Thread(target=self.__sync_run)
        t.setDaemon(True)
        t.start()
        self.__sync_thread = t
        return VALID_STORE
예제 #4
0
maxSeconds = 1

skip = {
    #    'http://www.w3.org/2002/03owlt/I5.2/Manifest002#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/equivalentClass/Manifest004#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/cardinality/Manifest001#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/cardinality/Manifest002#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/cardinality/Manifest003#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/cardinality/Manifest004#test': "no DT theory",
    #    'http://www.w3.org/2002/03owlt/description-logic/Manifest903#test': "loop",
}

maxFailed = 999999
failed = 0
system = URIRef('http://www.w3.org/2003/08/surnia/data/surnia')


def run(store, test, name, input, entailed, expected, resultStore):

    global failed

    axiomTags = []

    dtlist = []
    for dt in store.objects(test, OTEST["supportedDatatype"]):
        dtlist.append(dt)

    for er in store.objects(test, RTEST["entailmentRules"]):
        if str(er) == "http://www.w3.org/1999/02/22-rdf-syntax-ns":
            tag = "RDF"
예제 #5
0
 def setUp(self):
     self.gold = Ontology(
         identifier=URIRef(u'http://purl.org/linguistics/data/myonto/'))
     self.gold.parse(ELTK_HOME + "/examples/inputfiles/test_ontology.owl")
예제 #6
0
    def open(self, path, create=True):
        if self.__open:
            return
        homeDir = path
        #NOTE: The identifeir is appended to the path as the location for the db
        #This provides proper isolation for stores which have the same path but different identifiers
        if SUPPORT_MULTIPLE_STORE_ENVIRON:
            fullDir = join(homeDir,self.identifier)
        else:
            fullDir = homeDir
        envsetflags  = db.DB_CDB_ALLDB
        envflags = db.DB_INIT_MPOOL | db.DB_INIT_LOCK | db.DB_THREAD | db.DB_INIT_TXN | db.DB_RECOVER
        if not exists(fullDir):
            if create==True:
                makedirs(fullDir)
                self.create(path)
            else:                
                return NO_STORE
        if self.__identifier is None:
            self.__identifier = URIRef(pathname2url(abspath(fullDir)))
        self.db_env = db_env = db.DBEnv()
        db_env.set_cachesize(0, 1024*1024*50) # TODO
        #db_env.set_lg_max(1024*1024)
        #db_env.set_flags(envsetflags, 1)
        db_env.open(fullDir, envflags | db.DB_CREATE,0)

        #Transaction object
        self.dbTxn = db_env.txn_begin()

        self.__open = True

        dbname = None
        dbtype = db.DB_BTREE
        dbopenflags = db.DB_THREAD

        dbmode = 0660
        dbsetflags   = 0

        # create and open the DBs
        self.__indicies = [None,] * 3
        self.__indicies_info = [None,] * 3
        for i in xrange(0, 3):
            index_name = to_key_func(i)(("s", "p", "o"), "c")
            index = db.DB(db_env)
            index.set_flags(dbsetflags)
            index.open(index_name, dbname, dbtype, dbopenflags|db.DB_CREATE, dbmode,txn=self.dbTxn)
            self.__indicies[i] = index
            self.__indicies_info[i] = (index, to_key_func(i), from_key_func(i))

        lookup = {}
        for i in xrange(0, 8):
            results = []
            for start in xrange(0, 3):
                score = 1
                len = 0
                for j in xrange(start, start+3):
                    if i & (1<<(j%3)):
                        score = score << 1
                        len += 1
                    else:
                        break
                tie_break = 2-start
                results.append(((score, tie_break), start, len))

            results.sort()
            score, start, len = results[-1]

            def get_prefix_func(start, end):
                def get_prefix(triple, context):
                    if context is None:
                        yield ""
                    else:
                        yield context
                    i = start
                    while i<end:
                        yield triple[i%3]
                        i += 1
                    yield ""
                return get_prefix

            lookup[i] = (self.__indicies[start], get_prefix_func(start, start + len), from_key_func(start), results_from_key_func(start, self._from_string))


        self.__lookup_dict = lookup

        self.__contexts = db.DB(db_env)
        self.__contexts.set_flags(dbsetflags)
        self.__contexts.open("contexts", dbname, dbtype, dbopenflags|db.DB_CREATE, dbmode,txn=self.dbTxn)

        self.__namespace = db.DB(db_env)
        self.__namespace.set_flags(dbsetflags)
        self.__namespace.open("namespace", dbname, dbtype, dbopenflags|db.DB_CREATE, dbmode,txn=self.dbTxn)

        self.__prefix = db.DB(db_env)
        self.__prefix.set_flags(dbsetflags)
        self.__prefix.open("prefix", dbname, dbtype, dbopenflags|db.DB_CREATE, dbmode,txn=self.dbTxn)

        self.__i2k = db.DB(db_env)
        self.__i2k.set_flags(dbsetflags)
        self.__i2k.open("i2k", dbname, db.DB_HASH, dbopenflags|db.DB_CREATE, dbmode,txn=self.dbTxn)

        self.__needs_sync = False
        t = Thread(target=self.__sync_run)
        t.setDaemon(True)
        t.start()
        self.__sync_thread = t
        return VALID_STORE
예제 #7
0
def _process_DOM(dom,base,outputFormat,options,local=False) :
	"""Core processing. The transformers ("pre-processing") is done
	on the DOM tree, the state is initialized, and the "real" RDFa parsing is done. Finally,
	the result (which is an RDFLib Graph) is serialized using RDFLib's serializers.

	The real work is done in the L{parser function<Parse.parse_one_node>}.

	@param dom: XML DOM Tree node (for the top level)
	@param base: URI for the default "base" value (usually the URI of the file to be processed)
	@param outputFormat: serialization format
	@param options: Options for the distiller
	@type options: L{Options}
	@keyword local: whether the call is for a local usage or via CGI (influences the way
	exceptions are handled)
	@return: serialized graph
	@rtype: string
	"""
	def _register_XML_serializer(formatstring) :
		from rdflib.plugin import register
		from rdflib.syntax import serializer, serializers
		register(formatstring,serializers.Serializer,"pyRdfa.serializers.PrettyXMLSerializer","PrettyXMLSerializer")
	def _register_Turtle_serializer(formatstring) :
		from rdflib.plugin import register
		from rdflib.syntax import serializer, serializers
		register(formatstring,serializers.Serializer,"pyRdfa.serializers.TurtleSerializer","TurtleSerializer")

	# Exchaning the pretty xml serializer agaist the version stored with this package
	if outputFormat == "pretty-xml" :
		outputFormat = "my-xml"
		_register_XML_serializer(outputFormat)
	elif outputFormat == "turtle" or outputFormat == "n3" :
		outputFormat = "my-turtle"
		_register_Turtle_serializer(outputFormat)

	# Create the RDF Graph
	graph   = Graph()
	# get the DOM tree

	html 	= dom.documentElement

	# Perform the built-in and external transformations on the HTML tree. This is,
	# in simulated form, the hGRDDL approach of Ben Adida
	for trans in options.transformers + builtInTransformers :
		trans(html,options)

	# collect the initial state. This takes care of things
	# like base, top level namespace settings, etc.
	# Ensure the proper initialization
	state = ExecutionContext(html,graph,base=base,options=options)

	# The top level subject starts with the current document; this
	# is used by the recursion
	subject = URIRef(state.base)

	# parse the whole thing recursively and fill the graph
	if local :
		parse_one_node(html,graph,subject,state,[])
		if options.warning_graph != None :
			for t in options.warning_graph : graph.add(t)
		retval = graph.serialize(format=outputFormat)
	else :
		# This is when the code is run as part of a Web CGI service. The
		# difference lies in the way exceptions are handled...
		try :
			# this is a recursive procedure through the full DOM Tree
			parse_one_node(html,graph,subject,state,[])
		except :
			# error in the input...
			(type,value,traceback) = sys.exc_info()
			msg = 'Error in RDFa content: "%s"' % value
			raise RDFaError, msg
		# serialize the graph and return the result
		retval = None
		try :
			if options.warning_graph != None :
				for t in options.warning_graph : graph.add(t)
			retval = graph.serialize(format=outputFormat)
		except :
			# XML Parsing error in the input
			(type,value,traceback) = sys.exc_info()
			msg = 'Error in graph serialization: "%s"' % value
			raise RDFaError, msg
	return retval
예제 #8
0
 def next_li(self):
     self.li += 1
     return URIRef(RDFNS + "_%s" % self.li)
예제 #9
0
def getURI(self):
    return URIRef(self.uri)
예제 #10
0
class OWLClass(type):

    #used to get OWLClass to be of type 'RDFSClass'
    __metaclass__ = RDFSClass

    def __init__(cls, name, bases, dict):

        #call the init method of the superclass
        super(OWLClass, cls).__init__(name, bases, dict)

    uri = URIRef(u'http://www.w3.org/2002/07/owl#Class')

    @staticmethod
    def new(uri, bases=(OWLThing, )):
        """
        'new' creates a new class in memory
        """

        #has to be string
        uri = quote(URIRef(uri))

        name = getLocalName(uri)

        #private list of instances
        _instances = []

        cls = OWLClass(name, bases, {
            'uri': uri,
            'name': name,
            '_instances': _instances,
            'bases': bases
        })

        #add the appropriate class methods
        cls.getURI = instancemethod(getURI, cls)
        cls.getInstances = instancemethod(getInstances, cls)

        return cls

    def __call__(cls, *lstArgs, **dictArgs):
        """
        Creates an instance (OWL individual)
        """

        #create the instance of cls in memory
        instance = cls.__new__(cls)

        #doesn't seem to be necessary
        #instance.__init__()

        #set attributes

        #gives the illusion of instantiating multiple classes
        #this is a dirty hack, but perhaps all that's possible given
        #Python's semantics
        #print type(instance)

        instance._types = []

        if len(lstArgs) == 2:
            instance._types = lstArgs[1]

        instance._types.append(__builtin__.type(instance))

        #store the type name for use in Ontology.py
        if len(lstArgs) == 2:
            instance._type_names = []
            for t in lstArgs[1]:
                instance._type_names.append(t.name)

        #quote is not strictly needed here, but it's added to be
        #consistent with the uri of 'cls' in 'new'
        instance.uri = quote(URIRef(lstArgs[0]))

        instance.name = getLocalName(instance.uri)

        #instance.uri=instance.uri.replace('#','/')

        #instance.getTypeNames=instancemethod(getTypeNames,instance,cls)

        instance.getURI = instancemethod(getURI, instance, cls)

        #print 'created a '+cls.name
        cls._instances.append(instance)

        return instance
예제 #11
0
class RDFSResource(type):
    """
    RDFSResource is at the top of the metaclass hierarchy, the mother of all types, except for Python's builtin type.
    """
    uri = URIRef(u'http://www.w3.org/2000/01/rdf-schema#Resource')
예제 #12
0
    def parse(self, source, sink, baseURI=None):
        self.sink = RDFaSink(sink)
        self.triple = self.sink.triple
        self.prefix = self.sink.prefix
        self.baseuri = baseURI or source.getPublicId()
        f = source.getByteStream()
        events = pulldom.parse(f)
        self.handler = events.pulldom
        for (event, node) in events:

            if event == pulldom.START_DOCUMENT:
                self.abouts += [(URIRef(""), node)]

            if event == pulldom.END_DOCUMENT:
                assert len(self.elementStack) == 0

            if event == pulldom.START_ELEMENT:

                # keep track of parent node
                self.elementStack += [node]

                #if __debug__: print [e.tagName for e in self.elementStack if e]

                found = filter(lambda x: x in node.attributes.keys(),
                               rdfa_attribs)

                # keep track of xml:lang xml:base
                baseuri = node.getAttributeNS(
                    xml,
                    "base") or node.getAttribute("xml:base") or self.baseuri
                self.baseuri = _urljoin(self.baseuri, baseuri)
                self.xmlbases.append(self.baseuri)

                if node.hasAttributeNS(
                        xml, "lang") or node.hasAttribute("xml:lang"):
                    lang = node.getAttributeNS(
                        xml, 'lang') or node.getAttribute('xml:lang')

                    if lang == '':
                        # xml:lang could be explicitly set to '', we need to capture that
                        lang = None
                else:
                    # if no xml:lang is specified, use parent lang
                    lang = self.lang

                self.lang = lang
                self.langs.append(lang)

                # node is not an RDFa element.
                if len(found) == 0: continue

                parentNode = self.elementStack[-2]

                if "about" in found:
                    self.abouts += [
                        (self.extractCURIEorURI(node.getAttribute("about")),
                         node)
                    ]
                elif "id" in found:
                    self.abouts += [
                        (self.extractCURIEorURI("#" + node.getAttribute("id")),
                         node)
                    ]

                subject = self.abouts[-1][0]

                # meta/link subject processing
                if (node.tagName == "meta" or node.tagName == "link"):
                    if not ("about" in found) and parentNode:
                        if parentNode and parentNode.tagName == "head":
                            subject = URIRef("")
                        elif (parentNode.hasAttribute("about")):
                            subject = self.extractCURIEorURI(
                                parentNode.getAttribute("about"))
                        elif parentNode.hasAttributeNS(
                                xml, "id") or parentNode.hasAttribute("id"):
                            # TODO: is this the right way to process xml:id by adding a '#'
                            id = parentNode.getAttributeNS(
                                xml, "id") or parentNode.getAttribute("id")
                            subject = self.extractCURIEorURI("#" + id)
                        else:
                            subject = self.generateBlankNode(parentNode)

                if 'property' in found:
                    predicate = self.extractCURIEorURI(
                        node.getAttribute('property'))
                    literal = None
                    datatype = None
                    plaintext = False

                    if node.hasAttribute('datatype'):
                        sdt = node.getAttribute('datatype')
                        if sdt <> 'plaintext':
                            datatype = self.extractCURIEorURI(sdt)
                        else:
                            plaintext = True

                    if node.hasAttribute("content"):
                        literal = Literal(node.getAttribute("content"),
                                          lang=lang,
                                          datatype=datatype)
                    else:
                        events.expandNode(node)

                        # because I expanded, I won't get an END_ELEMENT
                        self._popStacks(event, node)

                        content = ""
                        for child in node.childNodes:
                            if datatype or plaintext:
                                content += self._getNodeText(child)
                            else:
                                content += child.toxml()
                        content = content.strip()
                        literal = Literal(content,
                                          datatype=datatype or rdf.XMLLiteral)

                    if literal:
                        self.triple(subject, predicate, literal)

                if "rel" in found:
                    rel = node.getAttribute("rel").strip()
                    if string.lower(rel) in reserved_links:
                        rel = xhtml["#" + string.lower(rel)]

                    predicate = self.extractCURIEorURI(rel)
                    if node.hasAttribute("href"):
                        object = self.extractCURIEorURI(
                            node.getAttribute("href"))
                        self.triple(subject, predicate, object)

                if "rev" in found:
                    predicate = self.extractCURIEorURI(
                        node.getAttribute("rev"))
                    if node.hasAttribute("href"):
                        object = self.extractCURIEorURI(
                            node.getAttribute("href"))
                        self.triple(object, predicate, subject)

                # role is in the primer, but not in the syntax.
                # could be deprecated.
                # Assumptions:
                # - Subject resolution as always (including meta/link)
                # - Attribute Value is a CURIE or URI
                # - It adds another triple, besides prop, rel, rev.
                if "role" in found:
                    type = self.extractCURIEorURI(node.getAttribute('role'))
                    self.triple(subject, rdf.type, type)

            if event == pulldom.END_ELEMENT:
                self._popStacks(event, node)

        # share with sink any prefix mappings
        for nsc in self.handler._ns_contexts:
            for ns, prefix in nsc.items():
                self.prefix(prefix, ns)

        f.close()
예제 #13
0
 def term(self, name):
     return URIRef(self + name)
예제 #14
0
 def absolutize(self, uri):
     s = urljoin(self.current.base, uri, allow_fragments=1)
     if uri and uri[-1] == "#":
         return URIRef(''.join((s, "#")))
     else:
         return URIRef(s)
    print 'Content-Type: text/html\r'
    print '\r'
    print "<H1>Error</H1>"
    print "Please fill in the 'description' field with an appropriate Turtle graph."

else:
    tmpfile = mkstemp()[1]
    data = convert(form['description'].value,
                   'turtle',
                   'ntriples',
                   infile=tmpfile)

    graph = TripleStore()
    graph.parse(StringIO(data), format='nt')

    thisURI = URIRef('http://%s%s' %
                     (os.environ['HTTP_HOST'], os.environ['REQUEST_URI']))

    graphNode = URIRef('file://%s' % tmpfile)

    FOAF = Namespace("http://xmlns.com/foaf/0.1/")

    try:
        bnode = graph.objects(graphNode, FOAF["primaryTopic"]).next()
    except StopIteration:
        print 'Content-Type: text/plain\r'
        print '\r'
        print 'no primary topic given'

    graph2 = TripleStore()

    for (s, p, o) in graph.triples((None, None, None)):
예제 #16
0
    def property_element_start(self, name, qname, attrs):
        name, atts = self.convert(name, qname, attrs)
        current = self.current
        absolutize = self.absolutize
        next = self.next
        object = None
        current.list = None

        if not name.startswith(RDFNS):
            current.predicate = absolutize(name)
        elif name == LI:
            current.predicate = current.next_li()
        elif name in PROPERTY_ELEMENT_EXCEPTIONS:
            self.error("Invalid property element URI: %s" % name)
        else:
            current.predicate = absolutize(name)

        id = atts.get(ID, None)
        if id is not None:
            if not is_ncname(id):
                self.error("rdf:ID value is not a value NCName: %s" % id)
            current.id = absolutize("#%s" % id)
        else:
            current.id = None

        resource = atts.get(RESOURCE, None)
        nodeID = atts.get(NODE_ID, None)
        parse_type = atts.get(PARSE_TYPE, None)
        if resource is not None and nodeID is not None:
            self.error(
                "Property element cannot have both rdf:nodeID and rdf:resource"
            )
        if resource is not None:
            object = absolutize(resource)
            next.start = self.node_element_start
            next.end = self.node_element_end
        elif nodeID is not None:
            if not is_ncname(nodeID):
                self.error("rdf:nodeID value is not a valid NCName: %s" %
                           nodeID)
            if nodeID in self.bnode:
                object = self.bnode[nodeID]
            else:
                subject = BNode()
                self.bnode[nodeID] = subject
                object = subject
            next.start = self.node_element_start
            next.end = self.node_element_end
        else:
            if parse_type is not None:
                for att in atts:
                    if att != PARSE_TYPE and att != ID:
                        self.error("Property attr '%s' now allowed here" % att)
                if parse_type == "Resource":
                    current.subject = object = BNode()
                    current.char = self.property_element_char
                    next.start = self.property_element_start
                    next.end = self.property_element_end
                elif parse_type == "Collection":
                    current.char = None
                    next.start = self.node_element_start
                    next.end = self.list_node_element_end
                else:  #if parse_type=="Literal":
                    # All other values are treated as Literal
                    # See: http://www.w3.org/TR/rdf-syntax-grammar/#parseTypeOtherPropertyElt
                    #object = Literal("", current.language, XMLLiteral)
                    object = Literal("", "", XMLLiteral)
                    current.char = self.literal_element_char
                    current.declared = {}
                    next.start = self.literal_element_start
                    next.char = self.literal_element_char
                    next.end = self.literal_element_end
                current.object = object
                return
            else:
                object = None
                current.char = self.property_element_char
                next.start = self.node_element_start
                next.end = self.node_element_end

        datatype = current.datatype = atts.get(DATATYPE, None)
        language = current.language
        if datatype is not None:
            # TODO: check that there are no atts other than datatype and id
            pass
        else:
            for att in atts:
                if not att.startswith(RDFNS):
                    predicate = absolutize(att)
                elif att in PROPERTY_ELEMENT_ATTRIBUTES:
                    continue
                elif att in PROPERTY_ATTRIBUTE_EXCEPTIONS:
                    self.error("""Invalid property attribute URI: %s""" % att)
                else:
                    predicate = absolutize(att)

                if att == TYPE:
                    o = URIRef(atts[att])
                else:
                    o = Literal(atts[att], language, datatype)

                if object is None:
                    object = BNode()
                self.store.add((object, predicate, o))
        if object is None:
            object = Literal("", language, datatype)
        current.object = object
예제 #17
0
    def makeLinkedData(self, filename, ns='http://purl.org/linguistics/data/'):
        """
        Create a Termset object

        :param uri: a uri
        :type uri: rdflib.URIRef.URIRef
        :rtype: eltk.kb.LinkedData.Termset
        """
        self.ns = ns

        #read the file
        Reader.read(self, filename)

        #the data container
        self.termset = Termset()

        #termset object
        mytermset = GOLD.Termset(
            URIRef(u'http://purl.org/linguistics/data/termset/mytermset'))

        #process file line by line
        lines = self.inputfile.readlines()

        for line in lines:

            if line.startswith('#'): continue

            data = line.split(',')

            #fix spaces between term name
            name = data[1].strip().replace(' ', '_')

            #if no orthographic representation is given, use abbreviation
            if name == '':
                name = data[0].strip()

            #create a gold:Term
            term_uri = URIRef(u'http://purl.org/linguistics/data/term/' + name)

            #create a term
            term = GOLD.Term(URIRef(term_uri), [])

            #add term to data store

            self.termset += (term, RDFtype, GOLD.Term)

            #add term to termset

            self.termset += (mytermset, GOLD.hasTerm, term)

            #link term to its abbreviation
            if data[0]:

                self.termset += (term, GOLD.abbreviation, data[0].strip())

            #link term to its representation
            if data[1].strip():

                self.termset += (term, GOLD.orthographicRep, data[1].strip())

            #attempt to reference OGLD entity referred to by term
            try:
                #retrieve GOLD entity referred to by term
                entity = getattr(GOLD, data[2].strip())

                #link term to the appropriate GOLD entity
                self.termset += (term, GOLD.hasMeaning, entity)

            #link term to its comment
            #need to alter Ontology.py for this

            except AttributeError:
                print 'Skipping \"' + name + '\", not in current version of GOLD.'

        return self.termset
예제 #18
0
 def __getitem__(self, key, default=None):
     return URIRef(self + key)
예제 #19
0
    def setUp(self):

        #create onto URI
        self.uri = URIRef(u'http://purl.org/linguistics/test_onto.owl')
        #create onto
        self.myonto = Ontology(self.uri)