Ejemplo n.º 1
0
    def composition(goals: Set[Goal],
                    name: str = None,
                    description: str = None) -> Goal:
        if name is None:
            names = []
            for goal in goals:
                names.append(goal.name)
            names.sort()
            conj_name = ""
            for name in names:
                conj_name += name + "||"
            name = conj_name[:-2]

        set_of_contracts = set()
        new_goal_world = None
        for g in goals:
            set_of_contracts.add(g.specification)
            if g.world is not None:
                if new_goal_world is None:
                    new_goal_world = g.world
                else:
                    if new_goal_world is not g.world:
                        raise GoalException(
                            "conjoining goals that have different 'variables'")

        try:
            new_contract = Contract.composition(set_of_contracts)

        except IncompatibleContracts as e:

            raise GoalAlgebraOperationFail(
                goals=goals,
                operation=GoalFailOperations.composition,
                contr_ex=e)

        except InconsistentContracts as e:

            raise GoalAlgebraOperationFail(
                goals=goals,
                operation=GoalFailOperations.composition,
                contr_ex=e)

        except UnfeasibleContracts as e:

            raise GoalAlgebraOperationFail(
                goals=goals,
                operation=GoalFailOperations.composition,
                contr_ex=e)

        new_goal = Goal(name=name,
                        description=description,
                        specification=new_contract,
                        world=new_goal_world)

        return new_goal
Ejemplo n.º 2
0
from world.simple_gridworld import SimpleGridWorld, SeA, SeB, GoA, GoB

sw = SimpleGridWorld()

t = sw.typeset
"""We are using the Init pattern which is basically atom corresponding to the type at time zero, 
    e.g. type: a => atom: a"""

c1 = Contract(assumptions=Init(t["se_a"]), guarantees=Init(t["a"]))
print(c1)

c2 = Contract(assumptions=Init(t["se_b"]), guarantees=Init(t["b"]))
print(c2)

try:
    c12 = Contract.composition({c1, c2})
    print(c12)
except ContractException as e:
    print(e.message)
    print("Unsatisfiable because se_a and se_b are mutually exclusive")
"""We can compute the conjunction"""

try:
    c12 = Contract.conjunction({c1, c2})
    print(c12)
    print("Conjunction is OK")
except ContractException as e:
    print(e.message)
"""Let's change the assumptions to be not mutually exclusive"""