use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationDescrVisitor method visit.
public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
for (TypeDeclarationDescr typeDeclaration : typeDeclarationDescrs) {
Import objectImport = data.getImportByName(typeDeclaration.getTypeName());
String objectTypeName;
if (objectImport == null) {
objectTypeName = typeDeclaration.getTypeName();
} else {
objectTypeName = objectImport.getName();
}
ObjectType objectType = this.data.getObjectTypeByFullName(objectTypeName);
if (objectType == null) {
objectType = new ObjectType(typeDeclaration);
objectType.setName(typeDeclaration.getTypeName());
objectType.setFullName(typeDeclaration.getTypeName());
data.add(objectType);
}
for (String fieldName : typeDeclaration.getFields().keySet()) {
Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
if (field == null) {
field = ObjectTypeFactory.createField(typeDeclaration.getFields().get(fieldName), fieldName, objectType);
field.setFieldType(typeDeclaration.getFields().get(fieldName).getPattern().getObjectType());
data.add(field);
}
}
for (AnnotationDescr annDescr : typeDeclaration.getAnnotations()) {
Map<String, Object> values = typeDeclaration.getAnnotation(annDescr.getName()).getValueMap();
for (String value : values.keySet()) {
objectType.getMetadata().put(annDescr.getName(), value);
}
}
}
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class ModelBuilderImpl method registerTypeDeclarations.
private void registerTypeDeclarations(Collection<CompositePackageDescr> packages) {
for (CompositePackageDescr packageDescr : packages) {
InternalKnowledgePackage pkg = getOrCreatePackageRegistry(packageDescr).getPackage();
for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
normalizeAnnotations(typeDescr, pkg.getTypeResolver(), false);
TypeDeclaration type = new TypeDeclaration(typeDescr.getTypeName());
type.setResource(typeDescr.getResource());
TypeDeclarationFactory.processAnnotations(typeDescr, type);
pkg.addTypeDeclaration(type);
}
}
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools-wb by kiegroup.
the class FactModelPersistence method toModel.
private static List<FactMetaModel> toModel(String drl) throws DroolsParserException {
Preconditions.checkNotNull(drl, "The string representing DRL can't be null!");
if (drl.startsWith("#advanced") || drl.startsWith("//advanced")) {
throw new DroolsParserException("Using advanced editor");
}
final DrlParser parser = new DrlParser();
final StringReader reader = new StringReader(drl);
final PackageDescr pkg = parser.parse(reader);
if (parser.hasErrors()) {
throw new DroolsParserException("The model drl " + drl + " is not valid");
}
if (pkg == null) {
return emptyList();
}
final List<TypeDeclarationDescr> types = pkg.getTypeDeclarations();
final List<FactMetaModel> list = new ArrayList<FactMetaModel>(types.size());
for (final TypeDeclarationDescr td : types) {
final FactMetaModel mm = new FactMetaModel();
mm.setName(td.getTypeName());
mm.setSuperType(td.getSuperTypeName());
final Map<String, TypeFieldDescr> fields = td.getFields();
for (Map.Entry<String, TypeFieldDescr> en : fields.entrySet()) {
final String fieldName = en.getKey();
final TypeFieldDescr descr = en.getValue();
final FieldMetaModel fm = new FieldMetaModel(fieldName, descr.getPattern().getObjectType());
mm.getFields().add(fm);
}
for (final AnnotationDescr descr : td.getAnnotations()) {
final String annotationName = descr.getName();
final Map<String, String> values = extractStringValues(descr);
final AnnotationMetaModel am = new AnnotationMetaModel(annotationName, values);
mm.getAnnotations().add(am);
}
list.add(mm);
}
return list;
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class ClassHierarchyManager method inheritFields.
public void inheritFields(PackageRegistry pkgRegistry, AbstractClassTypeDeclarationDescr typeDescr, Collection<AbstractClassTypeDeclarationDescr> sortedTypeDescriptors, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs) {
TypeDeclarationDescr tDescr = (TypeDeclarationDescr) typeDescr;
boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, pkgRegistry);
boolean inferFields = !isNovel && typeDescr.getFields().isEmpty();
for (QualifiedName qname : tDescr.getSuperTypes()) {
// descriptor needs fields inherited from superclass
mergeInheritedFields(tDescr, unresolvedTypes, unprocessableDescrs, pkgRegistry.getTypeResolver());
}
if (inferFields) {
// not novel, but only an empty declaration was provided.
// after inheriting the fields from supertypes, now we fill in the locally declared fields
Class existingClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkgRegistry);
buildDescrsFromFields(existingClass, tDescr, pkgRegistry, tDescr.getFields());
}
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationBuilder method mergeTypeDescriptors.
private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
boolean isDef1 = isDefinition(prev);
boolean isDef2 = isDefinition(descr);
if (isDef1 && isDef2) {
return false;
}
if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
return false;
}
if (prev.getSuperTypes().isEmpty()) {
for (QualifiedName qn : descr.getSuperTypes()) {
((TypeDeclarationDescr) prev).addSuperType(qn);
}
}
if (prev.getFields().isEmpty()) {
for (String fieldName : descr.getFields().keySet()) {
prev.addField(descr.getFields().get(fieldName));
}
}
for (AnnotationDescr ad : descr.getAnnotations()) {
prev.addQualifiedAnnotation(ad);
}
for (AnnotationDescr ad : prev.getAnnotations()) {
if (!descr.getAnnotations().contains(ad)) {
descr.addQualifiedAnnotation(ad);
}
}
return true;
}
Aggregations