[Reasoner4j-user] SF.net SVN: reasoner4j:[36] trunk/reasoner4j
Status: Pre-Alpha
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2010-12-23 18:15:18
|
Revision: 36
http://reasoner4j.svn.sourceforge.net/reasoner4j/?rev=36&view=rev
Author: luc_peuvrier
Date: 2010-12-23 18:15:10 +0000 (Thu, 23 Dec 2010)
Log Message:
-----------
added utilities for rete table content test
Modified Paths:
--------------
trunk/reasoner4j/doc/builtin.txt
trunk/reasoner4j/doc/todo.txt
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/AssertedPremiseTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/BuiltinPremiseTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/MergeTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseVariableTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ReteRule.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/VariableTable.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/retemgr/service/ReteBuilder.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremise.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremiseAndDefault.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AssertedPremise.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/Premise.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefault.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefaultNotListening.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseNotListening.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestBuitin.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestReteTable.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/rete/entity/MockTable.java
trunk/reasoner4j/src/test/resources/log4j.properties
Added Paths:
-----------
trunk/reasoner4j/doc/conception.txt
trunk/reasoner4j/doc/doc.txt
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITableVisitor.java
trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/NullVariableTable.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/ReteRuleToString.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulator.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTable.java
trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTest.java
Removed Paths:
-------------
trunk/reasoner4j/doc/concept.txt
Modified: trunk/reasoner4j/doc/builtin.txt
===================================================================
--- trunk/reasoner4j/doc/builtin.txt 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/doc/builtin.txt 2010-12-23 18:15:10 UTC (rev 36)
@@ -102,7 +102,7 @@
?LL listRest ?LL2 <-- asserted
?LL2 listFirst ?L <-- asserted
?LL2 listRest nil <-- asserted
- ?LL listAppend ?NEWL. <-- builtin or asserted
+ ?LL listAppend ?NEWL. <-- builtin
?LN listFirst ?L
?LN listRest ?N
Deleted: trunk/reasoner4j/doc/concept.txt
===================================================================
--- trunk/reasoner4j/doc/concept.txt 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/doc/concept.txt 2010-12-23 18:15:10 UTC (rev 36)
@@ -1,145 +0,0 @@
-http://www.w3.org/2000/10/swap/doc/Rules
-
- ==============================
- @forAll :x, :y.
- { :x :parent :y } => { :y :child :x }.
-
-can be wrote
-
- { ?x parent ?y } => { ?y :child ?x }.
-
-x and y are universal variables
- ==============================
- @forSome :a, :b.
- :a :hates :b.
- :b :loves :a.
-
-can be wrote
-
- _:a :hates _:b.
- _:b :loves _:a.
-
-a and b are existential variables
-
------------------------------------------------------------------------------------------------------
-
-fact: subject predicate object
-
-subject, predicate, object: fact member
-
-fact has sources:
-- basic fact
-- consequence of one or more rules
-
-rule:
-
-if "premises" then "conclusions"
-
-premise
-
-conclusion
-
------------------------------------------------------------------------------------------------------
-
-Fact <>--- Premise
- ----- Subject
- ----- Predicate
- ----- Object
-
-Rule <>--- Premise
- <>--- Conclusion
-
-Premise ----- RuleMemberSubject
- ----- RuleMemberPredicate
- ----- RuleMemberObject
- <>--- FactSet filtered view of fact
-
-Conclusion ----- RuleMemberSubject
- ----- RuleMemberPredicate
- ----- RuleMemberObject
- <>--- FactSet source of fact
-
-RuleMember<XXX>
-an variable or a FactMember<XXX>
-
- ==============================
-
-{
- ?M a Map
- ?M entry ?E
- ?E user ?U
- ?U id ?ID
-}
-=> {
- ?E key ?ID
-}
-
-rete
-
- 0
-?M a Map : ?M --+
- 0 1 0 1
-?M entry ?E : ?M ?E --+-- ?M ?E --+
- 0 1 0 1 2
-?E user ?U : ?E ?U --------------+-- ?M ?E ?U --+
- 0 1 0 1 2 3
-?U id ?ID : ?U ?ID -----------------------------+-- ?M ?E ?U ?ID
-
-?M a Map : ?M --+
-?E user ?U : ?E ?U --+-- ?M ?E ?U --+
-?M entry ?E : ?M ?E -----------------+-- ?M ?E ?U --+
-?U id ?ID : ?U ?ID --------------------------------+-- ?M ?E ?U ?ID
-
-
-
- r1: ?A ?B r2: ?A ?C r1^r2: ?A ?B ?C
-
- --------------------------------------
--> a x
- --------------------------------------
- a x
--> b x
- --------------------------------------
- a x
- b x
--> a y a x y
- --------------------------------------
-
-
------------------------------------------------------------------------------------------------------
-
-
-? ? ?
-s ? ?
-? p ?
-? ? o
-s p ?
-s ? o
-? p o
-s p o
-
-fact:
-
-s -> po(s)
-p -> so(p)
-o -> sp(o)
-
-po(s): PredicateObjectOfSubject
-
-p -> o(s,p) ObjectOfSubjectPredicate
-o -> p(s,o) PredicateOfSubjectObject
-
-so(p): SubjectObjectOfPredicate
-
-s -> o(s,p) ObjectOfSubjectPredicate
-o -> s(p,o) SubjectOfPredicateObject
-
-sp(o): SubjectPredicateOfObject
-
-s -> p(s,o) PredicateOfSubjectObject
-p -> s(p,o) SubjectOfPredicateObject
-
-o(s,p) ObjectOfSubjectPredicate
-p(s,o) PredicateOfSubjectObject
-s(p,o) SubjectOfPredicateObject
-
Copied: trunk/reasoner4j/doc/conception.txt (from rev 34, trunk/reasoner4j/doc/concept.txt)
===================================================================
--- trunk/reasoner4j/doc/conception.txt (rev 0)
+++ trunk/reasoner4j/doc/conception.txt 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,145 @@
+http://www.w3.org/2000/10/swap/doc/Rules
+
+ ==============================
+ @forAll :x, :y.
+ { :x :parent :y } => { :y :child :x }.
+
+can be wrote
+
+ { ?x parent ?y } => { ?y :child ?x }.
+
+x and y are universal variables
+ ==============================
+ @forSome :a, :b.
+ :a :hates :b.
+ :b :loves :a.
+
+can be wrote
+
+ _:a :hates _:b.
+ _:b :loves _:a.
+
+a and b are existential variables
+
+-----------------------------------------------------------------------------------------------------
+
+fact: subject predicate object
+
+subject, predicate, object: fact member
+
+fact has sources:
+- basic fact
+- consequence of one or more rules
+
+rule:
+
+if "premises" then "conclusions"
+
+premise
+
+conclusion
+
+-----------------------------------------------------------------------------------------------------
+
+Fact <>--- Premise
+ ----- Subject
+ ----- Predicate
+ ----- Object
+
+Rule <>--- Premise
+ <>--- Conclusion
+
+Premise ----- RuleMemberSubject
+ ----- RuleMemberPredicate
+ ----- RuleMemberObject
+ <>--- FactSet filtered view of fact
+
+Conclusion ----- RuleMemberSubject
+ ----- RuleMemberPredicate
+ ----- RuleMemberObject
+ <>--- FactSet source of fact
+
+RuleMember<XXX>
+an variable or a FactMember<XXX>
+
+ ==============================
+
+{
+ ?M a Map
+ ?M entry ?E
+ ?E user ?U
+ ?U id ?ID
+}
+=> {
+ ?E key ?ID
+}
+
+rete
+
+ 0
+?M a Map : ?M --+
+ 0 1 0 1
+?M entry ?E : ?M ?E --+-- ?M ?E --+
+ 0 1 0 1 2
+?E user ?U : ?E ?U --------------+-- ?M ?E ?U --+
+ 0 1 0 1 2 3
+?U id ?ID : ?U ?ID -----------------------------+-- ?M ?E ?U ?ID
+
+?M a Map : ?M --+
+?E user ?U : ?E ?U --+-- ?M ?E ?U --+
+?M entry ?E : ?M ?E -----------------+-- ?M ?E ?U --+
+?U id ?ID : ?U ?ID --------------------------------+-- ?M ?E ?U ?ID
+
+
+
+ r1: ?A ?B r2: ?A ?C r1^r2: ?A ?B ?C
+
+ --------------------------------------
+-> a x
+ --------------------------------------
+ a x
+-> b x
+ --------------------------------------
+ a x
+ b x
+-> a y a x y
+ --------------------------------------
+
+
+-----------------------------------------------------------------------------------------------------
+
+
+? ? ?
+s ? ?
+? p ?
+? ? o
+s p ?
+s ? o
+? p o
+s p o
+
+fact:
+
+s -> po(s)
+p -> so(p)
+o -> sp(o)
+
+po(s): PredicateObjectOfSubject
+
+p -> o(s,p) ObjectOfSubjectPredicate
+o -> p(s,o) PredicateOfSubjectObject
+
+so(p): SubjectObjectOfPredicate
+
+s -> o(s,p) ObjectOfSubjectPredicate
+o -> s(p,o) SubjectOfPredicateObject
+
+sp(o): SubjectPredicateOfObject
+
+s -> p(s,o) PredicateOfSubjectObject
+p -> s(p,o) SubjectOfPredicateObject
+
+o(s,p) ObjectOfSubjectPredicate
+p(s,o) PredicateOfSubjectObject
+s(p,o) SubjectOfPredicateObject
+
Added: trunk/reasoner4j/doc/doc.txt
===================================================================
--- trunk/reasoner4j/doc/doc.txt (rev 0)
+++ trunk/reasoner4j/doc/doc.txt 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,51 @@
+IMmotor#assertFact IKnowledgeBase#assertFact:
+---------------------------------------------
+
+ works has adding rule "true => (x,y,z)" added fact: this is asserted fact
+
+IMmotor#retractFact IKnowledgeBase#retractFact and retract conclusion:
+----------------------------------------------------------------------
+ retract always, do not take care of assertion cause inferred (rules conclusion) or asserted (added in KB)
+ inferred fact:
+ asserted as rule conclusion
+ asserted fact:
+ added in KB, always true
+
+Premise:
+--------
+
+Premise PremiseTable PremiseVariableTable (fact set view)
+ BuiltinPremiseTable VariableTable (a premise fact set)
+ need for retraction
+PremiseNotListening PremiseTableNotListening PremiseVariableTable (fact set view)
+AssertedPremise AssertedPremiseTable VariableTable (a premise fact set)
+ need for retraction
+PremiseAndDefault PremiseTable PremiseVariableTableAndDefault (fact set view)
+PremiseAndDefaultNotListening PremiseTableNotListening PremiseVariableTableAndDefault (fact set view)
+
+Conclusion:
+-----------
+
+AssertConclusion AssertConclusionNotifier
+ReplaceConclusion ReplaceConclusionNotifier
+RetractConclusion RetractConclusionNotifier
+
+existential in conclusion:
+--------------------------
+
+use blankNode builtin
+see TestExistential
+
+ :o1 :p1 ?X
+ :o2 :p1 ?Y
+=>
+ ?X :p2 ?E
+ ?E :p3 ?Y
+
+Premise(":o1",":p1",?X)
+Premise(":o2",":p1",?Y)
+Premise("","blankNode",?E)
+=>
+Conclusion(?X,":p2",?E)
+Conclusion(?E,":p3",?Y)
+
Modified: trunk/reasoner4j/doc/todo.txt
===================================================================
--- trunk/reasoner4j/doc/todo.txt 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/doc/todo.txt 2010-12-23 18:15:10 UTC (rev 36)
@@ -1,11 +1,9 @@
-doc:
-IMmotor#assertFact IKnowledgeBase#assertFact:
- works has adding rule "true => (x,y,z)" added fact: this is asserted fact
-IMmotor#retractFact IKnowledgeBase#retractFact and retract conclusion:
- retract always, do not take care of assertion cause inferred (rules conclusion) or asserted (added in KB)
-inferred fact: asserted as rule conclusion
---------------------------------------------------------------------------------------------------------
+is blankNode premise needed ?
+-----------------------------
+why not AssertedPremise("","",?E) instead of Premise("","blankNode",?E)
+to test
+
IBuitinTermProvider chain
-------------------------
@@ -13,14 +11,18 @@
motor#addBuitinProvider
motor#defaultBuitinProvider(boolean enabled); //disabled by default
-existential in conclusion:
---------------------------
+to test
+-------
- :o1 :p1 ?X
- :o2 :p1 ?Y
-=>
- ?X :p2 ?E
- ?E :p3 ?Y
+builtin mathSum
+builtin listAppend
+
+inList builtin
+--------------
+
+
+
+
premise negation
----------------
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/AssertedPremiseTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/AssertedPremiseTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/AssertedPremiseTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -186,4 +186,11 @@
public Iterator<List<T>> iterator() {
return variableTable.iterator();
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ visitor.endVisit(this);
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/BuiltinPremiseTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/BuiltinPremiseTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/BuiltinPremiseTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -26,6 +26,7 @@
import java.util.Iterator;
import java.util.List;
+import net.sf.reasoner4j.Fortest;
import net.sf.reasoner4j.builtin.IBuiltin;
import net.sf.reasoner4j.fact.IFact;
import net.sf.reasoner4j.rule.IPremise;
@@ -125,4 +126,11 @@
public void dispose() {
// nothing to do
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ visitor.endVisit(this);
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -42,4 +42,6 @@
Iterator<List<T>> removeSelect(List<T> key);
Iterator<List<T>> iterator();
+
+ void accept(ITableVisitor<T> visitor);
}
Added: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITableVisitor.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITableVisitor.java (rev 0)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ITableVisitor.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j.rete.entity;
+
+import net.sf.reasoner4j.Fortest;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <T>
+ */
+@Fortest
+public interface ITableVisitor<T> {
+
+ void beginVisit(ITable<T> table);
+
+ void endVisit(ITable<T> table);
+}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/MergeTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/MergeTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/MergeTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -169,4 +169,13 @@
public Iterator<List<T>> iterator() {
return variableTable.iterator();
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ leftTable.accept(visitor);
+ rightTable.accept(visitor);
+ visitor.endVisit(this);
+ }
}
Added: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/NullVariableTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/NullVariableTable.java (rev 0)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/NullVariableTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j.rete.entity;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <T>
+ */
+public class NullVariableTable<T> implements IVariableTable<T> {
+
+ @Override
+ public void addListener(final ITableListener<T> listener) {
+ // no implementation
+ }
+
+ @Override
+ public void removeListener(final ITableListener<T> listener) {
+ // no implementation
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<List<T>> select(final List<T> key) {
+ // no implementation
+ return Collections.EMPTY_LIST.iterator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<List<T>> removeSelect(final List<T> key) {
+ // no implementation
+ return Collections.EMPTY_LIST.iterator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<List<T>> iterator() {
+ // no implementation
+ return Collections.EMPTY_LIST.iterator();
+ }
+
+ @Override
+ public boolean add(List<T> variableValueSet) {
+ // no implementation
+ return true;
+ }
+
+ @Override
+ public boolean remove(List<T> variableValueSet) {
+ // no implementation
+ return true;
+ }
+
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ // no implementation
+ }
+}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -76,4 +76,11 @@
public boolean isAsserted() {
return EnumPremiseType.ASSERTED.equals(premise.getType());
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ visitor.endVisit(this);
+ }
}
\ No newline at end of file
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseVariableTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseVariableTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/PremiseVariableTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
+import net.sf.reasoner4j.Fortest;
import net.sf.reasoner4j.fact.IFact;
import net.sf.reasoner4j.fact.IFactSet;
import net.sf.reasoner4j.rule.IPremise;
@@ -149,4 +150,10 @@
public Iterator<List<T>> iterator() {
return new FactIterator();
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.endVisit(this);
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ReteRule.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ReteRule.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/ReteRule.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -66,10 +66,6 @@
return rule;
}
- public ITable<T> getRootTable() {
- return rootTable;
- }
-
public void setRootTable(final ITable<T> rootTable) {
this.rootTable = rootTable;
}
@@ -86,7 +82,17 @@
}
@Fortest
+ public ITable<T> getRootTable() {
+ return rootTable;
+ }
+
+ @Fortest
public List<IPremiseTable<T>> getPremiseTableList() {
return premiseTableList;
}
+
+ @Fortest
+ public void accept(final ITableVisitor<T> visitor) {
+ rootTable.accept(visitor);
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/VariableTable.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/VariableTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rete/entity/VariableTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -31,6 +31,8 @@
import java.util.Map.Entry;
import java.util.Set;
+import net.sf.reasoner4j.Fortest;
+
/**
*
* @author luc peuvrier
@@ -181,4 +183,16 @@
public Iterator<List<T>> iterator() {
return new TableIterator();
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ visitor.endVisit(this);
+ }
+
+ @Override
+ public String toString() {
+ return "VariableTable [table=" + table + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/retemgr/service/ReteBuilder.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/retemgr/service/ReteBuilder.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/retemgr/service/ReteBuilder.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -114,26 +114,30 @@
final IPremise<T> premise = premiseReteNode.getPremise();
final VariableKeyValueMapping<T> mapping = premiseReteNode.getMapping();
final IPremiseTable<T> premiseTable;
+ final IBuiltin<T> buitin = buitin(premise);
switch (premise.getType()) {
case ASSERTED:
+ assertNotBuitlin(buitin, premise);
+ // FIXMELUC use of premiseTableFactory
premiseTable = new AssertedPremiseTable<T>(
reteRule.getIdentifier(), premise, mapping,
motor.getKnowledgeBase(), motor.getBlankNodeProvider());
reteRule.add(premiseTable);
break;
case DEFAULT:
+ assertNotBuitlin(buitin, premise);
premiseTable = premiseTableFactory.create(premise, mapping,
motor.getAllFactSet());
reteRule.add(premiseTable);
break;
case STANDARD:
- final IBuiltin<T> buitin = buitin(premise);
if (buitin == null) {
final PremiseTable<T> factPremiseTable = premiseTableFactory
.create(premise, mapping, motor.getAllFactSet());
reteRule.add(factPremiseTable);
premiseTable = factPremiseTable;
} else {
+ // FIXMELUC use of premiseTableFactory
premiseTable = new BuiltinPremiseTable<T>(premise,
reteRule.getIdentifier(), buitin, mapping);
}
@@ -150,6 +154,13 @@
return motor.builtin(fact);
}
+ private void assertNotBuitlin(final IBuiltin<T> buitin,
+ final IPremise<T> premise) {
+ if (buitin != null) {
+ throw new ReasonerException("can not be a buitlin : " + premise);
+ }
+ }
+
@Override
public void endVisit(final MergeReteNode<T> mergeReteNode) {
final ITable<T> rightTable = stack.pop();
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremise.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremise.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremise.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -57,6 +57,11 @@
}
@Override
+ public String toString() {
+ return "[type=" + type + ", listening=" + listening + "]";
+ }
+
+ @Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremiseAndDefault.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremiseAndDefault.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AbstractPremiseAndDefault.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -59,4 +59,11 @@
public T getDefaultObject() {
return defaultObject;
}
+
+ @Override
+ public String toString() {
+ return "[defaultSubject=" + defaultSubject + ", defaultPredicate="
+ + defaultPredicate + ", defaultObject=" + defaultObject + ", "
+ + super.toString() + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AssertedPremise.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AssertedPremise.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/AssertedPremise.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -37,4 +37,9 @@
predicateVariableName, predicate, objectVariableName, object,
true);
}
+
+ @Override
+ public String toString() {
+ return "AssertedPremise [" + super.toString() + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/Premise.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/Premise.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/Premise.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -37,4 +37,9 @@
predicateVariableName, predicate, objectVariableName, object,
true);
}
+
+ @Override
+ public String toString() {
+ return "Premise [" + super.toString() + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefault.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefault.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefault.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -39,4 +39,9 @@
predicateVariableName, predicate, defaultPredicate,
objectVariableName, object, defaultObject, true);
}
+
+ @Override
+ public String toString() {
+ return "PremiseAndDefault [" + super.toString() + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefaultNotListening.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefaultNotListening.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseAndDefaultNotListening.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -40,4 +40,9 @@
predicateVariableName, predicate, defaultPredicate,
objectVariableName, object, defaultObject, false);
}
+
+ @Override
+ public String toString() {
+ return "PremiseAndDefaultNotListening [" + super.toString() + "]";
+ }
}
Modified: trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseNotListening.java
===================================================================
--- trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseNotListening.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/main/java/net/sf/reasoner4j/rule/PremiseNotListening.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -37,4 +37,9 @@
predicateVariableName, predicate, objectVariableName, object,
false);
}
+
+ @Override
+ public String toString() {
+ return "PremiseNotListening [" + super.toString() + "]";
+ }
}
Added: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/ReteRuleToString.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/ReteRuleToString.java (rev 0)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/ReteRuleToString.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j;
+
+import java.util.Iterator;
+import java.util.List;
+
+import net.sf.reasoner4j.rete.entity.ITable;
+import net.sf.reasoner4j.rete.entity.ITableVisitor;
+import net.sf.reasoner4j.rete.entity.ReteRule;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <T>
+ */
+@Fortest
+public class ReteRuleToString<T> implements ITableVisitor<T> {
+
+ private StringBuilder stringBuilder;
+
+ private int tableNumber;
+
+ public String toString(final ReteRule<T> reteRule) {
+ String result = null;
+ try {
+ stringBuilder = new StringBuilder();
+ tableNumber = 0;
+ reteRule.accept(this);
+ result = stringBuilder.toString();
+ } finally {
+ stringBuilder = null;
+ }
+ return result;
+ }
+
+ @Override
+ public void beginVisit(final ITable<T> table) {
+ // no implementation
+ }
+
+ @Override
+ public void endVisit(final ITable<T> table) {
+ stringBuilder.append("table #");
+ stringBuilder.append(tableNumber++);
+ stringBuilder.append(' ');
+ stringBuilder.append(table.getClass().getName());
+ stringBuilder.append('\n');
+ final Iterator<List<T>> iterator = table.iterator();
+ while (iterator.hasNext()) {
+ final List<T> line = iterator.next();
+ stringBuilder.append("- ");
+ stringBuilder.append(line.toString());
+ stringBuilder.append('\n');
+ }
+ }
+}
Modified: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestBuitin.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestBuitin.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestBuitin.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -45,7 +45,7 @@
*/
public class TestBuitin extends AbstractTestCase {
- public void test1() {
+ public void testPropertySetToProductResult() {
final MainKnowledgeBaseFactory<String> kbFactory = new MainKnowledgeBaseFactory<String>();
final MotorFactory<String> motorFactory = new MotorFactory<String>();
final IKnowledgeBase<String> kb = kbFactory.create();
@@ -160,7 +160,7 @@
assertFalse(iterator.hasNext());
}
- public void test2() {
+ public void testPropertyIsProductResultOfAnOtherProperty() {
final MainKnowledgeBaseFactory<String> kbFactory = new MainKnowledgeBaseFactory<String>();
final MotorFactory<String> motorFactory = new MotorFactory<String>();
final IKnowledgeBase<String> kb = kbFactory.create();
Modified: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestReteTable.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestReteTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/TestReteTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -26,8 +26,6 @@
import java.util.Iterator;
import java.util.List;
-import org.apache.log4j.Logger;
-
import net.sf.reasoner4j.builtin.IBuiltin;
import net.sf.reasoner4j.builtin.impl.BlankNodeBuiltin;
import net.sf.reasoner4j.fact.Fact;
@@ -37,17 +35,19 @@
import net.sf.reasoner4j.kb.MainKnowledgeBaseFactory;
import net.sf.reasoner4j.motor.IMotor;
import net.sf.reasoner4j.motor.MotorFactory;
-import net.sf.reasoner4j.rete.entity.AssertedPremiseTable;
-import net.sf.reasoner4j.rete.entity.BuiltinPremiseTable;
import net.sf.reasoner4j.rete.entity.ITable;
import net.sf.reasoner4j.rete.entity.MergeTable;
import net.sf.reasoner4j.rete.entity.ReteRule;
-import net.sf.reasoner4j.rule.AssertedPremise;
+import net.sf.reasoner4j.retetest.ReteSimulator;
+import net.sf.reasoner4j.retetest.ReteSimulatorTable;
import net.sf.reasoner4j.rule.AssertConclusion;
+import net.sf.reasoner4j.rule.AssertedPremise;
import net.sf.reasoner4j.rule.IPremise;
import net.sf.reasoner4j.rule.Premise;
import net.sf.reasoner4j.rule.Rule;
+import org.apache.log4j.Logger;
+
/**
*
* @author luc peuvrier
@@ -64,6 +64,7 @@
final IMotor<String> motor = motorFactory.create(kb, this, this);
final IBuiltin<String> testBuiltin = new BlankNodeBuiltin<String>();
motor.addBuiltin(testBuiltin);
+ final ReteSimulator<String> reteSimulator = new ReteSimulator<String>();
Rule<String> rule = new Rule<String>();
// ?X prop ?E.
@@ -72,6 +73,9 @@
/* predicate */null, "prop",
/* object */"?E", null);
rule.addPremise(premise);
+ final ReteSimulatorTable<String> table0 = reteSimulator
+ .addPremise(premise);
+ assertEquals(0, table0.getTableNumber());
// x blankNode ?L
premise = new Premise<String>(
@@ -79,6 +83,9 @@
/* predicate */null, "blankNode",
/* object */"?L", null);
rule.addPremise(premise);
+ final ReteSimulatorTable<String> table1 = reteSimulator
+ .addPremiseBuiltin(premise);
+ assertEquals(1, table1.getTableNumber());
// ?L listFirst ?E
AssertedPremise<String> aPremise = new AssertedPremise<String>(
@@ -86,6 +93,9 @@
/* predicate */null, "listFirst",
/* object */"?E", null);
rule.addPremise(aPremise);
+ final ReteSimulatorTable<String> table3 = reteSimulator
+ .addPremise(aPremise);
+ assertEquals(3, table3.getTableNumber());
// ?X list ?L.
aPremise = new AssertedPremise<String>(
@@ -93,6 +103,9 @@
/* predicate */null, "list",
/* object */"?L", null);
rule.addPremise(aPremise);
+ final ReteSimulatorTable<String> table5 = reteSimulator
+ .addPremise(aPremise);
+ assertEquals(5, table5.getTableNumber());
// ?L listOf ?X.
final AssertConclusion<String> conclusion = new AssertConclusion<String>(
@@ -112,27 +125,26 @@
.getLeftTable();
final MergeTable<String> firstMergeTable = (MergeTable<String>) secondMergeTable
.getLeftTable();
- final AssertedPremiseTable<String> assertedPremiseTable =
- /**/(AssertedPremiseTable<String>) secondMergeTable.getRightTable();
- final BuiltinPremiseTable<String> blankNodePremiseTable =
- /**/(BuiltinPremiseTable<String>) firstMergeTable.getRightTable();
final String[][] expectedEmpty = new String[0][];
assertTableContent(expectedEmpty, firstMergeTable);
assertTableContent(expectedEmpty, secondMergeTable);
assertTableContent(expectedEmpty, thirdMergeTable);
+ assertEquals("", reteSimulator.check(reteRule));
/*
* assert ("a", "prop", "propOfA")
*/
motor.assertFact(new Fact<String>("a", "prop", "propOfA"));
+ table1.setValue(new String[] { "_:0" });
+ table0.addValue(new String[] { "a", "propOfA" });
+ table3.addValue(new String[] { "_:0", "propOfA" });
+ table5.addValue(new String[] { "a", "_:0" });
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(logTable(
- "after assert (\"a\", \"prop\", \"propOfA\")",
- thirdMergeTable, secondMergeTable, firstMergeTable,
- assertedPremiseTable, blankNodePremiseTable));
+ "after assert (\"a\", \"prop\", \"propOfA\")", reteRule));
}
Iterator<IFact<String>> iterator = kb.view(null, "listOf", "a")
.iterator();
@@ -148,17 +160,21 @@
assertTableContent(firstExpected, secondMergeTable);
assertTableContent(firstExpected, thirdMergeTable);
+ assertEquals("", reteSimulator.check(reteRule));
+
/*
* assert ("b", "prop", "propOfB")
*/
motor.assertFact(new Fact<String>("b", "prop", "propOfB"));
+ table1.setValue(new String[] { "_:1" });
+ table0.addValue(new String[] { "b", "propOfB" });
+ table3.addValue(new String[] { "_:1", "propOfB" });
+ table5.addValue(new String[] { "b", "_:1" });
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(logTable(
- "after assert (\"b\", \"prop\", \"propOfB\")",
- thirdMergeTable, secondMergeTable, firstMergeTable,
- assertedPremiseTable, blankNodePremiseTable));
+ "after assert (\"b\", \"prop\", \"propOfB\")", reteRule));
}
iterator = kb.view(null, "listOf", "b").iterator();
assertTrue(iterator.hasNext());
@@ -173,17 +189,20 @@
assertTableContent(secondExpected, secondMergeTable);
assertTableContent(secondExpected, thirdMergeTable);
+ assertEquals("", reteSimulator.check(reteRule));
+
/*
* retract ("a", "prop", "propOfA")
*/
motor.retractFact(new Fact<String>("a", "prop", "propOfA"));
+ table0.removeValue(new String[] { "a", "propOfA" });
+ table3.removeValue(new String[] { "_:0", "propOfA" });
+ table5.removeValue(new String[] { "a", "_:0" });
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(logTable(
- "after retract (\"a\", \"prop\", \"propOfA\")",
- thirdMergeTable, secondMergeTable, firstMergeTable,
- assertedPremiseTable, blankNodePremiseTable));
+ "after retract (\"a\", \"prop\", \"propOfA\")", reteRule));
}
assertFalse(kb.contains("a", "list", firstListId));
final String[][] thirdExpected = new String[][] {
@@ -192,6 +211,7 @@
assertTableContent(thirdExpected, secondMergeTable);
assertTableContent(thirdExpected, thirdMergeTable);
+ assertEquals("", reteSimulator.check(reteRule));
}
private void assertTableContent(final String[][] expected,
@@ -217,35 +237,12 @@
assertEquals("bad list size", expectedLength, count);
}
- private String logTable(final String title,
- final MergeTable<String> thirdMergeTable,
- final MergeTable<String> secondMergeTable,
- final MergeTable<String> firstMergeTable,
- final AssertedPremiseTable<String> assertedPremiseTable,
- final BuiltinPremiseTable<String> blankNodePremiseTable) {
+ private String logTable(final String title, final ReteRule<String> reteRule) {
+ final ReteRuleToString<String> reteRuleToString = new ReteRuleToString<String>();
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(title);
stringBuilder.append('\n');
- stringBuilder.append(log("blank node premise table",
- blankNodePremiseTable));
- stringBuilder.append(log("first merge table", firstMergeTable));
- stringBuilder
- .append(log("asserted premise table", assertedPremiseTable));
- stringBuilder.append(log("second merge table", secondMergeTable));
- stringBuilder.append(log("third merge table", thirdMergeTable));
+ stringBuilder.append(reteRuleToString.toString(reteRule));
return stringBuilder.toString();
}
-
- private String log(final String title, final ITable<String> table) {
- final StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(title);
- stringBuilder.append('\n');
- final Iterator<List<String>> iterator = table.iterator();
- while (iterator.hasNext()) {
- final List<String> line = iterator.next();
- stringBuilder.append(line.toString());
- stringBuilder.append('\n');
- }
- return stringBuilder.toString();
- }
}
Modified: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/rete/entity/MockTable.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/rete/entity/MockTable.java 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/rete/entity/MockTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
+import net.sf.reasoner4j.Fortest;
import net.sf.reasoner4j.rete.entity.AbstractTable;
import net.sf.reasoner4j.rete.entity.IVariableTable;
@@ -52,4 +53,11 @@
public Iterator<List<T>> iterator() {
return variableTable.iterator();
}
+
+ @Fortest
+ @Override
+ public void accept(final ITableVisitor<T> visitor) {
+ visitor.beginVisit(this);
+ visitor.endVisit(this);
+ }
}
Added: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulator.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulator.java (rev 0)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulator.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j.retetest;
+
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import net.sf.reasoner4j.rete.entity.ITable;
+import net.sf.reasoner4j.rete.entity.ITableVisitor;
+import net.sf.reasoner4j.rete.entity.ReteRule;
+import net.sf.reasoner4j.rule.IPremise;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <T>
+ */
+public class ReteSimulator<T> implements ITableVisitor<T> {
+
+ private ReteSimulatorTable<T> rootTable;
+
+ private ReteSimulatorTable<T> currentTable;
+
+ private boolean leftVisited;
+
+ private int tableNumber;
+
+ private StringBuilder stringBuilder;
+
+ private Deque<String> resultStack;
+
+ public ReteSimulatorTable<T> addPremise(final IPremise<T> premise) {
+ final ReteSimulatorTable<T> result = new ReteSimulatorTable<T>(
+ tableNumber++, premise, false);
+ link(result);
+ return result;
+ }
+
+ public ReteSimulatorTable<T> addPremiseBuiltin(final IPremise<T> premise) {
+ final ReteSimulatorTable<T> result = new ReteSimulatorTable<T>(
+ tableNumber++, premise, true);
+ link(result);
+ return result;
+ }
+
+ private void link(final ReteSimulatorTable<T> result) {
+ if (rootTable == null) {
+ rootTable = result;
+ } else {
+ rootTable = new ReteSimulatorTable<T>(tableNumber++, rootTable,
+ result);
+ }
+ }
+
+ public ReteSimulatorTable<T> getRootTable() {
+ return rootTable;
+ }
+
+ public String check(final ReteRule<T> reteRule) {
+ currentTable = null;
+ leftVisited = false;
+ stringBuilder = new StringBuilder();
+ resultStack = new LinkedList<String>();
+ reteRule.accept(this);
+ final String result = stringBuilder.toString();
+ currentTable = null;
+ stringBuilder = null;
+ resultStack = null;
+ return result;
+ }
+
+ @Override
+ public void beginVisit(final ITable<T> table) {
+ if (currentTable == null) {
+ currentTable = rootTable;
+ } else if (leftVisited) {
+ currentTable = currentTable.getRightTable();
+ } else {
+ currentTable = currentTable.getLeftTable();
+ }
+ final Iterator<List<T>> iterator = table.iterator();
+ final String result = currentTable.assertValue(iterator);
+ resultStack.push(result);
+ }
+
+ @Override
+ public void endVisit(final ITable<T> table) {
+ final String result = resultStack.pop();
+ if (result != null) {
+ stringBuilder.append(result);
+ }
+ final ReteSimulatorTable<T> oldCurrentTable = currentTable;
+ currentTable = currentTable.getParentTable();
+ leftVisited = currentTable == null
+ || currentTable.getLeftTable() == oldCurrentTable;
+ }
+}
Added: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTable.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTable.java (rev 0)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTable.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,328 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j.retetest;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import net.sf.reasoner4j.ReasonerException;
+import net.sf.reasoner4j.rule.IPremise;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <T>
+ */
+public class ReteSimulatorTable<T> {
+
+ private ReteSimulatorTable<T> parentTable;
+
+ private final List<String> variableNameList = new ArrayList<String>();
+
+ private final Set<List<T>> valueTable = new HashSet<List<T>>();
+
+ private ReteSimulatorTable<T> leftTable;
+
+ private ReteSimulatorTable<T> rightTable;
+
+ private final int tableNumber;
+
+ private boolean builtin;
+
+ private List<T> settedValues;
+
+ public ReteSimulatorTable(final int tableNumber, final IPremise<T> premise,
+ final boolean builtin) {
+ this.tableNumber = tableNumber;
+ this.builtin = builtin;
+ String variableName = premise.getSubjectVariableName();
+ if (variableName != null) {
+ variableNameList.add(variableName);
+ }
+ variableName = premise.getPredicateVariableName();
+ if (variableName != null) {
+ variableNameList.add(variableName);
+ }
+ variableName = premise.getObjectVariableName();
+ if (variableName != null) {
+ variableNameList.add(variableName);
+ }
+ }
+
+ public ReteSimulatorTable(final int tableNumber,
+ final ReteSimulatorTable<T> leftTable,
+ final ReteSimulatorTable<T> rightTable) {
+ this.tableNumber = tableNumber;
+ this.leftTable = leftTable;
+ this.rightTable = rightTable;
+ leftTable.setParentTable(this);
+ rightTable.setParentTable(this);
+ variableNameList.addAll(leftTable.variableNameList);
+ for (String variableName : rightTable.variableNameList) {
+ if (!variableNameList.contains(variableName)) {
+ variableNameList.add(variableName);
+ }
+ }
+ }
+
+ public int getTableNumber() {
+ return tableNumber;
+ }
+
+ public ReteSimulatorTable<T> getParentTable() {
+ return parentTable;
+ }
+
+ public void setParentTable(final ReteSimulatorTable<T> parentTable) {
+ this.parentTable = parentTable;
+ }
+
+ public ReteSimulatorTable<T> getLeftTable() {
+ return leftTable;
+ }
+
+ public ReteSimulatorTable<T> getRightTable() {
+ return rightTable;
+ }
+
+ public void setValue(final T[] aValues) {
+ if (!builtin) {
+ throw new ReasonerException("only for builtin");
+ }
+ settedValues = toList(aValues);
+ valueTable.add(settedValues);
+ }
+
+ public void addValue(final T[] aValues) {
+ if (builtin) {
+ throw new ReasonerException("not for builtin");
+ }
+ final List<T> values = toList(aValues);
+ addValue(values);
+ }
+
+ private List<T> toList(final T[] aValues) {
+ final List<T> values = new ArrayList<T>(aValues.length);
+ for (T elt : aValues) {
+ values.add(elt);
+ }
+ return values;
+ }
+
+ public void addValue(final List<T> values) {
+ if (valueTable.add(values)) {
+ if (parentTable != null) {
+ parentTable.addValue(this, values);
+ }
+ }
+ }
+
+ public void addValue(final ReteSimulatorTable<T> childTable,
+ final List<T> childValues) {
+ final ReteSimulatorTable<T> otherChildTable;
+ if (childTable == leftTable) {
+ otherChildTable = rightTable;
+ } else {
+ otherChildTable = leftTable;
+ }
+ final List<T> otherChildKey = otherChildKey(childTable, childValues,
+ otherChildTable);
+
+ final Iterator<List<T>> iterator = otherChildTable
+ .select(otherChildKey);
+ while (iterator.hasNext()) {
+ final List<T> otherValue = iterator.next();
+ final List<T> thisValue = thisValue(childTable, childValues,
+ otherChildTable, otherValue);
+ // valueTable.add(thisValue);
+ addValue(thisValue);
+ }
+ }
+
+ public void removeValue(final T[] aValues) {
+ final List<T> values = toList(aValues);
+ removeValue(values);
+ }
+
+ public void removeValue(final List<T> values) {
+ if (valueTable.remove(values)) {
+ if (parentTable != null) {
+ parentTable.removeValue(this, values);
+ }
+ }
+ }
+
+ public void removeValue(final ReteSimulatorTable<T> childTable,
+ final List<T> childValues) {
+ final ReteSimulatorTable<T> otherChildTable;
+ if (childTable == leftTable) {
+ otherChildTable = rightTable;
+ } else {
+ otherChildTable = leftTable;
+ }
+ final List<T> otherChildKey = otherChildKey(childTable, childValues,
+ otherChildTable);
+
+ final Iterator<List<T>> iterator = otherChildTable
+ .removeSelect(otherChildKey);
+ while (iterator.hasNext()) {
+ final List<T> otherValue = iterator.next();
+ final List<T> thisValue = thisValue(childTable, childValues,
+ otherChildTable, otherValue);
+ // valueTable.remove(thisValue);
+ removeValue(thisValue);
+ }
+ }
+
+ private List<T> otherChildKey(final ReteSimulatorTable<T> childTable,
+ final List<T> childValues,
+ final ReteSimulatorTable<T> otherChildTable) {
+ final List<T> otherChildKey = new ArrayList<T>(
+ otherChildTable.variableNameList.size());
+ for (int count = 0; count < otherChildTable.variableNameList.size(); count++) {
+ otherChildKey.add(null);
+ }
+ int otherValueIndex = 0;
+ for (String variableName : otherChildTable.variableNameList) {
+ final int index = childTable.variableNameList.indexOf(variableName);
+ if (index != -1) {
+ otherChildKey.set(otherValueIndex, childValues.get(index));
+ }
+ otherValueIndex++;
+ }
+ return otherChildKey;
+ }
+
+ private List<T> thisValue(final ReteSimulatorTable<T> childTable,
+ final List<T> childValues,
+ final ReteSimulatorTable<T> otherChildTable,
+ final List<T> otherValue) {
+ final List<T> thisValue = new ArrayList<T>(variableNameList.size());
+ for (String variableName : variableNameList) {
+ int index = childTable.variableNameList.indexOf(variableName);
+ if (index == -1) {
+ index = otherChildTable.variableNameList.indexOf(variableName);
+ if (index == -1) {
+ throw new ReasonerException("must found");
+ }
+ thisValue.add(otherValue.get(index));
+ } else {
+ thisValue.add(childValues.get(index));
+ }
+ }
+ return thisValue;
+ }
+
+ private Iterator<List<T>> select(final List<T> key) {
+ return select(key, false);
+ }
+
+ private Iterator<List<T>> removeSelect(final List<T> key) {
+ return select(key, true);
+ }
+
+ private Iterator<List<T>> select(final List<T> key, final boolean remove) {
+ final List<List<T>> selection = new LinkedList<List<T>>();
+ if (builtin && !remove) {
+ if (settedValues != null) {
+ selection.add(settedValues);
+ settedValues = null;
+ }
+ } else {
+ for (List<T> entry : valueTable) {
+ boolean match = true;
+ final Iterator<T> iteratorOnKey = key.iterator();
+ final Iterator<T> iteratorOnEntry = entry.iterator();
+ while (match && iteratorOnEntry.hasNext()) {
+ final T keyElt = iteratorOnKey.next();
+ final T entryElt = iteratorOnEntry.next();
+ if (keyElt != null) {
+ match = keyElt.equals(entryElt);
+ }
+ }
+ if (match) {
+ selection.add(entry);
+ }
+ }
+ if (remove) {
+ valueTable.removeAll(selection);
+ }
+ }
+ return selection.iterator();
+ }
+
+ public boolean assertValue(final T[][] values) {
+ if (values.length != valueTable.size()) {
+ return false;
+ }
+ for (T[] entry : values) {
+ if (!valueTable.contains(toList(entry))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public String assertValue(final Iterator<List<T>> iterator) {
+ final List<List<T>> otherList = new LinkedList<List<T>>();
+ final List<String> diff = new LinkedList<String>();
+ while (iterator.hasNext()) {
+ final List<T> next = iterator.next();
+ if (!valueTable.contains(next)) {
+ diff.add(next.toString() + " not on simulator table");
+ }
+ otherList.add(next);
+ }
+ for (List<T> value : valueTable) {
+ if (!otherList.contains(value)) {
+ diff.add(value.toString() + " not in rete");
+ }
+ }
+ final String result;
+ if (diff.isEmpty()) {
+ result = null;
+ } else {
+ final StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("table #");
+ stringBuilder.append(tableNumber);
+ stringBuilder.append('\n');
+ stringBuilder.append(variableNameList.toString());
+ stringBuilder.append('\n');
+ for (String value : diff) {
+ stringBuilder.append(value);
+ stringBuilder.append('\n');
+ }
+ result = stringBuilder.toString();
+ }
+ return result;
+ }
+
+ public Iterator<List<T>> iterator() {
+ return valueTable.iterator();
+ }
+}
Added: trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTest.java
===================================================================
--- trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTest.java (rev 0)
+++ trunk/reasoner4j/src/test/java/net/sf/reasoner4j/retetest/ReteSimulatorTest.java 2010-12-23 18:15:10 UTC (rev 36)
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2010 Luc Peuvrier
+ *
+ * This file is a part of reasoner4j.
+ *
+ * reasoner4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License.
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * reasoner4j is distributed in the hope that it will be useful, but
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.reasoner4j.retetest;
+
+import junit.framework.TestCase;
+import net.sf.reasoner4j.rule.Premise;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+public class ReteSimulatorTest extends TestCase {
+
+ public void test() {
+ final ReteSimulator<String> reteSimulator = new ReteSimulator<String>();
+ ReteSimulatorTable<String> firstTable = reteSimulator
+ .addPremise(new Premise<String>(
+ /**/null, "subject",
+ /**/"?P", null,
+ /**/null, "object"));
+ ReteSimulatorTable<String> secondTable = reteSimulator
+ .addPremise(new Premise<String>(
+ /**/"?S", null,
+ /**/"?P", null,
+ /**/null, "object"));
+ ReteSimulatorTable<String> rootTable = reteSimulator.getRootTable();
+
+ assertTrue(rootTable.assertValue(new String[][] {}));
+ firstTable.addValue(new String[] { "a" });
+ assertTrue(rootTable.assertValue(new String[][] {}));
+ secondTable.addValue(new String[] { "x", "y" });
+ assertTrue(rootTable.assertValue(new String[][] {}));
+ secondTable.addValue(new String[] { "x", "a" });
+ assertTrue(rootTable.assertValue(new String[][] { { "a", "x" } }));
+ firstTable.addValue(new String[] { "y" });
+ assertTrue(rootTable.assertValue(new String[][] { { "a", "x" },
+ { "y", "x" } }));
+ secondTable.removeValue(new String[] { "x", "a" });
+ assertTrue(rootTable.assertValue(new String[][] { { "y", "x" } }));
+ }
+}
Modified: trunk/reasoner4j/src/test/resources/log4j.properties
===================================================================
--- trunk/reasoner4j/src/test/resources/log4j.properties 2010-12-21 05:45:04 UTC (rev 35)
+++ trunk/reasoner4j/src/test/resources/log4j.properties 2010-12-23 18:15:10 UTC (rev 36)
@@ -19,6 +19,6 @@
log4j.logger.net.sf.joafip.meminspector=warn
log4j.logger.net.sf.joafip.java.util=warn
-#log4j.logger.net.sf.reasoner4j.TestReteTable=debug
+log4j.logger.net.sf.reasoner4j.TestReteTable=debug
#log4j.logger.net.sf.reasoner4j.TestExistential=debug
-log4j.logger.net.sf.reasoner4j.TestBasicInference=debug
\ No newline at end of file
+#log4j.logger.net.sf.reasoner4j.TestBasicInference=debug
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|