def test_init20(self):
        self.assertTrue("reference_test_model" not in sys.modules)

        def scenario_model_callback(scenario_tree, scenario_name, node_list):
            self.assertTrue(isinstance(scenario_tree, ScenarioTree))
            instance = reference_test_model.create_instance()
            if scenario_name == "s1":
                instance.p = 1.0
            elif scenario_name == "s2":
                instance.p = 2.0
            else:
                assert scenario_name == "s3"
                instance.p = 3.0
            return instance

        with ScenarioTreeInstanceFactory(
                model=scenario_model_callback,
                scenario_tree=join(
                    testdatadir,
                    "reference_test_scenario_tree.dat")) as factory:
            my_scenario_tree = factory.generate_scenario_tree()
        with ScenarioTreeInstanceFactory(
                model=scenario_model_callback,
                scenario_tree=my_scenario_tree) as factory:
            self.assertTrue(factory.model_directory() is None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)
        self.assertEqual(factory._closed, True)
        self.assertEqual(len(factory._archives), 0)
 def test_init7(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     scenario_tree_model = CreateAbstractScenarioTreeModel().\
         create_instance(
             join(testdatadir, "reference_test_scenario_tree.dat"))
     with self.assertRaises(ValueError):
         with ScenarioTreeInstanceFactory(
                 model=reference_test_model,
                 scenario_tree=scenario_tree_model) as factory:
             pass
     with ScenarioTreeInstanceFactory(model=reference_test_model,
                                      scenario_tree=scenario_tree_model,
                                      data=testdatadir) as factory:
         self.assertTrue(factory.model_directory() is None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     self.assertEqual(factory._closed, True)
     with ScenarioTreeInstanceFactory(
             model=reference_test_model,
             scenario_tree=join(
                 testdatadir,
                 "reference_test_scenario_tree.dat")) as factory:
         self.assertTrue(factory.model_directory() is None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self._check_factory(factory)
     self.assertEqual(len(factory._archives), 0)
    def test_init18(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        self.assertTrue("ScenarioStructure" not in sys.modules)
        nx_tree = import_file(os.path.join(testdatadir,
                                           "ScenarioStructure.py")).G

        def scenario_model_callback(scenario_tree, scenario_name, node_list):
            self.assertIs(scenario_tree, nx_tree)
            instance = reference_test_model.create_instance()
            if scenario_name == "s1":
                instance.p = 1.0
            elif scenario_name == "s2":
                instance.p = 2.0
            else:
                assert scenario_name == "s3"
                instance.p = 3.0
            return instance

        with ScenarioTreeInstanceFactory(model=scenario_model_callback,
                                         scenario_tree=nx_tree) as factory:
            self.assertTrue(factory.model_directory() is None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)
        self.assertEqual(factory._closed, True)
        self.assertEqual(len(factory._archives), 0)
    def test_init12(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        scenario_tree_model = CreateAbstractScenarioTreeModel().\
            create_instance(
                join(testdatadir, "reference_test_scenario_tree.dat"))

        def scenario_model_callback(scenario_tree, scenario_name, node_list):
            self.assertIs(scenario_tree, scenario_tree_model)
            instance = reference_test_model.create_instance()
            if scenario_name == "s1":
                instance.p = 1.0
            elif scenario_name == "s2":
                instance.p = 2.0
            else:
                assert scenario_name == "s3"
                instance.p = 3.0
            return instance

        with ScenarioTreeInstanceFactory(
                model=scenario_model_callback,
                scenario_tree=scenario_tree_model) as factory:
            self.assertTrue(factory.model_directory() is None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)
        self.assertEqual(factory._closed, True)
        self.assertEqual(len(factory._archives), 0)
 def test_init14(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     scenario_tree_model = CreateAbstractScenarioTreeModel().\
         create_instance(
             join(testdatadir, "reference_test_scenario_tree.dat"))
     with ScenarioTreeInstanceFactory(model=reference_test_model,
                                      scenario_tree=scenario_tree_model,
                                      data=testdatadir) as factory:
         scenario_tree = factory.generate_scenario_tree()
     self.assertEqual(factory._closed, True)
     self.assertEqual(len(factory._archives), 0)
     # start with a scenario tree (not a scenario tree model)
     with ScenarioTreeInstanceFactory(model=reference_test_model,
                                      scenario_tree=scenario_tree,
                                      data=testdatadir) as factory:
         self._check_factory(factory)
     self.assertEqual(factory._closed, True)
     self.assertEqual(len(factory._archives), 0)
 def test_init1_default(self):
     self.assertTrue("ReferenceModel" not in sys.modules)
     with ScenarioTreeInstanceFactory(model=testdatadir,
                                      scenario_tree=testdatadir) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self._check_factory(factory)
     self.assertTrue("ReferenceModel" in sys.modules)
 def test_init22(self):
     self.assertTrue("both_callbacks" not in sys.modules)
     with ScenarioTreeInstanceFactory(model=join(testdatadir,
                                                 "both_callbacks.py"),
                                      scenario_tree=None) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self._check_factory(factory)
     self.assertEqual(len(factory._archives), 0)
     self.assertTrue("both_callbacks" in sys.modules)
 def test_init15(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     with ScenarioTreeInstanceFactory(
             model=join(testdatadir, "reference_test_model.py"),
             scenario_tree=join(testdatadir,
                                "ScenarioStructure.py")) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self._check_factory(factory)
     self.assertTrue("reference_test_model" in sys.modules)
 def test_init10(self):
     with ScenarioTreeInstanceFactory(
             model=testdatadir,
             scenario_tree=join(testdatadir,
                                "reference_test_scenario_tree.dat"),
             data=join(testdatadir, "yaml_data")) as factory:
         self.assertEqual(len(factory._archives), 0)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self.assertTrue(factory.data_directory(),
                         join(testdatadir, "yaml_data"))
         self._check_factory(factory)
     self.assertEqual(len(factory._archives), 0)
 def test_init11(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     scenario_tree_model = CreateAbstractScenarioTreeModel().\
         create_instance(
             join(testdatadir, "reference_test_scenario_tree.dat"))
     scenario_tree_model.ScenarioBasedData = False
     with ScenarioTreeInstanceFactory(model=reference_test_model,
                                      scenario_tree=scenario_tree_model,
                                      data=testdatadir) as factory:
         self.assertTrue(factory.model_directory() is None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     self.assertEqual(factory._closed, True)
     self.assertEqual(len(factory._archives), 0)
 def test_init4_default(self):
     self.assertTrue("ReferenceModel" not in sys.modules)
     archive_copy = self._get_testfname_prefix() + ".archive_copy.tgz"
     shutil.copyfile(join(testdatadir, "archive_test.tgz"), archive_copy)
     with ScenarioTreeInstanceFactory(model=join(testdatadir,
                                                 "archive_test.tgz,"),
                                      scenario_tree=join(
                                          testdatadir,
                                          archive_copy + ",")) as factory:
         self.assertEqual(len(factory._archives), 2)
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self._check_factory(factory)
     self.assertEqual(len(factory._archives), 0)
     os.remove(archive_copy)
     self.assertTrue("ReferenceModel" in sys.modules)
 def test_init8(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     self.assertTrue(
         "reference_test_scenario_tree_model" not in sys.modules)
     with ScenarioTreeInstanceFactory(
             model=reference_test_model,
             scenario_tree=join(testdatadir,
                                "reference_test_scenario_tree_model.py"),
             data=testdatadir) as factory:
         self.assertTrue(factory.model_directory() is None)
         self.assertTrue(factory.scenario_tree_directory() is not None)
         self.assertTrue(factory._scenario_tree_module is not None)
         self._check_factory(factory)
     self.assertEqual(factory._closed, True)
     self.assertEqual(len(factory._archives), 0)
     self.assertTrue("reference_test_model" not in sys.modules)
     self.assertTrue("reference_test_scenario_tree_model" in sys.modules)
    def test_init13(self):
        model = reference_test_model.create_instance()
        scenario_tree_model = CreateAbstractScenarioTreeModel().\
            create_instance(
                join(testdatadir, "reference_test_scenario_tree.dat"))
        with ScenarioTreeInstanceFactory(
                model=model, scenario_tree=scenario_tree_model) as factory:
            self.assertTrue(factory.model_directory() is None)
            self.assertTrue(factory.scenario_tree_directory() is None)

            scenario_tree = factory.generate_scenario_tree()
            instances = factory.construct_instances_for_scenario_tree(
                scenario_tree, verbose=True)
            self.assertEqual(len(instances), 3)
            self.assertEqual(instances["s1"].p(), model.p())
            self.assertEqual(instances["s2"].p(), model.p())
            self.assertEqual(instances["s3"].p(), model.p())

        self.assertEqual(factory._closed, True)
        self.assertEqual(len(factory._archives), 0)
    def test_init17(self):
        self.assertTrue("reference_test_model" not in sys.modules)
        self.assertTrue("ScenarioStructure" not in sys.modules)
        nx_tree = import_file(os.path.join(testdatadir,
                                           "ScenarioStructure.py")).G
        with ScenarioTreeInstanceFactory(model=join(testdatadir,
                                                    "reference_test_model.py"),
                                         scenario_tree=nx_tree) as factory:
            self.assertEqual(len(factory._archives), 0)
            self.assertTrue(factory.model_directory() is not None)
            self.assertTrue(factory.scenario_tree_directory() is None)
            self._check_factory(factory)

            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)
            # check that we can modify the networkx tree to redefine
            # bundles
            nx_tree.nodes["s1"]["bundle"] = 0
            nx_tree.nodes["s2"]["bundle"] = 0
            nx_tree.nodes["s3"]["bundle"] = 0
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 1)
            nx_tree.nodes["s1"]["bundle"] = 0
            nx_tree.nodes["s2"]["bundle"] = 1
            nx_tree.nodes["s3"]["bundle"] = 2
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), True)
            self.assertEqual(len(scenario_tree.bundles), 3)
            nx_tree.nodes["s1"]["bundle"] = None
            nx_tree.nodes["s2"]["bundle"] = None
            nx_tree.nodes["s3"]["bundle"] = None
            scenario_tree = factory.generate_scenario_tree()
            self.assertEqual(scenario_tree.contains_bundles(), False)

        self.assertTrue("reference_test_model" in sys.modules)
        self.assertTrue("ScenarioStructure" in sys.modules)
 def test_init6(self):
     self.assertTrue("reference_test_model" not in sys.modules)
     scenario_tree_model = CreateAbstractScenarioTreeModel().\
         create_instance(
             join(testdatadir, "reference_test_scenario_tree.dat"))
     with ScenarioTreeInstanceFactory(
             model=join(testdatadir, "reference_test_model.py"),
             scenario_tree=scenario_tree_model) as factory:
         self.assertTrue(factory.model_directory() is not None)
         self.assertTrue(factory.scenario_tree_directory() is None)
         self._check_factory(factory)
     with self.assertRaises(TypeError):
         with ScenarioTreeInstanceFactory(model=join(
                 testdatadir, "reference_test_model.py"),
                                          scenario_tree=int) as f:
             pass
     with self.assertRaises(ValueError):
         with ScenarioTreeInstanceFactory(model=join(
                 testdatadir, "reference_test_model.py"),
                                          scenario_tree=None) as f:
             pass
     with self.assertRaises(TypeError):
         with ScenarioTreeInstanceFactory(
                 model=None, scenario_tree=scenario_tree_model) as f:
             pass
     with self.assertRaises(IOError):
         with ScenarioTreeInstanceFactory(
                 model=join(testdatadir,
                            "reference_test_model_does_not_exist.py"),
                 scenario_tree=scenario_tree_model) as f:
             pass
     with self.assertRaises(ValueError):
         with ScenarioTreeInstanceFactory(
                 model=join(testdatadir, "reference_test_model.py"),
                 scenario_tree=CreateAbstractScenarioTreeModel()) as f:
             pass
     self.assertEqual(len(factory._archives), 0)
     self.assertTrue("reference_test_model" in sys.modules)