Search in sources :

Example 1 with ObjectType

use of org.drools.verifier.components.ObjectType in project drools by kiegroup.

the class PackageDescrVisitor method visitImports.

private void visitImports(List<ImportDescr> importDescrs) throws IOException, ClassNotFoundException {
    HashSet<String> imports = new HashSet<String>();
    for (ImportDescr i : importDescrs) {
        String fullName = i.getTarget();
        String name = fullName.substring(fullName.lastIndexOf(".") + 1);
        imports.add(fullName);
        Import objectImport = new Import(i, rulePackage);
        objectImport.setName(fullName);
        objectImport.setShortName(name);
        data.add(objectImport);
        ObjectType objectType = this.data.getObjectTypeByFullName(fullName);
        if (objectType == null) {
            objectType = new ObjectType(i);
        }
        objectType.setName(name);
        objectType.setFullName(fullName);
        data.add(objectType);
    }
    PackageHeaderLoader packageHeaderLoader = new PackageHeaderLoader(imports, jars);
    for (String factTypeName : packageHeaderLoader.getClassNames()) {
        String name = factTypeName.substring(factTypeName.lastIndexOf(".") + 1);
        Collection<String> fieldNames = packageHeaderLoader.getFieldNames(factTypeName);
        for (String fieldName : fieldNames) {
            ObjectType objectType = this.data.getObjectTypeByObjectTypeNameAndPackageName(name, rulePackage.getName());
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(objectType.getDescr(), fieldName, objectType);
                field.setFieldType(packageHeaderLoader.getFieldType(objectType.getName(), fieldName));
                data.add(field);
            }
        }
    }
}
Also used : PackageHeaderLoader(org.drools.verifier.jarloader.PackageHeaderLoader) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) HashSet(java.util.HashSet)

Example 2 with ObjectType

use of org.drools.verifier.components.ObjectType 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);
            }
        }
    }
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 3 with ObjectType

use of org.drools.verifier.components.ObjectType in project drools by kiegroup.

the class VerifierTest method testFactTypesFromJar.

