Esempio n. 1
0
    def generate(self, m: clingo.Model) -> None:
        self._raw_model = m.__str__()

        self.infra_agents = []
        self.product_agents = []

        predicates = m.symbols(atoms=True)
        node_func = {
            "infra_agent": self.add_infra_agent,
            "product_agent": self.add_product_agent,
        }
        edge_func = {"depends_on": self.add_edge}

        for predicate in predicates:
            if predicate.name in node_func:
                node_func[predicate.name](predicate.arguments[0].number)

        self.edges = [
            set() for x in range(len(self.infra_agents) + len(self.product_agents))
        ]

        for predicate in predicates:
            if predicate.name in edge_func:
                edge_func[predicate.name](
                    predicate.arguments[0].number, predicate.arguments[1].number
                )

        print(self._raw_model)
Esempio n. 2
0
    def on_model(self, m: clingo.Model) -> None:
        # Separate into 'class(?)', 'interface(?)', 'implements(?, ?)', 'extends(?, ?)'
        # 'add_method(?, ?)',
        # 'add_method(?, ?)', 'has_method_with_parameter(?, ?)'
        predicates = m.symbols(atoms=True)
        node_func = {
            "class": self._add_class,
            "interface": self._add_interface
        }
        edge_func = {
            "extends": self._add_extend,
            "implements": self._add_implement,
            "add_method": self._add_method,
            "has_method_with_parameter": self._add_to_parameter_set,
        }
        # Two passes,
        #   First pass creates individual nodes like class, interface.
        for predicate in predicates:
            if predicate.name in node_func:
                node_func[predicate.name](predicate.arguments[0].string)

        #   Second pass creates edge between two nodes.
        for predicate in predicates:
            if predicate.name in edge_func:
                edge_func[predicate.name](
                    predicate.arguments[0].string,
                    predicate.arguments[1].string,
                )
Esempio n. 3
0
    def set_transition(self, model: clingo.Model):
        next_reward = None
        next_state = set()
        available_actions = set()

        for symbol in model.symbols(shown=True):

            if symbol.name == 'nextState':
                # ˙Atom is of the form `state(f(...))`
                # where`f(...)` is an uninterpreted function belonging to the state representation.
                f = symbol.arguments[0]
                next_state.add(str(f))

            if symbol.name == 'nextReward':
                # Atom is of the form `nextReward(r)`, and `r` is the reward.
                next_reward = symbol.arguments[0].number

            if symbol.name == 'nextExecutable':
                # Atom is of the form `nextExecutable(f(...))`
                # where`f(...)` is an uninterpreted function representing an executable action.
                available_actions.add(str(symbol.arguments[0]))

        self.state = frozenset(next_state)
        self.available_actions = available_actions

        # Update trajectory:
        self.action_history.append(self.action)  # A[t]
        self.state_history.append(frozenset(next_state))  # S[t+1]
        self.reward_history.append(next_reward)  # R[t+1]
Esempio n. 4
0
    def on_model(self, m: clingo.Model) -> bool:
        # Same set of parameters and search algorithm will produce the same
        # result set. To make sure two different agent using the same settings
        # can produce different output, we are counting models in the result
        # set. The first agent using the same configuration gets first one,
        # the second agent using the same configuration gets second one, and so
        # on so forth.
        self.model_count -= 1
        if self.model_count > 0:
            return True

        # Separate into 'class(?)', 'interface(?)', 'funcs(?)',
        # 'implements(?, ?)', 'extends(?, ?)', 'add_method(?, ?)',
        # 'add_static_method(?, ?)', 'has_method_with_parameter(?, ?)'
        # 'invokes_function(?, ?)', 'creates_in_body(?, ?)'
        # 'invokes_in_method(?, ?, ?)', 'invokes_static_method(?, ?, ?)'
        # 'invokes_in_body(?, ?, ?)', 'has_parameter_and_argument(?, ?, ?)'
        predicates = m.symbols(atoms=True)
        node_func = {
            "class": self._add_class,
            "interface": self._add_interface,
            "funcs": self._add_function,
        }
        edge_func = {
            "extends": self._add_extend,
            "implements": self._add_implement,
            "add_method": self._add_method,
            "add_static_method": self._add_static_method,
            "has_method_with_parameter": self._add_to_parameter_set,
            "invokes_function": self._add_invoke_function,
            "creates_in_body": self._add_object_in_function,
        }
        trip_func = {
            "invokes_in_method": self._add_invoke,
            "invokes_static_method": self._add_invoke_static_method,
            "invokes_in_body": self._add_invoke_in_function,
            "has_parameter_and_argument": self._add_parameter_to_function,
        }
        # Three passes,
        #   First pass creates individual nodes like class, interface, function.
        for predicate in predicates:
            if predicate.name in node_func:
                node_func[predicate.name](predicate.arguments[0].string)

        #   Second pass creates edge between two nodes.
        for predicate in predicates:
            if predicate.name in edge_func:
                edge_func[predicate.name](predicate.arguments[0].string,
                                          predicate.arguments[1].string)
        #   Third pass creates relationships between three nodes.
        for predicate in predicates:
            if predicate.name in trip_func:
                trip_func[predicate.name](
                    predicate.arguments[0].string,
                    predicate.arguments[1].string,
                    predicate.arguments[2].string,
                )
        self._find_stubs()
        return False
