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_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 createRelation(topology, source, relationType, target, attributeType = None, attributeValue = None): relationtype = RelationType(relationType) relation = Relation(relationtype, source, target) if attributeType != None: relationAttributeType = RelationAttributeType(attributeType) relation.createAttribute(relationAttributeType, attributeValue) topology.insertRelation(relation) return relation
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 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 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 createRelation(topology, source, relationType, target, attributeType=None, attributeValue=None): relationtype = RelationType(relationType) relation = Relation(relationtype, source, target) if attributeType != None: relationAttributeType = RelationAttributeType(attributeType) relation.createAttribute(relationAttributeType, attributeValue) topology.insertRelation(relation) return relation
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 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 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_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 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_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_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 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 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_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_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_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_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 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)