@Test
public void testFactTypesFromJar() {
    VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder();
    // Check that the builder works.
    assertFalse(vBuilder.hasErrors());
    assertEquals(0, vBuilder.getErrors().size());
    Verifier verifier = vBuilder.newVerifier();
    try {
        JarInputStream jar = new JarInputStream(this.getClass().getResourceAsStream("model.jar"));
        verifier.addObjectModel(jar);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    verifier.addResourcesToVerify(new ClassPathResource("imports.drl", Verifier.class), ResourceType.DRL);
    assertFalse(verifier.hasErrors());
    assertEquals(0, verifier.getErrors().size());
    boolean works = verifier.fireAnalysis();
    assertTrue(works);
    VerifierReport result = verifier.getResult();
    Collection<ObjectType> objectTypes = result.getVerifierData().getAll(VerifierComponentType.OBJECT_TYPE);
    assertNotNull(objectTypes);
    assertEquals(3, objectTypes.size());
    Collection<Field> fields = result.getVerifierData().getAll(VerifierComponentType.FIELD);
    assertNotNull(fields);
    assertEquals(10, fields.size());
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) JarInputStream(java.util.jar.JarInputStream) VerifierReport(org.drools.verifier.data.VerifierReport) VerifierBuilder(org.drools.verifier.builder.VerifierBuilder) IOException(java.io.IOException) ClassPathResource(org.drools.core.io.impl.ClassPathResource) Test(org.junit.Test)

Example 4 with ObjectType

use of org.drools.verifier.components.ObjectType in project drools by kiegroup.

the class VerifierTestStandalone method main.

public static final void main(String[] args) {
    try {
        Collection<String> fileNames = new ArrayList<String>();
        // Test data
        // fileNames.add("MissingRangesForDates.drl");
        // fileNames.add("MissingRangesForDoubles.drl");
        // fileNames.add("MissingRangesForInts.drl");
        // fileNames.add("MissingRangesForVariables.drl");
        // fileNames.add("Misc.drl");
        // fileNames.add("Misc2.drl");
        // fileNames.add("Misc3.drl");
        fileNames.add("Enums.drl");
        // fileNames.add("ConsequenceTest.drl");
        // fileNames.add("optimisation/OptimisationRestrictionOrderTest.drl");
        // fileNames.add("optimisation/OptimisationPatternOrderTest.drl");
        Verifier verifier = VerifierBuilderFactory.newVerifierBuilder().newVerifier();
        for (String s : fileNames) {
            verifier.addResourcesToVerify(ResourceFactory.newClassPathResource(s, Verifier.class), ResourceType.DRL);
        }
        verifier.fireAnalysis();
        // System.out.print(a.getResultAsPlainText());
        // System.out.print(a.getResultAsXML());
        // a.writeComponentsHTML("/stash/");
        // a.writeComponentsHTML("/Users/michaelneale/foo.html");
        // a.writeComponentsHTML("/home/trikkola/");
        // a.writeComponentsHTML("c:/");
        VerifierReport result = verifier.getResult();
        VerifierReportWriter reportwriter = VerifierReportWriterFactory.newHTMLReportWriter();
        FileOutputStream out = new FileOutputStream("/Users/rikkola/Desktop/testReport.zip");
        reportwriter.writeReport(out, result);
        Collection<VerifierMessageBase> msgs = result.getBySeverity(Severity.ERROR);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("ERR: " + msg.getMessage());
        }
        msgs = result.getBySeverity(Severity.WARNING);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("WARN (" + msg.getClass().getSimpleName() + "): " + msg.getMessage());
            System.out.println("\t FAULT: [" + msg.getClass().getSimpleName() + "] " + msg.getFaulty());
            if (msg instanceof VerifierMessage) {
                System.out.println("\t CAUSES (message):");
                VerifierMessage amsg = (VerifierMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t [" + c.getClass().getSimpleName() + "]" + c);
                }
            } else if (msg instanceof VerifierRangeCheckMessage) {
                System.out.println("\t CAUSES (range):");
                VerifierRangeCheckMessage amsg = (VerifierRangeCheckMessage) msg;
                for (Iterator iterator2 = amsg.getCauses().iterator(); iterator2.hasNext(); ) {
                    Cause c = (Cause) iterator2.next();
                    System.out.println("\t\t" + c);
                }
            }
        }
        msgs = result.getBySeverity(Severity.NOTE);
        for (Iterator iterator = msgs.iterator(); iterator.hasNext(); ) {
            VerifierMessageBase msg = (VerifierMessageBase) iterator.next();
            System.out.println("NOTE: " + msg.getMessage());
            System.out.println("\t" + msg.getFaulty());
        }
        Collection<ObjectType> classes = result.getVerifierData().getAll(VerifierComponentType.OBJECT_TYPE);
        for (Iterator iterator = classes.iterator(); iterator.hasNext(); ) {
            ObjectType c = (ObjectType) iterator.next();
            Collection<VerifierRule> cr = result.getVerifierData().getRulesByObjectTypePath(c.getPath());
            System.err.println("Class rules:" + cr);
            Set<Field> flds = c.getFields();
            for (Iterator iterator2 = flds.iterator(); iterator2.hasNext(); ) {
                Field f = (Field) iterator2.next();
                cr = result.getVerifierData().getRulesByFieldPath(f.getPath());
                System.err.println("Field rules: " + cr);
            }
        }
    // System.err.println(a.getResultAsPlainText());
    // System.out.println(result.toString());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : VerifierMessage(org.drools.verifier.report.components.VerifierMessage) VerifierRangeCheckMessage(org.drools.verifier.report.components.VerifierRangeCheckMessage) ArrayList(java.util.ArrayList) VerifierMessageBase(org.drools.verifier.report.components.VerifierMessageBase) VerifierRule(org.drools.verifier.components.VerifierRule) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) VerifierReport(org.drools.verifier.data.VerifierReport) FileOutputStream(java.io.FileOutputStream) Cause(org.drools.verifier.report.components.Cause) VerifierReportWriter(org.drools.verifier.report.VerifierReportWriter) Iterator(java.util.Iterator)

Example 5 with ObjectType

use of org.drools.verifier.components.ObjectType in project drools by kiegroup.

the class VerifierDataMapsTest method saveVerifierComponentAndGet.

private void saveVerifierComponentAndGet(Restriction component) {
    VerifierData data = VerifierReportFactory.newVerifierData();
    ObjectType objectType = new ObjectType(new PackageDescr("testPackage1"));
    Field field = new Field(new PackageDescr("testPackage1"));
    field.setObjectTypePath(objectType.getPath());
    component.setFieldPath(field.getPath());
    assertNotNull(component.getFieldPath());
    data.add(objectType);
    data.add(field);
    data.add(component);
    Collection<VerifierComponent> all = data.getAll();
    assertEquals(3, all.size());
    assertTrue(all.contains(objectType));
    assertTrue(all.contains(field));
    assertTrue(all.contains(component));
    Collection<VerifierComponent> components = data.getAll(component.getVerifierComponentType());
    assertEquals(1, components.size());
    assertEquals(component, components.toArray()[0]);
    VerifierComponent component2 = data.getVerifierObject(component.getVerifierComponentType(), component.getPath());
    assertNotNull(component2);
    assertEquals(component, component2);
    Collection<VerifierComponent> fields = data.getAll(field.getVerifierComponentType());
    assertEquals(1, fields.size());
    assertEquals(field, fields.toArray()[0]);
    VerifierComponent field2 = data.getVerifierObject(field.getVerifierComponentType(), field.getPath());
    assertNotNull(field2);
    assertEquals(field, field2);
    Collection<VerifierComponent> objectTypes = data.getAll(objectType.getVerifierComponentType());
    assertEquals(1, objectTypes.size());
    assertEquals(objectType, objectTypes.toArray()[0]);
    VerifierComponent objectType2 = data.getVerifierObject(objectType.getVerifierComponentType(), objectType.getPath());
    assertNotNull(objectType2);
    assertEquals(objectType, objectType2);
}
Also used : ObjectType(org.drools.verifier.components.ObjectType) EnumField(org.drools.verifier.components.EnumField) Field(org.drools.verifier.components.Field) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Aggregations

ObjectType (org.drools.verifier.components.ObjectType)14 Field (org.drools.verifier.components.Field)7 VerifierRule (org.drools.verifier.components.VerifierRule)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 Test (org.junit.Test)4 Import (org.drools.verifier.components.Import)3 VerifierReport (org.drools.verifier.data.VerifierReport)3 IOException (java.io.IOException)2 JarInputStream (java.util.jar.JarInputStream)2 ClassPathResource (org.drools.core.io.impl.ClassPathResource)2 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)2 EnumField (org.drools.verifier.components.EnumField)2 Pattern (org.drools.verifier.components.Pattern)2 PatternVariable (org.drools.verifier.components.PatternVariable)2 SubPattern (org.drools.verifier.components.SubPattern)2 VerifierData (org.drools.verifier.data.VerifierData)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1