Permalink
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (105 sloc) 5.62 KB
==============
RDF Marshaller
==============
With RDF marshaller you can export any Archetype content to RDF. It provides a
few general adaptors for ATContentTypes and ATVocabularyManager. You can then
look in eea.soer to find out how to customize these adaptors for your own RDF
schemas and own content types.
>>> self.setRoles(['Manager'])
>>> folder = self.portal[self.portal.invokeFactory('Folder', id='valentine')]
>>> news = folder[folder.invokeFactory('News Item', id='news1')]
>>> news.setTitle('my news item')
>>> news.setCreators(['ab','sasha','ture'])
>>> news.setDescription('my description')
>>> news.setText(u'my news rocks!')
>>> folder.portal_workflow.doActionFor(news, 'publish')
>>> folder.portal_workflow.doActionFor(folder, 'publish')
Add an Image so we can test marshalling binary objects.
>>> img = folder[folder.invokeFactory('Image', id='img1',
... title='Image title')]
>>> from Products.CMFCore.utils import getToolByName
>>> from Products.ATVocabularyManager.config import TOOL_NAME
>>> ps = getToolByName(self.portal, 'portal_skins', None)
>>> imgdata = ps.plone_images['logo.png']._data
>>> img.setImage(imgdata)
>>> zipf= folder[folder.invokeFactory('File', id='zip1',
... title='Zip file')]
>>> zipf.setFile('svenska tecken öäå')
>>> pdff= folder[folder.invokeFactory('File', id='pdf1',
... title='Pdf file')]
>>> pdff.setFile('svenska tecken öäå')
>>> folder.setRights('copyright by me!!!')
>>> from Products.Marshall.registry import getComponent
>>> marshaller = getComponent('surfrdf')
>>> import surf
>>> surf.ns.register(NEWSITEM='%s#' % self.portal.portal_types['News Item'].absolute_url())
>>> surf.ns.register(FOLDER='%s#' % self.portal.portal_types['Folder'].absolute_url())
Let's marshall the folder and the news item
>>> content_type, length, data = marshaller.marshall(folder)
>>> content_type, length, datanews = marshaller.marshall(news)
>>> #print data
Lets verify our RDF export
>>> from StringIO import StringIO
>>> store = surf.Store(reader='rdflib', writer='rdflib', rdflib_store ='IOMemory')
>>> session = surf.Session(store)
>>> from eea.rdfmarshaller.interfaces import IObject2Surf
>>> from zope.component import getMultiAdapter
>>> adapter = getMultiAdapter((folder, session), interface=IObject2Surf)
Load folder triples into the rdf store
>>> store.load_triples(source=StringIO(data))
True
Load news triples into the rdf store
>>> store.load_triples(source=StringIO(datanews))
True
>>> Folder = session.get_class(surf.ns.FOLDER['Folder'])
>>> folderIds = [obj.folder_id.first.strip() for obj in Folder.all().order()]
>>> folderIds
[u'valentine']
>>> NewsItem = session.get_class(surf.ns.NEWSITEM['NewsItem'])
>>> [(obj.newsitem_id.first.strip(), obj.newsitem_text.first.strip())
... for obj in NewsItem.all().order()]
[(u'news1', u'<p>my news rocks!</p>')]
Exporting a vocabulary
>>> surf.ns.register(SIMPLEVOCABULARY='%s#' % self.portal.portal_types['SimpleVocabulary'].absolute_url())
>>> surf.ns.register(SIMPLEVOCABULARYTERM='%s#' % self.portal.portal_types['SimpleVocabularyTerm'].absolute_url())
>>> self.setupVocabularies()
>>> vocab = self.portal.portal_vocabularies['eea.rdfmarshaller.vocab.testing']
>>> content_type, length, data = marshaller.marshall(vocab)
>>> store.load_triples(source=StringIO(data))
True
>>> SimpleVocabulary = session.get_class(surf.ns.SIMPLEVOCABULARY['SimpleVocabulary'])
>>> [obj.simplevocabulary_id.first.strip() for obj in SimpleVocabulary.all()]
[u'eea.rdfmarshaller.vocab.testing']
Load all the vocabolary terms into store
>>> [store.load_triples(source=StringIO(marshaller.marshall(term)[2])) for term in vocab.objectValues()]
[True, True, True, True, True, True]
>>> SimpleVocabularyTerm = session.get_class(surf.ns.SIMPLEVOCABULARYTERM['SimpleVocabularyTerm'])
>>> sorted([(obj.simplevocabularyterm_id.first.strip(), obj.dcterms_title.first.strip()) for obj in SimpleVocabularyTerm.all()], key=lambda t:t[0])
[(u'air pollution', u'Air pollution'), (u'biodiversity', u'Nature protection and biodiversity'), (u'climate change', u'Climate change mitigation'), (u'freshwater', u'Freshwater'), (u'land', u'Land use'), (u'waste', u'Waste')]
AT modifiers
---------------
rdfmarshaller allows arbitrary constructs to change the @@rdf output. These
are called "modifiers" and are registered as ZCA subscribers to the
ISurfResourceModifier, adapting the intended context type.
The hasPart modifier
~~~~~~~~~~~~~~~~~~~~
A folder uses dcterms:hasPart to tell which content it has.
>>> folder = self.portal[self.portal.invokeFactory('Folder', id='myfolder')]
>>> news1 = folder[folder.invokeFactory('News Item', id='news1')]
>>> folder.portal_workflow.doActionFor(news1, 'publish')
>>> news2 = folder[folder.invokeFactory('News Item', id='news2')]
>>> folder.portal_workflow.doActionFor(news2, 'publish')
>>> content_type, length, data = marshaller.marshall(folder)
>>> store.load_triples(source=StringIO(data))
True
>>> parts = [part for part in session.get_resource(folder.absolute_url(), Folder).dcterms_hasPart]
>>> parts.sort()
>>> parts
[rdflib.URIRef('http://nohost/plone/myfolder/news1'), rdflib.URIRef('http://nohost/plone/myfolder/news2')]
The objectProvides modifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All AT objects have eea:objectProvides information:
>>> folder = list(session.get_class(surf.ns.FOLDER['Folder']).all())[0]
>>> provides = list(folder.eea_objectProvides.order())
>>> print provides[0]
AccessControl.interfaces.IOwned