def test_owl_text(self): self.assertEqual('this_is_a_long_string', OWLText('this_is_a_long_string')) self.assertEqual('this_is_a_long_string', OWLText('this_is_a\nlong _string')) self.assertEqual('this_is_another_long_string', OWLText('this is another\n\nlong string'))
def predicates(self, item=None, name=None, diagram=None): """ Returns a collection of predicate nodes belonging to the given diagram. If no diagram is supplied the lookup is performed across the whole Project Index. :type item: Item :type name: str :type diagram: Diagram :rtype: set """ try: if not item and not name: collection = set() if not diagram: for i in self[K_PREDICATE]: for j in self[K_PREDICATE][i]: collection.update( *self[K_PREDICATE][i][j][K_NODE].values()) else: for i in self[K_PREDICATE]: for j in self[K_PREDICATE][i]: collection.update( self[K_PREDICATE][i][j][K_NODE][diagram.name]) return collection if item and not name: collection = set() if not diagram: for i in self[K_PREDICATE][item]: collection.update( *self[K_PREDICATE][item][i][K_NODE].values()) else: for i in self[K_PREDICATE][item]: collection.update( self[K_PREDICATE][item][i][K_NODE][diagram.name]) return collection if not item and name: collection = set() name = OWLText(name) if not diagram: for i in self[K_PREDICATE]: collection.update( *self[K_PREDICATE][i][name][K_NODE].values()) else: for i in self[K_PREDICATE]: collection.update( self[K_PREDICATE][i][name][K_NODE][diagram.name]) return collection if item and name: name = OWLText(name) if not diagram: return set.union( *self[K_PREDICATE][item][name][K_NODE].values()) return self[K_PREDICATE][item][name][K_NODE][diagram.name] except KeyError: return set()
def meta(self, item, name): """ Retrieves metadata for the given predicate, expressed as pair (item, name). :type item: Item :type name: str :rtype: dict """ try: name = OWLText(name) return self[K_PREDICATE][item][name][K_META] except KeyError: return dict()
def unsetMeta(self, item, name): """ Unset metadata for the given predicate type/name combination. :type item: Item :type name: str :rtype: bool """ name = OWLText(name) if item in self[K_PREDICATE]: if name in self[K_PREDICATE][item]: if K_META in self[K_PREDICATE][item][name]: del self[K_PREDICATE][item][name][K_META] return True return False
def removeItem(self, diagram, item): """ Remove the given item from the Project index. :type diagram: Diagram :type item: AbstractItem :rtype: bool """ i = item.type() if diagram.name in self[K_ITEMS]: if item.id in self[K_ITEMS][diagram.name]: del self[K_ITEMS][diagram.name][item.id] if not self[K_ITEMS][diagram.name]: del self[K_ITEMS][diagram.name] if diagram.name in self[K_TYPE]: if i in self[K_TYPE][diagram.name]: self[K_TYPE][diagram.name][i] -= {item} if not self[K_TYPE][diagram.name][i]: del self[K_TYPE][diagram.name][i] if not self[K_TYPE][diagram.name]: del self[K_TYPE][diagram.name] if item.isNode(): if diagram.name in self[K_NODE]: if item.id in self[K_NODE][diagram.name]: del self[K_NODE][diagram.name][item.id] if not self[K_NODE][diagram.name]: del self[K_NODE][diagram.name] if item.isPredicate(): k = OWLText(item.text()) if i in self[K_PREDICATE]: if k in self[K_PREDICATE][i]: if diagram.name in self[K_PREDICATE][i][k][K_NODE]: self[K_PREDICATE][i][k][K_NODE][ diagram.name] -= {item} if not self[K_PREDICATE][i][k][K_NODE][ diagram.name]: del self[K_PREDICATE][i][k][K_NODE][ diagram.name] if not self[K_PREDICATE][i][k][K_NODE]: del self[K_PREDICATE][i][k] if not self[K_PREDICATE][i]: del self[K_PREDICATE][i] if item.isEdge(): if diagram.name in self[K_EDGE]: if item.id in self[K_EDGE][diagram.name]: del self[K_EDGE][diagram.name][item.id] if not self[K_EDGE][diagram.name]: del self[K_EDGE][diagram.name] return True return False
def setMeta(self, item, name, meta): """ Set metadata for the given predicate type/name combination. :type item: Item :type name: str :type meta: dict :rtype: bool """ try: name = OWLText(name) self[K_PREDICATE][item][name][K_META] = meta except KeyError: return False else: return True
def addItem(self, diagram, item): """ Add the given item to the Project index. :type diagram: Diagram :type item: AbstractItem :rtype: bool """ i = item.type() if diagram.name not in self[K_ITEMS]: self[K_ITEMS][diagram.name] = dict() if item.id not in self[K_ITEMS][diagram.name]: self[K_ITEMS][diagram.name][item.id] = item if diagram.name not in self[K_TYPE]: self[K_TYPE][diagram.name] = dict() if i not in self[K_TYPE][diagram.name]: self[K_TYPE][diagram.name][i] = set() self[K_TYPE][diagram.name][i] |= {item} if item.isNode(): if diagram.name not in self[K_NODE]: self[K_NODE][diagram.name] = dict() self[K_NODE][diagram.name][item.id] = item if item.isPredicate(): k = OWLText(item.text()) if i not in self[K_PREDICATE]: self[K_PREDICATE][i] = dict() if k not in self[K_PREDICATE][i]: self[K_PREDICATE][i][k] = {K_NODE: dict()} if diagram.name not in self[K_PREDICATE][i][k][K_NODE]: self[K_PREDICATE][i][k][K_NODE][diagram.name] = set() self[K_PREDICATE][i][k][K_NODE][diagram.name] |= {item} if item.isEdge(): if diagram.name not in self[K_EDGE]: self[K_EDGE][diagram.name] = dict() self[K_EDGE][diagram.name][item.id] = item return True return False
def test_owl_text(): assert 'this_is_a_long_string' == OWLText('this_is_a_long_string') assert 'this_is_a_long_string' == OWLText('this_is_a\nlong _string') assert 'this_is_another_long_string' == OWLText('this is another\n\nlong string')