[jboss-svn-commits] JBL Code SVN: r18069 - in labs/jbossrules/branches/temporal_rete: drools-compiler/src/main/java/org/drools/lang/descr and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Jan 23 18:34:57 EST 2008
Author: tirelli
Date: 2008-01-23 18:34:57 -0500 (Wed, 23 Jan 2008)
New Revision: 18069
Added:
labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/TypeDeclarationError.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
Modified:
labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/lang/descr/TypeDeclarationDescr.java
labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/Package.java
Log:
JBRULES-1090: adding type declaration support
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2008-01-23 22:25:02 UTC (rev 18068)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2008-01-23 23:34:57 UTC (rev 18069)
@@ -42,10 +42,13 @@
import org.drools.lang.descr.PackageDescr;
import org.drools.lang.descr.QueryDescr;
import org.drools.lang.descr.RuleDescr;
+import org.drools.lang.descr.TypeDeclarationDescr;
import org.drools.process.core.Process;
+import org.drools.rule.Declaration;
import org.drools.rule.ImportDeclaration;
import org.drools.rule.Package;
import org.drools.rule.Rule;
+import org.drools.rule.TypeDeclaration;
import org.drools.rule.builder.RuleBuildContext;
import org.drools.rule.builder.RuleBuilder;
import org.drools.xml.XmlPackageReader;
@@ -79,7 +82,7 @@
private Dialect dialect;
- private DialectRegistry dialectRegistry;
+ private DialectRegistry dialectRegistry;
/**
* Use this when package is starting from scratch.
@@ -146,7 +149,6 @@
this.dialect = this.dialectRegistry.getDialect( this.configuration.getDefaultDialect() );
-
if ( this.pkg != null ) {
this.dialectRegistry.initAll( this );
}
@@ -177,7 +179,7 @@
*/
public void addPackageFromXml(final Reader reader) throws DroolsParserException,
IOException {
- final XmlPackageReader xmlReader = new XmlPackageReader( this.configuration.getSemanticModules() );
+ final XmlPackageReader xmlReader = new XmlPackageReader( this.configuration.getSemanticModules() );
try {
xmlReader.read( reader );
@@ -214,7 +216,7 @@
*/
public void addRuleFlow(Reader processSource) {
ProcessBuilder processBuilder = new ProcessBuilder( this );
-
+
try {
processBuilder.addProcessFromFile( processSource );
this.results.addAll( processBuilder.getErrors() );
@@ -224,29 +226,29 @@
}
this.results.add( new RuleFlowLoadError( "Unable to load the rule flow.",
e ) );
- }
+ }
- this.results = this.dialectRegistry.addResults( this.results );
+ this.results = this.dialectRegistry.addResults( this.results );
}
-
- public void addProcessFromXml(Reader reader) {
+
+ public void addProcessFromXml(Reader reader) {
ProcessBuilder processBuilder = new ProcessBuilder( this );
XmlProcessReader xmlReader = new XmlProcessReader( this.configuration.getSemanticModules() );
try {
- Process process = xmlReader.read( reader );
+ Process process = xmlReader.read( reader );
processBuilder.buildProcess( process );
- this.results.addAll( processBuilder.getErrors() );
+ this.results.addAll( processBuilder.getErrors() );
} catch ( Exception e ) {
if ( e instanceof RuntimeException ) {
throw (RuntimeException) e;
}
this.results.add( new RuleFlowLoadError( "Unable to load the rule flow.",
e ) );
- }
+ }
- this.results = this.dialectRegistry.addResults( this.results );
+ this.results = this.dialectRegistry.addResults( this.results );
}
-
+
private void addSemanticModules() {
//this.configuration.getSemanticModules();
}
@@ -260,18 +262,9 @@
validateUniqueRuleNames( packageDescr );
String dialectName = null;
- //MN: not needed as overrides are done in the compiler before here
- //as we can have mixed dialect types - still not quite right here.
- // for ( Iterator it = packageDescr.getAttributes().iterator(); it.hasNext(); ) {
- // AttributeDescr value = ( AttributeDescr ) it.next();
- // if ( "dialect".equals( value.getName() )) {
- // dialectName = value.getValue();
- // break;
- // }
- // }
// The Package does not have a default dialect, so set it
- if ( dialectName == null && this.dialect == null ) {
+ if ( this.dialect == null ) {
this.dialect = this.dialectRegistry.getDialect( this.configuration.getDefaultDialect() );
}
@@ -361,7 +354,7 @@
private void newPackage(final PackageDescr packageDescr) {
this.pkg = new Package( packageDescr.getName(),
this.configuration.getClassLoader() );
-
+
this.typeResolver.addImport( this.pkg.getName() + ".*" );
this.dialectRegistry.initAll( this );
@@ -373,12 +366,63 @@
final List imports = packageDescr.getImports();
for ( final Iterator it = imports.iterator(); it.hasNext(); ) {
ImportDescr importEntry = (ImportDescr) it.next();
- ImportDeclaration importDecl = new ImportDeclaration( importEntry.getTarget(), importEntry.isEvent() );
+ ImportDeclaration importDecl = new ImportDeclaration( importEntry.getTarget(),
+ importEntry.isEvent() );
pkg.addImport( importDecl );
- this.typeResolver.addImport( importDecl.getTarget() );
+ this.typeResolver.addImport( importDecl.getTarget() );
this.dialectRegistry.addImport( importDecl.getTarget() );
}
+ for ( TypeDeclarationDescr typeDescr : packageDescr.getDeclarations() ) {
+ TypeDeclaration type = new TypeDeclaration( typeDescr.getIdentifier() );
+
+ // is it a regular fact or an event?
+ String role = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_ROLE );
+ if ( role != null ) {
+ type.setRole( role );
+ }
+
+ // is it a POJO or a template?
+ String templateName = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_CLASS );
+ if( templateName != null ) {
+ type.setFormat( TypeDeclaration.Format.TEMPLATE );
+ FactTemplate template = this.pkg.getFactTemplate( templateName );
+ if( template != null ) {
+ type.setTypeTemplate( template );
+ } else {
+ this.results.add( new TypeDeclarationError( "Template not found '" + template + "' for type '" + type.getTypeName() + "'",
+ typeDescr.getLine() ) );
+ }
+ } else {
+ String className = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_CLASS );
+ if ( className == null ) {
+ className = type.getTypeName();
+ }
+ type.setFormat( TypeDeclaration.Format.POJO );
+ Class clazz;
+ try {
+ clazz = typeResolver.resolveType( className );
+ type.setTypeClass( clazz );
+ } catch ( final ClassNotFoundException e ) {
+ this.results.add( new TypeDeclarationError( "Class not found '" + className + "' for type '" + type.getTypeName() + "'",
+ typeDescr.getLine() ) );
+ }
+ }
+
+ String clockStrategy = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_CLOCK_STRATEGY );
+ if ( clockStrategy != null ) {
+ type.setClockStrategy( clockStrategy );
+ }
+ String timestamp = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_TIMESTAMP );
+ if ( timestamp != null ) {
+ type.setTimestampAttribute( timestamp );
+ }
+ String duration = typeDescr.getAttribute( TypeDeclarationDescr.ATTR_DURATION );
+ if ( duration != null ) {
+ type.setDurationAttribute( duration );
+ }
+ }
+
for ( final Iterator it = packageDescr.getFunctionImports().iterator(); it.hasNext(); ) {
String importEntry = ((FunctionImportDescr) it.next()).getTarget();
this.dialectRegistry.addStaticImport( importEntry );
@@ -395,12 +439,13 @@
try {
clazz = typeResolver.resolveType( className );
this.pkg.addGlobal( identifier,
- clazz );
+ clazz );
} catch ( final ClassNotFoundException e ) {
this.results.add( new GlobalError( identifier,
global.getLine() ) );
}
}
+
}
private void addFunction(final FunctionDescr functionDescr) {
@@ -494,7 +539,7 @@
public Dialect getDefaultDialect() {
return this.dialect;
- }
+ }
/**
* Return the ClassFieldExtractorCache, this should only be used internally, and is subject to change
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/TypeDeclarationError.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/TypeDeclarationError.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/compiler/TypeDeclarationError.java 2008-01-23 23:34:57 UTC (rev 18069)
@@ -0,0 +1,40 @@
+package org.drools.compiler;
+
+/*
+ * Copyright 2005 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class TypeDeclarationError extends DroolsError {
+ private String errorMessage;
+ private int[] line;
+
+ public TypeDeclarationError(final String errorMessage, final int line) {
+ this.errorMessage = errorMessage;
+ this.line = new int[] { line };
+ }
+
+ public int[] getErrorLines() {
+ return this.line;
+ }
+
+ public String getMessage() {
+ return this.errorMessage;
+ }
+
+ public String toString() {
+ return this.getMessage();
+ }
+
+}
\ No newline at end of file
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/lang/descr/TypeDeclarationDescr.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/lang/descr/TypeDeclarationDescr.java 2008-01-23 22:25:02 UTC (rev 18068)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/main/java/org/drools/lang/descr/TypeDeclarationDescr.java 2008-01-23 23:34:57 UTC (rev 18069)
@@ -20,6 +20,32 @@
import java.util.Map;
public class TypeDeclarationDescr extends BaseDescr {
+
+ /**
+ * The attribute key used to define what role the type assumes (fact, event)
+ */
+ public static final String ATTR_ROLE = "role";
+ /**
+ * The attribute key used to define what is the clock strategy used for that type
+ */
+ public static final String ATTR_CLOCK_STRATEGY = "clock_strategy";
+ /**
+ * The attribute key used to define what is the attribute to read the timestamp from
+ */
+ public static final String ATTR_TIMESTAMP = "timestamp_attribute";
+ /**
+ * The attribute key used to define what is the attribute to read the duration from
+ */
+ public static final String ATTR_DURATION = "duration_attribute";
+ /**
+ * The attribute key used to define what is the class name that implements the type
+ */
+ public static final String ATTR_CLASS = "class";
+ /**
+ * The attribute key used to define what is the template name that implements the type
+ */
+ public static final String ATTR_TEMPLATE = "template";
+
private static final long serialVersionUID = 400L;
private String identifier;
Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/Package.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/Package.java 2008-01-23 22:25:02 UTC (rev 18068)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/Package.java 2008-01-23 23:34:57 UTC (rev 18069)
@@ -82,6 +82,8 @@
private Map ruleFlows;
private PackageCompilationData packageCompilationData;
+
+ private Map<String, TypeDeclaration> typeDeclarations;
/** This is to indicate the the package has no errors during the compilation/building phase */
private boolean valid = true;
@@ -122,6 +124,7 @@
ClassLoader parentClassLoader) {
this.name = name;
this.imports = new HashMap<String, ImportDeclaration>();
+ this.typeDeclarations = new HashMap<String, TypeDeclaration>();
this.staticImports = Collections.EMPTY_SET;
this.rules = new LinkedHashMap();
this.ruleFlows = Collections.EMPTY_MAP;
@@ -149,6 +152,7 @@
stream.writeObject( this.name );
stream.writeObject( this.imports );
stream.writeObject( this.staticImports );
+ stream.writeObject( this.typeDeclarations );
stream.writeObject( this.functions );
stream.writeObject( this.factTemplates );
stream.writeObject( this.ruleFlows );
@@ -176,6 +180,7 @@
this.name = (String) stream.readObject();
this.imports = (Map<String, ImportDeclaration>) stream.readObject();
this.staticImports = (Set) stream.readObject();
+ this.typeDeclarations = (Map<String, TypeDeclaration>) stream.readObject();
this.functions = (List) stream.readObject();
this.factTemplates = (Map) stream.readObject();
this.ruleFlows = (Map) stream.readObject();
@@ -216,6 +221,18 @@
public Map<String, ImportDeclaration> getImports() {
return this.imports;
}
+
+ public void addTypeDeclaration( final TypeDeclaration typeDecl ) {
+ this.typeDeclarations.put( typeDecl.getTypeName(), typeDecl );
+ }
+
+ public void removeTypeDeclaration( final String type ) {
+ this.typeDeclarations.remove( type );
+ }
+
+ public Map<String, TypeDeclaration> getTypeDeclarations() {
+ return this.typeDeclarations;
+ }
public void addStaticImport(final String functionImport) {
if ( this.staticImports == Collections.EMPTY_SET ) {
Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java 2008-01-23 23:34:57 UTC (rev 18069)
@@ -0,0 +1,240 @@
+/*
+ * Copyright 2008 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jan 23, 2008
+ */
+
+package org.drools.rule;
+
+import java.util.Map;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.facttemplates.FactTemplate;
+
+/**
+ * The type declaration class stores all type's metadata
+ * declared in source files.
+ *
+ * @author etirelli
+ */
+public class TypeDeclaration {
+
+ public static enum Role {
+ FACT("fact"),
+ EVENT("event");
+
+ private static Map<String, Role> roles;
+ Role( String text ) {
+ Role.roles.put( text, this );
+ }
+
+ public static Role parseRole( String roleStr ) {
+ return roles.get( roleStr.toLowerCase() );
+ }
+ }
+
+ public static enum Format {
+ POJO("pojo"),
+ TEMPLATE("template");
+
+ private static Map<String, Format> formats;
+ Format( String format ) {
+ Format.formats.put( format, this );
+ }
+
+ public static Format parseFormat( String format ) {
+ return formats.get( format.toLowerCase() );
+ }
+ }
+
+ public static enum ClockStrategy {
+ NONE("none"),
+ PSEUDO("pseudo"),
+ SYSTEM("system"),
+ HEARTBEAT("heartbeat"),
+ ATTRIBUTE("attribute");
+
+ public static final String ATTR_NAME = "clock_strategy";
+
+ private static Map<String, ClockStrategy> strategies;
+ ClockStrategy( String strategy ) {
+ ClockStrategy.strategies.put( strategy, this );
+ }
+
+ public static ClockStrategy parseStrategy( String strategy ) {
+ return strategies.get( strategy.toLowerCase() );
+ }
+ }
+
+ private final String typeName;
+ private Role role;
+ private Format format;
+ private ClockStrategy clockStrategy;
+ private String timestampAttribute;
+ private String durationAttribute;
+ private Class<?> typeClass;
+ private FactTemplate typeTemplate;
+
+ public TypeDeclaration( String typeName ) {
+ this.typeName = typeName;
+ this.role = Role.FACT;
+ this.format = Format.POJO;
+ this.clockStrategy = ClockStrategy.NONE;
+ this.durationAttribute = null;
+ this.timestampAttribute = null;
+ this.typeClass = null;
+ this.typeTemplate = null;
+ }
+
+ /**
+ * @return the type
+ */
+ public String getTypeName() {
+ return typeName;
+ }
+
+ /**
+ * @return the category
+ */
+ public Role getRole() {
+ return role;
+ }
+
+ /**
+ * @param role the category to set
+ */
+ public void setRole(Role role) {
+ this.role = role;
+ }
+
+ /**
+ * @param role the category to set
+ */
+ public void setRole(String role) {
+ Role r = Role.parseRole( role );
+ if( r == null ) {
+ throw new RuntimeDroolsException( "Unknown role '"+role+"' declared for type '"+this.typeName+"'");
+ }
+ this.role = r;
+ }
+
+ /**
+ * @return the format
+ */
+ public Format getFormat() {
+ return format;
+ }
+
+ /**
+ * @param format the format to set
+ */
+ public void setFormat(Format format) {
+ this.format = format;
+ }
+
+ /**
+ * @param format the format to set
+ */
+ public void setFormat(String format) {
+ Format f = Format.parseFormat( format );
+ if( f == null ) {
+ throw new RuntimeDroolsException( "Unknown format '"+format+"' declared for type '"+this.typeName+"'");
+ }
+ this.format = f;
+ }
+
+ /**
+ * @return the clockStrategy
+ */
+ public ClockStrategy getClockStrategy() {
+ return clockStrategy;
+ }
+
+ /**
+ * @param clockStrategy the clockStrategy to set
+ */
+ public void setClockStrategy(ClockStrategy clockStrategy) {
+ this.clockStrategy = clockStrategy;
+ }
+
+ /**
+ * @param clockStrategy the clockStrategy to set
+ */
+ public void setClockStrategy(String clockStrategy) {
+ ClockStrategy c = ClockStrategy.parseStrategy( clockStrategy );
+ if( c == null ) {
+ throw new RuntimeDroolsException( "Unknown clock strategy '"+clockStrategy+"' declared for type '"+this.typeName+"'");
+ }
+ this.clockStrategy = c;
+ }
+
+ /**
+ * @return the timestampAttribute
+ */
+ public String getTimestampAttribute() {
+ return timestampAttribute;
+ }
+
+ /**
+ * @param timestampAttribute the timestampAttribute to set
+ */
+ public void setTimestampAttribute(String timestampAttribute) {
+ this.timestampAttribute = timestampAttribute;
+ }
+
+ /**
+ * @return the durationAttribute
+ */
+ public String getDurationAttribute() {
+ return durationAttribute;
+ }
+
+ /**
+ * @param durationAttribute the durationAttribute to set
+ */
+ public void setDurationAttribute(String durationAttribute) {
+ this.durationAttribute = durationAttribute;
+ }
+
+ /**
+ * @return the typeClass
+ */
+ public Class< ? > getTypeClass() {
+ return typeClass;
+ }
+
+ /**
+ * @param typeClass the typeClass to set
+ */
+ public void setTypeClass(Class< ? > typeClass) {
+ this.typeClass = typeClass;
+ }
+
+ /**
+ * @return the typeTemplate
+ */
+ public FactTemplate getTypeTemplate() {
+ return typeTemplate;
+ }
+
+ /**
+ * @param typeTemplate the typeTemplate to set
+ */
+ public void setTypeTemplate(FactTemplate typeTemplate) {
+ this.typeTemplate = typeTemplate;
+ }
+
+
+}
More information about the jboss-svn-commits
mailing list