Search in sources :

Example 81 with ObjectOutput

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

the class BaseMannersTest method getPathDone.

/**
 * <pre>
 * rule pathDone() {
 *     Context context; Seating seating;
 *     when {
 *         context : Context( state == Context.MAKE_PATH )
 *         seating : Seating( pathDone == false )
 *     } then {
 *         seating.setPathDone( true );
 *         context.setName( Context.CHECK_DONE );
 *     }
 * }
 * </pre>
 *
 * @return
 * @throws InvalidRuleException
 */
private RuleImpl getPathDone() throws InvalidRuleException {
    final RuleImpl rule = new RuleImpl("pathDone");
    // -----------
    // context : Context( state == Context.MAKE_PATH )
    // -----------
    final Pattern contextPattern = new Pattern(0, this.contextType, "context");
    contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.MAKE_PATH));
    rule.addPattern(contextPattern);
    final Declaration contextDeclaration = rule.getDeclaration("context");
    // ---------------
    // seating : Seating( pathDone == false )
    // ---------------
    final Pattern seatingPattern = new Pattern(1, this.seatingType, "seating");
    seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", false));
    rule.addPattern(seatingPattern);
    final Declaration seatingDeclaration = rule.getDeclaration("seating");
    // ------------
    // context.setName( Context.CHECK_DONE );
    // seating.setPathDone( true );
    // ------------
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
            try {
                RuleImpl rule = drools.getRule();
                LeftTuple tuple = (LeftTuple) drools.getTuple();
                Context context = (Context) drools.get(contextDeclaration);
                Seating seating = (Seating) drools.get(seatingDeclaration);
                seating.setPathDone(true);
                // if ( seating.getId() == 6 ) {
                // System.err.println( "pause" );
                // }
                drools.update(tuple.get(seatingDeclaration));
                context.setState(Context.CHECK_DONE);
                drools.update(tuple.get(contextDeclaration), context);
            // System.err.println( "path done" + seating );
            } catch (Exception e) {
                e.printStackTrace();
                throw new ConsequenceException(e);
            }
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    return rule;
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) LeftTuple(org.drools.core.reteoo.LeftTuple) InvalidRuleException(org.drools.core.rule.InvalidRuleException) IOException(java.io.IOException) ConsequenceException(org.drools.core.spi.ConsequenceException) IntrospectionException(java.beans.IntrospectionException) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) ConsequenceException(org.drools.core.spi.ConsequenceException)

Example 82 with ObjectOutput

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

the class BaseMannersTest method getFindSeating.

/**
 * <pre>
 *    rule findSeating() {
 *       Context context;
 *       int seatingId, seatingPid;
 *       String seatingRightGuestName, leftGuestName;
 *       Sex rightGuestSex;
 *       Hobby rightGuestHobby;
 *       Count count;
 *
 *       when {
 *           context : Context( state == Context.ASSIGN_SEATS )
 *           Seating( seatingId:id, seatingPid:pid, pathDone == true
 *                    seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
 *           Guest( name == seatingRightGuestName, rightGuestSex:sex, rightGuestHobby:hobby )
 *           Guest( leftGuestName:name , sex != rightGuestSex, hobby == rightGuestHobby )
 *
 *           count : Count()
 *
 *           not ( Path( id == seatingId, guestName == leftGuestName) )
 *           not ( Chosen( id == seatingId, guestName == leftGuestName, hobby == rightGuestHobby) )
 *       } then {
 *           int newSeat = rightSeat + 1;
 *           drools.assert( new Seating( coung.getValue(), rightSeat, rightSeatName, leftGuestName, newSeat, countValue, id, false );
 *           drools.assert( new Path( countValue, leftGuestName, newSeat );
 *           drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );
 *
 *           System.err.println( &quot;seat &quot; + rightSeat + &quot; &quot; + rightSeatName + &quot; &quot; + leftGuestName );
 *
 *           count.setCount(  countValue + 1 );
 *           context.setPath( Context.MAKE_PATH );
 *       }
 *    }
 * </pre>
 *
 * @return
 * @throws InvalidRuleException
 */
