def setUp(self):
     spec = Specification()
     spec.add(MaterialInputConstraint(Material(type="wood", quantity=1)))
     spec.add_output_material(Material("Furniture", 1))
     self.machine = ProductionUnit(spec, config={"rate_by_minute":0.25})
     self.worker = Worker()
     self.input = Material("wood", quantity=3)
class TestOperation(TestCase):

    def setUp(self):
        spec = Specification()
        spec.add(MaterialInputConstraint(Material(type="wood", quantity=1)))
        spec.add_output_material(Material("Furniture", 1))
        self.machine = ProductionUnit(spec, config={"rate_by_minute":0.25})
        self.worker = Worker()
        self.input = Material("wood", quantity=3)

    def test_equals(self):
        self.assertEquals(StartOperation(self.machine, self.worker), StartOperation(self.machine, self.worker))

    def test_load_equals(self):
        op1 = LoadOperation(Material("input"),self.machine, self.worker)
        op2 = LoadOperation(Material("other"),self.machine, self.worker)
        op3 = LoadOperation(Material("input"),self.machine, self.worker)
        self.assertFalse(op1==op2)
        self.assertEquals(op1, op3)

    def test_load_operation(self):
        load_op = LoadOperation(self.input, production_unit=self.machine, time_to_perform=3, worker=self.worker)

        load_op.run(during=1)
        self.assertEquals(self.machine.inputs.count(), 1)

        load_op.run(during=2)
        self.assertEquals(self.machine.inputs.count(), 3)

    def test_load_all_in_one_operation(self):
        load_op = AllInOneLoadOperation(self.input, production_unit=self.machine, time_to_perform=3, worker=self.worker)

        load_op.run(during=1)
        self.assertEquals(self.machine.inputs.count(), 0)

        load_op.run(during=2)
        self.assertEquals(self.machine.inputs.count(), 3)

    def test_produce_operation(self):
        StartOperation(production_unit=self.machine,worker=self.worker).run(during=1)
        LoadOperation(self.input, production_unit=self.machine, time_to_perform=1, worker=self.worker).run(during=1)
        produce_op = ProduceOperation(production_unit=self.machine)

        produce_op.run(during=1)
        self.assertEquals(len(self.machine.get_outputs()), 0)

        produce_op.run(during=3)
        self.assertEquals(len(self.machine.get_outputs()), 1)

    def test_has_worker_constraint(self):
        self.assertRaises(NoWorkerToPerformAction, LoadOperation(Material("k"), self.machine).run)
    def setUp(self):
        self.unaffected_production_unit, spec, zone = create_machine(
            material_type_input="yarn")

        self.worker = Worker()
        self.affected_production_unit = ProductionUnit(spec)

        self.inputs = Material("yarn")
        self.started_production_unit = ProductionUnit(spec)
        self.started_production_unit.perform_next_operation(self.worker)

        self.loaded_production_unit = ProductionUnit(spec)
        self.loaded_production_unit.perform_next_operation(self.worker)
        self.loaded_production_unit.perform_next_operation(self.worker)

        config = {'rate_by_minute': 0.2}
        spec_four = Specification()
        spec_four.add(
            MaterialInputConstraint(Material(type="flour", quantity=2)))
        spec_four.add(
            MaterialInputConstraint(Material(type="water", quantity=1)))
        spec_four.add_output_material(Material("bread", 1))

        self.four_a_pain = ProductionUnit(spec_four, config)

        self.four_a_pain.perform_next_operation(self.worker)
        self.four_a_pain.perform_next_operation(self.worker)
Ejemplo n.º 4
0
def create_machine(material_type_input="input",
                   material_type_output="output",
                   stocking_zone_size=40,
                   rate=1):
    spec = Specification()
    config = {'rate_by_minute': rate}
    spec.add(
        MaterialInputConstraint(Material(type=material_type_input,
                                         quantity=1)))
    spec.add_output_material(Material(type=material_type_output, quantity=1))
    stock_zone = StockingZone(size=stocking_zone_size)
    machine = ProductionUnit(spec, config, output_stocking_zone=stock_zone)
    return machine, spec, stock_zone
    def setUp(self):
        self.unaffected_production_unit, spec, zone = create_machine(material_type_input="yarn")

        self.worker = Worker()
        self.affected_production_unit = ProductionUnit(spec)

        self.inputs = Material("yarn")
        self.started_production_unit = ProductionUnit(spec)
        self.started_production_unit.perform_next_operation(self.worker)

        self.loaded_production_unit = ProductionUnit(spec)
        self.loaded_production_unit.perform_next_operation(self.worker)
        self.loaded_production_unit.perform_next_operation(self.worker)

        config = {'rate_by_minute': 0.2}
        spec_four = Specification()
        spec_four.add(MaterialInputConstraint(Material(type="flour", quantity=2)))
        spec_four.add(MaterialInputConstraint(Material(type="water", quantity=1)))
        spec_four.add_output_material(Material("bread", 1))

        self.four_a_pain = ProductionUnit(spec_four, config)

        self.four_a_pain.perform_next_operation(self.worker)
        self.four_a_pain.perform_next_operation(self.worker)
