GetFactHandlesCommand implements
    ExecutableCommand<Collection<FactHandle>> { public Collection<FactHandle> execute( Context context ) { KieSession ksession = ((RegistryContext) context).lookup( KieSession.class ); Collection<FactHandle> disconnectedFactHandles = new ArrayList<FactHandle>(); if ( filter != null ) { Collection<InternalFactHandle> factHandles = ksession.getFactHandles( this.filter ); if(factHandles != null && disconnected){ for(InternalFactHandle factHandle: factHandles){ InternalFactHandle handle = factHandle.clone(); handle.disconnect(); disconnectedFactHandles.add(handle); } if (outIdentifier != null) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, disconnectedFactHandles ); } return disconnectedFactHandles; } else { Collection<FactHandle> ksessionFactHandles = ksession.getFactHandles( this.filter ); if (outIdentifier != null) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, new ArrayList<FactHandle>(ksessionFactHandles)); } return ksessionFactHandles; } } else { Collection<InternalFactHandle> factHandles = ksession.getFactHandles( ); if(factHandles != null && disconnected){ for(InternalFactHandle factHandle: factHandles){ InternalFactHandle handle = factHandle.clone(); handle.disconnect(); disconnectedFactHandles.add(handle); } if (outIdentifier != null) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, disconnectedFactHandles); } return disconnectedFactHandles; } else { Collection<FactHandle> ksessionFactHandles = ksession.getFactHandles(); if (outIdentifier != null) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, new ArrayList<FactHandle>(ksessionFactHandles)); } return ksessionFactHandles; } } } GetFactHandlesCommand();  GetFactHandlesCommand(ObjectFilter filter);  GetFactHandlesCommand(ObjectFilter filter, boolean disconnected);  GetFactHandlesCommand(boolean disconnected); boolean isDisconnected(); void setDisconnected( boolean disconnected ); Collection<FactHandle> execute( Context context ); String getOutIdentifier(); void setOutIdentifier(String outIdentifier); String toString();  }
@Test public void getOneFactHandleTest() { String randomFact = "" + random.nextLong(); ksession.insert(randomFact); GetFactHandlesCommand command = new GetFactHandlesCommand(); Object result = runner.execute(command, context); verifyThatCollectionContains1FactHandleWithThisFact(randomFact, result); }
@Test public void getMultipleFactHandleTest() { HashSet<String> factSet = new HashSet<String>(); int numFacts = 4; for( int i = 0; i < numFacts; ++i ) { factSet.add("" + random.nextInt()); } for( String fact : factSet ) { ksession.insert(fact); } GetFactHandlesCommand command = new GetFactHandlesCommand(); Object result = runner.execute(command, context); verifyThatCollectionContainsTheseFactHandle(factSet, result); }
@Test public void getEmptyDisconnectedFactHandlesTest() { GetFactHandlesCommand command = new GetFactHandlesCommand(true); Object result = runner.execute(command, context); if( result instanceof Collection<?> ) { assertNotNull(result); assertTrue(((Collection<?>) result).isEmpty()); } else { fail("result of command was NOT a collection of FactHandles"); } }
@Test public void getOneDisconnectedFactHandleTest() { System.out.println( Thread.currentThread().getStackTrace()[1].getMethodName() ); String randomFact = "" + random.nextLong(); ksession.insert(randomFact); GetFactHandlesCommand command = new GetFactHandlesCommand(false); Object result = runner.execute(command, context); verifyThatCollectionContains1FactHandleWithThisFact(randomFact, result); FactHandle factHandle = (FactHandle) ((Collection<FactHandle>) result).toArray()[0]; command = new GetFactHandlesCommand(false); result = runner.execute(command, context); verifyThatCollectionContains1FactHandleWithThisFact(randomFact, result); FactHandle connectedFactHandle = (FactHandle) ((Collection<FactHandle>) result).toArray()[0]; command = new GetFactHandlesCommand(true); result = runner.execute(command, context); verifyThatCollectionContains1FactHandleWithThisFact(randomFact, result); FactHandle disconnectedFactHandle = (FactHandle) ((Collection<FactHandle>) result).toArray()[0]; assertTrue( factHandle == connectedFactHandle ); assertTrue( ! (factHandle == disconnectedFactHandle) ); }
@Test public void getMultipleDisconnectedFactHandleTest() { System.out.println( "\nTest: " + Thread.currentThread().getStackTrace()[1].getMethodName() ); HashSet<String> factSet = new HashSet<String>(); int numFacts = 4; for( int i = 0; i < numFacts; ++i ) { factSet.add("" + random.nextInt()); } for( String fact : factSet ) { ksession.insert(fact); } GetFactHandlesCommand command = new GetFactHandlesCommand(false); Object result = runner.execute(command, context); verifyThatCollectionContainsTheseFactHandle(factSet, result); Collection<FactHandle> factHandles = ((Collection<FactHandle>) result); command = new GetFactHandlesCommand(false); result = runner.execute(command, context); verifyThatCollectionContainsTheseFactHandle(factSet, result); Collection<FactHandle> connectedFactHandles = ((Collection<FactHandle>) result); command = new GetFactHandlesCommand(true); result = runner.execute(command, context); verifyThatCollectionContainsTheseFactHandle(factSet, result); Collection<FactHandle> disconnectedFactHandles = ((Collection<FactHandle>) result); HashSet<FactHandle> factHandlesCopy = new HashSet<FactHandle>(factHandles); for( int i = 0; i < connectedFactHandles.size(); ++i ) { for( Object connectedFact : connectedFactHandles ) { Iterator<FactHandle> iter = factHandlesCopy.iterator(); while(iter.hasNext() ) { Object fact = iter.next(); if( fact == connectedFact ) { iter.remove(); } } } } assertTrue( factHandlesCopy.isEmpty() ); for( int i = 0; i < disconnectedFactHandles.size(); ++i ) { for( Object disconnectedFact : disconnectedFactHandles ) { for( Object fact : factHandles ) { assertTrue( ! (fact == disconnectedFact) ); } } } assertTrue( factHandles.size() == disconnectedFactHandles.size() ); }
@Test public void getEmptyFactHandlesTest() { GetFactHandlesCommand command = new GetFactHandlesCommand(); Object result = runner.execute(command, context); if( result instanceof Collection<?> ) { assertNotNull(result); assertTrue(((Collection<?>) result).isEmpty()); } else { fail("result of command was NOT a collection of FactHandles"); } }