Exemplo n.º 1
0
  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)
Exemplo n.º 2
0
  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)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 11
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)
Exemplo n.º 12
0
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
Exemplo n.º 13
0
    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)
Exemplo n.º 14
0
    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')")
Exemplo n.º 15
0
  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')")
Exemplo n.º 16
0
    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)
Exemplo n.º 17
0
  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)
Exemplo n.º 18
0
  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)
Exemplo n.º 19
0
    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)
Exemplo n.º 20
0
  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)
Exemplo n.º 21
0
    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)