Пример #1
0
def test_add_network_type_parameter():
    db = StatsDatabase(testing=True)
    db.load_data_castalia(castalia_output_file())

    # column name
    c_name = Column("name")
    # column SUM(data) AS data
    c_data = ColumnExpr("data", Operator(SUM, [ConstantExpr("data")]))
    # filter --> name = "Consumed Energy"
    f = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    # create derived table containing the SUM of all "Consumed Energy"
    dt = DerivedTable("dt", [c_name, c_data], [DATA_TABLE], f,
                      [DATA_TABLE.col["name"]])

    # generate a view based on the above derived table
    create_view_for_derived(db, dt)

    # network parameter
    pm_node = PlotModel("parameter",
                        "network",
                        dt,
                        None, (dt.col["data"], ),
                        title="Total Consumed Energy")

    # create JsonOutput object
    jo = JsonOutput("simulation_results", "46345634563456")

    # fetch stats from db
    stats = db.execute("SELECT data FROM dt")

    # add parameter to jsonOutput object
    add_network_type_parameter(jo, pm_node, stats)

    correct_json = """{
  "node_parameter_results": [],
  "node_plot_results": [],
  "node_2_node_results": [],
  "network_parameter_results": [
    {
      "value": 19.371669999999998,
      "unit": "",
      "name": "Total Consumed Energy"
    }
  ],
  "type": "simulation_results",
  "network_plot_results": [],
  "simulation_id": "46345634563456"
}"""

    assert json.loads(correct_json) == json.loads(jo.get_json_string())
Пример #2
0
def test_collect_tables_for_pml():
    ca = Column("a")
    cb = Column("b")
    cc = Column("c")
    cd = Column("d")
    ce = Column("e")
    cf = Column("f")
    t = Table("t", [ca, cb, cc, cd, ce, cf])
    dt1_filter = Operator(LAND, [
        Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]),
        Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)])
    ])
    dt2_filter = Operator(LOR, [
        Operator(
            LESS,
            [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]),
             ColumnRef(cd)]),
        Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)])
    ])
    dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter)
    dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc])

    dt3 = DerivedTable("dt3", [ca, cd], [dt1, dt2], None)

    pml = PlotModel("plot", "network", dt3, (ca, ), (cd, ), ["a", "d"],
                    {'a': less_than(0.125)})
    assert collect_tables_for_pml([pml]) == [t, dt1, dt2, dt3]
Пример #3
0
def test_operator():

    p = Function("plus", False)
    a = ConstantExpr(3)
    b = ConstantExpr(5)
    op = Operator(p, [a, b])
    assert op.operands[0] == a
    assert op.operands[1] == b
    assert a.parent == op
    assert b.parent == op
Пример #4
0
def test_model2plots(tmp_dir):
    curdir = os.getcwd()
    # change dir so that the generated plots will go into that dir
    os.chdir(tmp_dir)

    #name = "Consumed Energy"
    table_filter = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    dt = DerivedTable(
        "test_model2plots_1",
        [Column("node"), Column("data"),
         Column("name")], [DATA_TABLE], table_filter)
    table_filter2 = Operator(
        EQ, [ColumnRef(DATA_TABLE.col["label"]),
             ConstantExpr("\"TX pkts\"")])
    dt2 = DerivedTable(
        "test_model2plots_2",
        [Column("node"), Column("data"),
         Column("label")], [DATA_TABLE], table_filter2)

    x = DATA_TABLE.col["node"]
    y = DATA_TABLE.col["data"]
    axes = None
    select = {"name": "Consumed Energy"}  # all ?

    pm = PlotModel("plot", "network", dt, (x, ), (y, ), axes, select)

    x = DATA_TABLE.col["node"]
    y = DATA_TABLE.col["data"]
    axes = None
    select = {"label": "TX pkts"}  # all ?

    pm2 = PlotModel("plot", "network", dt2, (x, ), (y, ), axes, select)

    jo = JsonOutput("simulation_results", "SIM1391536771845")
    model2plots([pm, pm2], jo, castalia_output_file())

    # restore the working directory to its previous value
    os.chdir(curdir)
