use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class AbstractTraitFactory method buildClassDefinition.
public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
ClassFieldInspector inspector = new ClassFieldInspector(klazz);
ClassFieldAccessorStore store = getClassFieldAccessorStore();
ClassDefinition def;
if (!klazz.isInterface()) {
String className = wrapperClass.getName();
String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
String[] interfaces = new String[klazz.getInterfaces().length + 1];
for (int j = 0; j < klazz.getInterfaces().length; j++) {
interfaces[j] = klazz.getInterfaces()[j].getName();
}
interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
def = new ClassDefinition(className, superClass, interfaces);
def.setDefinedClass(wrapperClass);
Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
def.setTraitable(true, tbl != null && tbl.logical());
Map<String, Field> fields = inspector.getFieldTypesField();
for (Field f : fields.values()) {
if (f != null) {
FieldDefinition fld = new FieldDefinition();
fld.setName(f.getName());
fld.setTypeName(f.getType().getName());
fld.setInherited(true);
ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
fld.setReadWriteAccessor(accessor);
if (inspector.getGetterMethods().containsKey(f.getName())) {
fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
}
if (inspector.getSetterMethods().containsKey(f.getName())) {
fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
}
def.addField(fld);
}
}
} else {
String className = klazz.getName();
String superClass = Object.class.getName();
String[] interfaces = new String[klazz.getInterfaces().length];
for (int j = 0; j < klazz.getInterfaces().length; j++) {
interfaces[j] = klazz.getInterfaces()[j].getName();
}
def = new ClassDefinition(className, superClass, interfaces);
def.setDefinedClass(klazz);
Map<String, Method> properties = inspector.getGetterMethods();
for (String key : properties.keySet()) {
Method m = properties.get(key);
if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(key)) {
FieldDefinition fld = new FieldDefinition();
fld.setName(getterToFieldName(m.getName()));
fld.setTypeName(m.getReturnType().getName());
fld.setInherited(true);
ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
fld.setReadWriteAccessor(accessor);
fld.setGetterName(m.getName());
fld.setSetterName(inspector.getSetterMethods().get(key).getName());
def.addField(fld);
}
}
}
return def;
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class TraitRegistry method bind.
private BitSet bind(String trait, String traitable) throws UnsupportedOperationException {
ClassDefinition traitDef = getTrait(trait);
if (traitDef == null) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a trait ");
}
ClassDefinition traitableDef = getTraitable(traitable);
if (traitableDef == null) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a traitable ");
}
int j = 0;
BitSet bitmask = new BitSet(traitDef.getFields().size());
for (FactField field : traitDef.getFields()) {
String alias = ((FieldDefinition) field).resolveAlias();
FieldDefinition concreteField = traitableDef.getFieldByAlias(alias);
if (concreteField != null) {
if (!traitableDef.isFullTraiting() && !concreteField.getType().isAssignableFrom(field.getType())) {
throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " :" + " trait field " + field.getName() + ":" + ((FieldDefinition) field).getTypeName() + " is incompatible with" + " concrete hard field " + concreteField.getName() + ":" + concreteField.getTypeName() + ". Consider enabling logical traiting" + " mode using @Traitable( logical = true )");
}
bitmask.set(j);
}
j++;
}
return bitmask;
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class TraitRegistry method mergeHierarchy.
private static HierarchyEncoder<String> mergeHierarchy(TraitRegistry first, TraitRegistry second) {
for (String traitName : second.getHierarchy().getSortedMembers()) {
ClassDefinition trait = second.traits.get(traitName);
List<String> parentTraits = new ArrayList<String>();
for (String candidateIntf : trait.getInterfaces()) {
if (first.getHierarchy().getCode(candidateIntf) != null) {
parentTraits.add(candidateIntf);
}
}
first.getHierarchy().encode(traitName, parentTraits);
}
return first.getHierarchy();
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class TraitHelper method makeTraitable.
private <K> TraitableBean makeTraitable(K core, TraitFactory builder, boolean logical, Activation activation) {
boolean needsWrapping = !(core instanceof TraitableBean);
ClassDefinition coreDef = lookupClassDefinition(core);
TraitableBean<K, ? extends TraitableBean> inner = needsWrapping ? builder.asTraitable(core, coreDef) : (TraitableBean<K, ? extends TraitableBean>) core;
if (needsWrapping) {
InternalFactHandle h = (InternalFactHandle) lookupFactHandle(core);
WorkingMemoryEntryPoint ep = h != null ? h.getEntryPoint() : ((StatefulKnowledgeSessionImpl) workingMemory).getEntryPoint("DEFAULT");
ObjectTypeConfigurationRegistry reg = ep.getObjectTypeConfigurationRegistry();
ObjectTypeConf coreConf = reg.getObjectTypeConf(ep.getEntryPoint(), core);
ObjectTypeConf innerConf = reg.getObjectTypeConf(ep.getEntryPoint(), inner);
if (coreConf.isTMSEnabled()) {
innerConf.enableTMS();
}
if (inner._getFieldTMS() != null && inner._getFieldTMS().needsInit()) {
inner._getFieldTMS().init(workingMemory);
}
} else {
TraitFieldTMS ftms = inner._getFieldTMS();
if (ftms != null) {
FactHandle handle = lookupFactHandle(inner);
if (handle == null) {
handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
}
if (ftms.needsInit()) {
ftms.init(workingMemory);
}
}
}
return inner;
}
use of org.drools.core.factmodel.ClassDefinition in project drools by kiegroup.
the class KieModuleMavenTest method testKieModulePojoDependencies.
@Test
public void testKieModulePojoDependencies() throws Exception {
KieServices ks = KieServices.Factory.get();
// Create and deploy a standard mavenized pojo jar
String pojoNS = "org.kie.pojos";
ReleaseId pojoID = KieServices.Factory.get().newReleaseId(pojoNS, "pojojar", "2.0.0");
String className = "Message";
ClassDefinition def = new ClassDefinition(pojoNS + "." + className);
def.addField(new FieldDefinition("text", String.class.getName()));
byte[] messageClazz = ClassBuilderFactory.getDefaultBeanClassBuilder().buildClass(def, null);
MemoryFileSystem mfs = new MemoryFileSystem();
mfs.write(pojoNS.replace('.', '/') + "/" + className + ".class", messageClazz);
byte[] pomContent = generatePomXml(pojoID).getBytes();
mfs.write("META-INF/maven/" + pojoID.getGroupId() + "/" + pojoID.getArtifactId() + "/pom.xml", pomContent);
mfs.write("META-INF/maven/" + pojoID.getGroupId() + "/" + pojoID.getArtifactId() + "/pom.properties", generatePomProperties(pojoID).getBytes());
byte[] pojojar = mfs.writeAsBytes();
MavenRepository.getMavenRepository().installArtifact(pojoID, pojojar, pomContent);
// Create and deploy a kjar that depends on the previous pojo jar
String kjarNS = "org.kie.test1";
ReleaseId kjarID = KieServices.Factory.get().newReleaseId(kjarNS, "rkjar", "1.0.0");
String rule = getRule(kjarNS, pojoNS, "R1");
String pom = generatePomXml(kjarID, pojoID);
byte[] rkjar = createKJar(ks, kjarID, pom, rule);
KieModule kmodule = deployJar(ks, rkjar);
assertNotNull(kmodule);
KieContainer kContainer = ks.newKieContainer(kjarID);
KieSession kSession = kContainer.newKieSession();
List<?> list = new ArrayList<Object>();
kSession.setGlobal("list", list);
kSession.fireAllRules();
assertEquals(1, list.size());
}
Aggregations