2004-07-24 16:05:20 +01:00
|
|
|
/**
|
|
|
|
* @file roles.c
|
|
|
|
* \brief role related logic.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "term.h"
|
|
|
|
#include "termlist.h"
|
2004-07-24 16:05:20 +01:00
|
|
|
#include "knowledge.h"
|
2004-07-24 16:08:35 +01:00
|
|
|
#include "system.h"
|
2004-07-24 16:05:20 +01:00
|
|
|
#include "memory.h"
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "constraint.h"
|
2004-07-24 16:05:20 +01:00
|
|
|
#include "debug.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "tracebuf.h"
|
2004-07-24 20:07:29 +01:00
|
|
|
#include "role.h"
|
2004-07-24 16:05:20 +01:00
|
|
|
|
2004-07-24 16:08:35 +01:00
|
|
|
extern int globalLatex; // from system.c
|
2004-07-24 16:05:20 +01:00
|
|
|
|
|
|
|
//! Allocate memory the size of a roledef struct.
|
|
|
|
Roledef
|
|
|
|
makeRoledef ()
|
|
|
|
{
|
|
|
|
return (Roledef) memAlloc (sizeof (struct roledef));
|
|
|
|
}
|
|
|
|
|
2004-07-29 15:47:46 +01:00
|
|
|
//! Print a role event.
|
2004-07-24 16:05:20 +01:00
|
|
|
void
|
|
|
|
roledefPrint (Roledef rd)
|
|
|
|
{
|
|
|
|
if (rd == NULL)
|
|
|
|
{
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("[Empty roledef]");
|
2004-07-24 16:05:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rd->type == READ && rd->internal)
|
|
|
|
{
|
|
|
|
/* special case: internal read == choose ! */
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("CHOOSE(");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->message);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf (")");
|
2004-07-24 16:05:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rd->type == READ)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("READ");
|
2004-07-24 16:05:20 +01:00
|
|
|
if (rd->type == SEND)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("SEND");
|
2004-07-24 16:05:20 +01:00
|
|
|
if (rd->type == CLAIM)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("CLAIM");
|
2004-07-24 16:05:20 +01:00
|
|
|
if (rd->label != NULL)
|
|
|
|
{
|
|
|
|
if (globalLatex)
|
|
|
|
{
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("$_{");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->label);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("}$");
|
2004-07-24 16:05:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("_");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (globalLatex)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("$");
|
|
|
|
eprintf ("(");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->from);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf (",");
|
2004-07-24 16:05:20 +01:00
|
|
|
if (rd->type == CLAIM)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf (" ");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->to);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf (", ");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (rd->message);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf (" )");
|
2004-07-24 16:05:20 +01:00
|
|
|
if (globalLatex)
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("$");
|
2004-07-24 16:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Duplicate a single role event node.
|
|
|
|
/**
|
|
|
|
*\sa roledefDelete()
|
|
|
|
*/
|
|
|
|
Roledef
|
|
|
|
roledefDuplicate1 (const Roledef rd)
|
|
|
|
{
|
|
|
|
Roledef newrd;
|
|
|
|
|
|
|
|
if (rd == NULL)
|
|
|
|
return NULL;
|
|
|
|
newrd = makeRoledef ();
|
|
|
|
memcpy (newrd, rd, sizeof (struct roledef));
|
|
|
|
newrd->next = NULL;
|
|
|
|
return newrd;
|
|
|
|
}
|
2004-08-09 11:05:58 +01:00
|
|
|
|
2004-07-24 16:05:20 +01:00
|
|
|
//! Duplicate a role event list.
|
|
|
|
/**
|
|
|
|
*\sa roledefDelete()
|
|
|
|
*/
|
|
|
|
Roledef
|
|
|
|
roledefDuplicate (Roledef rd)
|
|
|
|
{
|
|
|
|
Roledef newrd;
|
|
|
|
|
|
|
|
if (rd == NULL)
|
|
|
|
return NULL;
|
|
|
|
newrd = roledefDuplicate1 (rd);
|
|
|
|
newrd->next = roledefDuplicate (rd->next);
|
|
|
|
return newrd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Delete a role event or event list.
|
|
|
|
/**
|
|
|
|
*\sa roledefDuplicate()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
roledefDelete (Roledef rd)
|
|
|
|
{
|
|
|
|
if (rd == NULL)
|
|
|
|
return;
|
|
|
|
roledefDelete (rd->next);
|
|
|
|
memFree (rd, sizeof (struct roledef));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Destroy a role event or event list.
|
|
|
|
void
|
|
|
|
roledefDestroy (Roledef rd)
|
|
|
|
{
|
|
|
|
if (rd == NULL)
|
|
|
|
return;
|
|
|
|
roledefDestroy (rd->next);
|
|
|
|
termDelete (rd->from);
|
|
|
|
termDelete (rd->to);
|
|
|
|
termDelete (rd->message);
|
|
|
|
memFree (rd, sizeof (struct roledef));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Make a new role event with the specified parameters.
|
|
|
|
/**
|
|
|
|
*@return A pointer to a new role event with the given parameters.
|
|
|
|
*/
|
|
|
|
Roledef
|
|
|
|
roledefInit (int type, Term label, Term from, Term to, Term msg, Claimlist cl)
|
|
|
|
{
|
|
|
|
Roledef newEvent;
|
|
|
|
|
|
|
|
newEvent = makeRoledef ();
|
|
|
|
newEvent->internal = 0;
|
|
|
|
newEvent->type = type;
|
|
|
|
newEvent->label = label;
|
|
|
|
newEvent->from = from;
|
|
|
|
newEvent->to = to;
|
|
|
|
newEvent->message = msg;
|
|
|
|
newEvent->forbidden = NULL; // no forbidden stuff
|
|
|
|
newEvent->knowPhase = -1; // we haven't explored any knowledge yet
|
|
|
|
newEvent->claiminfo = cl; // only for claims
|
2004-08-10 16:17:00 +01:00
|
|
|
newEvent->bind_run = -1; // unbound goal
|
|
|
|
newEvent->bind_index = -1; // unbound goal
|
2004-07-24 16:05:20 +01:00
|
|
|
newEvent->next = NULL;
|
|
|
|
return newEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Add a role event to an existing list, with the given parameters.
|
|
|
|
/**
|
|
|
|
*\sa roledefInit()
|
|
|
|
*/
|
|
|
|
Roledef
|
2004-08-09 11:05:58 +01:00
|
|
|
roledefAdd (Roledef rd, int type, Term label, Term from, Term to, Term msg,
|
|
|
|
Claimlist cl)
|
2004-07-24 16:05:20 +01:00
|
|
|
{
|
|
|
|
Roledef scan;
|
|
|
|
|
|
|
|
if (rd == NULL)
|
|
|
|
return roledefInit (type, label, from, to, msg, cl);
|
|
|
|
|
|
|
|
scan = rd;
|
|
|
|
while (scan->next != NULL)
|
|
|
|
scan = scan->next;
|
|
|
|
scan->next = roledefInit (type, label, from, to, msg, cl);
|
|
|
|
return rd;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Create an empty role structure with a name.
|
|
|
|
Role
|
|
|
|
roleCreate (Term name)
|
|
|
|
{
|
|
|
|
Role r;
|
|
|
|
|
|
|
|
r = memAlloc (sizeof (struct role));
|
|
|
|
r->nameterm = name;
|
2004-08-12 14:22:49 +01:00
|
|
|
r->roledef = NULL;
|
2004-07-24 16:05:20 +01:00
|
|
|
r->locals = NULL;
|
2004-08-12 10:14:31 +01:00
|
|
|
r->variables = NULL;
|
2004-08-12 14:22:49 +01:00
|
|
|
r->next = NULL;
|
2004-07-24 16:05:20 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Print a role.
|
|
|
|
void
|
|
|
|
rolePrint (Role r)
|
|
|
|
{
|
|
|
|
Roledef rd;
|
|
|
|
|
|
|
|
if (r == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
indent ();
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("[[Role : ");
|
2004-07-24 16:05:20 +01:00
|
|
|
termPrint (r->nameterm);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("]]\n");
|
2004-07-24 16:05:20 +01:00
|
|
|
locVarPrint (r->locals);
|
|
|
|
|
|
|
|
rd = r->roledef;
|
|
|
|
while (rd != NULL)
|
|
|
|
{
|
|
|
|
roledefPrint (rd);
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("\n");
|
2004-07-24 16:05:20 +01:00
|
|
|
rd = rd->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Print a list of roles.
|
|
|
|
void
|
|
|
|
rolesPrint (Role r)
|
|
|
|
{
|
|
|
|
if (r == NULL)
|
|
|
|
{
|
2004-07-29 15:47:46 +01:00
|
|
|
eprintf ("Empty role.");
|
2004-07-24 16:05:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (r != NULL)
|
|
|
|
{
|
|
|
|
rolePrint (r);
|
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-12 10:14:31 +01:00
|
|
|
|
|
|
|
//! Iterate over the events in a roledef list
|
|
|
|
/**
|
|
|
|
* Function gets roledef pointer
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
roledef_iterate_events (Roledef rd, int (*func) ())
|
|
|
|
{
|
|
|
|
while (rd != NULL)
|
|
|
|
{
|
|
|
|
if (!func (rd))
|
|
|
|
return 0;
|
|
|
|
rd = rd->next;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2004-08-12 12:22:49 +01:00
|
|
|
|
|
|
|
//! Roledef length
|
|
|
|
/**
|
|
|
|
* Would be faster hard-coded,
|
|
|
|
* but this just shows the use of the iteration.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
roledef_length (const Roledef rd)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int countplus (Roledef rd)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
roledef_iterate_events (rd, countplus);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Yield roledef pointer for a given index
|
|
|
|
Roledef
|
|
|
|
roledef_shift (Roledef rd, int i)
|
|
|
|
{
|
|
|
|
while (i > 0 && rd != NULL)
|
|
|
|
{
|
|
|
|
rd = rd->next;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
return rd;
|
|
|
|
}
|