Пример #5
0
def test_expression2sql():
    def op_test(tuple_op, string):
        op = Operator(tuple_op[0], tuple_op[1])
        assert expression2sql(op) == string

    assert expression2sql(ConstantExpr(5)) == "5"
    assert expression2sql(ColumnRef(Column("test_col"))) == "test_col"
    col = Column("col")
    assert expression2sql(ColumnExpr("col", Operator(
        SUM, [ColumnRef(col)]))) == "SUM(col) AS col"

    # inline functions
    operands = [ColumnRef(Column("test_col")), ConstantExpr(2)]
    vs = "(test_col %s 2)"  # validation string
    op_test((PLUS, operands), vs % "+")
    op_test((MINUS, operands), vs % "-")
    op_test((DIV, operands), vs % "/")
    op_test((MULT, operands), vs % "*")
    op_test((EQ, operands), vs % "=")
    op_test((NOTEQ, operands), vs % "<>")
    op_test((LESS, operands), vs % "<")
    op_test((LESS_EQ, operands), vs % "<=")
    op_test((GREATER, operands), vs % ">")
    op_test((GREATER_EQ, operands), vs % ">=")

    operands2 = [ColumnRef(Column("test_col")), ConstantExpr(5)]
    assert expression2sql(
        Operator(
            LAND,
            [Operator(GREATER_EQ, operands),
             Operator(LESS_EQ, operands2)
             ])) == "((test_col >= 2) AND (test_col <= 5))"

    assert expression2sql(
        Operator(
            LOR,
            [Operator(GREATER_EQ, operands),
             Operator(LESS_EQ, operands2)
             ])) == "((test_col >= 2) OR (test_col <= 5))"

    #agregate functions
    operands = [ColumnRef(col)]
    vs = "%s(col)"  # validation string

    op_test((AVG, operands), vs % "AVG")
    op_test((COUNT, operands), vs % "COUNT")
    op_test((MAX, operands), vs % "MAX")
    op_test((MIN, operands), vs % "MIN")
    op_test((SUM, operands), vs % "SUM")