private RuleImpl getFindSeating() throws InvalidRuleException {
    final RuleImpl rule = new RuleImpl("findSeating");
    // ---------------
    // context : Context( state == Context.ASSIGN_SEATS )
    // ---------------
    final Pattern contextPattern = new Pattern(0, this.contextType, "context");
    contextPattern.addConstraint(getLiteralConstraint(contextPattern, "state", Context.ASSIGN_SEATS));
    rule.addPattern(contextPattern);
    final Declaration contextDeclaration = rule.getDeclaration("context");
    // -------------------------------
    // Seating( seatingId:id, seatingPid:pid, pathDone == true
    // seatingRightSeat:rightSeat seatingRightGuestName:rightGuestName )
    // -------------------------------
    final Pattern seatingPattern = new Pattern(1, this.seatingType);
    setFieldDeclaration(seatingPattern, "id", "seatingId");
    setFieldDeclaration(seatingPattern, "pid", "seatingPid");
    seatingPattern.addConstraint(getLiteralConstraint(seatingPattern, "pathDone", true));
    setFieldDeclaration(seatingPattern, "rightSeat", "seatingRightSeat");
    setFieldDeclaration(seatingPattern, "rightGuestName", "seatingRightGuestName");
    rule.addPattern(seatingPattern);
    final Declaration seatingIdDeclaration = rule.getDeclaration("seatingId");
    final Declaration seatingPidDeclaration = rule.getDeclaration("seatingPid");
    final Declaration seatingRightGuestNameDeclaration = rule.getDeclaration("seatingRightGuestName");
    final Declaration seatingRightSeatDeclaration = rule.getDeclaration("seatingRightSeat");
    // --------------
    // Guest( name == seatingRightGuestName, rightGuestSex:sex,
    // rightGuestHobby:hobby )
    // ---------------
    final Pattern rightGuestPattern = new Pattern(2, this.guestType);
    rightGuestPattern.addConstraint(getBoundVariableConstraint(rightGuestPattern, "name", seatingRightGuestNameDeclaration, "=="));
    setFieldDeclaration(rightGuestPattern, "sex", "rightGuestSex");
    setFieldDeclaration(rightGuestPattern, "hobby", "rightGuestHobby");
    rule.addPattern(rightGuestPattern);
    final Declaration rightGuestSexDeclaration = rule.getDeclaration("rightGuestSex");
    final Declaration rightGuestHobbyDeclaration = rule.getDeclaration("rightGuestHobby");
    // ----------------
    // Guest( leftGuestName:name , sex != rightGuestSex, hobby ==
    // rightGuestHobby )
    // ----------------
    final Pattern leftGuestPattern = new Pattern(3, this.guestType);
    setFieldDeclaration(leftGuestPattern, "name", "leftGuestName");
    leftGuestPattern.addConstraint(getBoundVariableConstraint(rightGuestPattern, "hobby", rightGuestHobbyDeclaration, "=="));
    leftGuestPattern.addConstraint(getBoundVariableConstraint(leftGuestPattern, "sex", rightGuestSexDeclaration, "!="));
    rule.addPattern(leftGuestPattern);
    final Declaration leftGuestNameDeclaration = rule.getDeclaration("leftGuestName");
    // ---------------
    // count : Count()
    // ---------------
    final Pattern count = new Pattern(4, this.countType, "count");
    rule.addPattern(count);
    final Declaration countDeclaration = rule.getDeclaration("count");
    // --------------
    // not ( Path( id == seatingId, guestName == leftGuestName) )
    // --------------
    final Pattern notPathPattern = new Pattern(5, this.pathType);
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "id", seatingIdDeclaration, "=="));
    notPathPattern.addConstraint(getBoundVariableConstraint(notPathPattern, "guestName", leftGuestNameDeclaration, "=="));
    final GroupElement notPath = GroupElementFactory.newNotInstance();
    notPath.addChild(notPathPattern);
    rule.addPattern(notPath);
    // ------------
    // not ( Chosen( id == seatingId, guestName == leftGuestName, hobby ==
    // rightGuestHobby ) )
    // ------------
    final Pattern notChosenPattern = new Pattern(6, this.chosenType);
    notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "id", seatingIdDeclaration, "=="));
    notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "guestName", leftGuestNameDeclaration, "=="));
    notChosenPattern.addConstraint(getBoundVariableConstraint(notChosenPattern, "hobby", rightGuestHobbyDeclaration, "=="));
    final GroupElement notChosen = GroupElementFactory.newNotInstance();
    notChosen.addChild(notChosenPattern);
    rule.addPattern(notChosen);
    // ------------
    // int newSeat = rightSeat + 1;
    // drools.assert( new Seating( coung.getValue(), rightSeat,
    // rightSeatName, leftGuestName, newSeat, countValue, id, false );
    // drools.assert( new Path( countValue, leftGuestName, newSeat );
    // drools.assert( new Chosen( id, leftGuestName, rightGuestHobby ) );
    // 
    // System.err.println( "seat " + rightSeat + " " + rightSeatName + " " +
    // leftGuestName );
    // 
    // count.setCount( countValue + 1 );
    // context.setPath( Context.MAKE_PATH );
    // ------------
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper drools, WorkingMemory workingMemory) throws ConsequenceException {
            try {
                // MemoryVisitor visitor = new MemoryVisitor( ( InternalWorkingMemory ) workingMemory );
                // visitor.visit( workingMemory.getRuleBase() );
                RuleImpl rule = drools.getRule();
                LeftTuple tuple = (LeftTuple) drools.getTuple();
                Context context = (Context) drools.get(contextDeclaration);
                Count count = (Count) drools.get(countDeclaration);
                int seatId = seatingIdDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingIdDeclaration).getObject());
                int seatingRightSeat = seatingRightSeatDeclaration.getExtractor().getIntValue((InternalWorkingMemory) workingMemory, tuple.get(seatingRightSeatDeclaration).getObject());
                String leftGuestName = (String) drools.get(leftGuestNameDeclaration);
                String rightGuestName = (String) drools.get(seatingRightGuestNameDeclaration);
                Hobby rightGuestHobby = (Hobby) drools.get(rightGuestHobbyDeclaration);
                Seating seating = new Seating(count.getValue(), seatId, false, seatingRightSeat, rightGuestName, seatingRightSeat + 1, leftGuestName);
                drools.insert(seating);
                Path path = new Path(count.getValue(), seatingRightSeat + 1, leftGuestName);
                drools.insert(path);
                Chosen chosen = new Chosen(seatId, leftGuestName, rightGuestHobby);
                drools.insert(chosen);
                count.setValue(count.getValue() + 1);
                // if ( count.getValue() == 5 ) {
                // drools.retractObject( tuple.getFactHandleForDeclaration( countDeclaration ) );
                // } else {
                // drools.update( tuple.getFactHandleForDeclaration( countDeclaration ),
                // count );
                // }
                drools.update(tuple.get(countDeclaration), count);
                context.setState(Context.MAKE_PATH);
                drools.update(tuple.get(contextDeclaration), context);
                System.err.println("find seating : " + seating + " : " + path + " : " + chosen);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ConsequenceException(e);
            }
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    return rule;
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) LeftTuple(org.drools.core.reteoo.LeftTuple) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) InvalidRuleException(org.drools.core.rule.InvalidRuleException) IOException(java.io.IOException) ConsequenceException(org.drools.core.spi.ConsequenceException) IntrospectionException(java.beans.IntrospectionException) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) ConsequenceException(org.drools.core.spi.ConsequenceException)

