Search in sources :

Example 31 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class CrossProductTest method setUp.

@Before
public void setUp() throws Exception {
    final ObjectType list1ObjectType = new ClassObjectType(String.class);
    final ObjectType list2ObjectType = new ClassObjectType(String.class);
    final RuleImpl rule = new RuleImpl("rule-1");
    final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
    final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
    rule.addPattern(list1Pattern);
    rule.addPattern(list2Pattern);
    final Declaration s1Declaration = rule.getDeclaration("s1");
    final Declaration s2Declaration = rule.getDeclaration("s2");
    this.values = new ArrayList();
    rule.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
            final String s1 = (String) knowledgeHelper.get(s1Declaration);
            final String s2 = (String) knowledgeHelper.get(s2Declaration);
            CrossProductTest.this.values.add(new String[] { s1, s2 });
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    this.pkg = new KnowledgePackageImpl("org.drools");
    this.pkg.addRule(rule);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) IOException(java.io.IOException) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Before(org.junit.Before)

Example 32 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class ReteooRuleBuilderTest method testAddRuleWithPatterns.

@Test
public void testAddRuleWithPatterns() {
    final RuleImpl rule = new RuleImpl("only patterns");
    final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
    final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
    final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
    final GroupElement lhsroot = GroupElementFactory.newAndInstance();
    lhsroot.addChild(c1);
    lhsroot.addChild(c2);
    lhsroot.addChild(c3);
    rule.setLhs(lhsroot);
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            System.out.println("Consequence!");
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    final List terminals = this.builder.addRule(rule, this.rulebase);
    assertEquals("Rule must have a single terminal node", 1, terminals.size());
    final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) List(java.util.List) ObjectInput(java.io.ObjectInput) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 33 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class ProcessContextTest method testProcessContextGetAssignment.

@Test
public void testProcessContextGetAssignment() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    assertNotNull(ksession);
    CaseInformation caseInfo = new CaseInformation();
    caseInfo.assign("owner", new OrganizationalEntity() {

        @Override
        public String getId() {
            return "testUser";
        }

        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
        }

        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }
    });
    ksession.insert(caseInfo);
    ProcessContext processContext = new ProcessContext(ksession);
    CaseAssignment caseAssignment = processContext.getCaseAssignment();
    assertNotNull(caseAssignment);
    Collection<OrganizationalEntity> forRole = caseAssignment.getAssignments("owner");
    assertNotNull(forRole);
    assertEquals(1, forRole.size());
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ObjectInput(java.io.ObjectInput) Test(org.junit.Test)

Example 34 with ObjectOutput

use of java.io.ObjectOutput in project kie-wb-common by kiegroup.

the class KieAfterDecorator method readObjectFromADifferentClassloader.

private KieTuple readObjectFromADifferentClassloader(Object o) {
    ObjectInput in = null;
    ObjectOutput out;
    ByteArrayInputStream bis;
    ByteArrayOutputStream bos = null;
    try {
        bos = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.flush();
        byte[] objBytes = bos.toByteArray();
        bis = new ByteArrayInputStream(objBytes);
        in = new ObjectInputStream(bis);
        Object newObj = in.readObject();
        return new KieTuple(newObj);
    } catch (NotSerializableException nse) {
        nse.printStackTrace();
        StringBuilder sb = new StringBuilder("NotSerializableException:").append(nse.getMessage());
        return new KieTuple(sb.toString());
    } catch (IOException ioe) {
        StringBuilder sb = new StringBuilder("IOException:").append(ioe.getMessage());
        return new KieTuple(sb.toString());
    } catch (ClassNotFoundException cnfe) {
        StringBuilder sb = new StringBuilder("ClassNotFoundException:").append(cnfe.getMessage());
        return new KieTuple(sb.toString());
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder("Exception:").append(e.getMessage());
        return new KieTuple(sb.toString());
    } finally {
        try {
            if (bos != null) {
                bos.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ObjectInputStream(java.io.ObjectInputStream)

Example 35 with ObjectOutput

use of java.io.ObjectOutput in project instructure-android by instructure.

the class FileUtils method SerializableToFile.

/**
 * Converts a serializable object to the specified file.
 *
 * @param context
 * @param cacheFileName
 * @param serializable
 * @return
 */
public static boolean SerializableToFile(Context context, String cacheFileName, Serializable serializable) {
    if (context == null || cacheFileName == null || serializable == null) {
        return false;
    }
    try {
        cacheFileName += FILE_SUFFIX;
        File f = new File(context.getFilesDir(), FILE_DIRECTORY);
        File file = new File(f, cacheFileName);
        file.getParentFile().mkdirs();
        file.createNewFile();
        // Write to file.
        OutputStream outputStream = new FileOutputStream(file);
        OutputStream buffer = new BufferedOutputStream(outputStream);
        ObjectOutput output = new ObjectOutputStream(buffer);
        output.writeObject(serializable);
        output.flush();
        output.close();
        return true;
    } catch (Exception E) {
        return false;
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ObjectOutput (java.io.ObjectOutput)105 ObjectOutputStream (java.io.ObjectOutputStream)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 IOException (java.io.IOException)53 ObjectInput (java.io.ObjectInput)26 Test (org.junit.Test)24 ObjectInputStream (java.io.ObjectInputStream)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 WorkingMemory (org.drools.core.WorkingMemory)13 FileOutputStream (java.io.FileOutputStream)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 OutputStream (java.io.OutputStream)11 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 File (java.io.File)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7