Пример #6
0
def test_create_plot_for_model(tmp_dir):
    curdir = os.getcwd()
    # change dir so that the generated plots will go into that dir
    os.chdir(tmp_dir)

    jo = JsonOutput("simulation_results", "SIM1391536771845")
    #
    # network plot
    #
    ds = StatsDatabase(testing=True)
    ds.load_data_castalia(castalia_output_file())
    table_filter = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    dt = DerivedTable(
        "test_create_plot_for_model",
        [Column("node"), Column("data"),
         Column("name")], [DATA_TABLE], table_filter)
    create_view_for_derived(ds, dt)
    x = DATA_TABLE.col["node"]
    y = DATA_TABLE.col["data"]
    axes = None
    select = {"name": "Consumed Energy"}  # all ?

    pm1 = PlotModel("plot", "network", dt, (x, ), (y, ), axes, select)

    #
    # node parameter
    #
    q = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    rel = DerivedTable("nodepar_test",
                       [Column("node"), Column("data")], [DATA_TABLE], q)
    create_view_for_derived(ds, rel)
    pm2 = PlotModel("parameter",
                    "node",
                    rel, (rel.col["node"], ), (rel.col["data"], ),
                    title="Consumed Energy")

    #
    # network Parameter
    #
    # column name
    c_name = Column("name")
    # column SUM(data) AS data
    c_data = ColumnExpr("data", Operator(SUM, [ConstantExpr("\"data\"")]))
    # filter --> name = "Consumed Energy"
    f = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    # create derived table containing the SUM of all "Consumed Energy"
    dt = DerivedTable("networkpar_test", [c_name, c_data], [DATA_TABLE], f,
                      [DATA_TABLE.col["name"]])

    # generate a view based on the above derived table
    create_view_for_derived(ds, dt)

    pm3 = PlotModel("parameter",
                    "network",
                    dt,
                    None, (dt.col["data"], ),
                    title="Total Consumed Energy")

    #
    # node2node parameter
    #

    # DerivedTable for node2node parameter
    q = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Packets received per node\"")
    ])
    dt = DerivedTable(
        "testn2n",
        [Column("node"), Column("n_index"),
         Column("data")], [DATA_TABLE], q)

    pm4 = PlotModel("parameter",
                    "node2node",
                    dt, (dt.col["node"], dt.col["n_index"]),
                    (dt.col["data"], ),
                    title="Packets received per node")

    # generate a view based on the above derived table
    create_view_for_derived(ds, dt)

    create_plot_for_model(pm1, ds, jo)
    create_plot_for_model(pm2, ds, jo)
    create_plot_for_model(pm3, ds, jo)
    create_plot_for_model(pm4, ds, jo)

    correct_json = """{
  "node_parameter_results": [
    {
      "results": [
        {
          "name": "Consumed Energy",
          "value": 6.79813,
          "unit": ""
        }
      ],
      "node_id": "0"
    },
    {
      "results": [
        {
          "name": "Consumed Energy",
          "value": 6.28785,
          "unit": ""
        }
      ],
      "node_id": "1"
    },
    {
      "results": [
        {
          "name": "Consumed Energy",
          "value": 6.28569,
          "unit": ""
        }
      ],
      "node_id": "2"
    }
  ],
  "node_2_node_results": [
    {
      "parameters": [
        {
          "unit": "",
          "name": "Packets received per node",
          "value": 60.0
        }
      ],
      "plots": [],
      "node1_id": 0,
      "node2_id": 1
    },
    {
      "parameters": [
        {
          "unit": "",
          "name": "Packets received per node",
          "value": 71.0
        }
      ],
      "plots": [],
      "node1_id": 0,
      "node2_id": 2
    }
  ],
  "network_plot_results": [
    {
      "name": "node over data",
      "file_id": "test_create_plot_for_model:data(node).WHERE.name='Consumed.Energy'.png"
    }
  ],
  "node_plot_results": [],
  "simulation_id": "SIM1391536771845",
  "network_parameter_results": [
    {
      "name": "Total Consumed Energy",
      "value": 19.371669999999998,
      "unit": ""
    }
  ],
  "type": "simulation_results"
}"""
    assert json.loads(correct_json) == json.loads(jo.get_json_string())
    # restore the working directory to its previous value
    os.chdir(curdir)
Пример #7
0
def test_createviewforderived():
    d = StatsDatabase(testing=True)
    alist = [
        Attribute("a", "int"),
        Attribute("b", "int"),
        Attribute("c", "int"),
        Attribute("d", "int"),
        Attribute("e", "int"),
        Attribute("f", "int"),
    ]
    ca = Column("a")
    cb = Column("b")
    cc = Column("c")
    cd = Column("d")
    ce = Column("e")
    cf = Column("f")
    t = Table("t", [ca, cb, cc, cd, ce, cf])
    d.create_table("t", alist)
    dt1_filter = Operator(LAND, [
        Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]),
        Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)])
    ])
    dt2_filter = Operator(LOR, [
        Operator(
            LESS,
            [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]),
             ColumnRef(cd)]),
        Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)])
    ])
    dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter)
    dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc])

    dt3_filter = Operator(LAND, [
        Operator(LOR, [
            Operator(LESS, [
                Operator(PLUS, [ColumnRef(ca), ConstantExpr(2)]),
                ColumnRef(cd)
            ]),
            Operator(LESS_EQ, [ColumnRef(cd), ConstantExpr(5)])
        ]),
        Operator(EQ, [ColumnRef(ca), ColumnRef(cd)])
    ])

    dt3 = DerivedTable("dt3", [dupc(ca), dupc(cd)], [dt1, dt2], dt3_filter,
                       [ca])

    create_view_for_derived(d, dt1)
    create_view_for_derived(d, dt2)
    create_view_for_derived(d, dt3)

    assert d.relations["dt1"].name == "dt1"
    dt1_attr = d.get_attributes_of_relation("dt1")
    assert dt1_attr[0].name == "a"
    assert dt1_attr[1].name == "b"

    assert d.relations["dt2"].name == "dt2"
    dt2_attr = d.get_attributes_of_relation("dt2")
    assert dt2_attr[0].name == "c"
    assert dt2_attr[1].name == "d"

    assert d.relations["dt3"].name == "dt3"
    dt3_attr = d.get_attributes_of_relation("dt3")
    assert dt3_attr[0].name == "a"
    assert dt3_attr[1].name == "d"
