I think that Jeremic Zoran wrote:
>>I need the function that will remove all facts of specific deftemplate =
>>from Jess memory. It should be written as java function ...

Hi Zoran,

My class is attached (most of it is just a test harness).
Run it and see if this is what you want and let me know either way.
One thing that I didn't do was check if the deleted facts had children
(facts that inherited from a parent fact) -- I'll leave that for you to
code! :-D  If you're not using inheritance, then the code works fine.

Cheers!

Jason
------------------------

Jason Morris
Morris Technical Solutions
[EMAIL PROTECTED]
www.morristechnicalsolutions.com
fax/phone: 503.692.1088

import jess.*;
import java.util.*;

public class FactRetractor {

    public static void main(String[] args) {

        FactRetractor fr = new FactRetractor();
        // Retract by name
        fr.retractAllFactsOfType("MAIN::foo");
        // Retract by reference to deftemplate object
        Deftemplate deftemplate = fr.getDeftemplateByName("MAIN::bar");
        fr.retractAllFactsOfType(deftemplate);
    }

    StringBuffer batch;
    Rete engine;

    public FactRetractor() {
        init();
    }

    public Deftemplate getDeftemplateByName(String name) {
        Iterator itt = engine.listDeftemplates();
        Deftemplate deftemp = null;
        while (itt.hasNext()) {
            Deftemplate next = (Deftemplate) itt.next();
            if (next.getName().equals(name)) {
                deftemp = next;
            }
        }
        return deftemp;
    }

    public void init() {
        this.engine = new Rete();
        batch = new StringBuffer();
        try {
            engine.watchAll();
            // Get a deftemplate loaded
            batch.append("(deftemplate foo");
            batch.append(" (slot id)");
            batch.append(" (slot data))\n");
            batch.append("(deftemplate bar");
            batch.append(" (slot id)");
            batch.append(" (slot data))\n");
            engine.executeCommand(batch.toString());
            batch.setLength(0);
            engine.reset();

            // Generate some random facts
            for (int i = 0; i < 5; i++) {
                engine.assertString("(foo (id " + i + ") (data "
                        + Math.random() + "))");
            }
            for (int j = 0; j < 5; j++) {
                engine.assertString("(bar (id " + (j) + ") (data "
                        + Math.random() + "))");
            }

        } catch (JessException jex) {
            jex.printStackTrace(System.err);
        }
    }

    public void retractAllFactsOfType(Deftemplate deftemplate) {
        try {
            Iterator itt = engine.listFacts();
            while (itt.hasNext()) {
                Fact fact = (Fact) itt.next();
                if (fact.getDeftemplate().equals(deftemplate)) {
                    engine.retract(fact);
                }
            }
        } catch (JessException jex) {
            jex.printStackTrace(System.err);
        }
    }

    public void retractAllFactsOfType(String qualifiedDeftemplateName) {
        try {
            Iterator itt = engine.listFacts();
            while (itt.hasNext()) {
                Fact fact = (Fact) itt.next();
                if (fact.getDeftemplate().getName().equals(
                        qualifiedDeftemplateName)) {
                    engine.retract(fact);
                }
            }
        } catch (JessException jex) {
            jex.printStackTrace(System.err);
        }
    }
}

Reply via email to