PageRenderTime 89ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/drools-core/src/main/java/org/drools/core/reteoo/RightInputAdapterNode.java

https://github.com/esteban-aliverti/drools
Java | 377 lines | 259 code | 60 blank | 58 comment | 19 complexity | ad60d87a49a9278925003ba0d4b9cc70 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.reteoo;
  17. import java.io.IOException;
  18. import java.io.ObjectInput;
  19. import java.io.ObjectOutput;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.drools.core.RuleBaseConfiguration;
  23. import org.drools.core.common.InternalFactHandle;
  24. import org.drools.core.common.InternalRuleBase;
  25. import org.drools.core.common.InternalWorkingMemory;
  26. import org.drools.core.common.Memory;
  27. import org.drools.core.common.MemoryFactory;
  28. import org.drools.core.common.PropagationContextFactory;
  29. import org.drools.core.common.UpdateContext;
  30. import org.drools.core.util.AbstractBaseLinkedListNode;
  31. import org.drools.core.util.ObjectHashMap;
  32. import org.drools.core.marshalling.impl.PersisterHelper;
  33. import org.drools.core.marshalling.impl.ProtobufInputMarshaller;
  34. import org.drools.core.marshalling.impl.ProtobufMessages;
  35. import org.drools.core.reteoo.builder.BuildContext;
  36. import org.drools.core.spi.PropagationContext;
  37. /**
  38. * When joining a subnetwork into the main network again, RightInputAdapterNode adapts the
  39. * subnetwork's tuple into a fact in order right join it with the tuple being propagated in
  40. * the main network.
  41. */
  42. public class RightInputAdapterNode extends ObjectSource
  43. implements
  44. LeftTupleSinkNode,
  45. MemoryFactory {
  46. private static final long serialVersionUID = 510l;
  47. private LeftTupleSource tupleSource;
  48. private LeftTupleSource startTupleSource;
  49. private boolean tupleMemoryEnabled;
  50. private LeftTupleSinkNode previousTupleSinkNode;
  51. private LeftTupleSinkNode nextTupleSinkNode;
  52. public RightInputAdapterNode() {
  53. }
  54. /**
  55. * Constructor specifying the unique id of the node in the Rete network, the position of the propagating <code>FactHandleImpl</code> in
  56. * <code>ReteTuple</code> and the source that propagates the receive <code>ReteTuple<code>s.
  57. *
  58. * @param id
  59. * Unique id
  60. * @param source
  61. * The <code>TupleSource</code> which propagates the received <code>ReteTuple</code>
  62. */
  63. public RightInputAdapterNode(final int id,
  64. final LeftTupleSource source,
  65. final LeftTupleSource startTupleSource,
  66. final BuildContext context) {
  67. super( id,
  68. context.getPartitionId(),
  69. context.getRuleBase().getConfiguration().isMultithreadEvaluation() );
  70. this.tupleSource = source;
  71. this.tupleMemoryEnabled = context.isTupleMemoryEnabled();
  72. this.startTupleSource = startTupleSource;
  73. }
  74. public void readExternal(ObjectInput in) throws IOException,
  75. ClassNotFoundException {
  76. super.readExternal( in );
  77. tupleSource = (LeftTupleSource) in.readObject();
  78. tupleMemoryEnabled = in.readBoolean();
  79. previousTupleSinkNode = (LeftTupleSinkNode) in.readObject();
  80. nextTupleSinkNode = (LeftTupleSinkNode) in.readObject();
  81. startTupleSource = ( LeftTupleSource ) in.readObject();
  82. }
  83. public void writeExternal(ObjectOutput out) throws IOException {
  84. super.writeExternal( out );
  85. out.writeObject( tupleSource );
  86. out.writeBoolean( tupleMemoryEnabled );
  87. out.writeObject( previousTupleSinkNode );
  88. out.writeObject( nextTupleSinkNode );
  89. out.writeObject( startTupleSource );
  90. }
  91. public LeftTupleSource getStartTupleSource() {
  92. return startTupleSource;
  93. }
  94. public void setStartTupleSource(LeftTupleSource startTupleSource) {
  95. this.startTupleSource = startTupleSource;
  96. }
  97. /**
  98. * Creates and return the node memory
  99. */
  100. public Memory createMemory(final RuleBaseConfiguration config, InternalWorkingMemory wm) {
  101. RiaNodeMemory rianMem = new RiaNodeMemory();
  102. RiaPathMemory pmem = new RiaPathMemory(this);
  103. AbstractTerminalNode.initPathMemory(pmem, getLeftTupleSource(), getStartTupleSource(), wm, null );
  104. rianMem.setRiaPathMemory(pmem);
  105. return rianMem;
  106. }
  107. public LeftTuple createPeer(LeftTuple original) {
  108. JoinNodeLeftTuple peer = new JoinNodeLeftTuple();
  109. peer.initPeer( (BaseLeftTuple) original, this );
  110. original.setPeer( peer );
  111. return peer;
  112. }
  113. @SuppressWarnings("unchecked")
  114. public InternalFactHandle createFactHandle(final LeftTuple leftTuple,
  115. final PropagationContext context,
  116. final InternalWorkingMemory workingMemory) {
  117. InternalFactHandle handle = null;
  118. ProtobufMessages.FactHandle _handle = null;
  119. if( context.getReaderContext() != null ) {
  120. Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle> map = (Map<ProtobufInputMarshaller.TupleKey, ProtobufMessages.FactHandle>) context.getReaderContext().nodeMemories.get( getId() );
  121. if( map != null ) {
  122. _handle = map.get( PersisterHelper.createTupleKey( leftTuple ) );
  123. }
  124. }
  125. if( _handle != null ) {
  126. // create a handle with the given id
  127. handle = workingMemory.getFactHandleFactory().newFactHandle( _handle.getId(),
  128. leftTuple,
  129. _handle.getRecency(),
  130. workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( context.getEntryPoint(),
  131. leftTuple ),
  132. workingMemory,
  133. null ); // so far, result is not an event
  134. } else {
  135. handle = workingMemory.getFactHandleFactory().newFactHandle( leftTuple,
  136. workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( context.getEntryPoint(),
  137. leftTuple ),
  138. workingMemory,
  139. null ); // so far, result is not an event
  140. }
  141. return handle;
  142. }
  143. public void attach( BuildContext context ) {
  144. this.tupleSource.addTupleSink( this, context );
  145. if (context == null || context.getRuleBase().getConfiguration().isPhreakEnabled() ) {
  146. return;
  147. }
  148. for ( InternalWorkingMemory workingMemory : context.getWorkingMemories() ) {
  149. PropagationContextFactory pctxFactory =((InternalRuleBase)workingMemory.getRuleBase()).getConfiguration().getComponentFactory().getPropagationContextFactory();
  150. final PropagationContext propagationContext = pctxFactory.createPropagationContext(workingMemory.getNextPropagationIdCounter(), PropagationContext.RULE_ADDITION, null, null, null);
  151. this.tupleSource.updateSink( this,
  152. propagationContext,
  153. workingMemory );
  154. }
  155. }
  156. public void networkUpdated(UpdateContext updateContext) {
  157. this.tupleSource.networkUpdated(updateContext);
  158. }
  159. protected void doRemove(final RuleRemovalContext context,
  160. final ReteooBuilder builder,
  161. final InternalWorkingMemory[] workingMemories) {
  162. if ( !isInUse() ) {
  163. tupleSource.removeTupleSink(this);
  164. }
  165. }
  166. public boolean isLeftTupleMemoryEnabled() {
  167. return tupleMemoryEnabled;
  168. }
  169. public void setLeftTupleMemoryEnabled(boolean tupleMemoryEnabled) {
  170. this.tupleMemoryEnabled = tupleMemoryEnabled;
  171. }
  172. /**
  173. * Returns the next node
  174. * @return
  175. * The next TupleSinkNode
  176. */
  177. public LeftTupleSinkNode getNextLeftTupleSinkNode() {
  178. return this.nextTupleSinkNode;
  179. }
  180. /**
  181. * Sets the next node
  182. * @param next
  183. * The next TupleSinkNode
  184. */
  185. public void setNextLeftTupleSinkNode(final LeftTupleSinkNode next) {
  186. this.nextTupleSinkNode = next;
  187. }
  188. /**
  189. * Returns the previous node
  190. * @return
  191. * The previous TupleSinkNode
  192. */
  193. public LeftTupleSinkNode getPreviousLeftTupleSinkNode() {
  194. return this.previousTupleSinkNode;
  195. }
  196. /**
  197. * Sets the previous node
  198. * @param previous
  199. * The previous TupleSinkNode
  200. */
  201. public void setPreviousLeftTupleSinkNode(final LeftTupleSinkNode previous) {
  202. this.previousTupleSinkNode = previous;
  203. }
  204. public short getType() {
  205. return NodeTypeEnums.RightInputAdaterNode;
  206. }
  207. public int hashCode() {
  208. return this.tupleSource.hashCode() * 17 + ((this.tupleMemoryEnabled) ? 1234 : 4321);
  209. }
  210. /*
  211. * (non-Javadoc)
  212. *
  213. * @see java.lang.Object#equals(java.lang.Object)
  214. */
  215. public boolean equals(final Object object) {
  216. if ( this == object ) {
  217. return true;
  218. }
  219. if ( object == null || !(object instanceof RightInputAdapterNode) ) {
  220. return false;
  221. }
  222. final RightInputAdapterNode other = (RightInputAdapterNode) object;
  223. return this.tupleMemoryEnabled == other.tupleMemoryEnabled && this.tupleSource.equals( other.tupleSource );
  224. }
  225. @Override
  226. public String toString() {
  227. return "RightInputAdapterNode(" + id + ")[ tupleMemoryEnabled=" + tupleMemoryEnabled + ", tupleSource=" + tupleSource + ", source="
  228. + source + ", associations=" + associations.keySet() + ", partitionId=" + partitionId + "]";
  229. }
  230. public LeftTuple createLeftTuple(InternalFactHandle factHandle,
  231. LeftTupleSink sink,
  232. boolean leftTupleMemoryEnabled) {
  233. return new JoinNodeLeftTuple(factHandle, sink, leftTupleMemoryEnabled );
  234. }
  235. public LeftTuple createLeftTuple(final InternalFactHandle factHandle,
  236. final LeftTuple leftTuple,
  237. final LeftTupleSink sink) {
  238. return new JoinNodeLeftTuple(factHandle,leftTuple, sink );
  239. }
  240. public LeftTuple createLeftTuple(LeftTuple leftTuple,
  241. LeftTupleSink sink,
  242. PropagationContext pctx,
  243. boolean leftTupleMemoryEnabled) {
  244. return new JoinNodeLeftTuple(leftTuple,sink, pctx, leftTupleMemoryEnabled );
  245. }
  246. public LeftTuple createLeftTuple(LeftTuple leftTuple,
  247. RightTuple rightTuple,
  248. LeftTupleSink sink) {
  249. return new JoinNodeLeftTuple(leftTuple, rightTuple, sink );
  250. }
  251. public LeftTuple createLeftTuple(LeftTuple leftTuple,
  252. RightTuple rightTuple,
  253. LeftTuple currentLeftChild,
  254. LeftTuple currentRightChild,
  255. LeftTupleSink sink,
  256. boolean leftTupleMemoryEnabled) {
  257. return new JoinNodeLeftTuple(leftTuple, rightTuple, currentLeftChild, currentRightChild, sink, leftTupleMemoryEnabled );
  258. }
  259. public LeftTupleSource getLeftTupleSource() {
  260. return this.tupleSource;
  261. }
  262. public ObjectTypeNode.Id getLeftInputOtnId() {
  263. throw new UnsupportedOperationException();
  264. }
  265. public void setLeftInputOtnId(ObjectTypeNode.Id leftInputOtnId) {
  266. throw new UnsupportedOperationException();
  267. }
  268. @Override
  269. public long calculateDeclaredMask(List<String> settableProperties) {
  270. throw new UnsupportedOperationException();
  271. }
  272. public static class RiaNodeMemory extends AbstractBaseLinkedListNode<Memory> implements Memory {
  273. private RiaPathMemory pathMemory;
  274. public RiaNodeMemory() {
  275. }
  276. public RiaPathMemory getRiaPathMemory() {
  277. return pathMemory;
  278. }
  279. public void setRiaPathMemory(RiaPathMemory pathMemory) {
  280. this.pathMemory = pathMemory;
  281. }
  282. public SegmentMemory getSegmentMemory() {
  283. return pathMemory.getSegmentMemory();
  284. }
  285. public void setSegmentMemory(SegmentMemory segmentMemory) {
  286. pathMemory.setSegmentMemory(segmentMemory);
  287. }
  288. public short getNodeType() {
  289. return NodeTypeEnums.RightInputAdaterNode;
  290. }
  291. }
  292. public long getLeftInferredMask() {
  293. throw new UnsupportedOperationException();
  294. }
  295. public void modifyLeftTuple(InternalFactHandle factHandle,
  296. ModifyPreviousTuples modifyPreviousTuples,
  297. PropagationContext context,
  298. InternalWorkingMemory workingMemory) {
  299. throw new UnsupportedOperationException( "This method should never be called" );
  300. }
  301. @Override
  302. public void modifyLeftTuple(LeftTuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory) {
  303. throw new UnsupportedOperationException();
  304. }
  305. @Override
  306. public void updateSink(ObjectSink sink, PropagationContext context, InternalWorkingMemory workingMemory) {
  307. throw new UnsupportedOperationException();
  308. }
  309. @Override
  310. public void assertLeftTuple(LeftTuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory) {
  311. throw new UnsupportedOperationException();
  312. }
  313. @Override
  314. public void retractLeftTuple(LeftTuple leftTuple, PropagationContext context, InternalWorkingMemory workingMemory) {
  315. throw new UnsupportedOperationException();
  316. }
  317. }