Skip to content
Angelo Croatti edited this page May 21, 2020 · 15 revisions

JaCa-Android is a framework based on the JaCaMo platform that allows for designing and programming Android mobile apps using cognitive agents based on the BDI architecture and the Agents & Artifacts (A&A) environment conceptual model.

In the simplest case, a mobile app in JaCa-Android is programmed as a single standalone Jason BDI agent equipped with all the tools (artifacts) necessary for functioning as a user assistant, including artifacts for the UI, for modeling and accessing the user context, the device context, as well as to exploit the mobile environment facilities and services (based on Android in this case).

Hello World - The How/To guide

After having included the JaCa-Android library into your project (see README.md), consider following steps in order to use JaCa-Android.

NOTE: In the following description we assume you have already created a standard android project with an application module called app

[1] MAS SETUP

Add a res folder called resources (ensure that such folder is created at the same level of the standard android res folder. Create into this folder a sub folder with the same name you want to give to your MAS (we assume helloworld). This folder will be the parent of your MAS configuration (*.mas2j) and agents' code (*.asl)

Create into the folder the MAS2J, e.g. as follows (into resources/helloworld/project.mas2j)

MAS helloworld{
  infrastructure: Centralised
  
  environment: jaca.CartagoEnvironment("infrastructure")

  agents:
    main mainAgent agentArchClass jaca.CAgentArch;

    aslSourcePath: "helloworld/agents";
}

Note that the MAS name must be the same of the created folder within the resources one - helloworld in this case. Moreover the path must be specified for aslSourcePath is the path within the MAS dedicated folder where agents' code will be found - in this case resources/helloworld/agents/

[2] AGENTS' CODE

Add the agent (or agents) code (e.g., for the specified mainAgent agent into resources/helloworld/agents/mainAgent.asl)

!init.

+!init
  <-  makeArtifact("mainUI", "it.unibo.pslab.jaca_android.helloexample.MainUI",[],MainUI);
      focus(MainUI).

+ui_ready [artifact_name(Id,MainUI)]
  <- println("MainUI ready.").

[3] UI ARTIFACTS' CODE

Android activities can be managed as artifacts. In particular, the previous agent creates the MainUI artifact into the android app default package (its code in the MainUI.java file)

package it.unibo.pslab.jaca_android.helloexample;

import cartago.INTERNAL_OPERATION;
import it.unibo.pslab.jaca_android.core.ActivityArtifact;
import it.unibo.pslab.jaca_android.core.JaCaBaseActivity;
import android.widget.TextView;

public class MainUI extends ActivityArtifact {
    
    /* 
     * You must include a static reference to an Android activity extending JaCaBaseActivity class
     * in each Activity Artifact (declare also this activity into the android manifest)
     */
    public static class MainActivity extends JaCaBaseActivity {}

    /*
     * Inits the artifact, with the reference to the static activty and the reference to the layout file
     */
    public void init() {
        super.init(MainActivity.class, R.layout.activity_main, true);
    }

    /*
     * Allows for all operations generally added to the onCreate() callback of an activity AFTER the UI inflating
     */
    @INTERNAL_OPERATION
    protected void setup() {
        initUI();
    }

    private void initUI(){
        execute(() -> {
            TextView label = (TextView) findUIElement(R.id.textview);
            label.setText("Hello, JaCa-Android!");
        });
    }
}

In above example, note (1) the decalration of the static instance of the related activity, (2) the usage of method init() and setup(), (3) the usage of the method execute() allowing to demand a portion of code to the main thread (e.g. to update the UI).

Clone this wiki locally