JESS: Jess Access Control
"Michael Eugene Artz" <[email protected]>
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <32528_1294449606_p081JVOQ003678_BLU162-w5639AE5FC1F5B9F7420AF8CD0C0@phx.gbl> |
Hi, Im trying to do a simple Access control program using Jess. Everything pretty much shadows the Pricing example that comes with Jess. I changed only a few variable names and modified a couple of the parts that weren't need, because mine should be actually simpler. All I want to do is have a handfule of rules that check for a user info with respect to a certain file and decides whether they have read-write priveledges or just read priveledges. And also to assign read-write priveledges to users if they are owners of the respected file that they want to access. I am sorry if its just some simple error, but I have been stumped on this for some time, so I don't know what to do. Right now my Java classes are in seperate files in the same package, but Ill just cut and paste them all her
e so it will be easier to look at them. The file after all the JAva files is my .clp JEss file, then after that is the error I'm getting. It's a casting error but it doesn't make any sense
to me why I would be getting that. Anyway , they look like:
import java.util.ArrayList;
import java.util.Collection;
public interface Database
{
public Collection getFiles();
public Profile getProfile(int profileNumber);
}
import java.util.Iterator;
import jess.JessException;
public class Demo {
public static void main(String[] args) {
try {
DemoDatabase database = new DemoDatabase();
UserAccessEngine engine = new UserAccessEngine(database);
processRole(database, engine, 1);
processRole(database, engine, 2);
processRole(database, engine, 3);
processRole(database, engine, 4);
}
catch (JessException e)
{
e.printStackTrace();
}
}
private static void processRole(DemoDatabase database, UserAccessEngine engine, int aID) throws JessException
{
//Iterator files, prints out a message prior to getting the needed info
Iterator roles;
System.out.println("First and Last Name of User " + aID + ":");
String firstName = database.getProfile(aID).getfirst();
String lastName = database.getProfile(aID).getlast();
System.out.println(firstName + " " + lastName);
roles = engine.run(aID);
System.out.println("Roles for " + firstName + ":");
while (roles.hasNext())
{
System.out.println(" " + roles.next());
}
System.out.println();
}
}
import java.util.ArrayList;
import java.util.Map;
import java.util.Collection;
import java.util.HashMap;
/**
* A toy implementation of the Database interface with some
* hard-coded file and profile data.
*/
public class DemoDatabase implements Database {
private ArrayList files;
private Profile profile;
private Map profiles;
public DemoDatabase()
{
createFiles();
createProfiles();
}
private void createProfiles()
{
profiles = new HashMap();
ArrayList userProfiles = new ArrayList();
profiles.put(new Integer(1), userProfiles.add(new Profile(1, "Mike", "Artz")));
profiles.put(new Integer(2), userProfiles.add(new Profile(2, "Suzy", "Kolber")));
profiles.put(new Integer(3), userProfiles.add(new Profile(3, "Stan", "Mikita")));
profiles.put(new Integer(4), userProfiles.add(new Profile(4, "Nikita", "Kruschev")));
}
private void createFiles() {
files = new ArrayList();
files.add(new FileRead("Mike", "Artz", "file.txt", 1));
files.add(new FileRead("Tom", "Saywer", "picOfYourMom.jpg", 5));
files.add(new FileRead("Chris", "Webber", "NBAJAMS.exe", 6));
}
public Collection getFiles() {
return files;
}
public Profile getProfile(int profileNumber)
{
//return new Profile(1, "Mike", "Artz");
return (Profile)profiles.get(new Integer(profileNumber));
}
}
public class FilePermission
{
private String fileName;
private int profileNum;
private String last;
private String first;
private String role;
public FilePermission(String aFileName, int aProfileNum, String aLast, String aFirst, String aRole)
{
last = aLast;
first = aFirst;
fileName = aFileName;
profileNum = aProfileNum;
role = aRole;
}
public String getfileName()
{
return fileName;
}
public int getprofileNum()
{
return profileNum;
}
public String getlast()
{
return last;
}
public String getfirst()
{
return first;
}
public String getrole()
{
return role;
}
}
public class FileRead
{
private String ownerFirst;
private String ownerLast;
private String file;
private int ownerID;
public FileRead(String aFirst, String aLast, String aFile, int aOwnerID)
{
ownerFirst = aLast;
ownerLast = aFirst;
file = aFile;
ownerID = aOwnerID;
}
public String getownerFirst()
{
return ownerFirst;
}
public String getownerLast()
{
return ownerLast;
}
public String getFile()
{
return file;
}
public int getownerID()
{
return ownerID;
}
}
public class Profile
{
private String last;
private String first;
private int idNum;
public Profile(int aID, String aFirst, String aLast)
{
idNum = aID;
last = aLast;
first = aFirst;
}
public String getlast()
{
return last;
}
public String getfirst()
{
return first;
}
public int getidNum()
{
return idNum;
}
}
import jess.*;
import java.util.Iterator;
public class UserAccessEngine
{
private Rete engine;
private WorkingMemoryMarker marker;
private Database database;
public UserAccessEngine(Database aDatabase) throws JessException
{
// Create a Jess rule engine
engine = new Rete();
engine.reset();
// Load the user access rules
engine.batch("C:\\Users\\Michael\\workspace\\JessUserAccess\\lib\\users.clp");
// Load the user access data into working memory
database = aDatabase;
engine.addAll(database.getFiles());
// Mark end of user access data for later
marker = engine.mark();
}
private void loadOrderData(int profileNumber) throws JessException
{
// Retrive the profile from the database
Profile profile = database.getProfile(profileNumber);
if (profile != null)
{
// Add the profile to working memory
engine.add(profile);
}
}
public Iterator run(int profileNumber) throws JessException
{
// Remove any previous user access data, leaving only catalog data
engine.resetToMark(marker);
// Load data for this profile
loadOrderData(profileNumber);
// Fire the rules that apply to this profile
engine.run();
// Return the list of roles created by the rules
return engine.getObjects(new Filter.ByClass(FilePermission.class));
}
}
THIS IS MY JESS FILE
;; First define templates for the model classes so we can use them
;; in our user access rules. This doesn't create any model objects --
;; it just tells Jess to examine the classes and set up templates
;; using their properties
(import C:.Users.Michael.workspace.JessUserAccess.src*)
;; (deftemplate FilePermission (declare (from-class
;; FilePermission)))
(deftemplate FileRead (declare (from-class FileRead)))
(deftemplate Profile (declare (from-class Profile)))
;; Now define the user access rules themselves. Each rule matches a set
;; of conditions and then creates a Role object to represent a
;; role that user has on a file. The rules assume that
;; there will be just one User, along with all the Files.
(defglobal ?*read-write* = read-write)
(defglobal ?*read* = read)
(defrule owner-role
"Give a user the read-write role if he or she is the owner of the file."
(FileRead (ownerFirst ?ownerFirst)(ownerLast ?ownerLast)(file ?file)(ownerID ID?))
(Profile(first ?ownerFirst)(last ?ownerLast) (idNum ?ID))
=>
(add (new FilePermission ?file ?ID ?ownerLast ?ownerFirst
?*read-write*)))
(defrule read-role
"Give a user the read role if he or she is not the owner of the file."
(FileRead (ownerFirst ?ownerFirst)(ownerLast ?ownerLast)(file ?file) (ownerID ?ID))
(Profile(first ?First)(last ?Last) (idNum ?idNum))
=>
(add (new FilePermission ?file ?idNum ?Last ?First ?*read*)))
First and Last Name of User 1:Exception in thread "main"
java.lang.ClassCastException: java.lang.Boolean cannot be cast to Profile
at DemoDatabase.getProfile(DemoDatabase.java:49)
at Demo.processRole(Demo.java:31)
at Demo.main(Demo.java:13)
I wonder if I could this all much simpler and Im just wasting my time completely:(