Пример #8
0
def test_derived2sql():
    ca = Column("a")
    cb = Column("b")
    cc = Column("c")
    cd = Column("d")
    ce = Column("e")
    cf = Column("f")
    t = Table("t", [ca, cb, cc, cd, ce, cf])
    dt1_filter = Operator(LAND, [
        Operator(NOTEQ, [ColumnRef(ca), ConstantExpr(0)]),
        Operator(GREATER, [ColumnRef(ca), ConstantExpr(5)])
    ])
    dt2_filter = Operator(LOR, [
        Operator(
            LESS,
            [Operator(PLUS, [ColumnRef(cc), ConstantExpr(2)]),
             ColumnRef(cd)]),
        Operator(LESS_EQ, [ColumnRef(cc), ConstantExpr(5)])
    ])
    dt1 = DerivedTable("dt1", [ca, cb], [t], dt1_filter)

    dt2 = DerivedTable("dt2", [cc, cd], [t], dt2_filter, [cc])

    dt3_filter = Operator(LOR, [
        Operator(
            LESS,
            [Operator(PLUS, [ColumnRef(ca), ConstantExpr(2)]),
             ColumnRef(cd)]),
        Operator(LESS_EQ, [ColumnRef(cd), ConstantExpr(5)])
    ])

    dt3 = DerivedTable("dt3", [dupc(ca), dupc(cd)], [dt1, dt2], dt3_filter,
                       [ca])

    assert derived2sql(dt1) == "SELECT a,b FROM t WHERE ((a <> 0) AND (a > 5))"
    assert derived2sql(
        dt2
    ) == "SELECT c,d FROM t WHERE (((c + 2) < d) OR (c <= 5)) GROUP BY c"
    assert derived2sql(
        dt3
    ) == "SELECT a,d FROM dt1,dt2 WHERE (((a + 2) < d) OR (d <= 5)) GROUP BY a"
Пример #9
0
 def visit_Compare(self, node):
     a = self.visit(node.left)
     op_name = type(node.ops[0]).__name__
     op = self.get_func_by_name(op_name)
     b = self.visit(node.comparators[0])
     return Operator(op, [a, b])
Пример #10
0
 def visit_BoolOp(self, node):
     op_name = type(node.op).__name__
     op = self.get_func_by_name(op_name)
     return Operator(op, list(map(lambda x: self.visit(x), node.values)))
Пример #11
0
 def visit_BinOp(self, node):
     a = self.visit(node.left)
     op_name = type(node.op).__name__
     op = self.get_func_by_name(op_name)
     b = self.visit(node.right)
     return Operator(op, [a, b])