Example 83 with ObjectOutput

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

the class PackageDescrTest method testSerialization.

@Test
public void testSerialization() {
    PackageDescrBuilder builder = DescrFactory.newPackage().name("foo");
    String className = Person.class.getName();
    builder.newImport().target(className).end();
    PackageDescr descr = builder.getDescr();
    ImportDescr importDescr = new ImportDescr(className);
    ImportDescr badImportDescr = new ImportDescr(null);
    assertTrue(descr.getImports().contains(importDescr));
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        descr.writeExternal(out);
        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        PackageDescr newDescr = new PackageDescr();
        newDescr.readExternal(in);
        assertFalse(newDescr.getImports().contains(badImportDescr));
        assertTrue(newDescr.getImports().contains(importDescr));
    } catch (IOException ioe) {
        fail(ioe.getMessage());
    } catch (ClassNotFoundException cnfe) {
        fail(cnfe.getMessage());
    }
}
Also used : PackageDescrBuilder(org.drools.compiler.lang.api.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 84 with ObjectOutput

use of java.io.ObjectOutput in project imagingbook-common by imagingbook.

the class SerializationHelper method writeObject.

/**
 * Writes a serialized representation of an arbitrary Java object to
 * a file. Make sure the serialized object is composed of standard Java types
 * only to avoid class loader problems.
 *
 * @param any The object to be serialized.
 * @param fileName The file to write to.
 * @return The full path of the written file.
 */
public static String writeObject(Object any, String fileName) {
    File file = new File(fileName);
    String path = file.getAbsolutePath();
    try (FileOutputStream strm = new FileOutputStream(file);
        OutputStream buffer = new BufferedOutputStream(strm);
        ObjectOutput output = new ObjectOutputStream(buffer)) {
        output.writeObject(any);
    } catch (IOException e) {
        System.err.println("Output error.");
        return null;
    }
    return path;
}
Also used : ObjectOutput(java.io.ObjectOutput) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 85 with ObjectOutput

use of java.io.ObjectOutput in project compss by bsc-wdc.

the class Serializer method serializeBinary.

/**
 * Serializes an objects using the default java serializer
 *
 * @param o
 *            object to be serialized
 * @throws IOException
 *             Error writting the byte stream
 */
private static byte[] serializeBinary(Object o) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(o);
        return bos.toByteArray();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
        // No need to handle such exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
        // No need to handle such exception
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutput (java.io.ObjectOutput)123 ObjectOutputStream (java.io.ObjectOutputStream)88 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 IOException (java.io.IOException)63 ObjectInput (java.io.ObjectInput)38 ObjectInputStream (java.io.ObjectInputStream)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 Test (org.junit.Test)23 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)15 Consequence (org.drools.core.spi.Consequence)15 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)15 FileOutputStream (java.io.FileOutputStream)13 OutputStream (java.io.OutputStream)12 WorkingMemory (org.drools.core.WorkingMemory)12 ClassObjectType (org.drools.core.base.ClassObjectType)10 Declaration (org.drools.core.rule.Declaration)10 Test (org.junit.jupiter.api.Test)9 File (java.io.File)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8