Exemplo n.º 1
0
def getHostName(graph, ifUri):
    """
    Given a URI of an interface, return the name of the host
    If the host has a 'name' property, return that value.
    Otherwise, strip off the first part of the URI (until '#') and return the last part.
    """
    global rdf,rdfs,ndl,bindings,rdfcompatmode
    select = ("?hostUri","?hostName")
    where = [GraphPattern([
            ("?hostUri", ndl["hasInterface"], ifUri),
            ("?hostUri", ndl["name"], "?hostName"),
            ]),
        GraphPattern([
            ("?hostUri", ndl["hasInterface"], ifUri),
            ("?hostUri", rdfs["label"], "?hostName"),
            ])]
    # Create a SPARQLGraph wrapper object out of the normal Graph
    sparqlGrph = SPARQLGraph(graph)
    # Make the query
    if rdfcompatmode:
        result = sparqlGrph.query(select, where)
    else:
        result = Query.query(sparqlGrph, select, where, initialBindings=bindings)
    if result:
        hostUri, hostName = result[0]
        ifName = ifUri.replace(hostUri+":","")
        return (hostName,ifName)
    else:
        return (ifUri[ifUri.find("#"):],"")
Exemplo n.º 2
0
def getLocations(graph):
    locations = []
    global rdf, rdfs, ndl, bindings, rdfcompatmode
    sparqlGrph = SPARQLGraph(graph)
    for loc in graph.subjects(predicate=rdf["type"], object=ndl["Location"]):
        select = ("?hostName")
        where = [
            GraphPattern([
                ("?hostUri", ndl["locatedAt"], loc),
                ("?hostUri", ndl["name"], "?hostName"),
            ]),
            GraphPattern([
                ("?hostUri", ndl["locatedAt"], loc),
                ("?hostUri", rdfs["label"], "?hostName"),
            ])
        ]
        # Create a SPARQLGraph wrapper object out of the normal Graph
        # Make the query
        if rdfcompatmode:
            result = sparqlGrph.query(select, where)
        else:
            result = Query.query(sparqlGrph,
                                 select,
                                 where,
                                 initialBindings=bindings)
        if result:
            locations.append(result)
    return locations
Exemplo n.º 3
0
def getHostName(graph, ifUri):
    """
    Given a URI of an interface, return the name of the host
    If the host has a 'name' property, return that value.
    Otherwise, strip off the first part of the URI (until '#') and return the last part.
    """
    global rdf, rdfs, ndl, bindings, rdfcompatmode
    select = ("?hostUri", "?hostName")
    where = [
        GraphPattern([
            ("?hostUri", ndl["hasInterface"], ifUri),
            ("?hostUri", ndl["name"], "?hostName"),
        ]),
        GraphPattern([
            ("?hostUri", ndl["hasInterface"], ifUri),
            ("?hostUri", rdfs["label"], "?hostName"),
        ])
    ]
    # Create a SPARQLGraph wrapper object out of the normal Graph
    sparqlGrph = SPARQLGraph(graph)
    # Make the query
    if rdfcompatmode:
        result = sparqlGrph.query(select, where)
    else:
        result = Query.query(sparqlGrph,
                             select,
                             where,
                             initialBindings=bindings)
    if result:
        hostUri, hostName = result[0]
        ifName = ifUri.replace(hostUri + ":", "")
        return (hostName, ifName)
    else:
        return (ifUri[ifUri.find("#"):], "")
Exemplo n.º 4
0
def getConnections(graph):
    """
    Given a NDL triplet graph, return lists of external and
    internal connections.
    
    The method runs a SPARQL query on the graph.
    Next step is to filter out the equivalent connections, which is done using a
    stack, because lists cannot be altered while iterating over them.
    
    Difference between internal and external is currently based on whether the
    symmetric connectedTo property is present in the graph.
    
    The results are beautified using the getHostName method.
    """
    global rdf, rdfs, ndl, bindings, rdfcompatmode
    select = ("?ifA", "?ifB")
    where = GraphPattern([
        ("?ifA", ndl["connectedTo"], "?ifB"),
        ("?ifB", ndl["connectedTo"], "?ifA"),
    ])
    # Create a SPARQLGraph wrapper object out of the normal Graph
    sparqlGrph = SPARQLGraph(graph)
    # Make the query
    if rdfcompatmode:
        result = sparqlGrph.query(select, where)
    else:
        result = Query.query(sparqlGrph,
                             select,
                             where,
                             initialBindings=bindings)
    #print "Found %d connections" % len(result)
    internalConnections = []
    externalConnections = []
    while len(result) > 0:
        ifA, ifB = result.pop()
        if (ifB, ifA) in result:
            result.remove((ifB, ifA))
            internalConnections.append(
                (getHostName(graph, ifA), getHostName(graph, ifB)))
        else:
            externalConnections.append(
                (getHostName(graph, ifA), getHostName(graph, ifB)))
    locations = getLocations(graph)
    return internalConnections, externalConnections, locations
Exemplo n.º 5
0
def getConnections(graph):
    """
    Given a NDL triplet graph, return lists of external and
    internal connections.
    
    The method runs a SPARQL query on the graph.
    Next step is to filter out the equivalent connections, which is done using a
    stack, because lists cannot be altered while iterating over them.
    
    Difference between internal and external is currently based on whether the
    symmetric connectedTo property is present in the graph.
    
    The results are beautified using the getHostName method.
    """
    global rdf,rdfs,ndl,bindings,rdfcompatmode
    select = ("?ifA","?ifB")
    where = GraphPattern([
            ("?ifA", ndl["connectedTo"], "?ifB"),
            ("?ifB", ndl["connectedTo"], "?ifA"),
            ])
    # Create a SPARQLGraph wrapper object out of the normal Graph
    sparqlGrph = SPARQLGraph(graph)
    # Make the query
    if rdfcompatmode:
        result = sparqlGrph.query(select, where)
    else:
        result = Query.query(sparqlGrph, select, where, initialBindings=bindings)
    #print "Found %d connections" % len(result)
    internalConnections = []
    externalConnections = []
    while len(result) > 0:
        ifA,ifB = result.pop()
        if (ifB,ifA) in result:
            result.remove((ifB,ifA))
            internalConnections.append((getHostName(graph,ifA), getHostName(graph,ifB)))
        else:
            externalConnections.append((getHostName(graph,ifA), getHostName(graph,ifB)))
    locations = getLocations(graph)
    return internalConnections, externalConnections, locations
Exemplo n.º 6
0
def getLocations(graph):
    locations = []
    global rdf,rdfs,ndl,bindings,rdfcompatmode
    sparqlGrph = SPARQLGraph(graph)
    for loc in graph.subjects(predicate=rdf["type"], object=ndl["Location"]):
        select = ("?hostName")
        where = [GraphPattern([
                ("?hostUri", ndl["locatedAt"], loc),
                ("?hostUri", ndl["name"], "?hostName"),
                ]),
            GraphPattern([
                ("?hostUri", ndl["locatedAt"], loc),
                ("?hostUri", rdfs["label"], "?hostName"),
                ])]
        # Create a SPARQLGraph wrapper object out of the normal Graph
        # Make the query
        if rdfcompatmode:
            result = sparqlGrph.query(select, where)
        else:
            result = Query.query(sparqlGrph, select, where, initialBindings=bindings)
        if result:
            locations.append(result)
    return locations