Thanks very much for that help! It seems very simple now, but I couldn't
figure the problem was coming from their, so I kept altering and testing the
casting statment as opposed to checking the problem. I didn't see that. The
sample program uses the Set.add() to add things to orderItems to orders, but it
does it on a different statement and doesnt use the returned boolean. It sure
makes a lot of sense now!! I actually have been constructing this piece by
piece, I thought I was almost done. I already had completed all the simpler
classes by themselves and tested them and they were working. The last demo
database class Iis the one I have been really stuck on. Even still after
fixing that, after I run the program Im not getting the value of the file
permission though, only the location of it. I can't understand why this is
happening. Ill just repost the code. At the end of the code I posted is the
results of running the program. Any help about how to fix this, or ev
en about things I should be doing to better trouble shoot the problem wouldbe
very much appreciated.
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:
Mike Artz
Roles for Mike:
filepermiss...@186db54
filepermiss...@a97b0b
filepermiss...@cd2c3c
First and Last Name of User 2:
Suzy Kolber
Roles for Suzy:
filepermiss...@506411
filepermiss...@1d99a4d
filepermiss...@56a499
First and Last Name of User 3:
Stan Mikita
Roles for Stan:
filepermiss...@1e859c0
filepermiss...@c9ba38
filepermiss...@1e0be38
First and Last Name of User 4:
Nikita Kruschev
Roles for Nikita:
filepermiss...@1430b5c
filepermiss...@1aaa14a
filepermiss...@7a84e4
> From: [email protected]
> To: [email protected]
> Subject: Re: JESS: Jess Access Control
> Date: Fri, 7 Jan 2011 20:30:00 -0500
>
>
> On Jan 7, 2011, at 8:06 PM, Michael Eugene Artz wrote:
> > It's a casting error but it doesn't make any sense to me why I
> > would be getting that.
>
> userProfiles is a java.util.Set. The method Set.add() returns boolean
> (true means the add() succeeded, false means the object was already in
> the set.) So here you're mapping Integer to Boolean.
>
>
> > 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")));
> >
>
> Here you're assuming that the values in profiles are Profile objects,
> but as we said, they're Booleans.
>
>
> > public Profile getProfile(int profileNumber)
> > {
> > //return new Profile(1, "Mike", "Artz");
> > return (Profile)profiles.get(new Integer(profileNumber));
> > }
> >
>
> Which is what the error says: the values are Booleans, not Profiles.
> The trace tells you exactly what line causes the errors, so a
> println() or two would have easily told you the whole story.
>
>
> > 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:(
> >
>
>
> When I assemble a large program like this, I start with a small
> program that does one little thing, and I test it. I then add a little
> more, and test that, and so on. At each step, I make sure all the new
> code does what it's supposed to do. I keep all the test code so it can
> be run at any time to verify that the program still works. That way,
> you never find yourself looking at a big pile of code and wondering
> where the problem is: the problem is always just in that last little
> bit you just coded.
>
> Good luck!
>
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Informatics & Decision Sciences Phone: (925) 294-2154
> Sandia National Labs
> PO Box 969, MS 9012 [email protected]
> Livermore, CA 94550 http://www.jessrules.com
>
>
>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [email protected]'
> in the BODY of a message to [email protected], NOT to the list
> (use your own address!) List problems? Notify [email protected].
> --------------------------------------------------------------------
>