Author: mic...@jb... Date: 2006-01-09 03:09:02 -0500 (Mon, 09 Jan 2006) New Revision: 2036 Added: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ApplicationDataDef.java trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/FunctionDef.java trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ImportDef.java trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ApplicationDataDef.hbm.xml trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/FunctionDef.hbm.xml trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ImportDef.hbm.xml Modified: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/IVersionable.java trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleDef.java trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleSetDef.java trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/db/RepositoryImpl.java trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/RuleSetDef.hbm.xml trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/Tag.hbm.xml trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RulePersistenceTest.java trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RuleSetPersistenceTest.java trunk/labs/jbossrules/drools-repository/src/test/resources/hibernate.cfg.xml Log: more working. Still some broken bits. Added: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ApplicationDataDef.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ApplicationDataDef.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ApplicationDataDef.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,75 @@ +package org.drools.repository; + +/** + * Application data contains a definition of objects that may be provided to the + * rule engine to support the execution of rules. + * + * + * @author <a href="mailto:mic...@gm..."> Michael Neale</a> + */ +public class ApplicationDataDef extends Persistent + implements + IVersionable { + + private static final long serialVersionUID = 609004561319545529L; + + private String versionComment; + private long versionNumber; + private String identifier; + private String type; + + /** + * @param identifier + * the id by which the app data will be referenced in a rule. + * @param type + * The type of the object that will be bound to the identifier + * for the ruleset. + */ + public ApplicationDataDef(String identifier, + String type) { + this.identifier = identifier; + this.type = type; + } + + ApplicationDataDef() { + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public IVersionable copy() { + ApplicationDataDef clone = new ApplicationDataDef( this.identifier, + this.type ); + return clone; + } + + public String getVersionComment() { + return versionComment; + } + + public void setVersionComment(String versionComment) { + this.versionComment = versionComment; + } + + public long getVersionNumber() { + return versionNumber; + } + + public void setVersionNumber(long versionNumber) { + this.versionNumber = versionNumber; + } + +} Property changes on: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ApplicationDataDef.java ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/FunctionDef.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/FunctionDef.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/FunctionDef.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,78 @@ +package org.drools.repository; + +/** + * A FunctionDef contains the definition of a function that is used in one or more rules. + * Functions can be written in any language that is supported by the semantic framework. + * + * @author <a href="mailto:mic...@gm..."> Michael Neale</a> + */ +public class FunctionDef extends Persistent + implements + IVersionable { + + private static final long serialVersionUID = -7585928690157539970L; + + private long versionNumber; + private String versionComment; + private String functionContent; + private String description; + private String language; + + public String getLanguage(){ + return language; + } + + public void setLanguage(String language){ + this.language = language; + } + + public FunctionDef(String functionContent, String description) { + this.functionContent = functionContent; + this.description = description; + this.language = ""; + } + + FunctionDef() {} + + public IVersionable copy(){ + FunctionDef clone = new FunctionDef(this.functionContent, this.description); + clone.language = this.language; + return clone; + } + + public String getDescription(){ + return description; + } + + public void setDescription(String description){ + this.description = description; + } + + public String getFunctionContent(){ + return functionContent; + } + + public void setFunctionContent(String functionContent){ + this.functionContent = functionContent; + } + + + + public void setVersionNumber(long versionNumber){ + + this.versionNumber = versionNumber; + } + + public void setVersionComment(String comment){ + this.versionComment = comment; + } + + public long getVersionNumber(){ + return this.versionNumber; + } + + public String getVersionComment(){ + return this.versionComment; + } + +} Property changes on: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/FunctionDef.java ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/IVersionable.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/IVersionable.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/IVersionable.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -3,12 +3,24 @@ /** All assets that support major versioning must implement this. */ interface IVersionable { - /** must create a fresh copy OF THE SAME TYPE, with a null Id */ + /** of course they have to have an id ! + * Ids are always assigned by the database. + */ + Long getId(); + + /** Must create a fresh copy OF THE SAME TYPE, with a null Id */ IVersionable copy(); + /** + * The version number is used to group assets together in a RuleSet for instance + * The version number should NOT only be set by the repository, not by users. + */ void setVersionNumber(long versionNumber); + /** The version comment is used when major versions are created */ void setVersionComment(String comment); + + String getVersionComment(); long getVersionNumber(); Added: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ImportDef.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ImportDef.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ImportDef.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,54 @@ +package org.drools.repository; + +/** + * This holds a type import for a ruleset. + * @author <a href="mailto:mic...@gm..."> Michael Neale</a> + */ +public class ImportDef extends Persistent + implements + IVersionable { + + private static final long serialVersionUID = -5509795356918241886L; + + private long versionNumber; + private String versionComment; + private String type; + + /** + * @param type The type to import into the ruleset. + */ + public ImportDef(String type) { + this.type = type; + } + + ImportDef() {} + + /** The type to import into the ruleset */ + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getVersionComment() { + return versionComment; + } + public void setVersionComment(String versionComment) { + this.versionComment = versionComment; + } + public long getVersionNumber() { + return versionNumber; + } + public void setVersionNumber(long versionNumber) { + this.versionNumber = versionNumber; + } + + public IVersionable copy() { + // TODO Auto-generated method stub + return new ImportDef(this.type); + } + + + + +} Property changes on: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/ImportDef.java ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleDef.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleDef.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleDef.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -176,4 +176,8 @@ return newVersion; } + public String toString() { + return "{ id = " + this.getId() + "name= (" + this.name + ") version = " + this.getVersionNumber() + " }"; + } + } Modified: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleSetDef.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleSetDef.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/RuleSetDef.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -1,227 +1,326 @@ package org.drools.repository; -import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Set; /** - * The ruleset definition contains a grouping of rules for editing/release. - * The workingVersionNumber drives what version of rules will be included in this ruleset. - * Changing this number will mean that different versions of ruledefs are loaded etc. + * The ruleset definition contains a grouping of rules for editing/release. The + * workingVersionNumber drives what version of rules will be included in this + * ruleset. Changing this number will mean that different versions of ruledefs + * are loaded etc. * * @author <a href="mailto:mic...@gm..."> Michael Neale</a> */ -public class RuleSetDef extends Persistent implements Comparable { +public class RuleSetDef extends Persistent + implements + Comparable { private static final long serialVersionUID = 608068118653708104L; - - private String name; - private MetaData metaData; - private Set rules; - private Set tags; - private long workingVersionNumber; - private Set versionHistory; - private Set attachments; - public RuleSetDef(String name, MetaData meta) { + private String name; + private MetaData metaData; + private Set rules; + private Set tags; + private long workingVersionNumber; + private Set versionHistory; + private Set attachments; + private Set imports; + private Set applicationData; + private Set functions; + + public RuleSetDef(String name, + MetaData meta) { this.name = name; this.metaData = meta; this.tags = new HashSet(); this.rules = new HashSet(); this.attachments = new HashSet(); this.versionHistory = new HashSet(); + this.functions = new HashSet(); + this.applicationData = new HashSet(); + this.imports = new HashSet(); this.workingVersionNumber = 1; - } - + } + /** - * This is not for public consumption. Use the - * proper constructor instead. + * This is not for public consumption. Use the proper constructor instead. */ - RuleSetDef() { - } - + RuleSetDef() { + } - public Set getVersionHistory(){ + public Set getVersionHistory() { return versionHistory; } - - void setVersionHistory(Set versionHistory){ + void setVersionHistory(Set versionHistory) { this.versionHistory = versionHistory; } + public RuleSetDef addRule(RuleDef rule) { + return addAssetToSet( rule, + this.rules ); + } + + public RuleSetDef addAttachment(RuleSetAttachment attachmentFile) { + return addAssetToSet( attachmentFile, + this.attachments ); + } + + public RuleSetDef addImport(ImportDef importDef) { + return addAssetToSet( importDef, + this.imports ); + } + + public RuleSetDef addApplicationData(ApplicationDataDef appData) { + return addAssetToSet( appData, + this.applicationData ); + } + /** - * This adds a rule to the ruleset. + * Removes a rule from the current ruleset. This + * DOES NOT delete the rule, and DOES NOT effect any other versions + * of the ruleset. + */ + public void removeRule(RuleDef rule) { + //rule.setVersionNumber(-1); + this.rules.remove(rule); + } + + public void removeFunction(FunctionDef function) { + this.functions.remove(function); + } + + public void removeApplicationData(ApplicationDataDef appData) { + this.applicationData.remove(appData); + } + + public void removeImport(ImportDef imp) { + this.imports.remove(imp); + } + + public void removeAttachment(RuleSetAttachment attachment) { + this.attachments.remove(attachment); + } + + public RuleSetDef addFunction(FunctionDef function) { + return addAssetToSet( function, + this.functions ); + } + + /** + * This adds a versionable asset to the specified set. * - * If the rule already has an Id, and it is a different version number, then - * it will be copied for this ruleset. - * If it has the same version number, then it will be shared. + * Copy/versus linking: If the asset already has an Id, and it is a + * different version number, then it will be copied for the set. If it has + * the same version number, then it will be shared. Sharing is generally not + * recommended, but can be useful. */ - public RuleSetDef addRule(RuleDef rule) { - if (rule.getId() == null) { - rule.setVersionNumber(this.workingVersionNumber); - this.rules.add(rule); - } else if (rule.getVersionNumber() == this.workingVersionNumber) { - this.rules.add(rule); - } else { - RuleDef copy = (RuleDef) rule.copy(); - copy.setVersionNumber(this.workingVersionNumber); - copy.setVersionComment("Copied for this version."); - this.rules.add(copy); + RuleSetDef addAssetToSet(IVersionable asset, + Set set) { + if ( asset.getId() == null ) { + asset.setVersionNumber( this.workingVersionNumber ); + asset.setVersionComment( "New" ); + set.add( asset ); } + else if ( asset.getVersionNumber() == this.workingVersionNumber ) { + set.add( asset ); + } + else { + IVersionable copy = asset.copy(); + copy.setVersionNumber( this.workingVersionNumber ); + copy.setVersionComment( "Copied for this version." ); + set.add( copy ); + } return this; } - - public RuleSetDef addAttachment(RuleSetAttachment attachmentFile) { - attachmentFile.setVersionNumber(this.workingVersionNumber); - this.attachments.add(attachmentFile); - return this; - } - - public MetaData getMetaData(){ + + public MetaData getMetaData() { return metaData; } - public void setMetaData(MetaData metaData){ + + public void setMetaData(MetaData metaData) { this.metaData = metaData; } - - + /** The list of rules that are currently loaded for this ruleset */ - public Set getRules(){ + public Set getRules() { return rules; } - private void setRules(Set rules){ + + private void setRules(Set rules) { this.rules = rules; } - - public String getName(){ + + public String getName() { return name; } - private void setName(String name){ + + private void setName(String name) { this.name = name; } - public Set getTags(){ + public Set getTags() { return tags; } - private void setTags(Set tags){ + private void setTags(Set tags) { this.tags = tags; } - + public RuleSetDef addTag(String tag) { - this.tags.add(new Tag(tag)); + this.tags.add( new Tag( tag ) ); return this; } - public long getWorkingVersionNumber(){ + public long getWorkingVersionNumber() { return workingVersionNumber; } /** - * This will only be set when loading the RuleSet from the repository. - * When you load a ruleset, a version number is specified. - * This property is not persistent, as multiple people could be working on different versions - * at the same time. + * This will only be set when loading the RuleSet from the repository. When + * you load a ruleset, a version number is specified. This property is not + * persistent, as multiple people could be working on different versions at + * the same time. * - * DO NOT set this property. + * DO NOT set this property MANUALLY !!! * * @param workingVersionNumber */ - public void setWorkingVersionNumber(long workingVersionNumber){ + public void setWorkingVersionNumber(long workingVersionNumber) { this.workingVersionNumber = workingVersionNumber; } - /** - * This method increments the working version of the ruleset, creating a brand new version. - * This records the event in the version history. + /** + * This method increments the working version of the ruleset, creating a + * brand new version. This records the event in the version history. * - * Typically you would call this method when you want to make a stable version of a rule set (lock in all - * the related assets) and then move on to an "editing" version. You can always switch back to a previous version + * Typically you would call this method when you want to make a stable + * version of a rule set (lock in all the related assets) and then move on + * to an "editing" version. You can always switch back to a previous version * of a rulebase. * - * All rules and ruleset-attachments etc that are - * connected to this version of the ruleset are cloned with the new workingVersionNumber. + * All rules and ruleset-attachments etc that are connected to this version + * of the ruleset are cloned with the new workingVersionNumber. * - * This means that the previous state of the RuleSet is kept in tact (for instance, as a release of rules). - * Rules can then be edited, removed and so on without effecting any previous versions of rules and the ruleset. + * This means that the previous state of the RuleSet is kept in tact (for + * instance, as a release of rules). Rules can then be edited, removed and + * so on without effecting any previous versions of rules and the ruleset. * - * Previous rules can be retrieved by changing the value of workingVersionNumber. + * Previous rules can be retrieved by changing the value of + * workingVersionNumber. * - * Note that further to this, rules themselves will be versioned on save (think of that versioning as - * "minor" versions, and this sort of ruleset versions as major versions). + * Note that further to this, rules themselves will be versioned on save + * (think of that versioning as "minor" versions, and this sort of ruleset + * versions as major versions). * - * Ideally once a new version is created, the RuleSet should be stored and then loaded fresh, - * which will hide the non working versions of the rules. + * Ideally once a new version is created, the RuleSet should be stored and + * then loaded fresh, which will hide the non working versions of the rules. * - */ - public void createNewVersion(String comment, String newStatus) { + */ + public void createNewVersion(String comment, + String newStatus) { this.workingVersionNumber++; addNewVersionHistory( newStatus ); + + createAndAddNewVersions( this.rules, + comment, + this.workingVersionNumber ); + + createAndAddNewVersions( this.attachments, + comment, + this.workingVersionNumber ); + + createAndAddNewVersions( this.functions, + comment, + this.workingVersionNumber ); + + createAndAddNewVersions( this.applicationData, + comment, + this.workingVersionNumber ); - - //now have to create new rules and add to the collection - createAndAddNewVersions( this.rules, comment, this.workingVersionNumber ); - - //now attachments - createAndAddNewVersions( this.attachments, comment, this.workingVersionNumber ); + createAndAddNewVersions( this.imports, + comment, + this.workingVersionNumber ); - //create new functions, app data and imports etc. - System.out.println("DON'T FORGET FUNCTIONS ETC !!"); - } - private void addNewVersionHistory(String newStatus){ + private void addNewVersionHistory(String newStatus) { RuleSetVersionInfo newVersion = new RuleSetVersionInfo(); - newVersion.setStatus(newStatus); - newVersion.setVersionNumber(this.workingVersionNumber); - this.versionHistory.add(newVersion); + newVersion.setStatus( newStatus ); + newVersion.setVersionNumber( this.workingVersionNumber ); + this.versionHistory.add( newVersion ); } - /** This will work on any set of <code>IVersionable</code> objects. They are copied, and - * then added to the original set (with null Ids). The comment is added, as is the new version number. + /** + * This will work on any set of <code>IVersionable</code> objects. They + * are copied, and then added to the original set (with null Ids). The + * comment is added, as is the new version number. */ - private void createAndAddNewVersions(Set assets, String comment, long newVersionNumber){ - //as the Ids are null, copied objects - //will get a new identity, and have the new workingVersionNumber + private void createAndAddNewVersions(Set assets, + String comment, + long newVersionNumber) { + // as the Ids are null, copied objects + // will get a new identity, and have the new workingVersionNumber Set newVersions = new HashSet(); for ( Iterator iter = assets.iterator(); iter.hasNext(); ) { IVersionable old = (IVersionable) iter.next(); - if (old.getVersionNumber() == newVersionNumber - 1) { - //we only want to clone rules that are for the version being cloned + if ( old.getVersionNumber() == newVersionNumber - 1 ) { + // we only want to clone rules that are for the version being + // cloned IVersionable clone = (IVersionable) old.copy(); - clone.setVersionComment(comment); - clone.setVersionNumber(newVersionNumber); - newVersions.add(clone); + clone.setVersionComment( comment ); + clone.setVersionNumber( newVersionNumber ); + newVersions.add( clone ); } } - assets.addAll(newVersions); + assets.addAll( newVersions ); } - - - + public String toString() { - return "{ name=" + this.name + " , workingVersionNumber=" + this.workingVersionNumber + " }"; + return "{ name=" + this.name + " , workingVersionNumber=" + this.workingVersionNumber + " ruleCount:" + + this.rules.size() + " }"; } /** The name provides the natural ordering */ - public int compareTo(Object arg){ - if (arg instanceof RuleSetDef) { - return ((RuleSetDef) arg).name.compareTo(this.name); + public int compareTo(Object arg) { + if ( arg instanceof RuleSetDef ) { + return ((RuleSetDef) arg).name.compareTo( this.name ); } return 0; } - public Set getAttachments(){ + public Set getAttachments() { return attachments; } - private void setAttachments(Set attachments){ + private void setAttachments(Set attachments) { this.attachments = attachments; } - - + + public Set getApplicationData() { + return applicationData; + } + + private void setApplicationData(Set applicationData) { + this.applicationData = applicationData; + } + + public Set getFunctions() { + return functions; + } + + private void setFunctions(Set functions) { + this.functions = functions; + } + + public Set getImports() { + return imports; + } + + private void setImports(Set imports) { + this.imports = imports; + } + } Modified: trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/db/RepositoryImpl.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/db/RepositoryImpl.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/java/org/drools/repository/db/RepositoryImpl.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -9,24 +9,26 @@ import org.hibernate.Session; - +/** + * The repository manager takes care of storing and sychronising the repository data with + * the repository database. + * @author <a href="mailto:mic...@gm..."> Michael Neale</a> + */ public class RepositoryImpl implements Repository { /** This will simply save the current version of the rule */ - public RuleDef save(RuleDef newRule) { + public void save(RuleDef newRule) { Session session = getSession(); session.beginTransaction(); session.saveOrUpdate(newRule); session.getTransaction().commit(); - - return newRule; } - //DODGY METHODS START + public RuleDef loadRule(String ruleName, long versionNumber) { Session session = getSession(); session.beginTransaction(); @@ -49,7 +51,6 @@ session.getTransaction().commit(); return result; } - //DODGY METHODS END public List findRulesByTag(String tag) { Session session = getSession(); @@ -63,15 +64,18 @@ - public RuleSetDef save(RuleSetDef ruleSet) { + /** Save the ruleset. The Ruleset will not be reloaded. */ + public void save(RuleSetDef ruleSet) { Session session = getSession(); session.beginTransaction(); session.saveOrUpdate(ruleSet); session.getTransaction().commit(); - return ruleSet; } - /** This loads a RuleSet with the appropriate workingVersionNumber applied to its assets. + /** + * This loads a RuleSet with the appropriate workingVersionNumber applied to its assets. + * @param workingVersionNumber The version of the ruleset and rules you want to work on. + * @param ruleSetName The ruleset name to retrieve (ruleset names must be unique). */ public RuleSetDef loadRuleSet(String ruleSetName, long workingVersionNumber) { Session session = getSession(); @@ -82,9 +86,9 @@ RuleSetDef def = (RuleSetDef) session.createQuery("from RuleSetDef where name = :name") - .setString("name", ruleSetName ).uniqueResult(); + .setString("name", ruleSetName ).uniqueResult(); + def.setWorkingVersionNumber(workingVersionNumber); - def.setWorkingVersionNumber(workingVersionNumber); removeVersionFilter( session ); session.getTransaction().commit(); return def; Added: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ApplicationDataDef.hbm.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ApplicationDataDef.hbm.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ApplicationDataDef.hbm.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> + +<hibernate-mapping> + <class name="org.drools.repository.ApplicationDataDef" table="APP_DATA_DEFINITIONS"> + <id name="id" column="APP_DATA_ID"> + <generator class="native" /> + </id> + <property name="type" /> + <property name="identifier" /> + <property name="versionComment" /> + <property name="versionNumber" /> + </class> +</hibernate-mapping> \ No newline at end of file Property changes on: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ApplicationDataDef.hbm.xml ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/FunctionDef.hbm.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/FunctionDef.hbm.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/FunctionDef.hbm.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,17 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> + +<hibernate-mapping> + <class name="org.drools.repository.FunctionDef" table="FUNCTION_DEFINITIONS"> + <id name="id" column="FUNCTION_ID"> + <generator class="native"/> + </id> + <property name="description" /> + <property name="functionContent" /> + <property name="language" /> + <property name="versionComment" /> + <property name="versionNumber" /> + </class> +</hibernate-mapping> \ No newline at end of file Property changes on: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/FunctionDef.hbm.xml ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ImportDef.hbm.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ImportDef.hbm.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ImportDef.hbm.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -0,0 +1,15 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> + +<hibernate-mapping> + <class name="org.drools.repository.ImportDef" table="IMPORT_DEFINITIONS"> + <id name="id" column="IMPORT_ID"> + <generator class="native"/> + </id> + <property name="type" /> + <property name="versionComment" /> + <property name="versionNumber" /> + </class> +</hibernate-mapping> \ No newline at end of file Property changes on: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/ImportDef.hbm.xml ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/RuleSetDef.hbm.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/RuleSetDef.hbm.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/RuleSetDef.hbm.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -35,28 +35,44 @@ <set name="tags" table="RULESET_TAGS" lazy="false" cascade="all"> <key column="RULESET_ID"/> <one-to-many class="org.drools.repository.Tag"/> - </set> + </set> + <set name="versionHistory" table="RULESET_VERSION_HISTORY" lazy="false" cascade="all"> + <key column="RULESET_ID" /> + <one-to-many class="org.drools.repository.RuleSetVersionInfo"/> + </set> - <set name="rules" table="RULESET_RULES" lazy="false" cascade="all"> + <!-- now come the versioned assets --> + <set name="rules" lazy="false" cascade="all"> <key column="RULESET_ID"/> <one-to-many class="org.drools.repository.RuleDef"/> <filter name="workingVersionFilter" condition=":filteredVersionNumber = versionNumber" /> </set> - - <set name="versionHistory" table="RULESET_VERSION_HISTORY" lazy="false" cascade="all"> - <key column="RULESET_ID" /> - <one-to-many class="org.drools.repository.RuleSetVersionInfo"/> - </set> - + <set name="attachments" lazy="false" cascade="all"> <key column="RULESET_ID"/> <one-to-many class="org.drools.repository.RuleSetAttachment"/> <filter name="workingVersionFilter" condition=":filteredVersionNumber = versionNumber" /> </set> - - + <set name="imports" lazy="false" cascade="all"> + <key column="RULESET_ID"/> + <one-to-many class="org.drools.repository.ImportDef"/> + <filter name="workingVersionFilter" condition=":filteredVersionNumber = versionNumber" /> + </set> + + <set name="functions" lazy="false" cascade="all"> + <key column="RULESET_ID"/> + <one-to-many class="org.drools.repository.FunctionDef"/> + <filter name="workingVersionFilter" condition=":filteredVersionNumber = versionNumber" /> + </set> + + <set name="applicationData" lazy="false" cascade="all"> + <key column="RULESET_ID"/> + <one-to-many class="org.drools.repository.ApplicationDataDef"/> + <filter name="workingVersionFilter" condition=":filteredVersionNumber = versionNumber" /> + </set> + </class> <!-- This magic allows us to have a view of working versions only --> Modified: trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/Tag.hbm.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/Tag.hbm.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/main/resources/org/drools/repository/Tag.hbm.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -8,7 +8,10 @@ <!-- NOTE I really want to make tag strings normalised Need to add in logic for finding existing tags of same value - Or else use string as ID (not the best idea).--> + Or else use string as ID (not the best idea). + TODO: probably best to have a method with top 1000 tags cached - they re-use identity. + Then can look it up as needed. + --> <class name="org.drools.repository.Tag" table="DROOLS_TAGS"> <id name="id" column="TAG_ID"> <generator class="native" /> Modified: trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RulePersistenceTest.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RulePersistenceTest.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RulePersistenceTest.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -12,17 +12,16 @@ public void testStoreNewRuleDef() throws Exception { RepositoryImpl repo = getRepo(); - RuleDef def = repo.save(new RuleDef("myRule", "A rule")); + RuleDef def = new RuleDef("myRule", "A rule"); + repo.save(def); assertNotNull(def.getId()); - def = repo.save(new RuleDef("myRule2", "A rule2")); + repo.save(new RuleDef("myRule2", "A rule2")); def = new RuleDef("myRule3", "A rule3"); def.addTag("tag1").addTag("tag2").addTag("HR"); - def = repo.save(def); + repo.save(def); assertNotNull(def.getId()); } - - - + public void testRetreieveRuleWithTags() { RepositoryImpl repo = getRepo(); RuleDef newRule = new RuleDef("my rule", "content"); @@ -39,8 +38,7 @@ assertTrue(tagList[0].equals("HR") || tagList[0].equals("SALARY")); List rules = repo.findRulesByTag("HR"); - assertTrue(rules.size() > 0); - + assertTrue(rules.size() > 0); } public void testRuleCopy() { Modified: trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RuleSetPersistenceTest.java =================================================================== --- trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RuleSetPersistenceTest.java 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/test/java/org/drools/repository/RuleSetPersistenceTest.java 2006-01-09 08:09:02 UTC (rev 2036) @@ -4,15 +4,14 @@ import java.util.Iterator; import java.util.Set; -import org.drools.repository.AttachmentFile; -import org.drools.repository.MetaData; -import org.drools.repository.RuleDef; -import org.drools.repository.RuleSetAttachment; -import org.drools.repository.RuleSetDef; -import org.drools.repository.RuleSetVersionInfo; import org.drools.repository.db.PersistentCase; import org.drools.repository.db.RepositoryImpl; +/** + * Some quasi unit tests, and some quasi integration tests including versioning. + * @author <a href="mailto:mic...@gm..."> Michael Neale</a> + * + */ public class RuleSetPersistenceTest extends PersistentCase { public void testLoadSaveRuleSet() { @@ -87,7 +86,41 @@ assertEquals(2, def2.getVersionHistory().size()); } - public void testNewVersioning() { + public void testRemoveAsset() { + RuleSetDef def = new RuleSetDef("addRemove", null); + RuleDef rule = new RuleDef("addRemove Rule", "xxx"); + def.addRule(rule); + + RepositoryImpl repo = getRepo(); + + //save and load it fresh + repo.save(def); + def = repo.loadRuleSet("addRemove", 1); + assertEquals(1, def.getRules().size()); + + //create a new version + def.createNewVersion("new version", null); + repo.save(def); + + //load it fresh, and the remove the only rule + def = repo.loadRuleSet("addRemove", 2); + RuleDef onlyRule = (RuleDef) def.getRules().iterator().next(); + def.removeRule(onlyRule); + //assertEquals(0, def.getRules().size()); + repo.save(def); + + //load the new one, check its not there + def = repo.loadRuleSet("addRemove", 2); + assertEquals(0, def.getRules().size()); + + //load the old version, check its there + def = repo.loadRuleSet("addRemove", 1); + assertEquals(1, def.getRules().size()); + + + } + + public void testIntegrationNewVersioning() { RuleSetDef set = new RuleSetDef("InMemory", null); RuleDef def1 = new RuleDef("Rule1", "blah"); RuleDef def2 = new RuleDef("Rule2", "blah2"); @@ -144,8 +177,63 @@ assertEquals(1, att.getVersionNumber()); assertFalse("New version".equals(att.getVersionComment())); + RuleDef newRule = new RuleDef("blah42", "blah42"); + loaded.addRule(newRule); + //add some other assets to it. + loaded + .addApplicationData(new ApplicationDataDef("x", "XX")) + .addFunction(new FunctionDef("My func", "yeah")) + .addImport(new ImportDef("com.allenparsons.project")); + repo.save(loaded); + assertEquals(loaded.getWorkingVersionNumber(), newRule.getVersionNumber()); + assertNotNull(newRule.getId()); + loaded = repo.loadRuleSet("InMemory", 1); + assertEquals(1, loaded.getApplicationData().size()); + assertEquals(1, loaded.getFunctions().size()); + assertEquals(1, loaded.getImports().size()); + loaded = repo.loadRuleSet("InMemory", 2); + assertEquals(0, loaded.getApplicationData().size()); + assertEquals(0, loaded.getFunctions().size()); + assertEquals(0, loaded.getImports().size()); } + public void testParallelVersions() { + RuleSetDef def = new RuleSetDef("para", null); + def.addRule(new RuleDef("para1","sss")); + + RepositoryImpl repo = getRepo(); + repo.save(def); + + //create a new version + def = repo.loadRuleSet("para", 1); + def.createNewVersion("yeah", null); + repo.save(def); + + //load em up + RuleSetDef old = repo.loadRuleSet("para", 1); + RuleSetDef newRs = repo.loadRuleSet("para", 2); + assertEquals(1, old.getRules().size()); + assertEquals(1, newRs.getRules().size()); + + + newRs.addRule(new RuleDef("para2", "xxx")); + repo.save(newRs); + + + newRs = repo.loadRuleSet("para", 2); + old = repo.loadRuleSet("para", 1); + assertEquals(2, newRs.getRules().size()); + assertEquals(1, old.getRules().size()); + RuleDef originalRule = (RuleDef) old.getRules().iterator().next(); + //check that the original one still has the right version number + assertEquals(1, originalRule.getVersionNumber()); + + + + + + } + } Modified: trunk/labs/jbossrules/drools-repository/src/test/resources/hibernate.cfg.xml =================================================================== --- trunk/labs/jbossrules/drools-repository/src/test/resources/hibernate.cfg.xml 2006-01-09 04:08:45 UTC (rev 2035) +++ trunk/labs/jbossrules/drools-repository/src/test/resources/hibernate.cfg.xml 2006-01-09 08:09:02 UTC (rev 2036) @@ -38,8 +38,13 @@ <mapping resource="org/drools/repository/RuleSetDef.hbm.xml"/> <mapping resource="org/drools/repository/RuleSetAttachment.hbm.xml"/> <mapping resource="org/drools/repository/RuleSetVersionInfo.hbm.xml"/> + <mapping resource="org/drools/repository/ApplicationDataDef.hbm.xml" /> + <mapping resource="org/drools/repository/FunctionDef.hbm.xml" /> + <mapping resource="org/drools/repository/ImportDef.hbm.xml" /> + + </session-factory> </hibernate-configuration> \ No newline at end of file |