ClassCastException: JObject cannot be cast to java.util.Calendar

20 views
Skip to first unread message

Javed Ahmed

unread,
Mar 29, 2016, 11:55:11 PM3/29/16
to Brahms Forum

 Hi,

   Actually I am trying to save activity's start time, and I am using below brahms activity to get it.

 
           java getCurrentTime(Calendar out) {
              max_duration: 1;
              class: "brahms.base.util.GetCurrentTimeActivity";
              when: start;
           }

 then i want to pass this start time to my own below Java activity which will save time in to DB.

        java logStartTime(Calendar startTime) {
           max_duration: 1;
           class: "com.brahms.util.BrahmsDBUtil";
           when: start;
        }

  below is the brahms code of calling these both activities in model file.

 
         Calendar startTime;
         getCurrentTime(startTime);
         logStartTime(startTime);

    but I get an exception while casting it to java.util.Calendar, below is the sample code

    Calendar startTime=(Calendar)getParameterValue("startTime");


   any idea how to get the value from Brahms Calendar?


   any help regarding this will be highly appreciated.


 Thanks,
  -Javed

   


Maarten Sierhuis

unread,
Mar 30, 2016, 12:37:07 AM3/30/16
to Brahms Forum
Javed,

What you are forgetting is that the getCurrentTime activity does is that it creates an object of type Calendar and it puts the values of the calendar as beliefs in the newly created object. It is as if the agent creates a new Calendar object with values for the calendar time at that moment in the simulation. The agent then still has to "read the calendar" so that it gets the beliefs. Here is the code for an agent that does this:

agent AgentReadCurrentTime memberof CalendarUtilGroup {

activities:
communicate getTimeBeliefs(Calendar time) {
max_duration: 1;
with: time;
about: receive(time.hour = ?),
receive(time.minute = ?),
receive(time.second = ?);
}

workframes:
workframe workframe_1 {
do {
Calendar startTime; 
getCurrentTime(startTime);
getTimeBeliefs(startTime);
}
}
}

Here is the code for the getCurrentTime activity in the CalendarUtilGroup library:

group CalendarUtilGroup {
activities:
/**
* Creates or populates a Calendar object with its beliefs set to the
* current date and time based on the currently set or default
* time zone. In simulated mode the time will be initialized to
* the simulated date/time. In (distributed) real-time mode the
* actual system time is used.
*
* @return out a variable of type Calendar to which to either assign
* a newly created Calendar object if the variable is unbound
* or from which to retrieve the bound Calendar object in which 
* to store the beliefs about the current time
*/
java getCurrentTime(Calendar out) {
class: "brahms.base.util.GetCurrentTimeActivity";
} // getCurrentTime

I attach the AgentViewer screen shots to show that the agent get the beliefs.

After the agent has the beliefs you can now create a workframe that logs the beliefs in a file or whatever.


Maarten.
BrahmsAgentViewer 3.tiff
BrahmsAgentViewer 4.tiff

Maarten Sierhuis

unread,
Mar 30, 2016, 1:11:52 AM3/30/16
to Brahms Forum
Javed,

Let me add that you can use Java code within a workframe. This means you can easily write a Java class that prints out beliefs. To learn about how to use Java within workframes, and use Java objects within Brahms, please read the Brahms language document that is located in the Brahms installation folder in ../docs/language/LanguageSpecificationV3.0F.pdf.

Section 2.42 JAVA INTEGRATION, Page 112 gives an overview.

Maarten.

Javed Ahmed

unread,
Mar 30, 2016, 1:53:59 AM3/30/16
to Brahms Forum


  Thanks for reply Maarten, but for me , my same old code worked, what I did is to convert Calendar into java GregorianCalender object and then i got the values. I am not sure if
   this is right approach but it serves the purpose :).

           GregorianCalendar gregorianCalendar= BrahmsCalendar.getJavaCalendar((IObject)getParameterValue("startTime"), getContext());


   Thanks,
  - Javed
Reply all
Reply to author
Forward
0 new messages