Пример #12
0
    def get_plot_models():
        table_filter = Operator(EQ, [
            ColumnRef(DATA_TABLE.col["name"]),
            ConstantExpr("\"Consumed Energy\"")
        ])
        dt = DerivedTable(
            "test_plotModel2json1",
            [Column("node"), Column("data"),
             Column("name")], [DATA_TABLE], table_filter)
        x = DATA_TABLE.col["node"]
        y = DATA_TABLE.col["data"]
        axes = None
        select = {"name": "Consumed Energy"}  # all ?

        # create a plotmodel based on the above info
        pm1 = PlotModel("plot", "network", dt, (x, ), (y, ), axes, select)

        # node parameter
        q = Operator(EQ, [
            ColumnRef(DATA_TABLE.col["name"]),
            ConstantExpr("\"Consumed Energy\"")
        ])
        dt = DerivedTable("test",
                          [Column("node"), Column("data")], [DATA_TABLE], q)
        pm_node1 = PlotModel("parameter",
                             "node",
                             dt, (dt.col["node"], ), (dt.col["data"], ),
                             title="Consumed Energy")

        # DerivedTable for Network parameter
        # column SUM(data) AS data
        c_data = ColumnExpr("data", Operator(SUM, [ConstantExpr("data")]))
        # filter --> name = "Consumed Energy"
        f = Operator(EQ, [
            ColumnRef(DATA_TABLE.col["name"]),
            ConstantExpr("\"Consumed Energy\"")
        ])
        # create derived table containing the SUM of all "Consumed Energy"
        dt2 = DerivedTable("dt", [c_data], [DATA_TABLE], f)

        # network parameter
        pm_node2 = PlotModel("parameter",
                             "network",
                             dt2,
                             None, (dt.col["data"], ),
                             title="Total Consumed Energy")

        # DerivedTable for node2node parameter
        q = Operator(EQ, [
            ColumnRef(DATA_TABLE.col["name"]),
            ConstantExpr("\"Packets received per node\"")
        ])
        dt3 = DerivedTable(
            "testn2n",
            [Column("node"), Column("n_index"),
             Column("data")], [DATA_TABLE], q)

        # node2node parameter
        pm_node3 = PlotModel("parameter",
                             "node2node",
                             dt3, (dt3.col["node"], dt3.col["n_index"]),
                             (dt3.col["data"], ),
                             title="Packets received per node")

        return [pm1, pm_node1, pm_node2, pm_node3]
Пример #13
0
def test_plot2json():
    #name = "Consumed Energy"
    ds = StatsDatabase(testing=True)
    ds.load_data_castalia(castalia_output_file())
    table_filter = Operator(EQ, [
        ColumnRef(DATA_TABLE.columns[3]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    dt = DerivedTable(
        "test_plot2json1",
        [Column("node"), Column("data"),
         Column("name")], [DATA_TABLE], table_filter)
    create_view_for_derived(ds, dt)
    x = DATA_TABLE.col["node"]
    y = DATA_TABLE.col["data"]
    axes = None
    select = {"name": "Consumed Energy"}  # all ?

    # create a plotmodel based on the above info
    pm1 = PlotModel("plot",
                    "network",
                    dt, (x, ), (y, ),
                    axes,
                    select,
                    title="node over data")

    # dummy plot model for testing
    pm2 = PlotModel("plot",
                    "node",
                    dt, (x, ), (y, ),
                    axes,
                    select,
                    title="node over data")

    # create JsonOutput object
    jo = JsonOutput("simulation_results", "07987986875")

    plot2json(jo, pm1, "file1")
    plot2json(jo, pm2, "file2")

    correct_json = '''{
  "type": "simulation_results",
  "node_parameter_results": [],
  "simulation_id": "07987986875",
  "node_2_node_results": [],
  "network_parameter_results": [],
  "network_plot_results": [
    {
      "name": "node over data",
      "file_id": "file1"
    }
  ],
  "node_plot_results": [
    {
      "results": [
        {
          "name": "node over data",
          "file_id": "file2"
        }
      ],
      "node_id": "?"
    }
  ]
}'''

    assert json.loads(correct_json) == json.loads(jo.get_json_string())
Пример #14
0
def test_add_node2node_type_parameter():
    db = StatsDatabase(testing=True)
    db.load_data_castalia(castalia_output_file())

    # DerivedTable for node2node parameter
    q = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Packets received per node\"")
    ])
    dt = DerivedTable(
        "testn2n",
        [Column("node"), Column("n_index"),
         Column("data")], [DATA_TABLE], q)

    # node2node parameter
    pm_node = PlotModel("parameter",
                        "node2node",
                        dt, (dt.col["node"], dt.col["n_index"]),
                        (dt.col["data"], ),
                        title="Packets received per node")

    # generate a view based on the above derived table
    create_view_for_derived(db, dt)

    # create JsonOutput object
    jo = JsonOutput("simulation_results", "5678657421")

    # fetch stats from db
    stats = db.execute("SELECT node,n_index,data FROM testn2n")

    # add parameter to jsonOutput object
    add_node2node_type_parameter(jo, pm_node, stats)

    correct_json = """{
  "network_parameter_results": [],
  "node_plot_results": [],
  "network_plot_results": [],
  "simulation_id": "5678657421",
  "type": "simulation_results",
  "node_parameter_results": [],
  "node_2_node_results": [
    {
      "plots": [],
      "node2_id": 1,
      "node1_id": 0,
      "parameters": [
        {
          "name": "Packets received per node",
          "value": 60.0,
          "unit": ""
        }
      ]
    },
    {
      "plots": [],
      "node2_id": 2,
      "node1_id": 0,
      "parameters": [
        {
          "name": "Packets received per node",
          "value": 71.0,
          "unit": ""
        }
      ]
    }
  ]
}"""

    assert json.loads(correct_json) == json.loads(jo.get_json_string())