Ejemplo n.º 6
0
def get_factory(yaml_conf):
    yaml = load(yaml_conf)
    factory = Factory(name=yaml["name"])
    materials = create_materials(yaml)
    for production_unit in yaml["production_units"]:
        spec = create_spec(materials, production_unit)
        config = {}
        config["rate_by_minute"] = production_unit.get("rate", 1)
        factory.add_production_unit(
            ProductionUnit(spec=spec,
                           config=config,
                           name=production_unit["name"]))

    for worker in yaml.get("workers", []):
        working_hour = worker.get("working_hour", 8) * 60
        factory.add_worker(Worker(working_hour=working_hour))
    return factory
Ejemplo n.º 7
0
 def test_factory_add_production_unit(self):
     factory = Factory()
     factory.add_production_unit(ProductionUnit(None))
     self.assertEquals(len(factory.production_units), 1)
Ejemplo n.º 8
0
 def test_signals_generated(self):
     pu = ProductionUnit(None)
Ejemplo n.º 9
0
 def setUp(self):
     self.pu = ProductionUnit(None)
     self.pu2 = ProductionUnit(None)
class ProductionUnitTest(unittest.TestCase):
    def setUp(self):
        self.unaffected_production_unit, spec, zone = create_machine(
            material_type_input="yarn")

        self.worker = Worker()
        self.affected_production_unit = ProductionUnit(spec)

        self.inputs = Material("yarn")
        self.started_production_unit = ProductionUnit(spec)
        self.started_production_unit.perform_next_operation(self.worker)

        self.loaded_production_unit = ProductionUnit(spec)
        self.loaded_production_unit.perform_next_operation(self.worker)
        self.loaded_production_unit.perform_next_operation(self.worker)

        config = {'rate_by_minute': 0.2}
        spec_four = Specification()
        spec_four.add(
            MaterialInputConstraint(Material(type="flour", quantity=2)))
        spec_four.add(
            MaterialInputConstraint(Material(type="water", quantity=1)))
        spec_four.add_output_material(Material("bread", 1))

        self.four_a_pain = ProductionUnit(spec_four, config)

        self.four_a_pain.perform_next_operation(self.worker)
        self.four_a_pain.perform_next_operation(self.worker)

    def test_state_idle(self):
        self.assertEquals(self.unaffected_production_unit.get_state(),
                          ProductionUnit.IDLE)

    def test_cannot_start_without_worker(self):
        self.assertRaises(
            NoWorkerToPerformAction,
            LoadOperation(self.inputs, self.unaffected_production_unit).run)

    def test_start_state(self):
        self.assertEquals(self.started_production_unit.get_state(),
                          ProductionUnit.STARTED)

    def test_stop_operation_return_to_idle_state(self):
        StopOperation(self.started_production_unit, worker=self.worker).run()
        self.assertEquals(self.started_production_unit.get_state(),
                          ProductionUnit.IDLE)

    def test_illegal_state(self):
        self.assertRaises(
            IllegalStateToPerformAction,
            StopOperation(self.unaffected_production_unit,
                          worker=self.worker).run)

    def test_produce_without_input(self):
        self.assertRaises(InvalidInputLoaded,
                          ProduceOperation(
                              production_unit=self.started_production_unit,
                              worker=self.worker).run,
                          during=1)

    def test_produce_operation(self):
        ProduceOperation(self.loaded_production_unit, worker=self.worker).run()
        self.assertEquals(self.loaded_production_unit.get_state(),
                          ProductionUnit.PRODUCING)
        self.assertEquals(len(self.loaded_production_unit.get_outputs()), 1)
        self.assertEquals(self.loaded_production_unit.get_outputs()[0].type,
                          "output")

    def test_slower_machine_configuration(self):
        slower_gp, spec, zone = create_machine(material_type_input="yarn",
                                               rate=0.5)
        slower_gp.perform_next_operation(self.worker)
        slower_gp.perform_next_operation(self.worker)

        ProduceOperation(slower_gp, worker=self.worker).run(during=2)
        self.assertEquals(len(slower_gp.get_outputs()), 1)

    def test_invalid_input(self):
        input = Material("rocks")
        self.assertRaises(
            InvalidInputLoaded,
            LoadOperation(input,
                          production_unit=self.affected_production_unit,
                          worker=self.worker).run)

    def test_out_of_stock(self):
        self.assertRaises(InvalidInputLoaded,
                          ProduceOperation(self.loaded_production_unit,
                                           worker=self.worker).run,
                          during=3)

    def test_multiple_inputs(self):
        self.assertRaises(InvalidInputLoaded,
                          ProduceOperation(self.four_a_pain,
                                           worker=self.worker).run,
                          during=5)

    def test_complete_failure(self):
        ProduceOperation(production_unit=self.loaded_production_unit,
                         worker=self.worker).run()
        self.loaded_production_unit.add_event(Failure())
        self.assertEquals(self.loaded_production_unit.get_state(),
                          ProductionUnit.FAILURE)

        self.loaded_production_unit.add_event(Fix())
        self.assertEquals(self.loaded_production_unit.get_state(),
                          ProductionUnit.STARTED)

    def test_add_skill_constraint_to_operation(self):
        tech_production_unit, spec, zone = create_machine(
            material_type_input="iron")

        start_op = StartOperation(tech_production_unit, worker=self.worker)
        start_op.add_constraint(SkillConstraint(skill_name="blacksmith"))

        self.assertRaises(CannotPerformOperation, start_op.run)

        blacksmith = Worker()
        blacksmith.skills.append("blacksmith")
        StartOperation(tech_production_unit, worker=blacksmith).run()
        self.assertEquals(tech_production_unit.get_state(),
                          ProductionUnit.STARTED)

    def test_produce_consume_inputs(self):
        LoadOperation(Material("water", 1),
                      self.four_a_pain,
                      worker=self.worker).run()
        ProduceOperation(self.four_a_pain, worker=self.worker).run(during=5)
        self.assertRaises(InvalidInputLoaded,
                          ProduceOperation(self.four_a_pain,
                                           worker=self.worker).run,
                          during=5)

    def test_production_unit_with_stocking_area(self):
        stock_zone = StockingZone()
        self.loaded_production_unit.output_stocking_zone = stock_zone

        ProduceOperation(self.loaded_production_unit, worker=self.worker).run()

        self.assertEquals(stock_zone.count(), 1)

    def test_production_unit_with_limited_stocking_area(self):
        stock_zone = StockingZone(size=3)
        self.loaded_production_unit.output_stocking_zone = stock_zone

        LoadOperation(Material("yarn", 10),
                      self.loaded_production_unit,
                      worker=self.worker).run()
        try:
            ProduceOperation(self.loaded_production_unit,
                             worker=self.worker).run(during=5)
        except Event:
            pass

        self.assertEquals(stock_zone.count(), 3)
