def test_calculateDistance_differentOrMissingAttributes(self): # emporer penguin: emporer penguin # wings 2 2 # color blackwhite blackwhite # size big - # population - notsomany rt = RelationType("is_a") at = RelationAttributeType("wings") at2 = RelationAttributeType("color") at3 = RelationAttributeType("size") at4 = RelationAttributeType("population") source = Node("EmporerPenguin") target = Node("Bird") r = Relation(rt, source, target) a = r.createAttribute(at, 2) a2 = r.createAttribute(at2, "blackwhite") a3 = r.createAttribute(at3, "big") r2 = Relation(rt, source, target) a4 = r2.createAttribute(at, 2) a5 = r2.createAttribute(at2, "blackwhite") a6 = r2.createAttribute(at4, "notsomany") dist = gd.calculateRelationDistance(r, r2) self.assertEqual(dist, 0.125)
def test_RelationMatching(self): rt = RelationType("is_a") at = RelationAttributeType("wings") at2 = RelationAttributeType("color") at3 = RelationAttributeType("size") at4 = RelationAttributeType("population") source = Node("EmporerPenguin") target = Node("Bird") target2 = Node("Thing") r = Relation(rt, source, target) r.createAttribute(at, 2) r.createAttribute(at2, "blackwhite") r.createAttribute(at3, "big") r2 = Relation(rt, source, target) r2.createAttribute(at, 2) r2.createAttribute(at2, "blackwhite") r2.createAttribute(at4, "notsomany") r3 = Relation(rt, source, target2) r3.createAttribute(at, 4) r3.createAttribute(at2, "black") r3.createAttribute(at3, "enormous") t = Topology() t.insertNode(source) t.insertNode(target) t.insertNode(target2) t.insertRelation(r3) t.insertRelation(r) t.insertRelation(r2) matchedRelations = gd.matchRelations(r, source, t) self.assertEqual(matchedRelations[0], r) self.assertEqual(matchedRelations[1], r2) self.assertEqual(matchedRelations[2], r3)
def createRelation(env, type, source, target): relationtype = RelationType(type) relation = Relation(type, Node(source), Node(target)) # todo: do relations need to be saved in the environment? # todo: + what name should they get? return success
def test_basicViewOperations(self): n = Node("penguin") t = buildTopology() t.insertNode(n) r = Relation(RelationType("is_a"), n, Node("bird")) t.insertRelation(r) v = View(t) with self.assertRaises(AssertionError): v.includeNode(Node("beatle")) # does not exist in topology t self.assertEqual(len(v.nodes), 0) self.assertEqual(len(v.relations), 0) v.includeNode(n) v.includeRelation(r) self.assertIn(n, v.nodes) self.assertIn(r, v.relations) v.mend() self.assertIn( Node("bird"), v.nodes ) # after mend the Node(name="bird") should be in the view, too
def test_nodeWithAttributes(self): n = Node("pelican") at = NodeAttributeType("color") a = n.createAttribute(at, "white") self.assertIs(n.attributes[0], a) self.assertIs(a.type, at) self.assertEqual(str(a), "color: white") self.assertEqual(repr(a), "Attribute(type = 'color', value = 'white')")
def findPath(env, topology, source, target, allowedRelationTypes=None, iterations=None): t = env.vars[topology] path = t.findPath(Node(source), Node(target), allowedRelationTypes, iterations)
def test_hasAttributeOfType(self): rt = RelationType("has") at = RelationAttributeType("amount") source = Node("bird") target = Node("wing") r = Relation(rt, source, target) a = r.createAttribute(at, 2) x = r.hasAttributeOfType(at) self.assertTrue(x)
def test_SameAttributeTypeTwice(self): rt = RelationType("has") at = RelationAttributeType("amount") source = Node("bird") target = Node("wing") r = Relation(rt, source, target) a = r.createAttribute(at, 2) with self.assertRaises(AssertionError): a2 = r.createAttribute(at, 10)
def compareGraphs(env, graph1, graph2, node1, node2, iterations=None): g1 = env.vars[graph1] g2 = env.vars[graph2] n1 = Node(node1) n2 = Node(node2) dist = semNets.GraphDistance.calculateGraphDistance( g1, g2, n1, n2, iterations) return {"result": dist}
def relationGetAttributeValue(env, topology, type, source, target, attributeType): tmpRelation = Relation(RelationType(type), Node(source), Node(target)) t = env.vars[topology] assert t is not None r = t.tryGetRelation(tmpRelation) assert r is not None value = r.getAttributeValue(RelationAttributeType(attributeType)) return {"result": value}
def test_calculateDistance_differentSource(self): rt = RelationType("is_a") source = Node("EmporerPenguin") source2 = Node("LittlePenguin") target = Node("Bird") r = Relation(rt, source, target) r2 = Relation(rt, source2, target) dist = gd.calculateRelationDistance(r, r2) self.assertEqual(dist, 0.25)
def test_calculateDistance_differentTarget(self): rt = RelationType("is_a") source = Node("Anna") target = Node("Student") target2 = Node("Employee") r = Relation(rt, source, target) r2 = Relation(rt, source, target2) dist = gd.calculateRelationDistance(r, r2) self.assertEqual(dist, 0.25)
def relationHasAttribute(env, topology, type, source, target, attributeType, value): tmpRelation = Relation(RelationType(type), Node(source), Node(target)) attr = Attribute(RelationAttributeType(attributeType), value) t = env.vars[topology] assert t is not None r = t.tryGetRelation(tmpRelation) assert r is not None result = r.hasAttribute(attr) return {"result": result}
def test_relation(self): rt = RelationType("is-a") source = Node("pelican") target = Node("bird") r = Relation(rt, source, target) self.assertEqual(r.type, rt) self.assertEqual(r.source, source) self.assertEqual(r.target, target) self.assertEqual(str(r), "pelican is-a bird") self.assertEqual( repr(r), "Relation(type = 'is-a', source = 'pelican', target = 'bird')")
def test_existsPath(self): with open("findPathTest.json") as file: net = json.load(file) t = Topology() t.load(net) bool = t.existsPath(Node("pelican"), Node("creature")) bool2 = t.existsPath(Node("wings"), Node("creature")) self.assertTrue(bool) self.assertFalse(bool2)
def existsPath(env, topology, source, target, allowedRelationTypes=None, iterations=None): t = env.vars[topology] result = t.existsPath(Node(source), Node(target), allowedRelationTypes, iterations) #todo: allowedRelationTypes is list of strings. has to be list of RelationType!!! return {"result": result}
def test_relationWithAttributes(self): rt = RelationType("has") at = RelationAttributeType("amount") source = Node("bird") target = Node("wing") r = Relation(rt, source, target) a = r.createAttribute(at, 2) self.assertIs(r.attributes[0], a) self.assertIs(a.type, at) self.assertEqual(a.value, 2) self.assertEqual(str(a), "amount: 2") self.assertEqual(repr(a), "Attribute(type = 'amount', value = '2')")
def relationHasAttributeOfType(env, topology, type, source, target, attributeType): tmpRelation = Relation(RelationType(type), Node(source), Node(target)) t = env.vars[topology] assert t is not None r = None for rel in t.relations: if rel.source == tmpRelation.source and rel.target == tmpRelation.target and rel.type == tmpRelation.type: r = rel assert r is not None result = r.hasAttributeOfType(RelationAttributeType(attributeType)) return {"result": result}
def test_calculateAttributeDistance_sameRelation(self): rt = RelationType("is_a") at = RelationAttributeType("wings") at2 = RelationAttributeType("color") at3 = RelationAttributeType("size") source = Node("EmporerPenguin") target = Node("Bird") r = Relation(rt, source, target) a = r.createAttribute(at, 2) a2 = r.createAttribute(at2, "blackwhite") a3 = r.createAttribute(at3, "big") dist = gd.calculateRelationDistance(r, r) self.assertEqual(dist, 0)
def createNode(env, name): #todo: is this needed? or should it be chained with an addNode function for topologies and views? #todo: do we have to add the node to the environment? node = Node(name) env.define(name, node) return success
def load(self, data): ''' This function loads a topology from a dictionary. All previous nodes and relations, if existing, are wiped out. ''' self.nodes.clear() self.relations.clear() attributetypes = data.get("attributetypes") relationtypes = data.get("relationtypes") for node in data.get("nodes"): # load nodes self.nodes.append(Node(node)) if "node_attributes" in data: for node_attribute in data.get( "node_attributes"): # load node attributes at = NodeAttributeType(attributetypes[node_attribute[0]]) index = node_attribute[1] value = node_attribute[2] self.nodes[index].createAttribute(at, value) for relation in data.get("relations"): # load relations rt = RelationType(relationtypes[relation[0]]) source = self.nodes[relation[1]] target = self.nodes[relation[2]] self.relations.append(Relation(rt, source, target)) for relation_attribute in data.get( "relation_attributes"): # load relation attributes at = RelationAttributeType(attributetypes[relation_attribute[0]]) index = relation_attribute[1] value = relation_attribute[2] self.relations[index].createAttribute(at, value)
def test_existsNodeIncludingParentTopology(self): with open("graph1.json") as file: net = json.load(file) t = Topology() t.load(net) t2 = Topology() n = Node("black") t2.insertNode(n) t2.setParent(t) n2 = Node("violet") self.assertTrue(t2.existsNode(n)) self.assertTrue(t2.existsNode(n2))
def createNode(topology, nodeName): n = Node(nodeName) if topology.existsNode(n): AssertionError( "Node {0} is already existing in the topology or its parent". format(n.name)) topology.insertNode(n) return n
def test_calculateDistance_Symmetry(self): rt = RelationType("is_a") at = RelationAttributeType("wings") at2 = RelationAttributeType("size") source = Node("EmporerPenguin") source2 = Node("LittlePenguin") target = Node("Bird") r = Relation(rt, source, target) r2 = Relation(rt, source2, target) r.createAttribute(at, 2) r2.createAttribute(at, 2) r.createAttribute(at2, "big") r2.createAttribute(at2, "small") dist1 = gd.calculateRelationDistance(r, r2) dist2 = gd.calculateRelationDistance(r2, r) self.assertEqual(dist1, dist2)
def test_load(self): with open("TestData.json") as file: net = json.load(file) t = Topology() t.load(net) self.assertTrue(t.existsNode(Node("bird"))) self.assertTrue(t.existsNode(Node("pelican"))) self.assertTrue(len(t.nodes), 4) self.assertEqual(len(t.relations),4) self.assertEqual(t.nodes[1], Node("pelican")) self.assertEqual(t.nodes[1].attributes[0].type, NodeAttributeType("size")) self.assertEqual(t.nodes[1].attributes[0].value, "big") self.assertEqual(t.nodes[1].attributes[1].type, NodeAttributeType("color")) self.assertEqual(t.nodes[1].attributes[1].value, "white") r = Relation(RelationType("is_a"), Node("pelican"), Node("bird")) self.assertEqual(t.relations[0], r) r2 = Relation(RelationType("has"), Node("bird"), Node("wings")) r2.createAttribute(RelationAttributeType("amount"), 2) self.assertEqual(t.relations[1], r2)
def test_firstparsertests(self): # load static semantic network consisting of card names and so on # still missing: things like "game" and "turn" with open("homerock.json") as file: net1 = json.load(file) g1 = Topology() g1.load(net1) g2 = Topology() #with open("hearthstone_log_parseTransitionsTest.txt") as file: with open("hearthstone_2016_06_20_15_30_29.log") as file: g2 = parseLog(file, g1) test = g2.toJsonNamedTriples() with open("test.json", "w") as file: json.dump(test, file) relationtype_is_a = RelationType("is_a") relationtype_has = RelationType("has") water_elemental = Node("Water Elemental") friendly_deck = Node("Location_Friendly_Deck_0") minion_0 = Node("Minion_0") relation1 = Relation(relationtype_is_a, minion_0, water_elemental) relation2 = Relation(relationtype_has, friendly_deck, minion_0) self.assertTrue( g2.existsRelation(relation1), "The relation 'Minion_0 is_a Water Elemental' should exist") self.assertTrue( g2.existsRelation(relation2), "The relation 'Location_Friendly_Deck_0 has Minion_0' should exist" ) id = g2.getNodeByName("Minion_0").getAttributeValue( NodeAttributeType("id")) self.assertEqual( id, 17, "minion_0 should have the ID 17, but has {0}".format(id)) id = g2.getNodeByName("Minion_1").getAttributeValue( NodeAttributeType("id")) self.assertEqual( id, 29, "minion_1 should have the ID 23, but has {0}".format(id)) id = g2.getNodeByName("Minion_2").getAttributeValue( NodeAttributeType("id")) self.assertEqual( id, 13, "minion_2 should have the ID 13, but has {0}".format(id))
def relationCreateAttribute(env, topology, relationType, source, target, attributeType, value, attributes=None): tmpRelation = Relation(RelationType(relationType), Node(source), Node(target)) for type, value in attributes: tmpRelation.createAttribute(RelationAttributeType(type), value) t = env.vars[topology] assert t is not None r = t.tryGetRelation(tmpRelation) assert r is not None r.createAttribute(RelationAttributeType(attributeType), value) return success
def test_calculateDistance_customWeights(self): rt = RelationType("is_a") at = RelationAttributeType("wings") at2 = RelationAttributeType("size") source = Node("EmporerPenguin") source2 = Node("LittlePenguin") target = Node("Bird") r = Relation(rt, source, target) r2 = Relation(rt, source2, target) r.createAttribute(at, 2) r.createAttribute(at2, "big") r2.createAttribute(at2, "small") dist = gd.calculateRelationDistance(r, r2, wSource=0.5, wDifferentStringValue=0.3, wMissingAttribute=0.7) self.assertEqual(dist, 0.75)
def test_insertNodesAndRelations(self): t = Topology() n0 = Node("bird") n1 = Node("pelican") rt = RelationType("is_a") r = Relation(rt, n0, n1) self.assertFalse(t.existsNode(n0)) self.assertFalse(t.existsNode(n1)) self.assertFalse(t.existsRelation(r)) t.insertNode(n0) t.insertNode(n1) t.insertRelation(r) self.assertTrue(t.existsNode(n0)) self.assertTrue(t.existsNode(n1)) self.assertTrue(t.existsRelation(r)) with self.assertRaises(AssertionError): t.insertNode(n0) t.insertRelation(r) self.assertTrue(len(t.relations), 2)
def hasAnyRelation(topology, node): for rel in data: type = RelationType(rel["type"]) if rel["type"] != None else None target = Node(rel["target"]) if rel["target"] != None else None attrs = [ Attribute(RelationAttributeType(a["type"]), a[value]) for a in rel["attributes"] ] if rel["attributes"] != None else None relations = [r for r in topology.relations if r.source == node] for r in relations: if (type == None or r.type == type) and ( target == None or r.target == target) and (attrs == None or all( r.hasAttribute(a) for a in attrs)): return True return False