Пример #15
0
 def visit_Call(self, node):
     func_name = self.visit(node.func)
     func = self.get_func_by_name(func_name)
     arg = self.visit(node.args[0])
     return Operator(func, [arg])
Пример #16
0
 def op_test(tuple_op, string):
     op = Operator(tuple_op[0], tuple_op[1])
     assert expression2sql(op) == string
Пример #17
0
def test_add_node_type_parameter():
    db = StatsDatabase(testing=True)
    db.load_data_castalia(castalia_output_file())

    # node parameter
    q = Operator(EQ, [
        ColumnRef(DATA_TABLE.col["name"]),
        ConstantExpr("\"Consumed Energy\"")
    ])
    rel = DerivedTable("test", [Column("node"), Column("data")], [DATA_TABLE],
                       q)
    create_view_for_derived(db, rel)
    pm_node = PlotModel("parameter",
                        "node",
                        rel, (rel.col["node"], ), (rel.col["data"], ),
                        select={"name": "Consumed Energy"},
                        title="Consumed Energy")

    # create JsonOutput object
    jo = JsonOutput("simulation_results", "243241234143141")

    # fetch stats from db
    stats = db.execute(
        "SELECT node, data FROM dataTable WHERE name = \"Consumed Energy\"")

    # add parameters to jsonOutput object
    add_node_type_parameter(jo, pm_node, stats)

    correct_json = """{
  "node_2_node_results": [],
  "node_parameter_results": [
    {
      "results": [
        {
          "name": "Consumed Energy",
          "unit": "",
          "value": 6.79813
        }
      ],
      "node_id": "0"
    },
    {
      "results": [
        {
          "name": "Consumed Energy",
          "unit": "",
          "value": 6.28785
        }
      ],
      "node_id": "1"
    },
    {
      "results": [
        {
          "name": "Consumed Energy",
          "unit": "",
          "value": 6.28569
        }
      ],
      "node_id": "2"
    }
  ],
  "simulation_id": "243241234143141",
  "node_plot_results": [],
  "type": "simulation_results",
  "network_plot_results": [],
  "network_parameter_results": []
}"""

    assert json.loads(correct_json) == json.loads(jo.get_json_string())