class ProductionUnitTest(unittest.TestCase):
    def setUp(self):
        self.unaffected_production_unit, spec, zone = create_machine(material_type_input="yarn")

        self.worker = Worker()
        self.affected_production_unit = ProductionUnit(spec)

        self.inputs = Material("yarn")
        self.started_production_unit = ProductionUnit(spec)
        self.started_production_unit.perform_next_operation(self.worker)

        self.loaded_production_unit = ProductionUnit(spec)
        self.loaded_production_unit.perform_next_operation(self.worker)
        self.loaded_production_unit.perform_next_operation(self.worker)

        config = {'rate_by_minute': 0.2}
        spec_four = Specification()
        spec_four.add(MaterialInputConstraint(Material(type="flour", quantity=2)))
        spec_four.add(MaterialInputConstraint(Material(type="water", quantity=1)))
        spec_four.add_output_material(Material("bread", 1))

        self.four_a_pain = ProductionUnit(spec_four, config)

        self.four_a_pain.perform_next_operation(self.worker)
        self.four_a_pain.perform_next_operation(self.worker)

    def test_state_idle(self):
        self.assertEquals(self.unaffected_production_unit.get_state(), ProductionUnit.IDLE)

    def test_cannot_start_without_worker(self):
        self.assertRaises(NoWorkerToPerformAction, LoadOperation(self.inputs, self.unaffected_production_unit).run)

    def test_start_state(self):
        self.assertEquals(self.started_production_unit.get_state(), ProductionUnit.STARTED)

    def test_stop_operation_return_to_idle_state(self):
        StopOperation(self.started_production_unit, worker=self.worker).run()
        self.assertEquals(self.started_production_unit.get_state(), ProductionUnit.IDLE)

    def test_illegal_state(self):
        self.assertRaises(IllegalStateToPerformAction, StopOperation(self.unaffected_production_unit, worker=self.worker).run)

    def test_produce_without_input(self):
        self.assertRaises(InvalidInputLoaded, ProduceOperation(production_unit=self.started_production_unit, worker=self.worker).run, during=1)

    def test_produce_operation(self):
        ProduceOperation(self.loaded_production_unit, worker=self.worker).run()
        self.assertEquals(self.loaded_production_unit.get_state(), ProductionUnit.PRODUCING)
        self.assertEquals(len(self.loaded_production_unit.get_outputs()), 1)
        self.assertEquals(self.loaded_production_unit.get_outputs()[0].type, "output")

    def test_slower_machine_configuration(self):
        slower_gp, spec, zone = create_machine(material_type_input="yarn", rate=0.5)
        slower_gp.perform_next_operation(self.worker)
        slower_gp.perform_next_operation(self.worker)

        ProduceOperation(slower_gp, worker=self.worker).run(during=2)
        self.assertEquals(len(slower_gp.get_outputs()), 1)

    def test_invalid_input(self):
        input = Material("rocks")
        self.assertRaises(InvalidInputLoaded,
            LoadOperation(input, production_unit=self.affected_production_unit, worker=self.worker).run)

    def test_out_of_stock(self):
        self.assertRaises(InvalidInputLoaded, ProduceOperation(self.loaded_production_unit, worker=self.worker).run, during=3)

    def test_multiple_inputs(self):
        self.assertRaises(InvalidInputLoaded, ProduceOperation(self.four_a_pain,worker=self.worker).run, during=5)

    def test_complete_failure(self):
        ProduceOperation(production_unit=self.loaded_production_unit, worker=self.worker).run()
        self.loaded_production_unit.add_event(Failure())
        self.assertEquals(self.loaded_production_unit.get_state(), ProductionUnit.FAILURE)

        self.loaded_production_unit.add_event(Fix())
        self.assertEquals(self.loaded_production_unit.get_state(), ProductionUnit.STARTED)

    def test_add_skill_constraint_to_operation(self):
        tech_production_unit, spec, zone = create_machine(material_type_input="iron")

        start_op = StartOperation(tech_production_unit, worker=self.worker)
        start_op.add_constraint(SkillConstraint(skill_name="blacksmith"))

        self.assertRaises(CannotPerformOperation, start_op.run)

        blacksmith = Worker()
        blacksmith.skills.append("blacksmith")
        StartOperation(tech_production_unit, worker=blacksmith).run()
        self.assertEquals(tech_production_unit.get_state(), ProductionUnit.STARTED)

    def test_produce_consume_inputs(self):
        LoadOperation(Material("water", 1), self.four_a_pain, worker=self.worker).run()
        ProduceOperation(self.four_a_pain, worker=self.worker).run(during=5)
        self.assertRaises(InvalidInputLoaded, ProduceOperation(self.four_a_pain, worker=self.worker).run, during=5)

    def test_production_unit_with_stocking_area(self):
        stock_zone = StockingZone()
        self.loaded_production_unit.output_stocking_zone = stock_zone

        ProduceOperation(self.loaded_production_unit, worker=self.worker).run()

        self.assertEquals(stock_zone.count(), 1)

    def test_production_unit_with_limited_stocking_area(self):
        stock_zone = StockingZone(size=3)
        self.loaded_production_unit.output_stocking_zone = stock_zone

        LoadOperation(Material("yarn", 10), self.loaded_production_unit, worker=self.worker).run()
        try:
            ProduceOperation(self.loaded_production_unit, worker=self.worker).run(during=5)
        except Event:
            pass

        self.assertEquals(stock_zone.count(), 3)