### View Hello Resources (HTTP GET) Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/hal/passive/mams-hello-one/README.md Retrieve the current state of the Hello resource, which is a list of other resources. This uses a simple HTTP GET request. ```http GET http://localhost:9000/main/hello HTTP/1.1 ``` -------------------------------- ### Initial Output Example Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md This is the expected output after deploying the agent. It indicates the agent's base artifact and exposed URI. ```text [main]creating: main-base Creating base artifact: main Exposing Agent @ Base Uri: http://127.0.0.1:9000/main ``` -------------------------------- ### Example Usage of !delete and !read Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Demonstrates the invocation of the !delete subgoal to remove a resource, followed by a !read subgoal to confirm its deletion. This example shows how to verify that a resource has been successfully removed. ```java !delete(uri); !read(RESOURCE_URI); ``` -------------------------------- ### GET Request to Retrieve a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Retrieves the state of the '/main/hello' resource after creation. The response shows the created resource with its 'id' and 'name'. ```http HTTP/1.1 200 OK server: Netty date: Tue, 16 Apr 2024 14:01:56 +0100 location: content-type: application/json content-length: 28 [ { "id": "tester", "name": "Tester" } ] ``` -------------------------------- ### Main agent setup and resource creation Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md The Main agent's initial rule sets up the MAMS server, creates the agent's base, and initializes the 'sums' list resource. ```astra agent Main { constant string TYPE = "Sum"; constant string LIST_NAME = "sums"; rule +!main(list args) { // Setup the MAMS server on the default port (9000) MAMSAgent::!setup(); // Create the agents base (body) MAMSAgent::!created("base"); // Create the 'hello' list resource based on the Hello.java class. // The URL of this resource is: '/main/sums' Passive::!listResource(LIST_NAME, TYPE); } } ``` -------------------------------- ### Accept HTTP GET and POST Requests Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md This rule is used to accept GET and POST requests for the /main/hello endpoint. It ensures that the agent can handle incoming HTTP requests for the specified artifact. ```astra rule $cartago.signal(string AN, httpEvent(int index, string method)) : artifact("hello", AN, cartago.ArtifactId aid) { mams.javatypes.Active::!acceptRequest(index, AN); } ``` -------------------------------- ### View Updated Hello Resource (HTTP GET) Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello-one/README.md Perform a GET request after updating the resource to verify the changes. The response should reflect the new state. ```http HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:25:56 +0100 content-type: application/json content-length: 17 { "name": "Testee" } ``` -------------------------------- ### View Hello Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello-one/README.md Retrieves the current state of the Hello resource, which is a list of other resources. This is achieved using an HTTP GET request. ```APIDOC ## GET /main/hello ### Description Retrieves the state of the Hello resource, which is a list of other resources. ### Method GET ### Endpoint http://localhost:9000/main/hello ### Response #### Success Response (200) - **name** (string) - The name associated with the resource. #### Response Example { "name": "" } ``` -------------------------------- ### Example Usage of !create and !read Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Demonstrates the invocation of the !create subgoal to create a 'Rem' resource, followed by a !read subgoal to fetch its state. This shows the practical application of the resource creation rule. ```java !create(RESOURCE_URI, "Rem", "rem", string uri); !read(uri); ``` -------------------------------- ### Pinger Agent Initialization Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Handles the initial setup of the Pinger agent, connecting it to the MAMS environment and creating a list resource for incoming messages. ```Java initial !init(); rule +!init() { MAMSAgent::!init(); MAMSAgent::!created("base"); Passive::!listResource(LIST_NAME, TYPE); } ``` -------------------------------- ### Viewing Hello Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md Retrieves the current state of the Hello resource, which is a list of other resources. This is done using an HTTP GET request to the /main/hello endpoint. ```APIDOC ## GET /main/hello ### Description Retrieves the current state of the Hello resource. ### Method GET ### Endpoint /main/hello ### Response #### Success Response (200) - **name** (string) - The name representing the resource state. #### Response Example { "name": "" } #### Error Response (403) - **Forbidden** #### Response Example Forbidden ``` -------------------------------- ### PUT Request to Update a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Updates an existing 'hello' resource identified by '/main/hello/tester'. Only the 'name' field is modified in this example. ```http PUT http://localhost:9000/main/hello/tester HTTP/1.1 Content-Type: application/json { "name":"Testee" } ``` -------------------------------- ### Viewing Available Hello Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Retrieve the current state of the Hello resource, which is a list of other resources. This is done using an HTTP GET request to the /main/hello endpoint. ```APIDOC ## GET /main/hello ### Description Retrieves a list of available Hello resources. ### Method GET ### Endpoint /main/hello ### Parameters None ### Request Example None ### Response #### Success Response (200) - **[]** (array) - An empty array if no resources are present. - **[{"name": "Tester"}]** (array) - An array of resource objects, each with a 'name' field, if resources exist. #### Response Example ```json [ { "name": "Tester" } ] ``` ``` -------------------------------- ### GET request for Sum resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md Perform a GET request on the /main/sums endpoint to retrieve the list of 'Sum' resources. The response includes calculated totals. ```http HTTP/1.1 200 OK server: Netty date: Sun, 19 May 2024 11:41:02 +0100 content-type: application/json content-length: 32 [ { "left": 4, "right": 5, "total": 9 } ] ``` -------------------------------- ### View Updated Hello Resource State Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md Send a GET request after a successful PUT operation to verify the updated state of the Hello resource. ```http HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:25:56 +0100 content-type: application/json content-length: 17 { "name": "Testee" } ``` -------------------------------- ### Example Usage of !update and !read Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Illustrates the use of the !update subgoal to change the 'name' property of a resource to 'Bob', followed by a !read subgoal to verify the update. This showcases the resource modification process. ```java !update(uri, "Bob"); !read(uri); ``` -------------------------------- ### Viewing Available Hello Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Retrieves the current state of the Hello resource, which is a list of all created Hello objects. This is achieved using an HTTP GET request to the /main/hello endpoint. ```APIDOC ## GET /main/hello ### Description Retrieves the state of the Hello resource, which is a list of other resources. ### Method GET ### Endpoint /main/hello ### Response #### Success Response (200) - **(array)** - A JSON array containing Hello resource objects. #### Response Example ```json [ { "name": "Tester" } ] ``` ``` -------------------------------- ### Run MAMS Agent Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/hal/passive/mams-hello-one/README.md Deploy the MAMS agent to the base URI using the ASTRA Maven Plugin. This command starts the agent and exposes it for interaction. ```bash mvn astra:deploy ``` -------------------------------- ### GET Request to Verify Update Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Performs a GET request on a modified resource to verify the update. This is used after a PUT request to check the new state. ```http GET http://localhost:9000/main/hello/tester HTTP/1.1 ``` -------------------------------- ### Read resource implementation Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md This rule handles the HTTP GET request for a given URI. It checks for a 200 OK response and prints the resource content to the console. If the response is not successful, the agent fails. ```astra rule +!read(string uri) { mams.MAMSAgent::!get(uri, HttpResponse response); if (httpUtils.hasCode(response, 200)) { string content = httpUtils.bodyAsString(response); console.println("GET: " + uri); console.println(content); } else { S.fail(); } } ``` -------------------------------- ### Main agent behavior Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md This rule defines the main behavior of the agent, including setup and a sequence of CRUD operations on a resource. It uses a constant for the resource URI and calls helper rules for each operation. ```astra constant string RESOURCE_URI = "http://localhost:9000/main/hello"; rule +!main(list args) { mams.MAMSAgent::!setup(9001); !read(RESOURCE_URI); !create(RESOURCE_URI, "Rem", "rem", string uri); !read(uri); !update(uri, "Bob"); !read(uri); !delete(uri); !read(RESOURCE_URI); S.exit(); } ``` -------------------------------- ### Change Hello Resource (HTTP PUT) Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/hal/passive/mams-hello-one/README.md Modify the state of the Hello resource by sending a PUT request with a JSON body. The body should match the resource representation obtained via GET. ```http PUT http://localhost:9000/main/hello HTTP/1.1 Content-Type: application/json { "name":"Testee" } ``` -------------------------------- ### Main Agent Setup in ASTRA Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md This ASTRA code defines the Main agent, responsible for setting up the MAMS environment, creating 'pinger' and 'ponger' agents, and setting the initial goal for the 'ponger' agent. ```astra agent Main extends mams.MAMSAgent { rule +!main(list args) { MAMSAgent::!setup(); S.createAgent("pinger", "Pinger"); S.createAgent("ponger", "Ponger"); S.setMainGoal("ponger", ["http://localhost:9000/pinger"]); } } ``` -------------------------------- ### Delete Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Deletes a specific resource. A DELETE request to the resource's path will remove it from the system. Verification of deletion can be done by GETting the parent resource. ```APIDOC ## DELETE /main/hello/tester ### Description Deletes a specific resource. ### Method DELETE ### Endpoint /main/hello/tester ### Response #### Success Response (200) Indicates the resource was successfully deleted. ### Verification Perform a GET request to `/main/hello` to confirm the subresource is no longer present. ``` -------------------------------- ### Creating a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Create a new Hello resource by sending a POST request to the /main/hello endpoint with the resource content in the request body. The content should be a JSON object with a 'name' property. ```APIDOC ## POST /main/hello ### Description Creates a new Hello resource. ### Method POST ### Endpoint /main/hello ### Parameters #### Request Body - **name** (string) - Required - The name of the resource to create. #### Headers - **Slug** (string) - Optional - The identifier for the created resource. If not provided, a default naming scheme is used. ### Request Example ```json { "name":"Tester" } ``` ### Response #### Success Response (201) - **Location** (string) - The URI of the newly created resource. #### Response Example ``` HTTP/1.1 201 Created server: Netty date: Tue, 16 Apr 2024 13:57:10 +0100 content-type: application/json content-length: 0 Location: http://127.0.0.1:9000/main/hello/tester ``` ``` -------------------------------- ### Creating a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Creates a new Hello resource by sending a POST request to the /main/hello endpoint. The request body must contain the resource's data, typically a JSON object with a 'name' property. A 'Slug' header can be used to specify the identifier for the created resource. ```APIDOC ## POST /main/hello ### Description Creates a new Hello resource. The request body should contain the resource's content, and a 'Slug' header can be used to specify the resource's identifier. ### Method POST ### Endpoint /main/hello ### Parameters #### Request Body - **name** (string) - Required - The name of the Hello resource. ### Request Example ```json { "name":"Tester" } ``` ### Response #### Success Response (201) - **Location** (string) - The URI of the newly created resource. #### Response Example ```json HTTP/1.1 201 Created server: Netty date: Tue, 16 Apr 2024 13:57:10 +0100 content-type: application/json content-length: 0 Location: http://127.0.0.1:9000/main/hello/tester ``` ``` -------------------------------- ### Create a Hello Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Send a POST request to the /main/hello endpoint with a JSON body containing a 'name' property to create a new resource. The 'Slug' header can be used to specify the identifier for the created resource. ```http POST http://localhost:9000/main/hello HTTP/1.1 Content-Type: application/json Slug: tester { "name":"Tester" } ``` -------------------------------- ### Initial Hello Resource Response Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md The expected JSON response for an empty Hello resource. This indicates no specific resources are currently defined. ```json HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:18:03 +0100 content-type: application/json content-length: 11 { "name": "" } ``` -------------------------------- ### Initial Hello Resource Response Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello-one/README.md This is the expected initial response when querying the Hello resource list. ```http HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:18:03 +0100 content-type: application/json content-length: 11 { "name": "" } ``` -------------------------------- ### Accept HTTP Requests for Dynamically Named Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md This rule is required for resources created by POST HTTP Requests to the /main/hello endpoint. It accepts all HTTP Requests for artifacts whose names contain 'hello-'. ```astra rule $cartago.signal(string AN, httpEvent(int index, string method)) : artifact(string name, AN, cartago.ArtifactId aid) & S.contains(AN, "hello-") { mams.javatypes.Active::!acceptRequest(index, AN); } ``` -------------------------------- ### POST Request to Create a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Sends a POST request to create a new 'hello' resource. The 'id' field from the Java class is used as the identifier. ```http POST http://localhost:9000/main/hello HTTP/1.1 Content-Type: application/json { "id":"tester", "name":"Tester" } ``` -------------------------------- ### Hello Resource Update Response Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello-one/README.md This is the expected response after successfully updating the Hello resource. ```http HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:25:19 +0100 content-type: application/json content-length: 0 ``` -------------------------------- ### Create Resource with POST Request Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Defines a rule to create a resource using an HTTP POST request. It constructs a JSON object from provided parameters and sends it to a target URI. The response is checked for a 201 Created status, and the new resource's URI is extracted from the 'Location' header. ```java rule +!create(string target, string name, string slug, string uri) { JsonNode node = builder.createObject(); builder.addProperty(node, "name", name); mams.MAMSAgent::!httpRequest( httpUtils.post(target, [header("Slug", slug)], builder.toJsonString(node)), HttpResponse response ); if (httpUtils.hasCode(response, 201)) { uri = httpUtils.header(response, "Location"); } else { S.fail(); } } ``` -------------------------------- ### Compile MAMS Project Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/hal/passive/mams-hello-one/README.md Use this command to compile the Maven project. Ensure you are in the project's root directory. ```bash mvn compile ``` -------------------------------- ### Hello Resource Java Class Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Defines the structure of the Hello resource with a 'name' field. This class is used by the MAMS agent to represent resources. ```java public class Hello { public String name; } ``` -------------------------------- ### Change Hello Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello-one/README.md Updates the state of the Hello resource by sending a PUT request to the /main/hello endpoint with a JSON body representing the new resource state. ```APIDOC ## PUT /main/hello ### Description Changes the resource state by sending a PUT request to the /main/hello endpoint. The body of the request should match the resource representation. ### Method PUT ### Endpoint http://localhost:9000/main/hello ### Request Body - **name** (string) - Required - The new name to set for the resource. ### Request Example { "name": "Testee" } ### Response #### Success Response (200) This endpoint returns an empty body upon successful update. #### Response Example (No response body) ``` -------------------------------- ### Changing Hello Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md Updates the state of the Hello resource by sending a PUT request to the /main/hello endpoint with a JSON payload representing the new resource state. ```APIDOC ## PUT /main/hello ### Description Changes the state of the Hello resource. ### Method PUT ### Endpoint /main/hello ### Request Body - **name** (string) - Required - The desired new name for the resource. ### Request Example { "name":"Testee" } ### Response #### Success Response (200) - **Success** #### Response Example (No content) #### Error Response (403) - **Forbidden** #### Response Example Forbidden ``` -------------------------------- ### DELETE Request to Remove a Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Deletes a specific 'hello' resource identified by '/main/hello/tester' using a DELETE request. This is useful for cleaning up created resources. ```http DELETE http://localhost:9000/main/hello/tester HTTP/1.1 ``` -------------------------------- ### Modify Main Agent's !main Rule for Agent Creation and Messaging Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Update the '!main' rule in the 'Main' agent to set up the agent and handle incoming messages containing URIs. This involves creating a 'pinger' agent and processing messages with the 'uri(string)' formula. ```astra rule +!main(list args) { MAMSAgent::!setup(); S.createAgent("pinger", "Pinger"); } rule @message(inform, "pinger", uri(string uri)) { S.createAgent("ponger", "Ponger"); S.setMainGoal("ponger", [uri]); } ``` -------------------------------- ### Modify Pinger Agent's !init Rule to Send URI Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Update the '!init' rule in the 'Pinger' agent to retrieve its own URI and send it to the 'Main' agent using the defined 'uri(string)' formula. This involves initializing the agent and then sending the URI. ```astra rule +!init() { MAMSAgent::!init(); MAMSAgent::!created("base"); Passive::!listResource(LIST_NAME, TYPE); !agentUri(string uri); send(inform, system.getOwner(), uri(uri)); } ``` -------------------------------- ### Successful Resource Update Response Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md The expected 200 OK response after successfully updating the Hello resource state via a PUT request. ```http HTTP/1.1 200 OK server: Netty date: Mon, 23 Sep 2024 13:25:19 +0100 content-type: application/json content-length: 0 ``` -------------------------------- ### Using @Identifier with Resources Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Demonstrates how to use the @Identifier annotation in the Hello.java class to specify a field that should be used as the resource's identifier. This allows omitting the 'Slug' header during resource creation. ```APIDOC ## POST /main/hello (with @Identifier) ### Description Creates a new Hello resource using the field annotated with `@Identifier` as its unique identifier. This method allows omitting the `Slug` header from the request. ### Method POST ### Endpoint /main/hello ### Parameters #### Request Body - **id** (string) - Required - The identifier for the Hello resource (annotated with @Identifier). - **name** (string) - Required - The name of the Hello resource. ### Request Example ```json { "id":"tester", "name":"Tester" } ``` ### Response #### Success Response (201) - **Location** (string) - The URI of the newly created resource. #### Response Example ```json HTTP/1.1 201 Created server: Netty date: Tue, 16 Apr 2024 14:01:56 +0100 content-type: application/json content-length: 0 Location: http://127.0.0.1:9000/main/hello/tester ``` ``` -------------------------------- ### Agent response to POST request Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md This output indicates that the agent received and processed the POST request to the /main/sums endpoint. ```text [Web Server] Method: POST / Action: /main/sums ``` -------------------------------- ### Handle new passive resource creation Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md This rule triggers when a new passive resource is created. It retrieves the resource object, calculates the sum, and updates the resource. ```astra rule +itemResource(string artifact_name, TYPE) { // Get the java object representation !getObject(artifact_name, Sum sum); // Perform the sum calculation int total = oa.getInt(sum, "left") + oa.getInt(sum, "right"); // Store the sum in the object oa.set(sum, "total", total); // Update the resource !updateObject(artifact_name, sum); } ``` -------------------------------- ### Java Class with @Identifier Annotation Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello/README.md Defines a Java class 'Hello' where the 'id' field is designated as the resource identifier using the @Identifier annotation. This replaces the need for a 'Slug' header. ```java import mams.utils.Identifier; public class Hello { @Identifier public String id; public String name; } ``` -------------------------------- ### Add Rule for PUT Request Handling Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md This CArtAgo rule enables the agent to accept and process PUT requests for the 'hello' artifact. It calls the `acceptRequest` method. ```astra rule $cartago.signal(string AN, httpEvent(int index, "PUT")) : artifact("hello", AN, cartago.ArtifactId aid) { mams.javatypes.Active::!acceptRequest(index, AN); } ``` -------------------------------- ### Incorporate Shared Class into Main Agent Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Extend the 'Main' agent class with the 'Shared' class to enable it to use the defined communication types. ```astra agent Main extends mams.MAMSAgent, Shared { ... } ``` -------------------------------- ### Hello Resource Response After Rule Removal Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/active/mams-hello-one/README.md Response after commenting out a rule, demonstrating access control. A 403 Forbidden status is returned when the agent cannot process the request. ```http HTTP/1.1 403 Forbidden server: Netty date: Mon, 23 Sep 2024 13:19:34 +0100 content-type: text/plain; charset=UTF-8 content-length: 9 Forbidden ``` -------------------------------- ### Associate Shared Class with Pinger Agent Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Extend the 'Pinger' agent class with the 'Shared' class, as the 'pinger' agent will be sending messages that utilize the shared communication types. ```astra agent Pinger extends mams.javatypes.Passive, Shared { ... } ``` -------------------------------- ### Send POST request to create a Sum resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-summer/README.md Send a POST request with JSON payload to the /main/sums endpoint to create a new 'Sum' resource. The 'Slug' header is used for identification. ```http POST http://localhost:9000/main/sums HTTP/1.1 Content-Type: application/json Slug: sum1 { "left":4, "right":5 } ``` -------------------------------- ### Pinger Agent Context Extraction for Pong Message Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Extracts context information, specifically the artifact name and its URI, to be included in the pong message. This is crucial for the response. ```Java artifact(LIST_NAME, string artifact_name, cartago.ArtifactId id) & uri(artifact_name, string pinger_uri) ``` -------------------------------- ### Update Resource with PUT Request Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Defines a rule to update an existing resource using an HTTP PUT request. It constructs a new JSON representation of the resource and sends it to the target URI. The response is validated for a 200 OK status. ```java rule +!update(string target, string name) { JsonNode node = builder.createObject(); builder.addProperty(node, "name", name); mams.MAMSAgent::!put( target, builder.toJsonString(node), HttpResponse response ); if (~httpUtils.hasCode(response, 200)) { S.fail(); } } ``` -------------------------------- ### Pinger Agent Item Resource Handling Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Processes new resources added to the 'pings' list. It retrieves the URI and triggers the pong message response. ```Java rule +itemResource(string artifact_name, TYPE) { !itemProperty(artifact_name, URI, funct value); !pinged(F.valueAsString(value, 0)); } ``` -------------------------------- ### Define Shared ASTRA Types for Communication Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Create a shared ASTRA class to define the communication formulas, such as 'uri(string)'. This class will be incorporated into other agent classes that need to communicate. ```astra agent Shared { types communications { formula uri(string); } } ``` -------------------------------- ### Delete Resource with DELETE Request Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-helloagent/README.md Defines a rule to delete a resource using an HTTP DELETE request. It sends a DELETE request to the specified target URI and checks for a 200 OK response. If the response code is not 200, the plan fails. ```java rule +!delete(string target) { mams.MAMSAgent::!delete(target, HttpResponse response); if (~httpUtils.hasCode(response, 200)) { S.fail(); } } ``` -------------------------------- ### Pinger Agent Constants Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Defines constants for message type, list name, and URI within the Pinger agent. These improve code readability. ```Java agent Pinger extends mams.javatypes.Passive { module mams.JSONBuilder builder; constant string TYPE = "Message"; constant string LIST_NAME = "pings"; constant string URI = "uri"; } ``` -------------------------------- ### Ponger Agent Types and Initialization Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Defines the 'pongs' type for the Ponger agent and initializes its counter to 0. This counter manages the number of pong messages sent. ```Java types ponger { formula pongs(int); } initial pongs(0); ``` -------------------------------- ### Pinger Agent Pong Message Sending Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Constructs and sends a pong message in response to a ping. It uses JSONBuilder to create the message body and handles the HTTP POST request. ```Java rule +!pinged(string pongerUri) : artifact(LIST_NAME, string artifact_name, cartago.ArtifactId id) & uri(artifact_name, string pinger_uri) { JsonNode node = builder.createObject(); builder.addProperty(node, URI, pinger_uri); mams.MAMSAgent::!post(pongerUri, builder.toJsonString(node), HttpResponse response ); if (httpUtils.hasCode(response, 201)) { string uri = httpUtils.header(response, "Location"); } else { S.fail(); } } ``` -------------------------------- ### Ponger Agent Incremental Pong Message Sending Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Handles the sending of pong messages while incrementing the 'pongs' counter. This rule is active as long as the 'pongs' count is less than 10. ```Java rule +itemResource(string artifact_name, TYPE) : pongs(int X) { -+pongs(X+1); !itemProperty(artifact_name, URI, funct value); !pinged(F.valueAsString(value, 0)); } ``` -------------------------------- ### Update Resource Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-hello/README.md Updates a specific resource identified by its path. The resource's state can be modified by sending a PUT request with the updated data in the request body. Attempts to modify the `@Identifier` annotation will result in an error. ```APIDOC ## PUT /main/hello/tester ### Description Updates the state of a specific resource. ### Method PUT ### Endpoint /main/hello/tester ### Request Body - **name** (string) - Required - The new name for the resource. ### Request Example { "example": "{\n \"name\":\"Testee\"\n}" } ### Response #### Success Response (200) - **name** (string) - The updated name of the resource. #### Response Example { "example": "{\n \"name\":\"Testee\"\n}" } ### Error Handling - **500 Internal Server Error**: Returned if an attempt is made to modify the `@Identifier` annotation. ``` -------------------------------- ### Ponger Agent Stop Condition Source: https://gitlab.com/mams-ucd/mams-guide/-/blob/main/javatypes/passive/mams-pong/README.md Implements the rule to stop sending pong messages when the 'pongs' count reaches 10. It prints a message to the console. ```Java rule +itemResource(string artifact_name, TYPE) : pongs(10) { cartago.println("10 PONGS!!!"); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.