/drools-core/src/main/java/org/drools/core/common/TruthMaintenanceSystem.java

https://github.com/esteban-aliverti/drools · Java · 160 lines · 99 code · 26 blank · 35 comment · 6 complexity · bc17feea7123d6ee54ec3741aa6e625b MD5 · raw file

  1. /*
  2. * Copyright 2005 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.core.common;
  17. import java.io.IOException;
  18. import java.io.ObjectInput;
  19. import java.io.ObjectOutput;
  20. import org.drools.core.FactException;
  21. import org.drools.core.beliefsystem.BeliefSet;
  22. import org.drools.core.beliefsystem.BeliefSystem;
  23. import org.drools.core.util.ObjectHashMap;
  24. import org.drools.core.impl.StatefulKnowledgeSessionImpl;
  25. import org.drools.core.marshalling.impl.MarshallerReaderContext;
  26. import org.drools.core.marshalling.impl.MarshallerWriteContext;
  27. import org.drools.core.marshalling.impl.PersisterHelper;
  28. import org.drools.core.marshalling.impl.ProtobufMessages;
  29. import org.drools.core.marshalling.impl.ProtobufMessages.ActionQueue.Action;
  30. import org.drools.core.marshalling.impl.ProtobufMessages.ActionQueue.LogicalRetract;
  31. import org.drools.core.reteoo.ObjectTypeConf;
  32. import org.drools.core.rule.Rule;
  33. import org.drools.core.spi.Activation;
  34. import org.drools.core.spi.PropagationContext;
  35. /**
  36. * The Truth Maintenance System is responsible for tracking two things. Firstly
  37. * It maintains a Map to track the classes with the same Equality, using the
  38. * EqualityKey. The EqualityKey has an internal datastructure which references
  39. * all the handles which are equal. Secondly It maintains another map tracking
  40. * the justificiations for logically asserted facts.
  41. */
  42. public class TruthMaintenanceSystem {
  43. private AbstractWorkingMemory wm;
  44. private NamedEntryPoint ep;
  45. private ObjectHashMap equalityKeyMap;
  46. private BeliefSystem beliefSystem;
  47. public TruthMaintenanceSystem() {}
  48. public TruthMaintenanceSystem(final AbstractWorkingMemory wm,
  49. NamedEntryPoint ep) {
  50. this.wm = wm;
  51. //this.justifiedMap = new ObjectHashMap();
  52. this.equalityKeyMap = new ObjectHashMap();
  53. this.equalityKeyMap.setComparator( EqualityKeyComparator.getInstance() );
  54. InternalRuleBase rbase = ( InternalRuleBase ) wm.getRuleBase();
  55. beliefSystem = rbase.getConfiguration().getComponentFactory().getBeliefSystemFactory().createBeliefSystem(wm.getSessionConfiguration().getBeliefSystemType(), ep, this);
  56. }
  57. public ObjectHashMap getEqualityKeyMap() {
  58. return this.equalityKeyMap;
  59. }
  60. public Object put(final EqualityKey key) {
  61. return this.equalityKeyMap.put( key,
  62. key,
  63. false );
  64. }
  65. public EqualityKey get(final EqualityKey key) {
  66. return (EqualityKey) this.equalityKeyMap.get( key );
  67. }
  68. public EqualityKey get(final Object object) {
  69. return (EqualityKey) this.equalityKeyMap.get( object );
  70. }
  71. public EqualityKey remove(final EqualityKey key) {
  72. return (EqualityKey) this.equalityKeyMap.remove( key );
  73. }
  74. /**
  75. * Adds a justification for the FactHandle to the justifiedMap.
  76. *
  77. * @param handle
  78. * @param activation
  79. * @param context
  80. * @param rule
  81. * @param typeConf
  82. * @throws org.drools.core.FactException
  83. */
  84. public void readLogicalDependency(final InternalFactHandle handle,
  85. final Object object,
  86. final Object value,
  87. final Activation activation,
  88. final PropagationContext context,
  89. final Rule rule,
  90. final ObjectTypeConf typeConf) throws FactException {
  91. addLogicalDependency( handle, object, value, activation, context, rule, typeConf, true );
  92. }
  93. public void addLogicalDependency(final InternalFactHandle handle,
  94. final Object object,
  95. final Object value,
  96. final Activation activation,
  97. final PropagationContext context,
  98. final Rule rule,
  99. final ObjectTypeConf typeConf) throws FactException {
  100. addLogicalDependency( handle, object, value, activation, context, rule, typeConf, false );
  101. }
  102. public void addLogicalDependency(final InternalFactHandle handle,
  103. final Object object,
  104. final Object value,
  105. final Activation activation,
  106. final PropagationContext context,
  107. final Rule rule,
  108. final ObjectTypeConf typeConf,
  109. final boolean read) throws FactException {
  110. BeliefSet beliefSet = handle.getEqualityKey().getBeliefSet();
  111. if ( beliefSet == null ) {
  112. if ( context.getType() == PropagationContext.MODIFICATION ) {
  113. // if this was a update, chances are its trying to retract a logical assertion
  114. }
  115. beliefSet = beliefSystem.newBeliefSet( handle );
  116. handle.getEqualityKey().setBeliefSet( beliefSet );
  117. }
  118. final LogicalDependency node = beliefSystem.newLogicalDependency( activation, beliefSet, object, value );
  119. activation.getRule().setHasLogicalDependency( true );
  120. activation.addLogicalDependency( node );
  121. if ( read ) {
  122. // used when deserialising
  123. beliefSystem.read( node, beliefSet, context, typeConf );
  124. } else {
  125. beliefSystem.insert( node, beliefSet, context, typeConf );
  126. }
  127. }
  128. public void clear() {
  129. this.equalityKeyMap.clear();
  130. }
  131. public BeliefSystem getBeliefSystem() {
  132. return beliefSystem;
  133. }
  134. }