|
From: jiang <jia...@us...> - 2006-02-13 12:37:24
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/agent In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2933/src/org/cobricks/agent Added Files: Attribute.java AgentManagerImpl.java properties.txt ActionRating.java ActionSendMail.java Agent.java AgentManager.java Condition.java ActionSendMsg.java Rule.java DRLCreator.java AgentServlet.java ActionAnnotation.java Action.java ActionSetAttr.java AgentPresenter.java Log Message: Agent Component --- NEW FILE: AgentPresenter.java --- /* * Copyright (c) 2004-2005 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.io.File; import java.io.InputStream; import java.sql.Timestamp; import java.text.DateFormat; [...1533 lines suppressed...] return sh.toString(); } public final String addNewAction() { StringBuffer add=new StringBuffer("<form action='succ.html' method=post >"); add.append("<br><br><br><h1>To finish this rule:</h1>"); add.append("<input type=submit name=complete value='Click here to complete this rule'> "); add.append("<input type=hidden name=ruleflag value=true></form>"); add.append("<form action=completeRule.html method=post>"); add.append("<br><br><h1>To add more actions:</h1><br>"); add.append(" <h2>result:</h2><select name=result><option>Send Email <option>Send Message <option>Set Attribute <option selected>Set Annotation <option>Set Rating </select><p>"); add.append(" <table><tr><td><input type=submit value='Add Action'/></td>"); add.append(" <input type=hidden name=ruleflag value=false></form>"); add.append(" <input type=hidden name=flag value=true></tr></table></form>"); return add.toString(); } } --- NEW FILE: ActionSetAttr.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class ActionSetAttr extends Action { private String objectName; private int objectID; private String aName; private String aValue; public ActionSetAttr(){ type = Action.SETATTR; } public ActionSetAttr(String oName, int oID, String name, String value){ this(); objectName = oName; objectID = oID; aName = name; aValue = value; } // attrFromDB: UPDATE <objectName> <objectID> SET <aName>=<aValue> public ActionSetAttr(int actionID, String attrFromDB){ this.actionID = actionID; String[] strs = attrFromDB.split(" |="); objectName = strs[1]; objectID = Integer.parseInt(strs[2].trim()); aName = strs[4]; aValue = strs[5]; //TODO } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { String result = "UPDATE "+ objectName +" "+objectID+" SET " + aName+ "="+aValue; return result; } /* (non-Javadoc) * @see org.cobricks.agent.Action#toHtmlString() */ public String toHtmlString() { // TODO Auto-generated method stub return null; } public String getAName() { return aName; } public void setAName(String name) { aName = name; } public String getAValue() { return aValue; } public void setAValue(String value) { aValue = value; } public int getObjectID() { return objectID; } public void setObjectID(int objectID) { this.objectID = objectID; } public String getObjectName() { return objectName; } public void setObjectName(String objectName) { this.objectName = objectName; } } --- NEW FILE: ActionSendMsg.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class ActionSendMsg extends Action { private int toUserID; // Das Attribut toSelf ist ¨¹berfl¨¹?ig. //private boolean toSelf; private int priority; private String subject; public ActionSendMsg(){ type = Action.SENDMSG; priority = -1; } public ActionSendMsg(int userID, int pri, String subj, String cont){ this(); toUserID = userID; priority = pri; content = cont; subject=subj; } /** * format of msgFromDB: Send Message:<toUserID>:<priority>:<subject> * * @param actionID : id of the action in the database * @param msgFromDB: action descreption * @param content: message content */ public ActionSendMsg(int actionID, String msgFromDB, String content){ this.actionID = actionID; String[] strs = msgFromDB.split(":"); toUserID =Integer.parseInt( strs[1]); priority = Integer.parseInt(strs[2]); subject=strs[3]; this.content = content; } /** * format: Send Message:<toUserID>:<priority>:<subject> */ public String toString() { StringBuffer result = new StringBuffer("Send Message:"); result.append(toUserID+":"); result.append(priority+":"); if (subject == null || subject.equals("")) result.append("No subject"); else result.append(subject); return result.toString(); } /* (non-Javadoc) * @see org.cobricks.agent.Action#toHtmlString() */ public String toHtmlString() { // TODO Auto-generated method stub return null; } /** * @return Returns the priority. */ public int getPriority() { return priority; } /** * @param priority The priority to set. */ public void setPriority(int priority) { this.priority = priority; } /** * @return Returns the toUserID. */ public int getToUserID() { return toUserID; } /** * @param toUserID The toUserID to set. */ public void setToUserID(int toUserID) { this.toUserID = toUserID; } /** * @return Returns the subject. */ public String getSubject(){ return subject; } /** * @param subject The subject to set. */ public void setSubject(String subject){ this.subject=subject; } } --- NEW FILE: ActionRating.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.util.ArrayList; import java.util.Iterator; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class ActionRating extends Action { private int annoID; private String rating; public ActionRating(){ this.type = Action.SETRAT; } /* public ActionSendMail(String et, String subj, String cont){ this(); emailTo = et; subject = subj; content = cont; } */ // Format of mailFromDB: Set Rating:<annoID>:<rating> public ActionRating(int actionID, String ratFromDB){ this.actionID = actionID; String[] cont = ratFromDB.split(":",3); annoID = Integer.parseInt(cont[1]); rating = cont[2]; } /* * @see java.lang.Object#toString() */ public String toString() { StringBuffer result = new StringBuffer("Set Rating:"); result.append(annoID+":"); result.append(rating); return result.toString(); } /** * the output html format for Velocity template * * @see org.cobricks.agent.Action#toHtmlString() * @return the output html format for Velocity template (Ye Shen) * */ public String toHtmlString() { // TODO Auto-generated method stub return null; } /** * @return Returns the subject. */ public int getAnnoID() { return annoID; } /** * @param subject The subject to set. */ public void setAnnoID(int annoID) { this.annoID = annoID; } public String getRating() { return rating; } public void setRating(String rating) { this.rating = rating; } } --- NEW FILE: AgentManager.java --- /* * Copyright (c) 2005 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.util.List; import org.cobricks.core.ComponentManagerInterface; /** * Interface of the manager class of the Message Component. * * @author mic...@ac... * @version $Date: 2006/02/13 12:37:05 $ */ public interface AgentManager extends ComponentManagerInterface { public void obtainRules(); public void insertRuleForUser(int userID, Rule rule); public void delRule(int userID, Rule rule); public void updateRule(int userID, Rule rule); public List getRulesForUser(int userID); } --- NEW FILE: Condition.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.util.HashMap; import java.util.Iterator; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class Condition { private int conditionID; private String domain; private String action; private int userID; private String srcComponent; HashMap attrMap = new HashMap(); public Condition(){ attrMap = new HashMap(); } /** Get a condition from database * //Condition format: * <domain>:<action>:<userID>:<srcComponent>:{<aname>=|<|>|~<avaule>;}* * The character ~ stands for like * @param conditionID : the column rule_conditionID from table user_rulesConditions * @param conFromDB : the column rule_condition from table user_rulesConditions */ public Condition(int conditionID,String conFromDB){ this(conFromDB); this.conditionID = conditionID; } // Get a condition from database public Condition(String conFromDB){ String[] content = conFromDB.split(":"); domain = content[0]; System.out.println(domain); action = content[1]; System.out.println(action); userID = Integer.parseInt(content[2]); System.out.println(userID); srcComponent = content[3]; System.out.println(srcComponent); attrMap = new HashMap(); if (content.length==4) return; System.out.println(content[4]); String[] attrPaars = content[4].split(";"); System.out.println(attrPaars.length); System.out.println(attrPaars[0]); System.out.println(attrPaars[1]); for (int attIndex = 0; attIndex <attrPaars.length; attIndex++){ int type; if (attrPaars[attIndex].indexOf("=")> -1) type = Attribute.EQUAL; else if (attrPaars[attIndex].indexOf(">")> -1) type = Attribute.GREATER; else if (attrPaars[attIndex].indexOf("<")>-1) type = Attribute.SMALLER; else if (attrPaars[attIndex].indexOf("~")> -1) type = Attribute.LIKE; else continue; System.out.println(type); String[] attrPaar = attrPaars[attIndex].split("=|>|<|~"); System.out.println("attrpaar lentt"+attrPaar.length+attrPaar[0]); Attribute attr = new Attribute(attrPaar[0], type, attrPaar[1]); System.out.println(attr); attrMap.put(attrPaar[0], attr); } } // Get a condition from the Browser. Attention: we obtain the conditionID from the database. The value is // equivalent to the maximal ID in the table condition. public Condition(String d, String o, int userid, String s){ domain=d; srcComponent=s; userID= userid; action=o; } /** * <domain>:<action>:<userID>:<srcComponent>:{<aname>=<avaule>;}* * @return Returns the output for database. Save a condition into the database */ public String toString(){ StringBuffer result = new StringBuffer(); result.append(domain+":"+action+":"+userID+":"+srcComponent); if (attrMap ==null || attrMap.isEmpty()) return result.toString(); result.append(":"); for(Iterator keys= attrMap.keySet().iterator(); keys.hasNext();){ Attribute attr= (Attribute)attrMap.get(keys.next()); result.append(attr.toString()); result.append(";"); } result.deleteCharAt(result.length()-1); return result.toString(); } /** * add the attribut for condition * * @param name name of the attribut * @param value value of the attribut */ public void addAttr(String name, int type, String value){ if (type < 0 || type >3) return; Attribute attr = new Attribute (name, type, value); attrMap.put(name, attr); } /** * delete an attribut from the condition * * @param name name of the attribut */ public void delAttr(String name){ attrMap.remove(name); } /** * change the value of an attribut * * @param name name of the attribut * @param newvalue new value of the attribut */ public void changeAttr(String name, int type, String newValue){ delAttr(name); addAttr(name,type, newValue); } /** * the output html format for Velocity template * * @return the output html format for Velocity template (Ye Shen) */ public String toHtmlString(){ // TODO Auto-generated method stub StringBuffer show=new StringBuffer("<tr>"); show.append("<td>"+domain+"</td>"); show.append("<td>"+action+"</td>"); if(userID==0) show.append("<td><color=red>operator is not defined!</color></td>"); else show.append("<td>"+userID+"</td>"); show.append("<td>"+srcComponent+"</td>"); show.append("<td><table border>"); for (Iterator keys = attrMap.keySet().iterator();keys.hasNext();){ String key = (String)keys.next(); Attribute attr= (Attribute)attrMap.get(key); show.append("<tr><td>"+key+"</td><td>"); if (attr.getType()== Attribute.EQUAL) show.append("="); else if (attr.getType()== Attribute.GREATER) show.append(">"); else if (attr.getType()== Attribute.SMALLER) show.append("<"); else show.append(" like "); show.append("</td><td>"); show.append(attr.getValue()); show.append("</td></tr>"); } show.append("</table></td></tr>"); return show.toString(); } public HashMap getAttrMap() { return attrMap; } public int getUserID() { return userID; } public void setUserID(int userID) { this.userID = userID; } /** * @return Returns the conditionID. */ public int getConditionID() { return conditionID; } /** * @param conditionID The conditionID to set. */ public void setConditionID(int conditionID) { this.conditionID = conditionID; } /** * @return Returns the domain. */ public String getDomain() { return domain; } /** * @param domain The domain to set. */ public void setDomain(String domain) { this.domain = domain; } /** * @return Returns the option. */ public String getOption() { return action; } /** * @param option The option to set. */ public void setOption(String option) { this.action = option; } /** * @return Returns the srcComponent. */ public String getSrcComponent() { return srcComponent; } /** * @param srcComponent The srcComponent to set. */ public void setSrcComponent(String srcComponent) { this.srcComponent = srcComponent; } } --- NEW FILE: DRLCreator.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.util.Map; import java.util.Iterator; import java.util.List; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import org.apache.log4j.Logger; import org.cobricks.core.CoreManager; import org.cobricks.user.User; import org.cobricks.user.UserManager; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class DRLCreator { static Logger logger = Logger.getLogger(DRLCreator.class); Map totalRules; CoreManager coreManager; String DRL_dir; public DRLCreator(Map totalRules, String dir,CoreManager coreManager) { this.totalRules = totalRules; this.coreManager = coreManager; DRL_dir=dir; int key=-1; createDRL(key); } public DRLCreator(Map totalRules, String dir, int userid, CoreManager coreManager){ this.totalRules=totalRules; this.coreManager=coreManager; DRL_dir=dir; createDRL(userid); } public void createDRL(int key){ BufferedOutputStream bos; byte[] b; try { if(key==-1) bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent.java.drl"))); else bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"))); String top="<?xml version=\"1.0\"?>\n" + "<rule-set name=\"Agent\"\n" + "xmlns=\"http://drools.org/rules\"\n" + "xmlns:java=\"http://drools.org/semantics/java\"\n" + "xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "xs:schemaLocation=\"http://drools.org/rules rules.xsd\n" + "http://drools.org/semantics/java java.xsd\">\n"; b=top.getBytes(); bos.write(b); bos.close(); createDRL_function(key); Iterator iterator = totalRules.entrySet().iterator(); if(key==-1){ while(iterator.hasNext()){ List rulesForUser = (List)iterator.next(); if (rulesForUser == null || rulesForUser.isEmpty()) continue; for (int i=0; i<rulesForUser.size();i++){ Rule rule = (Rule)rulesForUser.get(i); String ru="<rule name=\""+rule.getRuleID()+"\">\n" + "<parameter identifier=\"coreManager\">\n" + "<class>CoreManager</class>\n" + "</parameter>\n" + "<parameter identifier=\"creator\">\n" + "<class>User</class>\n" + "</parameter>\n" + "<parameter identifier=\"domain\">\n" + "<class>String</class>\n" + "</parameter>\n"+ "<parameter identifier=\"opt\">\n" + "<class>String</class>\n" + "</parameter>\n"+ "<parameter identifier=\"src\">\n" + "<class>String</class>\n" + "</parameter>\n"; b=ru.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); createDRL_rule(key, rule.getConditions(), rule.getActions()); ru="</rule>\n"; b=ru.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); } } String end="</rule-set>"; b=end.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); } else{ List rulesForUser = (List)totalRules.get(new Integer(key)); if (rulesForUser == null || rulesForUser.isEmpty()) logger.debug("ERROR:this user has no rules!"); else {for (int i=0; i<rulesForUser.size();i++) { Rule rule = (Rule)rulesForUser.get(i); String ru="<rule name=\""+rule.getRuleID()+"\">\n" + "<parameter identifier=\"coreManager\">\n" + "<class>CoreManager</class>\n" + "</parameter>\n" + "<parameter identifier=\"creator\">\n" + "<class>User</class>\n" + "</parameter>\n" + "<parameter identifier=\"domain\">\n" + "<class>String</class>\n" + "</parameter>\n"+ "<parameter identifier=\"opt\">\n" + "<class>String</class>\n" + "</parameter>\n"+ "<parameter identifier=\"src\">\n" + "<class>String</class>\n" + "</parameter>\n"; b=ru.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); createDRL_rule(key, rule.getConditions(), rule.getActions()); ru="</rule>\n"; b=ru.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); } } String end="</rule-set>"; b=end.getBytes(); bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); bos.write(b); bos.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void createDRL_function(int key){ BufferedOutputStream bos; byte[] b; try { if(key==-1) bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent.java.drl"),true)); else bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); String ac= "<import>org.cobricks.user.*</import>\n" + "<import>org.cobricks.category.*</import>\n"+ "<import>org.cobricks.item.*</import>\n"+ "<import>org.cobricks.context.*</import>\n"+ "<import>org.cobricks.core.util.MailUtil</import>\n"+ "<import>org.cobricks.message.MessageManager</import>\n" + "<import>org.cobricks.message.MessageManagerImpl</import>\n" + "<import>org.cobricks.core.ComponentDirectory</import>\n" + "<import>org.cobricks.message.Message</import>\n" + "<import>org.cobricks.core.CoreManager</import>\n" + "<import>java.lang.Object</import>\n" + "<import>java.lang.String</import>\n" + "<import>java.io.IOException</import>\n"+ "<import>java.util.Date</import>\n"+ "<import>java.util.Map</import>\n"+ "<import>java.util.HashMap</import>\n"+ "<import>java.util.List</import>\n"+ "<import>java.util.Iterator</import>\n"+ "<java:functions>\n"; ac=ac+"public void sendMsg(CoreManager coreManager,User creator, int recipientID, int priority, String subject, String message_content)\n" + "{\n" + "try{\n" + "MessageManager m;\n" + "ComponentDirectory componentDirectory = coreManager.getComponentDirectory();\n" + "m= (MessageManager)componentDirectory.getManager(\"messageManager\");\n" + "Message message = new Message();\n" + "message.addRecipient(recipientID);\n" + "message.setSubject(subject);\n" + "message.setPriority(priority);\n" + "message.setContent(message_content);\n" + "m.sendMessage(message, creator);\n" + "}\n" + "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n" ; ac=ac+"public void sendEmail(CoreManager coreManager, String smtphost, String emailfrom, String emailaddr, String subject, String m)\n"+ "{\n"+ "try{\n"+ "String emailto=new String();\n"+ "if(emailaddr.split(\"@\").length==1){\n"+ "UserManager userManager = (UserManager)coreManager.getComponentDirectory().getManager(\"userManager\");\n"+ "int id=userManager.getUserIdForUserLogin(emailaddr);\n"+ "User emailtouser=userManager.getUser(id);\n"+ "emailto = emailtouser.getAttributeAsString(User.EMAIL);\n"+ "}\n"+ "else if(emailaddr.split(\"@\").length==2){\n"+ "emailto=emailaddr;"+ "}\n"+ "MailUtil.sendMail(smtphost, emailto, emailfrom, subject, m);\n"+ "}\n" + "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n"; ac=ac+"public void setAttr(CoreManager coreManager,Item item,int id, String aname, String value)"+ "{\n"+ "try{\n"+ "if(id==0){\n"+ "item.setAttribute(aname,value);\n"+ "}\n"+ "else{\n"+ "ItemManager itemManager=(ItemManager)coreManager.getComponentDirectory().getManager(\"itemManager\");\n"+ "Item item=itemManager.getItem(id);\n"+ "item.setAttribute(aname,value);\n"+ "}\n"+ "}\n" + "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n"; ac=ac+"public void setAttr(CoreManager coreManager, Category category,int id, String aname, String value)"+ "{\n"+ "try{\n"+ "if(id==0){\n"+ "id=category.getId();\n"+ "}\n"+ "else{\n"+ "CategoryManager categoryManager=(CategoryManager)coreManager.getComponentDirectory().getManager(\"categoryManager\");\n"+ "Category category=categoryManager.getCategory(id);\n"+ "category.setAttribute(aname,value);\n"+ "}\n"+ "}\n" + "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n"; ac=ac+"public void setAttr(CoreManager coreManager, Context context,int id, String aname, String value)"+ "{\n"+ "try{\n"+ "if(id==0){\n"+ "id=context.getId();\n"+ "}\n"+ "else{\n"+ "ContextManager contextManager=(ContextManager)coreManager.getComponentDirectory().getManager(\"contextManager\");\n"+ "Context context=contextManager.getContext(id);\n"+ "context.setAttribute(aname,value);\n"+ "}\n"+ "}\n" + "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n"; ac=ac+"public void setAnnotation(CoreManager coreManager, User creator, Item item, String lang, String title, String content, int rating){\n"+ "try{"+ "ItemManager itemManager=(ItemManager)coreManager.getComponentDirectory().getManager(\"itemManager\");\n"+ "int aitemid=item.getAnnotationCount()+1;\n"+ "Date datum=new Date();\n"+ "ItemAnnotation itemannotation=new ItemAnnotation(itemManager,aitemid, item.getId(), creator.getId(), datum, lang, title, content, rating);\n"+ "item.addItemAnnotation(itemannotation);\n"+ "}\n"+ "catch(Exception e){\n"+ "System.out.print(e.toString());"+ "}\n"+ "}\n"; ac=ac+"public void setRating(CoreManager coreManager, User creator, Item item, int rating)"+ "{\n"+ "try{\n"+ "setAnnotation(coreManager, creator, item, \" \",\"set Rating\",\" \",rating);\n"+ "}\n"+ "catch(Exception e){\n" + "System.out.print(e.toString());\n" + "}\n" + "}\n"; ac=ac+"</java:functions>\n"; b=ac.getBytes(); bos.write(b); bos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void createDRL_rule(int key, List c, List a){ BufferedOutputStream bos; byte[] b; try { if(key==-1) bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent.java.drl"),true)); else bos = new BufferedOutputStream(new FileOutputStream(DRL_dir.concat("Agent_"+key+".java.drl"),true)); String ru=null; Iterator i_c=c.iterator(); while(i_c.hasNext()){ Condition condition=(Condition)i_c.next(); if(condition.getDomain().equals("item")){ ru="<parameter identifier=\"item\">\n" + "<class>Item</class>\n" + "</parameter>\n"; } if(condition.getDomain().equals("category")){ ru="<parameter identifier=\"category\">\n" + "<class>Category</class>\n" + "</parameter>\n"; } if(condition.getDomain().equals("context")){ ru="<parameter identifier=\"context\">\n" + "<class>Context</class>\n" + "</parameter>\n"; } } Iterator i_c2=c.iterator(); while(i_c2.hasNext()){ Condition condition=(Condition)i_c2.next(); ru=ru+"<java:condition>domain.equals(\""+condition.getDomain()+"\")</java:condition>\n"+ "<java:condition>src.equals(\""+condition.getSrcComponent()+"\")</java:condition>\n"; int userID=condition.getUserID(); UserManager userManager = (UserManager) coreManager.getComponentDirectory().getManager("userManager"); User user=userManager.getUser(userID); String creator; if(userID==0){ creator=""; } else creator=user.getUserLogin(); if(!creator.equals("")){ ru=ru+"<java:condition>creator.getUserLogin().equals(\""+creator+"\")</java:condition>\n"; } ru=ru+"<java:condition>opt.equals(\""+condition.getOption()+"\")</java:condition>\n"; if(condition.getAttrMap()!=null){ Map attrMap = condition.getAttrMap(); Iterator attrkey = attrMap.keySet().iterator(); while (attrkey.hasNext()){ String attr_name = (String)attrkey.next(); Attribute attr=(Attribute)attrMap.get(attr_name); if(condition.getSrcComponent().equals("org.cobricks.item")){ if(attr.getName().equals("title")){ if(attr.getType()==0) ru=ru+"<java:condition>item.getTitle().equals(\""+ attr.getValue()+"\")</java:condition>\n"; else if(attr.getType()==1) ru=ru+"<java:condition>item.getTitle().compareTo(\""+ attr.getValue()+"\")>0</java:condition>\n"; else if(attr.getType()==2) ru=ru+"<java:condition>item.getTitle().compareTo(\""+ attr.getValue()+"\")<0</java:condition>\n"; else if(attr.getType()==3){ ru=ru+"<java:condition>item.getTitle().indexOf(\""+attr.getValue()+"\")>=0"+ "</java:condition>\n"; } } else if(attr.getName().equals("content")){ if(attr.getType()==0) ru=ru+"<java:condition>item.getContent().equals(\""+ attr.getValue()+"\")</java:condition>\n"; else if(attr.getType()==1) ru=ru+"<java:condition>item.getContent().compareTo(\""+ attr.getValue()+"\")>0</java:condition>\n"; else if(attr.getType()==2) ru=ru+"<java:condition>item.getContent().compareTo(\""+ attr.getValue()+"\")<0</java:condition>\n"; else if(attr.getType()==3) ru=ru+"<java:condition>item.getContent().indexOf(\""+attr.getValue()+"\")>=0"+ "</java:condition>\n"; } else { if(attr.getType()==0) ru=ru+"<java:condition>item.getAttributeAsString(\""+attr.getName()+"\").equals(\""+ attr.getValue()+"\")</java:condition>\n"; else if(attr.getType()==1) ru=ru+"<java:condition>item.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")>0</java:condition>\n"; else if(attr.getType()==2) ru=ru+"<java:condition>item.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")<0</java:condition>\n"; else if(attr.getType()==3) ru=ru+"<java:condition>item.getAttributeAsString(\""+attr.getName()+"\").indexOf(\""+attr.getValue()+"\")>=0"+ "</java:condition>\n"; } } else if (condition.getSrcComponent().equals("org.cobricks.category")){ if(attr.getType()==0) ru=ru+"<java:condition>category.getAttributeAsString(\""+attr.getName()+"\").equals(\""+ attr.getValue()+"\")</java:condition>\n"; else if(attr.getType()==1) ru=ru+"<java:condition>category.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")>0</java:condition>\n"; else if(attr.getType()==2) ru=ru+"<java:condition>category.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")<0</java:condition>\n"; else if(attr.getType()==3) ru=ru+"<java:condition>category.getAttributeAsString(\""+attr.getName()+"\").indexOf(\""+attr.getValue()+"\")>=0"+ "</java:condition>\n"; } else if(condition.getSrcComponent().equals("org.cobricks.context")){ if(attr.getType()==0) ru=ru+"<java:condition>context.getAttributeAsString(\""+attr.getName()+"\").equals(\""+ attr.getValue()+"\")</java:condition>\n"; else if(attr.getType()==1) ru=ru+"<java:condition>context.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")>0</java:condition>\n"; else if(attr.getType()==2) ru=ru+"<java:condition>context.getAttributeAsString(\""+attr.getName()+"\").compareTo(\""+ attr.getValue()+"\")<0</java:condition>\n"; else if(attr.getType()==3) ru=ru+"<java:condition>context.getAttributeAsString(\""+attr.getName()+"\").indexOf(\""+attr.getValue()+"\")>=0"+ "</java:condition>\n"; } } } } Iterator i_a=a.iterator(); ru=ru+"<java:consequence>\n"; while(i_a.hasNext()){ Action action=(Action)i_a.next(); switch(action.type){ case Action.SENDMSG: ActionSendMsg actMsg = (ActionSendMsg)action; ru=ru+"sendMsg( coreManager, creator,"+actMsg.getToUserID()+", "+actMsg.getPriority()+", \""+actMsg.getSubject()+"\",\""+actMsg.getContent()+"\");\n"; break; case Action.SENDMAIL: ActionSendMail actMail = (ActionSendMail)action; String smtphost=coreManager.getProperty("message.email.smtphost"); if (smtphost == null || smtphost.trim().length()<1) { logger.warn("no SMTP host defined"); } String emailfrom = coreManager.getProperty("message.email.defaultfrom"); ru=ru+"sendEmail( coreManager, \""+smtphost+"\", \""+emailfrom+"\", \""+ actMail.getEmailTo()+"\", \""+actMail.getSubject()+"\",\""+actMail.getContent()+"\");\n"; break; case Action.SETATTR: ActionSetAttr actSet = (ActionSetAttr)action; if(actSet.getObjectName().equals("item")) ru=ru+"setAttr( coreManager, item,"+actSet.getObjectID()+", \""+actSet.getAName()+ "\", \""+actSet.getAValue()+"\");\n"; else if(actSet.getObjectName().equals("category")) ru=ru+"setAttr( coreManager, category,"+actSet.getObjectID()+", \""+actSet.getAName()+ "\", \""+actSet.getAValue()+"\");\n"; else if(actSet.getObjectName().equals("context")) ru=ru+"setAttr( coreManager, context,"+actSet.getObjectID()+", \""+actSet.getAName()+ "\", \""+actSet.getAValue()+"\");\n"; else if(actSet.getObjectName().equals("course")) ru=ru+"setAttr( coreManager, course,"+actSet.getObjectID()+", \""+actSet.getAName()+ "\", \""+actSet.getAValue()+"\");\n"; break; case Action.SETANNO: ActionAnnotation actAnno=(ActionAnnotation)action; if(actAnno.getRating()==null||actAnno.getRating().equals("")||actAnno.getRating().equals(" "))actAnno.setRating("0"); ru=ru+"setAnnotation( coreManager, creator, item, \""+ actAnno.getLanguage()+"\",\""+actAnno.getTitle()+"\", \""+actAnno.getContent()+"\", "+actAnno.getRating()+");\n"; break; case Action.SETRAT: ActionRating actRat=(ActionRating)action; ru=ru+"setRating( coreManager,creator, item, "+actRat.getRating()+");\n"; } } ru=ru+"</java:consequence>\n"; b=ru.getBytes(); bos.write(b); bos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } --- NEW FILE: Attribute.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class Attribute { public static final int EQUAL = 0; public static final int GREATER = 1; public static final int SMALLER = 2; public static final int LIKE = 3; private String name; private String value; private int type; public Attribute (String name, int type, String value){ if (type < 0 || type > 3) type = 3; this.name = name; this.type = type; this.value = value; } /** * @return Returns the name. */ public String getName() { return name; } /** * @param name The name to set. */ public void setName(String name) { this.name = name; } /** * @return Returns the type. */ public int getType() { return type; } /** * @param type The type to set. */ public void setType(int type) { this.type = type; } /** * @return Returns the value. */ public String getValue() { return value; } /** * @param value The value to set. */ public void setValue(String value) { this.value = value; } public String toString(){ StringBuffer result = new StringBuffer(); result.append(name); if (type== EQUAL) result.append("="); else if (type== GREATER) result.append(">"); else if (type == SMALLER) result.append("<"); else result.append("~"); result.append(value); return result.toString(); } } --- NEW FILE: ActionAnnotation.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * This file is part of a free software package; you can redistribute * it and/or modify it under the terms of the Cobricks Software Licence; * either version 1.0 of the License, or (at your option) any later * version (see www.cobricks.de). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.util.ArrayList; import java.util.Iterator; /** * * @author Han Jiang * @author Tong Tong * @author Ye Shen * @version $Date: 2006/02/13 12:37:05 $ */ public class ActionAnnotation extends Action { private String title; private String rating; private String content; private String language; public ActionAnnotation(){ this.type = Action.SETANNO; } /* public ActionSendMail(String et, String subj, String cont){ this(); emailTo = et; subject = subj; content = cont; } */ // Format of mailFromDB: Set Annotation:<title>:<content>:<rating>:<language> // format ???????????????? public ActionAnnotation (int actionID, String AnnoFromDB){ this.actionID = actionID; String[] cont = AnnoFromDB.split(":",5); title = cont[1]; content = cont[2]; rating = cont[3]; language= cont[4]; } /* * @see java.lang.Object#toString() */ public String toString() { StringBuffer result = new StringBuffer("Set Annotation:"); result.append(title+":"); result.append(content+":"); result.append(rating+":"); result.append(language); return result.toString(); } /** * the output html format for Velocity template * * @see org.cobricks.agent.Action#toHtmlString() * @return the output html format for Velocity template (Ye Shen) * */ public String toHtmlString() { // TODO Auto-generated method stub return null; } /** * @return Returns the subject. */ public String getTitle() { return title; } /** * @param subject The subject to set. */ public void setTitle(String t) { title = t; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getRating() { return rating; } public void setRating(String rat) { rating = rat; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } } --- NEW FILE: AgentManagerImpl.java --- /* * Copyright (c) 2005 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.agent; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.apache.log4j.Logger; import org.cobricks.category.Category; import org.cobricks.category.CategoryManager; import org.cobricks.context.Context; import org.cobricks.context.ContextManager; import org.cobricks.core.CobricksException; import org.cobricks.core.ComponentDirectory; import org.cobricks.core.ComponentEvent; import org.cobricks.core.ComponentManagerAdaptor; import org.cobricks.core.CoreManager; import org.cobricks.core.db.DBAccess; import org.cobricks.course.Course; import org.cobricks.course.CourseManager; import org.cobricks.item.Item; import org.cobricks.item.ItemManager; import org.cobricks.user.User; import org.cobricks.user.UserManager; import org.drools.DroolsException; import org.xml.sax.SAXException; /** * Implementation of the MessageManager interface. * * @author mic...@ac... * @version $Date: 2006/02/13 12:37:05 $ */ public class AgentManagerImpl extends ComponentManagerAdaptor implements AgentManager { static Logger logger = Logger.getLogger(AgentManagerImpl.class); static String COMP_NAME = "Cobricks-2 Agent Manager"; static String COMP_VERSION = "V0.2 (Feb. 2004)"; private Map totalRules; static String DRL_dir; // private DRLCreator drlCreator; /** * */ public AgentManagerImpl() { super(); totalRules = new HashMap(); } public String getComponentName() { return COMP_NAME; } public String getComponentVersion() { return COMP_VERSION; } public void obtainRules(){ List ruleUserIDs = dbAccess.sqlQuery("SELECT * FROM user_rules"); if (ruleUserIDs.isEmpty()) return; for (int i = 0;i<ruleUserIDs.size();i++){ Map ruleID_userID = (Map)ruleUserIDs.get(i); int ruleID = Integer.parseInt(ruleID_userID.get("ruleid").toString()); int userID = Integer.parseInt(ruleID_userID.get("userid").toString()); obtainRuleForUser(ruleID, userID); } } private void obtainRuleForUser(int ruleID, int userID){ List rulesForUser = (List)totalRules.get(new Integer(userID)); if (rulesForUser == null){ rulesForUser = new ArrayList(); totalRules.put(new Integer(userID), rulesForUser); } Rule rule = new Rule(); rule.setRuleID(ruleID); List conditions = dbAccess.sqlQuery("SELECT rule_conditionID, rule_condition "+ " FROM user_rulesconditions WHERE ruleID="+ruleID); for (int i = 0;i<conditions.size();i++){ Map conditionMap = (Map)conditions.get(i); int conditionID = Integer.parseInt(conditionMap.get("rule_conditionid").toString()); String conFromDB = conditionMap.get("rule_condition").toString(); Condition condition = new Condition(conditionID, conFromDB); rule.addCondition(condition); } List actions = dbAccess.sqlQuery("SELECT rule_actionID, rule_action, message"+ " FROM user_rulesactions WHERE ruleID="+ruleID); for (int i = 0;i<actions.size();i++){ Map actionMap = (Map)actions.get(i); int actionID = Integer.parseInt(actionMap.get("rule_actionid").toString()); String conFromDB = actionMap.get("rule_action").toString(); String message = actionMap.get("message").toString(); Action action = null; if (conFromDB.startsWith("Send Email")){ action = new ActionSendMail(actionID,conFromDB, message); } else if (conFromDB.startsWith("Send Message")){ action = new ActionSendMsg(actionID, conFromDB, message); } else if (conFromDB.startsWith("Set Annotation")){ action = new ActionAnnotation(actionID, conFromDB); } else if (conFromDB.startsWith("Set Rating")){ action = new ActionRating(actionID, conFromDB); } else { action = new ActionSetAttr(actionID, conFromDB); } rule.setAction(action); } rulesForUser.add(rule); } public List getRulesForUser(int userID){ List rulesForUser = (List)totalRules.get(new Integer(userID)); return rulesForUser; } public void insertRuleForUser(int userID, Rule rule){ try{ // Add a rule in the object totalRules if(totalRules.get(new Integer(userID))!=null){ logger.debug("this user has rules already!"); List rulesForUser = (List)totalRules.get(new Integer(userID)); rulesForUser.add(rule); totalRules.remove(new Integer(userID)); totalRules.put(new Integer(userID),rulesForUser); } else{ logger.debug("this user has no rules!"); List rulesForUser=new ArrayList(); rulesForUser.add(rule); totalRules.put(new Integer(userID),rulesForUser); } // Add the new rule in the database logger.debug("hier add rule database!"); int newRuleID = Integer.parseInt( dbAccess.getNewPrimaryKey("user_rules","ruleID") ); Map user_rule = new HashMap(); rule.setRuleID(newRuleID); user_rule.put("userID", new Integer(userID)); user_rule.put("ruleID", new Integer(newRuleID)); dbAccess.sqlInsert("user_rules",user_rule); // Conditions logger.debug("hier add condition database"); List conditions = rule.getConditions(); for (int i=0; i<conditions.size();i++){ int newConditionID = Integer.parseInt( dbAccess.getNewPrimaryKey("user_rulesconditions","rule_conditionID") ); ((Condition)conditions.get(i)).setConditionID(newConditionID); Map condMap= new HashMap(); condMap.put("ruleID",new Integer(newRuleID)); condMap.put("rule_conditionID",new Integer(newConditionID)); condMap.put("rule_condition",conditions.get(i).toString()); dbAccess.sqlInsert("user_rulesconditions", condMap); } // Actions logger.debug("hier add action database"); List actions = rule.getActions(); for (int i=0; i<actions.size();i++){ int newActionID = Integer.parseInt( dbAccess.getNewPrimaryKey("user_rulesactions","rule_actionID") ); ((Action)actions.get(i)).setActionID(newActionID); Map actMap= new HashMap(); actMap.put("ruleID",new Integer(newRuleID)); actMap.put("rule_actionID",new Integer(newActionID)); actMap.put("rule_action",actions.get(i).toString()); actMap.put("message", ((Action)actions.get(i)).getContent()); dbAccess.sqlInsert("user_rulesactions", actMap); } logger.debug("hier create drl"); logger.debug(Integer.toString(userID)); DRLCreator drlCreator = new DRLCreator(totalRules, DRL_dir, userID, coreManager); } catch(Exception e){ logger.debug(e.toString()); } } public void delRule(int userID, Rule rule){ // remove the rule from the object totalRules List rulesForUser = (List)totalRules.get(new Integer(userID)); int ruleID = rule.getRuleID(); rulesForUser.remove(rule); // remove the rule from the database dbAccess.sqlDelete("user_rules", "ruleID="+ruleID); dbAccess.sqlDelete("user_rulesconditions", "ruleID="+ruleID); dbAccess.sqlDelete("user_rulesactions", "ruleID="+ruleID); DRLCreator drlCreator = new DRLCreator(totalRules, DRL_dir, userID, coreManager); } public void updateRule( int userID, Rule rule){ // update the rule in the database. // TODO update the rule in the totalRules? // Bis jetzt wird hier geleistet: Condition, Action: change // Noch nicht ber¨¹cksichtigt: Condition, Action :add or delete List conditions = rule.getConditions(); for (int i= 0;i<conditions.size();i++){ Condition condition = (Condition)conditions.get(i); Map condMap= new HashMap(); condMap.put("rule_condition",condition.toString()); dbAccess.sqlUpdate("user_rulesconditions", condMap, "rule_conditionID="+condition.getConditionID()); } List actions = rule.getActions(); for (int i= 0;i<actions.size();i++){ Action action = (Action)actions.get(i); Map condMap= new HashMap(); condMap.put("rule_action", action.toString()); condMap.put("",action.getContent()); dbAccess.sqlUpdate("user_rulesactions", condMap, "rule_actionID="+action.getActionID()); } DRLCreator drlCreator = new DRLCreator(totalRules, DRL_dir,userID, coreManager); } /** * */ public void init(String componentId, String managerId, Properties properties, CoreManager coreManager, DBAccess dbAccess) throws Exception { super.init(componentId, managerId, properties, coreManager, dbAccess); DRL_dir=properties.getProperty("agent.DRL_dir"); ComponentDirectory componentDirectory = coreManager.getComponentDirectory(); componentDirectory.registerManager(managerId, this); logger.info("Checking and updating the agent component " +"database tables"); Set set = dbAccess.getComponentTableNames(getComponentId()); Iterator i = set.iterator(); while (i.hasNext()) { String tablename = (String)i.next(); int res = dbAccess.checkTable(tablename); if (res == -1) dbAccess.createTable(tablename); else if (res == -2) { if (!dbAccess.updateTable(tablename)) logger.error("Updating table "+tablename+" failed."); } } obtainRules(); UserManager userManager = (UserManager) coreManager.getComponentDirectory().getManager("userManager"); if(userManager!=null){ List userIdList=userManager.getUserIdList(); for(int j=0; j<userIdList.size();j++){ String userID=userIdList.get(j).toString(); DRLCreator drlCreator = new DRLCreator(totalRules, DRL_dir,Integer.parseInt(userID), coreManager); } } } /** * */ /* ************************************************************ */ /** * Check which users want to receive the component event and deliver * it to the users as message. */ public void processEvent(ComponentEvent event) { logger.info("Receiving event "+event.getAction()+" for " +event.getDomain() +" from "+event.getSourceComponentId()); ComponentEventThread t = new ComponentEventThread(event); t.start(); } /* ************************************************************ */ /* Thread for processing component events */ class ComponentEventThread extends Thread { ComponentEvent event; ComponentEventThread(ComponentEvent event) { this.event = event; } public void run() { String opt=event.getAction(); logger.debug(opt); UserManager userManager = (UserManager) coreManager.getComponentDirectory().getManager("userManager"); User user = userManager.getUser(event.getUserId()); ... [truncated message content] |