@Test public void testTempTableGenerationIsolation() throws Throwable { Session s = openSession(); s.beginTransaction(); Truck truck = new Truck(); truck.setVin("123t"); truck.setOwner("Steve"); s.save(truck); // manually flush the session to ensure the insert happens s.flush(); // now issue a bulk delete against Car which should force the temp table to be // created. we need to test to ensure that this does not cause the transaction // to be committed... s.createQuery("delete from Vehicle").executeUpdate(); s.getTransaction().rollback(); s.close(); s = openSession(); s.beginTransaction(); List list = s.createQuery("from Car").list(); assertEquals("temp table gen caused premature commit", 0, list.size()); s.createQuery("delete from Car").executeUpdate(); s.getTransaction().rollback(); s.close(); }
/* * this method always checks the status of the bridge and updates the status * of the bridge before letting the bus in , and also gives the waiting * numbers to the subsequent trucks if there are already 4 trucks on the * bridge */ public void checkTheBridge(Truck aTruck) { if (aTruck.getDirection().equals("left") && aTruck.getWaitingNumber() == 0) aTruck.setWaitingNumber(updateWaitingTruck(1, 1)); else { if (aTruck.getWaitingNumber() == 0) aTruck.setWaitingNumber(updateWaitingTruck(2, 1)); } System.out.println( "For waiting number " + aTruck.getWaitingNumber() + "new truck on the bridge"); synchronized (bridgeLock) { try { while (aTruck.getWeight() + BridgeWeight > maxBridgeWeight || bridgeLeftCount + bridgeRightCount >= 4 || aTruck.getWaitingNumber() != servingTokenNumber) { bridgeLock.notifyAll(); bridgeLock.wait(); } BridgeWeight = BridgeWeight + aTruck.getWeight(); if (aTruck.getDirection().equals("left")) bridgeLeftCount++; else bridgeRightCount++; System.out.println( "Number of Trucks on Bridge " + (bridgeLeftCount + bridgeRightCount) + " Weight of the bridge " + BridgeWeight); servingTokenNumber++; bridgeLock.notifyAll(); } catch (Exception e) { System.out.println("Error:" + e); } } }
public static void main(String[] args) { // Create an instance of Convertible Convertible marysCar = new Convertible("yellow", 2015, "Mini", "ragtop"); System.out.println(marysCar); // Create an instance of Motorcycle Motorcycle joesBike = new Motorcycle("yellow", 1600); System.out.println(joesBike); // Creates John and Jen's vehicles Convertible johnsCar = new Convertible("silver", 2014, "Mini", "ragtop"); Truck jensTruck = new Truck("red", 2002, 4); // Compare instance variabless The == sign works for that boolean isSameColor = (marysCar.getColor() == joesBike.getColor()); System.out.println("\nMary's car and Joe's Bike the same color is: " + isSameColor); // Mary is married to Dave, so they share the same car Convertible davesCar = new Convertible(); davesCar = marysCar; System.out.println(davesCar); // Dave put some miles on the shared car davesCar.changeMileage(15000); // Check that Mary and Dave have the same car System.out.println("\nMary and Dave have the same car still? " + davesCar.equals(marysCar)); // Mary went to the service station. Does she need an oil change? marysCar.changeOil(); // Dang, Dave puts the miles on, but Mary changes the oil. Is it still a Vehicle? System.out.println("\nIs Mary's car still a Vehicle? " + (marysCar instanceof Vehicles)); // Test truck jensTruck.changeOil(); System.out.println(jensTruck); }
public void testLoadTruck() throws Exception { Database database = _category.getDatabase(); database.begin(); Truck truck = (Truck) database.load(Truck.class, new Integer(5)); database.commit(); assertNotNull(truck); assertEquals(5, truck.getId()); database.close(); }
public static void main(String[] args) { Truck<Product> shipment = new Truck<>(); Bike bike = new Bike(); Wheel wheel = new Wheel(); Person person = new Person(); shipment.add(bike); shipment.add(wheel); // shipment.add(person); // Compiler error }
public void testCreateAndLoadCar() throws Exception { Database database = _category.getDatabase(); database.begin(); Order order = new Order(); order.setId(11); order.setName("order 11"); database.create(order); database.commit(); database.begin(); ProductDetail detail = new ProductDetail(); detail.setId(11); detail.setCategory("category 11"); detail.setLocation("location 11"); database.create(detail); database.commit(); database.begin(); Truck truck = new Truck(); truck.setId(11); truck.setName("truck 11"); truck.setKw(112); truck.setMake("Fiat"); truck.setMaxWeight(3750); truck.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(11))); database.create(truck); Collection orders = new LinkedList(); orders.add(database.load(Order.class, new Integer(11))); truck.setOrders(orders); database.commit(); database.begin(); Object object = database.load(Car.class, new Integer(11)); assertNotNull(object); assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName()); Truck loadedTruck = (Truck) object; database.commit(); assertNotNull(loadedTruck); assertEquals(11, loadedTruck.getId()); database.begin(); database.remove(database.load(Car.class, new Integer(11))); database.remove(database.load(ProductDetail.class, new Integer(11))); database.remove(database.load(Order.class, new Integer(11))); database.commit(); database.close(); }
public void testLoadCar() throws Exception { Database database = _category.getDatabase(); database.begin(); Object object = database.load(Car.class, new Integer(5)); assertNotNull(object); assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName()); Truck truck = (Truck) object; database.commit(); assertNotNull(truck); assertEquals(5, truck.getId()); database.close(); }
public static void main(String[] args) { Car bs = new Truck(); bs.setSpeed(100); ((Truck) bs).setCargo(10); bs.showCurrentSpeed(); }
// this method enables the truck to cross the bridge public void crossthisTruck(Truck aTruck) { checkTheBridge(aTruck); // truck is on the road out side synchronized Block try { Thread.sleep(new Random().nextInt(200)); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (bridgeLock) { if (aTruck.getDirection().equals("left")) bridgeLeftCount--; else bridgeRightCount--; BridgeWeight -= aTruck.getWeight(); } System.out.println(aTruck.getWaitingNumber() + "leaving Bridge"); }
/** * This method describes the unloading process carried out by the master process (some kind of * <code>InternalTransporter</code> or <code>Crane</code>). In this simple case only the time it * takes to unload the containers (goods) is taken into consideration. If the user building the * model has to implement a more complex behavior, he has to overwrite this method in a subclass. * The time it takes to unload the containers (goods) is obtained from the method <code> * getUnloadTimeSample()</code>. * * @param master SimProcess : The master process which really unloads the cooperation. Should be a * subclass of <code>InternalTransporter</code> or <code>Crane</code>. * @param slave SimProcess : The slave process which is lead through the cooperation by the * master. Should be a subclass of <code>InternalTransporter</code> or <code>Truck</code>. */ protected void cooperation(SimProcess master, SimProcess slave) { // check if the master is an Internal Transporter and the slave is a // Truck if ((master instanceof InternalTransporter) && (slave instanceof Truck)) { InternalTransporter t = (InternalTransporter) master; Truck truck = (Truck) slave; // pick up a container from the truck t.pickUp(this.getUnloadTimeSample()); truck.setNumberOfExportGoods(truck.getNumberOfExportGoods() - 1); } else { // check if the master is a Crane and the slave is an // InternalTransporter if ((master instanceof desmoj.extensions.applicationDomains.harbour.Crane) && (slave instanceof InternalTransporter)) { Crane c = (Crane) master; InternalTransporter t = (InternalTransporter) slave; // unload the transporter until he is full and the crane still // has to unload something while ((t.getCurrentCapacity() < t.getCapacity()) && (c.getNumToUnloadUnits() > 0)) { c.unload(this.getUnloadTimeSample()); t.setCurrentCapacity(t.getCurrentCapacity() - 1); c.setNumToUnloadUnits(c.getNumToUnloadUnits() - 1); } } else { sendWarning( "The given master or slave process for an unloading is " + "not an InternalTransporter/a Crane or Truck. The unloading will not be carried out !", "Unloading : " + getName() + " Method: void " + "cooperation (SimProcess master, SimProcess slave)", "The given master or slave process is not right.", "It is recommended to use an InternalTransporter/ a Crane process as a master and " + "Truck or an InternalTransporter as a slave to carry out a unloading"); } } }
@Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (surname != null ? surname.hashCode() : 0); result = 31 * result + hoursWorkedThisMonth; result = 31 * result + (state != null ? state.hashCode() : 0); result = 31 * result + (currentCity != null ? currentCity.hashCode() : 0); result = 31 * result + (currentTruck != null ? currentTruck.hashCode() : 0); return result; }
/** * Run Method which simulates the number of threads running on the bridge * * @param None * @return None */ public void run() { while (true) { Truck theTruck; // Stores the Truck Object // synchronized on truck queue before removing the truck objects synchronized (theTruckQueue) { // If there are no Trucks then wait for trucks to arrive while (theTruckQueue.isEmpty()) { try { theTruckQueue.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } theTruck = theTruckQueue.remove(); } // Check for the maximum trucks allowed on the bridge and max weight // allowed on the bridge // Do not allow any more trucks if we exceed any of the limit while (theBridgeQueue.size() == MAX_SIZE || (theTruck.getWeight() + getTotalWeight()) >= MAX_WEIGHT) { System.out.println("Total number of trucks on Bridge: " + theBridgeQueue.size()); System.out.println("Total Weight on Bridge is: " + getTotalWeight()); Truck truckOnBridge = theBridgeQueue.remove(); System.out.println("The truck Entered " + truckOnBridge.getSide() + " of the Bridge left"); } // Allow the trucks on the bridge on first come first serve basis System.out.println("Truck " + theTruck.getSide() + " is Entering the Bridge"); theBridgeQueue.add(theTruck); System.out.println("Truck " + theTruck.getSide() + " is running on the Bridge"); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Driver driver = (Driver) o; if (id != driver.id) return false; if (hoursWorkedThisMonth != driver.hoursWorkedThisMonth) return false; if (name != null ? !name.equals(driver.name) : driver.name != null) return false; if (surname != null ? !surname.equals(driver.surname) : driver.surname != null) return false; if (state != driver.state) return false; if (currentCity != null ? !currentCity.equals(driver.currentCity) : driver.currentCity != null) return false; return currentTruck != null ? currentTruck.equals(driver.currentTruck) : driver.currentTruck == null; }
@Override public String toString() { return truck.toString() + " bidder"; }
/** * Will completely cancel the order by removing any FoodItems being cooked or prepared, remove the * Order from the waiting to be delivered queue, removing the order from the Truck that is * performing the driving to the Location or delivering to the customer. * * @param order The order to be canceled. */ public void cancelOrder(Order order) { // To avoid a LOT of potential work, we are going to see where // the Order is and try to cancel only the parts we need to. ResourceList<Order> deliverables = over_seer.getNewOrders(); if (deliverables.containsResource(order)) { // no point in doing all that looping and checks if the order // already cooked everything and is just waiting to be // delivered. if (!order.isAvailable()) { // first, check if any Chefs are preparing any FoodItems // contained in this Order. List<Chef> chefs = over_seer.getChefsWorkingOnOrder(order); for (Chef chef : chefs) { chef.cancelPreparation(); } // Next, check for any Ovens that may be cooking CookedItems // contained in this Order. List<Oven> ovens = over_seer.getOvensWorkingOnOrder(order); for (Oven oven : ovens) { oven.stopCookingFoodFromOrder(order); } } // Remove all FoodItems from the food waiting to be processed ResourceList<FoodItem> foods = over_seer.getFood(); for (FoodItem food : order.getFoodItems().getResourceQueue()) { foods.removeResource(food); } deliverables.removeResource(order); } else { // Since the order wasn't waiting to be delivered we need to find // the truck that is delivering it and cancel the order. Truck delivering_truck = over_seer.getOutboundTruckDeliveringOrder(order); if (delivering_truck != null) { delivering_truck.removeOrder(order); // Send the truck back to the pizza shop if he no longer has // orders to deliver. if (delivering_truck.getNumOrders() == 0) { int return_time = delivering_truck.getETAToLocation() - (delivering_truck.getAvailTime() - time.getCurrentTime()); delivering_truck.setETAToLocation(return_time); over_seer.getOutboundTrucks().removeResource(delivering_truck); over_seer.getInboundTrucks().addResource(delivering_truck); } } else { // Alright, now we need to check if it is already at the // Location but has *yet* to deliver the order. delivering_truck = over_seer.getTruckDeliveringOrderToCustomer(order); if (delivering_truck != null) { delivering_truck.removeOrder(order); // Send the truck back to the shop if it no longer needs // to give any customers their orders. if (delivering_truck.getNumOrders() == 0) { over_seer.getDeliveryTrucks().removeResource(delivering_truck); delivering_truck.setETAToLocation(delivering_truck.getETAToLocation()); over_seer.getInboundTrucks().addResource(delivering_truck); } } } } order.cancelOrder(); }
private void prepare() { Session s = openSession(); Transaction txn = s.beginTransaction(); polliwog = new Animal(); polliwog.setBodyWeight(12); polliwog.setDescription("Polliwog"); catepillar = new Animal(); catepillar.setBodyWeight(10); catepillar.setDescription("Catepillar"); frog = new Animal(); frog.setBodyWeight(34); frog.setDescription("Frog"); polliwog.setFather(frog); frog.addOffspring(polliwog); butterfly = new Animal(); butterfly.setBodyWeight(9); butterfly.setDescription("Butterfly"); catepillar.setMother(butterfly); butterfly.addOffspring(catepillar); s.save(frog); s.save(polliwog); s.save(butterfly); s.save(catepillar); Dog dog = new Dog(); dog.setBodyWeight(200); dog.setDescription("dog"); s.save(dog); Cat cat = new Cat(); cat.setBodyWeight(100); cat.setDescription("cat"); s.save(cat); zoo = new Zoo(); zoo.setName("Zoo"); Address add = new Address(); add.setCity("MEL"); add.setCountry("AU"); add.setStreet("Main st"); add.setPostalCode("3000"); zoo.setAddress(add); pettingZoo = new PettingZoo(); pettingZoo.setName("Petting Zoo"); Address addr = new Address(); addr.setCity("Sydney"); addr.setCountry("AU"); addr.setStreet("High st"); addr.setPostalCode("2000"); pettingZoo.setAddress(addr); s.save(zoo); s.save(pettingZoo); Joiner joiner = new Joiner(); joiner.setJoinedName("joined-name"); joiner.setName("name"); s.save(joiner); Car car = new Car(); car.setVin("123c"); car.setOwner("Kirsten"); s.save(car); Truck truck = new Truck(); truck.setVin("123t"); truck.setOwner("Steve"); s.save(truck); SUV suv = new SUV(); suv.setVin("123s"); suv.setOwner("Joe"); s.save(suv); Pickup pickup = new Pickup(); pickup.setVin("123p"); pickup.setOwner("Cecelia"); s.save(pickup); BooleanLiteralEntity bool = new BooleanLiteralEntity(); s.save(bool); txn.commit(); s.close(); }
/** * If bidder wins the bid for a certain parcel, this method is called to actually give the parcels * to the Truck. * * @param pars List of parcels to be allocated to the truck */ public void receiveParcels(ImmutableSet<DefaultParcel> pars) { for (DefaultParcel par : pars) { truck.addParcel(par); } }