Esempio n. 1
0
def read_d3js(path):
    f = open(path, 'r')
    decoded = json.loads(f.read())
    af = al.ArgumentationFramework(decoded['name'])
    for arg in decoded['nodes']:
        af.add_argument(arg)
    for att in decoded['edges']:
        af.add_attack((att['source'], att['target']))
Esempio n. 2
0
File: js.py Progetto: mszczot/alias
def read_json(path):
    f = open(path, 'r')
    decoded = json.loads(f.read())
    af = al.ArgumentationFramework(decoded['name'])
    for arg in decoded['arguments']:
        af.add_argument(arg['name'])
        for att in arg['attacks']:
            af.add_attack((arg['name'], att))
    return af
Esempio n. 3
0
def from_networkx(n):
    try:
        import networkx as nx
    except ImportError:
        raise ImportError("Networkx required for from_networkx()")

    try:
        assert isinstance(n, nx.DiGraph)
    except AssertionError:
        raise AssertionError("Input must be NetworkX DiGraph()")

    af = al.ArgumentationFramework()

    for node in n.nodes():
        af.add_argument(node)

    for edge in n.edges():
        af.add_attack(edge)

    return af
Esempio n. 4
0
File: n4j.py Progetto: mszczot/alias
def from_neo4j(framework, dbaddress='localhost', port=None, u='', p=''):
    try:
        from py2neo import Graph
    except ImportError:
        raise ImportError("py2neo is required for from_neo4j()")

    af = al.ArgumentationFramework(name=framework)

    graph_db = n4j_connect(dbaddress, port, u, p)
    argstmt = "MATCH (n:Argument) WHERE n.framework=\"" + framework + "\" RETURN n.name"
    attstmt = "MATCH (a:Argument)-->(b:Argument) WHERE a.framework = \"" + framework + "\" AND b.framework = \"" + framework + "\" RETURN a.name, b.name"

    nodes = []

    for r in graph_db.cypher.stream(argstmt):
        nodes.append(r[0])
        af.add_argument(r[0])

    for r in graph_db.cypher.stream(attstmt):
        af.add_attack((r[0], r[1]))

    return af
Esempio n. 5
0
def read_dot(path):
    """Generates an alias.ArgumentationFramework from a DOT graph
    description (.dot) file.

    Parameters
    ----------
    path : file or string
        File, directory or filename to be read.

    Returns
    -------
    framework : alias ArgumentationFramework

    Examples
    --------

    References
    ---------- 

    """

    try:
        from pyparsing import Word, Literal, alphas, alphanums, nums, OneOrMore
        from pyparsing import Forward, Optional, Keyword, Group, Suppress, ParseException
    except ImportError:
        raise ImportError("read_dot requires pyparsing")

    if not isinstance(path, str):
        return

    # Define DOT grammar
    # Punctuation and keywords
    LBR, RBR, SCOL, COMMA, EQ, LSQ, RSQ, COL = map(Literal, "{};,=[]:")
    strict, graph, digraph, node, edge, subgraph = map(
        Keyword, "strict graph digraph node edge subgraph".split())

    # Recursive rules
    stmt_list = Forward()
    a_list = Forward()
    edgeRHS = Forward()
    attr_list = Forward()

    comp = (Literal("n") | Literal("ne") | Literal("e") | Literal("se")
            | Literal("s") | Literal("sw") | Literal("w") | Literal("nw")
            | Literal("c") | Literal("_")).setName("comp")
    ID = Word(alphas, alphanums).setName("ID")
    edgeop = Suppress(Literal("--") | Literal("->"))
    port = (COL + ID + Optional((COL + comp))) | (COL + comp)
    node_id = ID + Optional(port)
    subg = (subgraph + Optional(ID)) + LBR + stmt_list + RBR

    edgeRHS << OneOrMore(edgeop + (node_id | subg))

    # Statements & attributes
    node_stmt = node_id("arg*") + Optional(attr_list)
    edge_stmt = ((node_id | subg) + edgeRHS)("att*") + Optional(attr_list)
    attr_stmt = (graph | node | edge) + attr_list

    a_list << OneOrMore(ID + EQ + ID + Optional(SCOL | COMMA))
    attr_list << OneOrMore((LSQ + Optional(a_list) + RSQ))

    stmt = (edge_stmt | node_stmt | attr_stmt | (ID + EQ + ID) | subg)
    stmt_list << OneOrMore(stmt + Optional(SCOL))

    dot = OneOrMore(
        Optional(strict) + (digraph | graph)("gtype") + Optional(ID)("gname") +
        LBR + stmt_list + RBR)

    f = open(path, 'r')
    f = f.read()

    try:
        parsed = dot.parseString(f)
    except ParseException as e:
        raise al.ParsingException(e)

    if parsed['gtype'] == 'graph':
        raise al.ParsingException('Graph must be a directed graph (digraph).')

    if 'gname' in parsed.keys():
        framework = al.ArgumentationFramework(parsed['gname'])
    else:
        head, tail = ntpath.split(path)
        framework = al.ArgumentationFramework(tail)

    if 'arg' in parsed.keys():
        for arg in parsed['arg']:
            framework.add_argument(arg)

    if 'att' in parsed.keys():
        for edge in parsed['att']:
            prev = -1
            for curr in edge:
                if prev >= 0:
                    framework.add_attack((edge[prev], curr))
                prev = prev + 1

    return framework
Esempio n. 6
0
def read_apx(path):
    """Generates an alias.ArgumentationFramework from an Aspartix (.apx) file.

    Parameters
    ----------
    path : file or string
        File, directory or filename to be read.

    Returns
    -------
    framework : alias ArgumentationFramework

    Examples
    --------

    References
    ---------- 
    http://www.dbai.tuwien.ac.at/research/project/argumentation/systempage/docu.htm
    """

    try:
        from pyparsing import Word, Literal, nums, alphas, alphanums, Keyword, Group, OneOrMore, Suppress
    except ImportError:
        raise ImportError("read_apx requires pyparsing")

    if not isinstance(path, str):
        return

    # Define apx grammar
    LPAR, RPAR, DOT, COMMA = map(Suppress, "().,")
    arg, attack, pref, val, valpref, support = map(
        Keyword, "arg att pref val valpref support".split())

    ID = Word(alphas, alphanums)
    id_pair = Group(ID + COMMA + ID)
    integer = Word(nums)
    int_pair = Group(integer + COMMA + integer)

    arg_cmd = (arg + LPAR + ID("arg*") + RPAR)
    attack_cmd = (attack + LPAR + id_pair("att*") + RPAR)
    pref_cmd = (pref + LPAR + id_pair("pref*") + RPAR)
    val_cmd = (val + LPAR + Group(ID + COMMA + integer)("val*") + RPAR)
    valpref_cmd = (valpref + LPAR + int_pair("valpref*") + RPAR)
    support_cmd = (support + LPAR + id_pair("support*") + RPAR)

    apx = OneOrMore((arg_cmd | attack_cmd | pref_cmd | val_cmd | valpref_cmd
                     | support_cmd) + DOT)

    f = open(path, 'r')
    f = f.read()

    head, tail = ntpath.split(path)
    framework = al.ArgumentationFramework(tail)

    try:
        parsed = apx.parseString(f)
    except ParseException as e:
        raise al.ParsingException(e)

    if 'arg' in parsed.keys():
        for arg in parsed['arg']:
            framework.add_argument(arg)

    if 'att' in parsed.keys():
        for att in parsed['att']:
            framework.add_attack((att[0], att[1]))

    return framework