Permalink
Browse files

add classes generated from type declarations to the kjar

  • Loading branch information...
1 parent b1933d3 commit f7d6c34adfd40e69278daa0ff1f7e4afeeb852ce @mariofusco mariofusco committed Dec 20, 2012
@@ -41,6 +41,7 @@
import org.drools.factmodel.EnumClassDefinition;
import org.drools.factmodel.EnumLiteralDefinition;
import org.drools.factmodel.FieldDefinition;
+import org.drools.factmodel.GeneratedFact;
import org.drools.factmodel.traits.Thing;
import org.drools.factmodel.traits.Trait;
import org.drools.factmodel.traits.TraitFactory;
@@ -663,7 +664,7 @@ public void addProcessFromXml( Resource resource ) {
throw new RuntimeException( "Unable to instantiate a process builder", processBuilderCreationFailure );
}
- if ( ResourceType.DRF.equals( ( (InternalResource) resource ).getResourceType() ) ) {
+ if ( ResourceType.DRF.equals( resource.getResourceType() ) ) {
this.results.add( new DeprecatedResourceTypeWarning(resource, "RF") );
}
@@ -2291,17 +2292,19 @@ private void updateTraitDefinition( TypeDeclaration type, Class concrete ) {
* @return
*/
private boolean isNovelClass( AbstractClassTypeDeclarationDescr typeDescr ) {
+ return getExistingDeclarationClass( typeDescr ) == null;
+ }
+
+ private Class<?> getExistingDeclarationClass( AbstractClassTypeDeclarationDescr typeDescr ) {
+ PackageRegistry reg = this.pkgRegistryMap.get( typeDescr.getNamespace() );
+ if (reg == null) {
+ return null;
+ }
+ String availableName = typeDescr.getType().getFullName();
try {
- PackageRegistry reg = this.pkgRegistryMap.get( typeDescr.getNamespace() );
- if ( reg != null ) {
- String availableName = typeDescr.getType().getFullName();
- reg.getTypeResolver().resolveType( availableName );
- return false;
- } else {
- return false;
- }
- } catch (ClassNotFoundException cnfe) {
- return true;
+ return reg.getTypeResolver().resolveType( availableName );
+ } catch (ClassNotFoundException e) {
+ return null;
}
}
@@ -2477,14 +2480,15 @@ private void generateDeclaredBean( AbstractClassTypeDeclarationDescr typeDescr,
}
// check whether it is necessary to build the class or not
- type.setNovel( isNovelClass( typeDescr ) );
+ Class<?> existingDeclarationClass = getExistingDeclarationClass( typeDescr );
+ type.setNovel( existingDeclarationClass == null );
// attach the class definition, it will be completed later
type.setTypeClassDef( def );
//if is not new, search the already existing declaration and
//compare them o see if they are at least compatibles
- if ( ! type.isNovel() ) {
+ if ( !type.isNovel() ) {
TypeDeclaration previousTypeDeclaration = this.pkgRegistryMap.get( typeDescr.getNamespace() ).getPackage().getTypeDeclaration( typeDescr.getTypeName() );
try {
@@ -2501,7 +2505,7 @@ private void generateDeclaredBean( AbstractClassTypeDeclarationDescr typeDescr,
//to the behavior previous these changes
if ( previousTypeDeclaration == null ) {
// new declarations of a POJO can't declare new fields
- if (!type.getTypeClassDef().getFields().isEmpty()){
+ if (!GeneratedFact.class.isAssignableFrom(existingDeclarationClass) && !type.getTypeClassDef().getFields().isEmpty()) {
type.setValid(false);
this.results.add(new TypeDeclarationError(typeDescr, "New declaration of "+typeDescr.getType().getFullName()
+" can't declare new fields"));
@@ -2538,7 +2542,7 @@ private void generateDeclaredBean( AbstractClassTypeDeclarationDescr typeDescr,
} else {
//if the declaration is novel, then it is a DEFINITION
- type.setNature( TypeDeclaration.Nature.DEFINITION );
+ type.setNature(TypeDeclaration.Nature.DEFINITION);
}
generateDeclaredBean( typeDescr,
@@ -1,15 +1,18 @@
package org.kie.builder.impl;
+import org.drools.builder.impl.KnowledgeBuilderImpl;
import org.drools.commons.jci.compilers.CompilationResult;
import org.drools.commons.jci.compilers.EclipseJavaCompiler;
import org.drools.commons.jci.compilers.EclipseJavaCompilerSettings;
import org.drools.commons.jci.problems.CompilationProblem;
import org.drools.commons.jci.readers.DiskResourceReader;
import org.drools.commons.jci.readers.ResourceReader;
+import org.drools.compiler.PackageRegistry;
import org.drools.compiler.io.memory.MemoryFileSystem;
import org.drools.core.util.StringUtils;
import org.drools.kproject.ReleaseIdImpl;
import org.drools.kproject.models.KieModuleModelImpl;
+import org.drools.rule.JavaDialectRuntimeData;
import org.drools.xml.MinimalPomParser;
import org.drools.xml.PomModel;
import org.kie.KieBaseConfiguration;
@@ -26,6 +29,8 @@
import org.kie.builder.Message.Level;
import org.kie.builder.ReleaseId;
import org.kie.builder.Results;
+import org.kie.definition.KiePackage;
+import org.kie.definition.type.FactType;
import org.kie.io.Resource;
import org.kie.io.ResourceType;
@@ -36,6 +41,7 @@
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -129,19 +135,43 @@ public KieBuilder buildAll() {
}
}
- buildKieModule(kModule, results);
+ if ( buildKieModule(kModule, results) ) {
+ addTypeDeclarationClassesToTrg();
+ }
}
return this;
}
- public static void buildKieModule(InternalKieModule kModule, ResultsImpl messages) {
+ private void addTypeDeclarationClassesToTrg() {
+ KieModuleModel kieModuleModel = kModule.getKieModuleModel();
+ for (String kieBaseNames : kieModuleModel.getKieBaseModels().keySet()) {
+ KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl)kModule.getKnowledgeBuilderForKieBase(kieBaseNames);
+ Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageBuilder().getPackageRegistry();
+
+ for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
+ PackageRegistry pkgRegistry = pkgRegistryMap.get(kPkg.getName());
+ JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData)pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
+
+ for (FactType factType : kPkg.getFactTypes()) {
+ String className = factType.getName();
+ String internalName = className.replace('.', '/') + ".class";
+ byte[] bytes = runtimeData.getStore().get(internalName);
+ trgMfs.write( internalName, bytes, true );
+ }
+ }
+ }
+ }
+
+ public static boolean buildKieModule(InternalKieModule kModule, ResultsImpl messages) {
KieModuleKieProject kProject = new KieModuleKieProject( kModule, null );
kProject.init();
kProject.verify(messages);
if ( messages.filterMessages( Level.ERROR ).isEmpty()) {
KieServices.Factory.get().getRepository().addKieModule( kModule );
+ return true;
}
+ return false;
}
private void addKBasesFilesToTrg() {
@@ -1,10 +1,6 @@
package org.drools.compiler;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
import junit.framework.Assert;
-
import org.drools.definitions.impl.KnowledgePackageImp;
import org.drools.rule.TypeDeclaration;
import org.junit.Test;
@@ -18,6 +14,10 @@
import org.kie.io.ResourceFactory;
import org.kie.io.ResourceType;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
public class TypeDeclarationTest {
@Test
@@ -236,7 +236,7 @@ public void testClashingTypeDeclarationInDifferentResources() {
ResourceType.DRL );
if (!kbuilder.hasErrors() ) {
- fail( kbuilder.getErrors().toString() );
+ fail( "An error should have been generated, redefinition of ClassA is not allowed" );
}
@@ -470,10 +470,11 @@ protected void buildDynamicPropertyMap( ClassWriter cw, ClassDefinition def ) {
protected void buildClassHeader(ClassVisitor cw,
ClassDefinition classDef) {
String[] original = classDef.getInterfaces();
- String[] interfaces = new String[original.length];
+ String[] interfaces = new String[original.length + 1];
for ( int i = 0; i < original.length; i++ ) {
interfaces[i] = BuildUtils.getInternalType( original[i] );
}
+ interfaces[original.length] = BuildUtils.getInternalType( GeneratedFact.class.getName() );
int classModifiers = Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER;
if ( classDef.isAbstrakt() ) {
@@ -0,0 +1,4 @@
+package org.drools.factmodel;
+
+public interface GeneratedFact {
+}
@@ -19,6 +19,7 @@
import org.drools.factmodel.BuildUtils;
import org.drools.factmodel.ClassDefinition;
import org.drools.factmodel.FieldDefinition;
+import org.drools.factmodel.GeneratedFact;
import org.mvel2.asm.AnnotationVisitor;
import org.mvel2.asm.ClassWriter;
import org.mvel2.asm.MethodVisitor;
@@ -44,15 +45,17 @@
if ( Object.class.getName().equals( classDef.getSuperClass() ) ) {
String[] tmp = BuildUtils.getInternalTypes( classDef.getInterfaces() );
- intfaces = new String[ tmp.length + 1 ];
+ intfaces = new String[ tmp.length + 2 ];
System.arraycopy( tmp, 0, intfaces, 0, tmp.length );
intfaces[ tmp.length ] = Type.getInternalName( Serializable.class );
+ intfaces[ tmp.length + 1 ] = Type.getInternalName( GeneratedFact.class );
} else {
String[] tmp = BuildUtils.getInternalTypes( classDef.getInterfaces() );
- intfaces = new String[ tmp.length + 2 ];
+ intfaces = new String[ tmp.length + 3 ];
System.arraycopy( tmp, 0, intfaces, 0, tmp.length );
intfaces[ tmp.length ] = BuildUtils.getInternalType( classDef.getSuperClass() );
intfaces[ tmp.length + 1 ] = Type.getInternalName( Serializable.class );
+ intfaces[ tmp.length + 2 ] = Type.getInternalName( GeneratedFact.class );
}
cw.visit( V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
@@ -16,6 +16,14 @@
package org.drools.rule;
+import org.drools.RuntimeDroolsException;
+import org.drools.core.util.KeyStoreHelper;
+import org.drools.core.util.StringUtils;
+import org.drools.spi.Constraint;
+import org.drools.spi.Wireable;
+import org.kie.internal.utils.CompositeClassLoader;
+import org.kie.internal.utils.FastClassLoader;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
@@ -37,21 +45,11 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
-import org.drools.RuntimeDroolsException;
-import org.drools.base.ClassFieldAccessorStore;
-import org.drools.core.util.KeyStoreHelper;
-import org.drools.core.util.StringUtils;
-import org.drools.spi.Constraint;
-import org.drools.spi.Wireable;
-import org.kie.internal.utils.CompositeClassLoader;
-import org.kie.internal.utils.FastClassLoader;
-
public class JavaDialectRuntimeData
implements
DialectRuntimeData,
@@ -88,7 +86,7 @@ public Object run() {
public JavaDialectRuntimeData() {
this.invokerLookups = new HashMap<String, Object>();
- this.classLookups = new HashMap();
+ this.classLookups = new HashMap<String,byte[]>();
this.store = new HashMap<String, byte[]>();
this.dirty = false;
}
@@ -132,8 +130,7 @@ public void writeExternal( ObjectOutput stream ) throws IOException {
}
stream.writeInt( this.classLookups.size() );
- for (Iterator it = this.classLookups.entrySet().iterator(); it.hasNext();) {
- Entry<String, byte[]> entry = (Entry<String,byte[]>) it.next();
+ for (Entry<String, byte[]> entry : this.classLookups.entrySet()) {
stream.writeObject( entry.getKey() );
stream.writeObject( entry.getValue() );
}
@@ -1,6 +1,5 @@
/target
/local
-/bin
# Eclipse, Netbeans and IntelliJ files
/.*
View
@@ -1,6 +1,5 @@
/target
/local
-/bin
# Eclipse, Netbeans and IntelliJ files
/.*
@@ -9,16 +8,3 @@
/*.ipr
/*.iws
/*.iml
-
-# Test logs and other files
-/*.tlog
-/*.db
-
-# The META-INF directory is generated by the maven-felix-plugin
-/META-INF
-
-# Test data
-btm*.tlog
-/settings-db.xml
-/JPADroolsFlow.*.db
-/lib-jdbc/
@@ -43,9 +43,7 @@ protected InternalKieModule createKieJar(KieServices ks, ReleaseId releaseId, St
return ( InternalKieModule ) kieBuilder.getKieModule();
}
- protected InternalKieModule createKieJarWithClass(KieServices ks, ReleaseId releaseId, int value, int factor, ReleaseId... dependencies) throws IOException {
- KieFileSystem kieFileSystem = ks.newKieFileSystem();
-
+ protected InternalKieModule createKieJarWithClass(KieServices ks, ReleaseId releaseId, boolean useTypeDeclaration, int value, int factor, ReleaseId... dependencies) throws IOException {
KieModuleModel kproj = ks.newKieModuleModel();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1")
@@ -56,11 +54,19 @@ protected InternalKieModule createKieJarWithClass(KieServices ks, ReleaseId rele
.setType(KieSessionModel.KieSessionType.STATEFUL)
.setClockType( ClockTypeOption.get("realtime") );
+ KieFileSystem kieFileSystem = ks.newKieFileSystem();
kieFileSystem
.writeKModuleXML(kproj.toXML())
- .writePomXML(getPom(releaseId, dependencies))
- .write("src/main/resources/" + kieBaseModel1.getName() + "/rule1.drl", createDRLForJavaSource(value))
- .write("src/main/java/org/kie/test/Bean.java", createJavaSource(factor));
+ .writePomXML(getPom(releaseId, dependencies));
+
+ if (useTypeDeclaration) {
+ kieFileSystem
+ .write("src/main/resources/" + kieBaseModel1.getName() + "/rule1.drl", createDRLWithTypeDeclaration(value, factor));
+ } else {
+ kieFileSystem
+ .write("src/main/resources/" + kieBaseModel1.getName() + "/rule1.drl", createDRLForJavaSource(value))
+ .write("src/main/java/org/kie/test/Bean.java", createJavaSource(factor));
+ }
KieBuilder kieBuilder = ks.newKieBuilder(kieFileSystem);
assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
@@ -118,9 +124,8 @@ private String createJavaSource(int factor) {
private String createDRLForJavaSource(int value) {
return "package org.kie.test\n" +
- //"import org.kie.test.Bean;\n" +
"global java.util.List list\n" +
- "rule Init\n" +
+ "rule Init salience 100\n" +
"when\n" +
"then\n" +
"insert( new Bean(" + value + ") );\n" +
@@ -132,4 +137,23 @@ private String createDRLForJavaSource(int value) {
" list.add( $b.getValue() );\n" +
"end\n";
}
+
+ private String createDRLWithTypeDeclaration(int value, int factor) {
+ return "package org.kie.test\n" +
+ "global java.util.List list\n" +
+ "declare Bean\n" +
+ " value : int\n" +
+ "end\n" +
+ "rule Init salience 100\n" +
+ "when\n" +
+ "then\n" +
+ "insert( new Bean(" + value + ") );\n" +
+ "end\n" +
+ "rule R1\n" +
+ "when\n" +
+ " $b : Bean()\n" +
+ "then\n" +
+ " list.add( $b.getValue() * " + factor + " );\n" +
+ "end\n";
+ }
}
Oops, something went wrong.

0 comments on commit f7d6c34

Please sign in to comment.