forked from jeroenh/AutoGOLE-Topologies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotopo.py
executable file
·68 lines (58 loc) · 2.22 KB
/
autotopo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import rdflib
RDFS = rdflib.Namespace("http://www.w3.org/2000/01/rdf-schema#")
class AGTopology:
def __init__(self, url):
self.url = url
self.store = rdflib.Graph()
try:
self.store.parse(url)
except Exception as e:
print e
class MasterTopology(object):
def __init__(self, mastertopologyfile):
super(MasterTopology, self).__init__()
self.mastertopologyfile = mastertopologyfile
self.store = rdflib.Graph()
try:
self.store.parse(mastertopologyfile)
except Exception as e:
print e
def getSeeAlsoLinks(self):
"Return all URIs that are the target of seeAlso links in this Topology."
links = []
for s,o in self.store.subject_objects(predicate=RDFS["seeAlso"]):
links.append(o)
return links
def mergeTopologies(masterFile,outfile):
"Parse the masterfile and open all the separate topologies."
m = MasterTopology(masterFile)
g = rdflib.Graph()
for l in m.getSeeAlsoLinks():
g.parse(l,format='xml')
print g.serialize(outfile)
def crawlTopology(startTopo):
"""
Crawl all topologies and return the list of all locations and all connections between them.
First we crawl through all rdfs:seeAlso links, gathering all the topologies.
Then for each topology, we gather all Locations.
For each Location, we get all the external connections, these are given as (Loc, Loc)
Returns: (List of all Locations, List of all connections)"""
alltops = startTopo.getAllTopologies()
allLocs = []
conList = []
for top in alltops:
for loc in top.getLocations():
if loc not in allLocs:
allLocs.append(loc)
cons = top.getConnections()
for con in cons:
# Check if (a,b) or (b,a) is already in the list
# Might be better to use a special object, but this works too
if not conInConnectionList(con, conList):
conList.append(con)
return allLocs, conList
def main():
mergeTopologies("master.owl","AutoGOLE-Topo.owl")
if __name__ == '__main__':
main()