//import input.InputReader; import java.util.ArrayList; //import java.security.InvalidParameterException; //import org.w3c.dom.*; //import actor.*; import data.*; import game.*; //import language.*; //import logic.*; //import transaction.*; import participant.*; /** * @author Rukmal Fernando * @author Hardik Kotecha * @author Radhika Srinivasan */ public class TestAndRun { public static void main(String[] args) { try { /* // Some Test Data Money m1 = new Money( 23.54 ); Money m2 = new Money( 11.01 ); Money m3 = new Money( new Float( 45.66 ) ); Price price1 = new Price( new Float( 0.5 ) ); Price price2 = new Price( 0.8 ); // test for invalid price boolean exceptionThrown = false; try { new Price( 101 ); } catch ( InvalidParameterException ipe ) { exceptionThrown = true; } assert( exceptionThrown ); Quality q1 = new Quality( 0.4 ); // test for invalid quality exceptionThrown = false; try { new Quality( new Float( 101 )); } catch ( InvalidParameterException ipe) { exceptionThrown = true; } assert( exceptionThrown ); ArrayList emptyDerivativeList = new ArrayList(); Player p1 = new Player( "Bob", m1, false ); Player p2 = new Player( "Fred", m2, true ); Player p3 = new Player( "Lucy", m3, false ); ArrayList players = new ArrayList(); players.add(p1); players.add(p2); players.add(p3); ArrayList expected = new ArrayList(); expected.add(p3); expected.add(p1); expected.add(p2); Derivative d1 = new Derivative( "d1", "bob", price1, null ); DigitalSignature testDigitalSignature = new DigitalSignature(); Transaction testCreate = new Create( "", d1 ); Transaction testBuy = new Buy( "", d1 ); Transaction testDeliver = new Deliver( "", d1 ); Transaction testFinish = new Finish( "", d1 ); History testHistory = History.getHistory(); History.add( testCreate ); History.add( testBuy ); History.add( testDeliver ); History.add( testFinish ); Administrator a1 = new Administrator( players ); SpeckerDerivativeGame g1 = new SpeckerDerivativeGame(); ArrayList sortedPlayers = a1.sortPlayers(); assert(sortedPlayers.size() == expected.size()); for( int i = 0; i < sortedPlayers.size(); i++ ) { Player thePlayer = sortedPlayers.get( i ); Player expectedPlayer = expected.get( i ); System.out.println(" player: " + expectedPlayer.getName() + ": " + expectedPlayer.getAccount().getAmount().toString() ); System.out.println(" player: " + thePlayer.getName() + ": " + thePlayer.getAccount().getAmount().toString() ); assert( (thePlayer.getName() ).compareTo( (expectedPlayer.getName() ) ) == 0 ); } InputReader xmlReader = InputReader.initialize(); Document testDoc = xmlReader.prepareDocument( "XML/RawMaterial.xml" ); RawMaterial testRawMaterial = xmlReader.parseRawMaterial( testDoc ); Document testDoc2 = xmlReader.prepareDocument( "XML/RawMaterial2.xml" ); RawMaterial testRawMaterial2 = xmlReader.parseRawMaterial( testDoc2 ); Document testDoc3 = xmlReader.prepareDocument( "XML/FinishedProduct.xml" ); FinishedProduct testFinishedProduct = xmlReader.parseFinishedProduct( testDoc3 ); ClauseType c1 = new ClauseType( 3, 2 ); ClauseType c2 = new ClauseType( 3, 1 ); ClauseType c3 = new ClauseType( 3, 0 ); assert( !c1.equals(c2) ); assert( c1.equals(c1) ); RawMaterialType testRawMaterialType = new RawMaterialType(); testRawMaterialType.addClauseType( c1 ); testRawMaterialType.addClauseType( c2 ); assert( testRawMaterialType.contains( c1 ) ); assert( testRawMaterialType.contains( c2 ) ); assert( !testRawMaterialType.contains( c3 ) ); // check to see if raw material type only contains unique clause types assert( testRawMaterial2.getType().getClauseTypes().size() == 2 ); assert( testRawMaterial.getType().equals( testRawMaterialType ) ); assert( testFinishedProduct.determineQuality( testRawMaterial ).getValue() == 1.0 ); FinishedProduct testFinishedProductTwo = new FinishedProduct(); assert( testRawMaterial.anyFreeVariables( testFinishedProductTwo ) ); assert( !testRawMaterial.anyFreeVariables( testFinishedProduct ) ); assert( testRawMaterial.findFirstFreeVariable( testFinishedProductTwo ).matches( "a" ) ); testFinishedProductTwo.addLiteral( new Pos( "a" ) ); assert( testRawMaterial.findFirstFreeVariable( testFinishedProductTwo ).matches( "b" ) ); testFinishedProductTwo.addLiteral( new Neg( "b" ) ); assert( testRawMaterial.findFirstFreeVariable( testFinishedProductTwo ).matches( "c" ) ); testFinishedProductTwo.addLiteral( new Pos( "c" ) ); assert( !testRawMaterial.anyFreeVariables( testFinishedProductTwo ) ); // tested with project example cnf so at best 8/9 clauses satisfied Solver testSolver = new Solver( testRawMaterial2 ); RawMaterialType sampleRawMaterialType = new RawMaterialType(); ClauseType c4 = new ClauseType( 1, 0 ); ClauseType c5 = new ClauseType( 1, 1 ); ClauseType c6 = new ClauseType( 2, 0 ); ClauseType c7 = new ClauseType( 2, 1 ); ClauseType c8 = new ClauseType( 2, 2 ); ClauseType c9 = new ClauseType( 3, 0 ); ClauseType c10 = new ClauseType( 3, 1 ); ClauseType c11 = new ClauseType( 3, 2 ); ClauseType c12 = new ClauseType( 3, 3 ); //sampleRawMaterialType.addClauseType( c4 ); //sampleRawMaterialType.addClauseType( c5 ); sampleRawMaterialType.addClauseType( c6 ); //sampleRawMaterialType.addClauseType( c7 ); sampleRawMaterialType.addClauseType( c8 ); //sampleRawMaterialType.addClauseType( c9 ); //sampleRawMaterialType.addClauseType( c10 ); //sampleRawMaterialType.addClauseType( c11 ); //sampleRawMaterialType.addClauseType( c12 ); Builder testBuilder = new Builder ( sampleRawMaterialType ); Solver testBuilderSolver = new Solver( testBuilder.getGeneratedRawMaterial() ); //assert( testBuilderSolver.getBestAssignment().determineQuality( // testBuilder.getGeneratedRawMaterial() ).getValue().compareTo( new Float(0.8) ) == 0); Document testDoc4 = xmlReader.prepareDocument( "C:/blackboard/players.xml" ); ArrayList testPlayers = xmlReader.parsePlayers( testDoc4 ); //Document testDoc5 = xmlReader.prepareDocument( "blackboard/store.xml" ); //Store testStore = xmlReader.parseStore( testDoc5 ); //Document testDoc6 = xmlReader.prepareDocument( "blackboard/history.xml" ); //System.out.println( testRawMaterial.generateXML() ); //System.out.println( testFinishedProduct.generateXML() ); //System.out.println( testStore.getDerivatives().get( 0 ).generateXML() ); //System.out.println( testPlayers.get( 0 ).generateXML() ); //System.out.println( testStore.generateXML() ); // BuyingAgent tests //BuyingAgent ba = new BuyingAgent(); //assert( ba.checkDerivative( testStore.getDerivatives().get( 0 ) ) ); //assert( !ba.checkDerivative( testStore.getDerivatives().get( 1 ) ) ); //assert( ba.checkDerivative( testStore.getDerivatives().get( 2 ) ) ); //CreationAgent ca = new CreationAgent( Store.findAvailableTypes(), "Bob" ); //System.out.print( ca.createDerivative().generateXML() ); // communications tests Administrator crazyAdmin = new Administrator( new History(), testPlayers ); crazyAdmin.changeTurn(); testPlayers.get( 0 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 1 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 0 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 1 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 0 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 1 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 0 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); crazyAdmin.changeTurn(); testPlayers.get( 1 ).takeTurn(); crazyAdmin.getCurrentPlayerTransactions(); */ // setup initial mock game config and objects Config.initialize( null ); // default config with no path ResetXml.reset(); // reset blackboard xml files History.readHistory(); // prep history singleton Store.readStore(); // prep store singleton // create mock players Player jacob = new Player( "Jacob", Config.getInitialMoney(), false ); Player jason = new Player( "Jason", Config.getInitialMoney(), false ); ArrayList players = new ArrayList(); players.add( jacob ); players.add( jason ); // create mock administrator Administrator god = new Administrator( players ); // mock game tests // console log will also accurately reflect player making responsible moves assert( Store.isEmpty() ); jacob.writeTransactions( jacob.prepareTransactions() ); god.processPlayerTransactions( jacob.generateDoneFilePath() ); assert( !Store.isEmpty() ); //jacob should have created one derivative assert( Store.getDerivatives().size() == 1 ); //should only be one jason.writeTransactions( jason.prepareTransactions() ); god.processPlayerTransactions( jason.generateDoneFilePath() ); //jason should have found derivatives in store to be overpriced, must rebuild //will also create another derivative assert( Store.getDerivatives().size() == 3 ); //one originally, one rebuilt, one new //for my sanity, i am going to manually lower a price to show a buy/deliver/finish cycle Derivative toFollow = Store.getDerivatives().get( 1 ); String toFollowName = toFollow.getName(); Store.getDerivatives().get( 1 ).setPrice( new Price( 0.3 ) ); jacob.writeTransactions( jacob.prepareTransactions() ); god.processPlayerTransactions( jacob.generateDoneFilePath() ); assert( History.exists( "Buy", toFollowName ) ); jason.writeTransactions( jason.prepareTransactions() ); god.processPlayerTransactions( jason.generateDoneFilePath() ); assert( History.exists( "Deliver", toFollowName ) ); jacob.writeTransactions( jacob.prepareTransactions() ); god.processPlayerTransactions( jacob.generateDoneFilePath() ); assert( History.exists( "Finish", toFollowName ) ); } catch ( Exception e ) { e.printStackTrace(); throw new AssertionError( e.getMessage() ); } } }