### Remote CArtAgO Workspace Interaction (Java) Source: https://context7.com/astra-language/cartago/llms.txt This Java code illustrates how to interact with remote CArtAgO workspaces. It covers starting a CArtAgO node, installing infrastructure, starting a service, and creating local artifacts on one node. On a second node, it demonstrates connecting to the remote workspace, joining a session, and executing operations on remote artifacts. Dependencies include `cartago` and `cartago.security` libraries. ```java import cartago.*; import cartago.security.AgentCredential; // Node 1: Setup infrastructure service try { CartagoService.startNode(); // Install RMI infrastructure layer CartagoService.installInfrastructureLayer("default"); // Start infrastructure service on specific address String serviceAddress = "localhost:20100"; CartagoService.startInfrastructureService("default", serviceAddress); System.out.println("CArtAgO node ready at: " + serviceAddress); // Create artifacts in local workspace ICartagoController controller = CartagoService.getController("main"); ArtifactId sensorId = controller.makeArtifact("sharedSensor", "Sensor", new ArtifactConfig("RemoteSensor1")); } catch (CartagoException e) { e.printStackTrace(); } // Node 2: Connect to remote workspace try { String remoteWspName = "main"; String remoteAddress = "localhost:20100"; String protocol = "default"; // RMI AgentCredential cred = new AgentCredential() {}; ICartagoListener listener = new ICartagoListener() { @Override public void notifyCartagoEvent(CartagoEvent ev) { System.out.println("Remote event: " + ev); } }; // Join remote workspace ICartagoSession remoteSession = CartagoService.startRemoteSession( remoteWspName, remoteAddress, protocol, cred, listener ); // Execute operations on remote artifacts Op updateOp = new Op("updateReading", 25.5, 60.0); remoteSession.doAction("sharedSensor", updateOp, null, 5000); System.out.println("Remote operation executed successfully"); } catch (CartagoException e) { System.err.println("Remote connection failed: " + e.getMessage()); } ``` -------------------------------- ### Start a CArtAgO Node and Session (Java) Source: https://context7.com/astra-language/cartago/llms.txt Initializes a local CArtAgO node, starts a session for an agent to join a workspace, and fetches workspace contexts. It requires the cartago library. The output includes joined workspaces and any Cartago events received. ```java import cartago.*; import cartago.security.AgentCredential; // Start a local CArtAgO node CartagoService.startNode(); // Create or get the main workspace (created by default) String workspaceName = "main"; // Start a session for an agent to join the workspace AgentCredential cred = new AgentCredential() { // Credential implementation }; ICartagoListener listener = new ICartagoListener() { @Override public void notifyCartagoEvent(CartagoEvent ev) { System.out.println("Event received: " + ev); } }; ICartagoSession session = CartagoService.startSession( workspaceName, cred, listener ); // Fetch workspace contexts List workspaces = session.getJoinedWorkspaces(); System.out.println("Joined workspaces: " + workspaces); // Shutdown when done CartagoService.shutdownNode(); ``` -------------------------------- ### Add Files using Command Line - Git Source: https://gitlab.com/astra-language/cartago/-/blob/main/README.md This snippet demonstrates how to add files to the Cartago project using the Git command line. It involves navigating to an existing repository, setting the remote origin, and pushing the main branch to the remote repository. ```git cd existing_repo git remote add origin https://gitlab.com/astra-language/cartago.git git branch -M main git push -uf origin main ``` -------------------------------- ### Execute Artifact Operations with Cartago Java Source: https://context7.com/astra-language/cartago/llms.txt Demonstrates how to perform operations on artifacts using the Cartago Java API. It covers creating, looking up, executing simple operations (like 'inc'), and retrieving values from artifacts, including the use of feedback parameters. Assumes a pre-existing session. ```java import cartago.*; // Assuming session is already created ICartagoContext ctx = session.getJoinedWorkspaces().get(0); WorkspaceId wspId = ctx.getWorkspaceId(); // Create an artifact in the workspace ArtifactId counterId = null; try { // Create artifact named "myCounter" of type "Counter" with initial value 0 Op createOp = new Op("makeArtifact", "myCounter", "Counter", new ArtifactConfig(0)); long actionId = session.doAction(createOp, null, -1); // Lookup the created artifact Op lookupOp = new Op("lookupArtifact", "myCounter"); session.doAction(lookupOp, null, -1); // Execute operations on the artifact Op incOp = new Op("inc"); session.doAction("myCounter", incOp, null, -1); Op incOp2 = new Op("inc"); session.doAction("myCounter", incOp2, null, -1); // Get value with output parameter OpFeedbackParam result = new OpFeedbackParam<>(); Op getOp = new Op("getValue", result); session.doAction("myCounter", getOp, null, -1); System.out.println("Counter value: " + result.get()); // Output: 2 } catch (CartagoException e) { System.err.println("Operation failed: " + e.getMessage()); e.printStackTrace(); } // Process events CartagoEvent event; while ((event = session.fetchNextPercept()) != null) { System.out.println("Percept: " + event); } ``` -------------------------------- ### Enter Critical Section (Build New) Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/test.txt This protocol enters a critical section by building a new one if it's not already known. It uses the 'build' command to create the 'cs' artifact and then acquires it. Pre-condition ensures the 'cs' artifact is not known. ```Cartago usageprot enter_critical_section1 { :function enterCS :precond not known_artifact("cs",_) :body { build("cs",[1],ToolId); use(ToolId,acquire); +inside_cs(ToolId) } } ``` -------------------------------- ### Create a Custom Artifact with Operations (Java) Source: https://context7.com/astra-language/cartago/llms.txt Defines a custom artifact named 'Counter' in Java, including initialization, observable properties, and operations for incrementing, decrementing, and retrieving the value. It demonstrates the use of @OPERATION, @GUARD, and signal/event generation. Requires the cartago library. ```java import cartago.*; public class Counter extends Artifact { // Initialization method called when artifact is created void init(int initialValue) { // Define an observable property named "count" defineObsProperty("count", initialValue); log("Counter initialized with value: " + initialValue); } // Operation annotation makes this method callable by agents @OPERATION public void inc() { ObsProperty prop = getObsProperty("count"); int currentValue = prop.intValue(); prop.updateValue(currentValue + 1); // Generate a signal/event signal("incremented", currentValue + 1); } @OPERATION public void dec() { ObsProperty prop = getObsProperty("count"); int currentValue = prop.intValue(); prop.updateValue(currentValue - 1); signal("decremented", currentValue - 1); } @OPERATION public void getValue(OpFeedbackParam result) { ObsProperty prop = getObsProperty("count"); result.set(prop.intValue()); } // Guard method that checks a condition @GUARD boolean isPositive() { ObsProperty prop = getObsProperty("count"); return prop.intValue() > 0; } // Operation with a guard - only executes when guard is true @OPERATION(guard="isPositive") public void decIfPositive() { ObsProperty prop = getObsProperty("count"); prop.updateValue(prop.intValue() - 1); } } ``` -------------------------------- ### Compute Sine using Cartago Tool Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/test.txt This protocol computes the sine of a given number using a 'calc' tool. It locates the tool, uses it to compute the sine, and senses the result. Dependencies include the 'calc' tool and its computeSin function. ```Cartago usageprot compute_sin { :function sin(X,Y) :body { locate("calc",ToolId); use(ToolId,computeSin(X),s0); sense(s0,sin(X,Y)) } } ``` -------------------------------- ### Enter Critical Section (Acquire Existing) Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/test.txt This protocol enters an existing critical section by acquiring it. It requires the 'cs' artifact to be known and the current context not to be already inside a critical section. It then marks the context as being inside the critical section. ```Cartago usageprot enter_critical_section2 { :function enterCS :precond known_artifact("cs",ToolId) & not inside_cs(_) :body { use(ToolId,acquire); +inside_cs(ToolId) } } ``` -------------------------------- ### Cartago Bounded Buffer Artifact with Guards (Java) Source: https://context7.com/astra-language/cartago/llms.txt Implements a bounded buffer artifact using Cartago's @GUARD and @OPERATION annotations for thread-safe producer-consumer synchronization. It defines observable properties for buffer state and uses await_time for delayed operations. ```java import cartago.*; public class BoundedBuffer extends Artifact { private static final int MAX_SIZE = 10; void init() { defineObsProperty("items", new Object[MAX_SIZE]); defineObsProperty("size", 0); defineObsProperty("capacity", MAX_SIZE); } @GUARD boolean notFull() { return getObsProperty("size").intValue() < MAX_SIZE; } @GUARD boolean notEmpty() { return getObsProperty("size").intValue() > 0; } // Operation waits until buffer is not full @OPERATION(guard="notFull") public void put(Object item) { ObsProperty itemsProp = getObsProperty("items"); ObsProperty sizeProp = getObsProperty("size"); int currentSize = sizeProp.intValue(); Object[] items = (Object[]) itemsProp.getValue(); items[currentSize] = item; itemsProp.updateValue(items); sizeProp.updateValue(currentSize + 1); signal("item_added", item, currentSize + 1); } // Operation waits until buffer is not empty @OPERATION(guard="notEmpty") public void get(OpFeedbackParam result) { ObsProperty itemsProp = getObsProperty("items"); ObsProperty sizeProp = getObsProperty("size"); int currentSize = sizeProp.intValue(); Object[] items = (Object[]) itemsProp.getValue(); Object item = items[0]; // Shift items for (int i = 0; i < currentSize - 1; i++) { items[i] = items[i + 1]; } items[currentSize - 1] = null; itemsProp.updateValue(items); sizeProp.updateValue(currentSize - 1); result.set(item); signal("item_removed", item, currentSize - 1); } // Manual await with time @OPERATION public void delayedOperation(int milliseconds) { log("Starting delayed operation..."); await_time(milliseconds); log("Delay completed"); signal("delay_finished", milliseconds); } } ``` -------------------------------- ### Define and Manipulate Observable Properties in Cartago Java Artifacts Source: https://context7.com/astra-language/cartago/llms.txt Shows how to define and manage observable properties within a Cartago artifact written in Java. This includes initializing properties with various data types, updating them, and signaling events with multiple parameters. It also demonstrates how to retrieve property values and handle invalid operations. ```java import cartago.*; public class Sensor extends Artifact { void init(String sensorName) { // Define properties with multiple values defineObsProperty("sensorName", sensorName); defineObsProperty("temperature", 20.0); defineObsProperty("humidity", 45.0); defineObsProperty("status", "active"); defineObsProperty("readings", 0); } @OPERATION public void updateReading(double temp, double humid) { // Update individual properties ObsProperty tempProp = getObsProperty("temperature"); tempProp.updateValue(temp); ObsProperty humidProp = getObsProperty("humidity"); humidProp.updateValue(humid); // Increment reading count ObsProperty countProp = getObsProperty("readings"); countProp.updateValue(countProp.intValue() + 1); // Signal with multiple parameters signal("reading_updated", temp, humid, countProp.intValue()); } @OPERATION public void getStatus(OpFeedbackParam status, OpFeedbackParam temp, OpFeedbackParam humid) { status.set(getObsProperty("status").stringValue()); temp.set(getObsProperty("temperature").doubleValue()); humid.set(getObsProperty("humidity").doubleValue()); } @OPERATION public void setStatus(String newStatus) { if (!newStatus.equals("active") && !newStatus.equals("inactive")) { failed("Invalid status", "invalid_status", newStatus); } getObsProperty("status").updateValue(newStatus); } @OPERATION public void reset() { getObsProperty("temperature").updateValue(0.0); getObsProperty("humidity").updateValue(0.0); getObsProperty("readings").updateValue(0); signal("sensor_reset"); } } ``` -------------------------------- ### Enter Critical Section (Already Inside) Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/test.txt This protocol handles the case where the context is already inside a critical section. It has no operations in its body, as the pre-condition 'inside_cs(_)' ensures the desired state is met. ```Cartago usageprot enter_critical_section3 { :function enterCS :precond inside_cs(_) :body {} } ``` -------------------------------- ### Control Linked Artifact Operations in CArtAgO (Java) Source: https://context7.com/astra-language/cartago/llms.txt This Java code demonstrates how to control linked artifacts by executing operations on them. It defines a Controller artifact with operations to reset, activate, and deactivate a linked sensor, and to monitor its status. It uses `execLinkedOp` for cross-artifact calls and handles `OperationException` for error management. Dependencies include the `cartago` library. ```java import cartago.*; @ARTIFACT_INFO( outports = { @OUTPORT(name = "out-1") } ) public class Controller extends Artifact { void init() { defineObsProperty("controllerStatus", "ready"); } @OPERATION public void controlSensor(String command) { if (!isLinked("out-1")) { failed("No sensor linked", "no_link"); return; } try { // Execute operation on linked artifact if (command.equals("reset")) { execLinkedOp("out-1", "reset"); log("Sensor reset completed"); } else if (command.equals("activate")) { execLinkedOp("out-1", "setStatus", "active"); log("Sensor activated"); } else if (command.equals("deactivate")) { execLinkedOp("out-1", "setStatus", "inactive"); log("Sensor deactivated"); } signal("command_executed", command); } catch (OperationException e) { failed("Failed to execute command", "operation_failed", command, e.getMessage()); } } @OPERATION public void monitorSensor() { try { OpFeedbackParam status = new OpFeedbackParam<>(); OpFeedbackParam temp = new OpFeedbackParam<>(); OpFeedbackParam humid = new OpFeedbackParam<>(); execLinkedOp("out-1", "getStatus", status, temp, humid); log("Sensor status: " + status.get()); log("Temperature: " + temp.get()); log("Humidity: " + humid.get()); signal("monitor_result", status.get(), temp.get(), humid.get()); } catch (OperationException e) { failed("Monitoring failed", "monitor_error", e.getMessage()); } } } // Usage: Link artifacts together // In agent code or through workspace controller: // linkArtifacts("myController", "out-1", "mySensor"); ``` -------------------------------- ### Cartago: Enter Critical Section Protocol Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/calculator.txt Defines protocols for entering a critical section using Cartago. It handles cases where the critical section artifact is not yet known, is known and the tool is not inside, or the tool is already inside. It involves building the artifact, acquiring the tool, and asserting the tool's presence inside the critical section. ```Cartago usageprot enter_critical_section1 { :function enterCS :precond not known_artifact("cs",_) :body { build("cs",[1],ToolId); use(ToolId,acquire); +inside_cs(ToolId) } } usageprot enter_critical_section2 { :function enterCS :precond known_artifact("cs",ToolId) & not inside_cs(_) :body { use(ToolId,acquire); +inside_cs(ToolId) } } usageprot enter_critical_section3 { :function enterCS :precond inside_cs(_) :body {} } ``` -------------------------------- ### Exit Critical Section Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/test.txt This protocol releases the critical section. It requires the context to be inside the critical section, uses the 'release' command on the tool, and then removes the 'inside_cs' state. ```Cartago usageprot exit_critical_section { :function exitCS :precond inside_cs(ToolId) :body { use(ToolId,release); -inside_cs(ToolId) } } ``` -------------------------------- ### Cartago: Exit Critical Section Protocol Source: https://gitlab.com/astra-language/cartago/-/blob/main/src/main/java/cartago/manual/parser/calculator.txt Defines the protocol for exiting a critical section in Cartago. This protocol requires that the tool is currently inside the critical section. It involves releasing the tool and asserting that the tool is no longer inside the critical section. ```Cartago usageprot exit_critical_section { :function exitCS :precond inside_cs(ToolId) :body { use(ToolId,release); -inside_cs(ToolId) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.