def addPimoStatements(statements): ctx = pimoContext() model = Nepomuk.ResourceManager.instance().mainModel() for statement in statements: statement.setContext(Soprano.Node(ctx)) model.addStatement(statement) return Soprano.Error.ErrorNone
def createPimoProperty(label, domainUri, rangeUri=Soprano.Vocabulary.RDFS.Resource(), comment=None, icon=None): if not rangeUri.isValid(): print "[Ginkgo] Invalid range" return QUrl() if not label or len(label) == 0: print "[Ginkgo] Empty label" return QUrl() domainClass = Nepomuk.Types.Class(domainUri) pimoThingClass = Nepomuk.Types.Class(PIMO.Thing) if domainUri != PIMO.Thing and not domainClass.isSubClassOf( pimoThingClass): print "[Ginkgo] New PIMO properties need to have a pimo:Thing related domain." #propertyUri = Nepomuk.ResourceManager.instance().generateUniqueUri(label) pimoxNs = "http://www.semanticdesktop.org/ontologies/pimox#" propertyId = label.replace(" ", "") propertyUri = QUrl(pimoxNs + propertyId) stmts = [] stmts.append( Soprano.Statement(Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDF.type()), Soprano.Node(Soprano.Vocabulary.RDF.Property()))) #stmts.append(Soprano.Statement(Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDFS.subPropertyOf()), Soprano.Node(PIMO.isRelated))) stmts.append( Soprano.Statement(Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDFS.domain()), Soprano.Node(domainUri))) stmts.append( Soprano.Statement(Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDFS.range()), Soprano.Node(rangeUri))) #TODO set the prefLabel of the resource corresponding to this property? #TODO why a property needs to be a subproperty of itself stmts.append( Soprano.Statement( Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDFS.subPropertyOf()), Soprano.Node(propertyUri))) stmts.append( Soprano.Statement(Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.RDFS.label()), Soprano.Node(Soprano.LiteralValue(label)))) stmts.append( Soprano.Statement( Soprano.Node(propertyUri), Soprano.Node(Soprano.Vocabulary.NAO.created()), Soprano.Node(Soprano.LiteralValue(QDateTime.currentDateTime())))) if addPimoStatements(stmts) == Soprano.Error.ErrorNone: #we reset the entity so that its properties will get refreshed domainClass.reset() return Nepomuk.Resource(propertyUri) return None
def pimoContext(): sparql = "select ?c ?onto where {?c a <%s> . OPTIONAL {?c a ?onto . FILTER(?onto=<%s>). } } " % ( str(PIMO.PersonalInformationModel.toString()), str(Soprano.Vocabulary.NRL.Ontology().toString())) model = Nepomuk.ResourceManager.instance().mainModel() it = model.executeQuery(sparql, Soprano.Query.QueryLanguageSparql) if it.next(): pimoContext = it.binding(0).uri() if not it.binding(1).isValid(): stmt = Soprano.Statement( Soprano.Node(pimoContext), Soprano.Node(Soprano.Vocabulary.RDF.type()), Soprano.Node(Soprano.Vocabulary.NRL.Ontology()), Soprano.Node(pimoContext)) model.addStatement(stmt) else: pimoContext = QUrl( Nepomuk.ResourceManager.instance().generateUniqueUri()) stmt = Soprano.Statement(Soprano.Node(pimoContext), Soprano.Node(Soprano.Vocabulary.RDF.type()), Soprano.Node(PIMO.PersonalInformationModel), Soprano.Node(pimoContext)) model.addStatement(stmt) stmt = Soprano.Statement( Soprano.Node(pimoContext), Soprano.Node(Soprano.Vocabulary.RDF.type()), Soprano.Node(Soprano.Vocabulary.NRL.Ontology()), Soprano.Node(pimoContext)) model.addStatement(stmt) it.close() return pimoContext
def createPimoClass(parentClassUri, label, comment=None, icon=None): if label is None or len(unicode(label).strip()) == 0: print "Class label cannot be empty." return None parentClass = Nepomuk.Types.Class(parentClassUri) pimoThingClass = Nepomuk.Types.Class(PIMO.Thing) if parentClassUri != PIMO.Thing and not parentClass.isSubClassOf( pimoThingClass): print "New PIMO class needs to be subclass of pimo:Thing." return None #TODO: see pimomodel.cpp # if ( !name.isEmpty() ) { # QString normalizedName = name.replace( QRegExp( "[^\\w\\.\\-_:]" ), "" ); # QUrl s = "nepomuk:/" + normalizedName; # while( 1 ) { # if ( !q->executeQuery( QString("ask where { { <%1> ?p1 ?o1 . } UNION { ?r2 <%1> ?o2 . } UNION { ?r3 ?p3 <%1> . } }") # .arg( QString::fromAscii( s.toEncoded() ) ), Soprano::Query::QueryLanguageSparql ).boolValue() ) { # return s; # } # s = "nepomuk:/" + normalizedName + '_' + KRandom::randomString( 20 ); # } # } #TODO: create a dedicated NS pimoxNs = "http://www.semanticdesktop.org/ontologies/pimox#" classId = label.replace(" ", "") classUri = QUrl(pimoxNs + classId) #classUri = Nepomuk.ResourceManager.instance().generateUniqueUri(label) stmts = [] stmts.append( Soprano.Statement(Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDF.type()), Soprano.Node(Soprano.Vocabulary.RDFS.Class()))) stmts.append( Soprano.Statement(Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDFS.subClassOf()), Soprano.Node(QUrl(parentClassUri)))) #this is needed for the class to show up as a child of the PIMO class chosen if parentClassUri != PIMO.Thing: stmts.append( Soprano.Statement( Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDFS.subClassOf()), Soprano.Node(PIMO.Thing))) #this is needed until we use an inferencer, otherwise searching for instances of resource won't include the instances of this class stmts.append( Soprano.Statement(Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDFS.subClassOf()), Soprano.Node(Soprano.Vocabulary.RDFS.Resource()))) #TODO: check why pimomodel.cpp does not use Soprano.Node wrapping #TODO: check why all classes in the db are subclass of themselves stmts.append( Soprano.Statement(Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDFS.subClassOf()), Soprano.Node(QUrl(classUri)))) stmts.append( Soprano.Statement(Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.RDFS.label()), Soprano.Node(Soprano.LiteralValue(label)))) stmts.append( Soprano.Statement( Soprano.Node(classUri), Soprano.Node(Soprano.Vocabulary.NAO.created()), Soprano.Node(Soprano.LiteralValue(QDateTime.currentDateTime())))) if addPimoStatements(stmts) == Soprano.Error.ErrorNone: #the parent's class needs a rerset for reloading its children classes parentClass.reset() return Nepomuk.Resource(classUri) return None