Esempio n. 5
0
    def set_available_actions(self, model: clingo.Model):
        available_actions = set()
        for symbol in model.symbols(shown=True):
            # We expect atoms of the form `currentExecutable(move(X, Y)`
            # but we are only interested in the first argument `move(X, Y)`
            available_actions.add(str(symbol.arguments[0]))

        self.available_actions = available_actions
Esempio n. 6
0
    def __extract_answer_set(self, answer_set: clingo.Model):
        """Extract the answer set (of type clingo.Model) into a list of predicates.

        :param answer_set: Answer set.
        :return: List of predicates in the answer set.
        """
        row = []
        symbols = answer_set.symbols(
            shown=True) if self.__shown_atoms_only else answer_set.symbols(
                atoms=True)
        for symbol in symbols:
            symbol_args = self.__get_arguments_representations(symbol)
            row_str = ARGUMENT_DELIMITER.join(
                [str(sym) for sym in symbol_args])
            if self.__show_predicates_symbols:
                row_str = f'{symbol.name}({row_str})'
            row.append(row_str)
        return row
Esempio n. 7
0
def mop(m: clingo.Model):
    av = set()
    for symbol in m.symbols(shown=True):
        # We expect atoms of the form `currentExecutable(move(X, Y)`
        # but we are only interested in the first argument `move(X, Y)`
        #av.add(str(symbol.arguments[0]))
        sym = str(symbol)
        av.add(sym)
    print(sorted(av))
Esempio n. 8
0
    def on_model(self, m: clingo.Model) -> bool:
        # Same set of parameters and search algorithm will produce the same
        # result set. To make sure two different agent using the same settings
        # can produce different output, we are counting models in the result
        # set. The first agent using the same configuration gets first one,
        # the second agent using the same configuration gets second one, and so
        # on so forth.
        self.model_count -= 1
        if self.model_count > 0:
            return True

        self._raw_model = m.__str__()
        return False
Esempio n. 9
0
    def on_model(self, m: clingo.Model) -> None:
        # Separate into 'class(?)', 'interface(?)', 'funcs(?)',
        # 'implements(?, ?)', 'extends(?, ?)', 'add_method(?, ?)',
        # 'add_static_method(?, ?)', 'has_method_with_parameter(?, ?)'
        # 'invokes_function(?, ?)', 'creates_in_body(?, ?)'
        # 'invokes_in_method(?, ?, ?)', 'invokes_static_method(?, ?, ?)'
        # 'invokes_in_body(?, ?, ?)', 'has_parameter_and_argument(?, ?, ?)'
        predicates = m.symbols(atoms=True)
        node_func = {
            "class": self._add_class,
            "interface": self._add_interface,
            "funcs": self._add_function,
        }
        edge_func = {
            "extends": self._add_extend,
            "implements": self._add_implement,
            "add_method": self._add_method,
            "add_static_method": self._add_static_method,
            "has_method_with_parameter": self._add_to_parameter_set,
            "invokes_function": self._add_invoke_function,
            "creates_in_body": self._add_object_in_function,
        }
        trip_func = {
            "invokes_in_method": self._add_invoke,
            "invokes_static_method": self._add_invoke_static_method,
            "invokes_in_body": self._add_invoke_in_function,
            "has_parameter_and_argument": self._add_parameter_to_function,
        }
        # Three passes,
        #   First pass creates individual nodes like class, interface, function.
        for predicate in predicates:
            if predicate.name in node_func:
                node_func[predicate.name](predicate.arguments[0].string)

        #   Second pass creates edge between two nodes.
        for predicate in predicates:
            if predicate.name in edge_func:
                edge_func[predicate.name](
                    predicate.arguments[0].string,
                    predicate.arguments[1].string,
                )
        #   Third pass creates relationships between three nodes.
        for predicate in predicates:
            if predicate.name in trip_func:
                trip_func[predicate.name](
                    predicate.arguments[0].string,
                    predicate.arguments[1].string,
                    predicate.arguments[2].string,
                )
Esempio n. 10
0
 def on_model(m: Model):
     self.assertTrue(m.contains(Function('a')))
     self.assertTrue(m.is_true(cast(SymbolicAtom, m.context.symbolic_atoms[Function('a')]).literal))
     self.assertFalse(m.is_true(1000))
     self.assertEqual(m.thread_id, 0)
     self.assertEqual(m.number, 1)
     self.assertFalse(m.optimality_proven)
     self.assertEqual(m.cost, [3])
     m.extend([Function('e')])
     self.assertSequenceEqual(m.symbols(theory=True), [Function('e')])
Esempio n. 11
0
    def on_model(self, m: clingo.Model):
        """Appends an answer set to the output.

        :param m: a clingo model
        """
        self.output.append(m.symbols(False, False, True, False, False))
Esempio n. 12
0
 def on_model(self, m: clingo.Model) -> None:
     self._raw_model = m.__str__()