[virtualcommons-svn] SF.net SVN: virtualcommons:[375] mentalmodels/trunk/src/main
Status: Beta
Brought to you by:
alllee
From: <see...@us...> - 2009-11-25 23:32:03
|
Revision: 375 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=375&view=rev Author: seematalele Date: 2009-11-25 23:31:53 +0000 (Wed, 25 Nov 2009) Log Message: ----------- create new entity DayGroupLocation.java, HibernateDayGroupLocationDao, then changed the applicationContext.xml and hibernate. Need to create Actionscript class in flex (Not needed though). Modified Paths: -------------- mentalmodels/trunk/src/main/db/init-mme.sql mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/GroupLocation.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Location.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/AnsweringService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/DayByDayDecisionsService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml mentalmodels/trunk/src/main/webapp/WEB-INF/hibernate.cfg.xml Added Paths: ----------- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/dao/HibernateDayGroupLocationDao.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/DayGroupLocation.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StrategyService.java Modified: mentalmodels/trunk/src/main/db/init-mme.sql =================================================================== --- mentalmodels/trunk/src/main/db/init-mme.sql 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/db/init-mme.sql 2009-11-25 23:31:53 UTC (rev 375) @@ -389,39 +389,8 @@ -- Table structure for table `group_location` -- -DROP TABLE IF EXISTS `group_location`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `group_location` ( - `id` bigint(20) NOT NULL AUTO_INCREMENT, - `current_population` double DEFAULT NULL, - `fish_leaving` double DEFAULT NULL, - `fish_return` double DEFAULT NULL, - `initial_population` double NOT NULL, - `max_capacity` double NOT NULL, - `dayByDayDecsion_id` bigint(20) DEFAULT NULL, - `group_id` bigint(20) NOT NULL, - `location_id` bigint(20) NOT NULL, - PRIMARY KEY (`id`), - KEY `FKCFF7B975224FD013` (`location_id`), - KEY `FKCFF7B97565663181` (`group_id`), - KEY `FKCFF7B975A4684961` (`dayByDayDecsion_id`), - CONSTRAINT `FKCFF7B975A4684961` FOREIGN KEY (`dayByDayDecsion_id`) REFERENCES `day_by_day_decision` (`id`), - CONSTRAINT `FKCFF7B975224FD013` FOREIGN KEY (`location_id`) REFERENCES `location` (`id`), - CONSTRAINT `FKCFF7B97565663181` FOREIGN KEY (`group_id`) REFERENCES `grp` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1; -/*!40101 SET character_set_client = @saved_cs_client */; -- --- Dumping data for table `group_location` --- - -LOCK TABLES `group_location` WRITE; -/*!40000 ALTER TABLE `group_location` DISABLE KEYS */; -/*!40000 ALTER TABLE `group_location` ENABLE KEYS */; -UNLOCK TABLES; - --- -- Table structure for table `grp` -- Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/dao/HibernateDayGroupLocationDao.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/dao/HibernateDayGroupLocationDao.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/dao/HibernateDayGroupLocationDao.java 2009-11-25 23:31:53 UTC (rev 375) @@ -0,0 +1,13 @@ +package edu.asu.commons.mme.dao; + +import edu.asu.commons.mme.entity.DayGroupLocation; + +public class HibernateDayGroupLocationDao extends HibernateDao<DayGroupLocation>{ + + public HibernateDayGroupLocationDao() { + super(DayGroupLocation.class); + + } + + +} Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/DayGroupLocation.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/DayGroupLocation.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/DayGroupLocation.java 2009-11-25 23:31:53 UTC (rev 375) @@ -0,0 +1,87 @@ +package edu.asu.commons.mme.entity; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name="day_group_location") +public class DayGroupLocation implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 1961926317422666537L; + @Id + @GeneratedValue + private Long id; + + @Column(name="current_population") + private Double currentPopulation; + + @Column(name="fish_leaving") + private Double fishLeaving; + + @Column(name="fish_return") + private Double fishReturned; + + private Integer dayNumber; + + @ManyToOne + @JoinColumn(nullable=true) + private GroupLocation groupLocation; + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public void setCurrentPopulation(Double currentPopulation) { + this.currentPopulation = currentPopulation; + } + + public Double getCurrentPopulation() { + return currentPopulation; + } + + public void setFishLeaving(Double fishLeaving) { + this.fishLeaving = fishLeaving; + } + + public Double getFishLeaving() { + return fishLeaving; + } + + public void setFishReturned(Double fishReturned) { + this.fishReturned = fishReturned; + } + + public Double getFishReturned() { + return fishReturned; + } + + public void setGroupLocation(GroupLocation groupLocation) { + this.groupLocation = groupLocation; + } + + public GroupLocation getGroupLocation() { + return groupLocation; + } + + public void setDayNumber(Integer dayNumber) { + this.dayNumber = dayNumber; + } + + public Integer getDayNumber() { + return dayNumber; + } +} Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/GroupLocation.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/GroupLocation.java 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/GroupLocation.java 2009-11-25 23:31:53 UTC (rev 375) @@ -27,14 +27,14 @@ @JoinColumn(nullable=false) private Location location; - @Column(name="current_population") + /*@Column(name="current_population") private Double currentPopulation; @Column(name="fish_leaving") private Double fishLeaving; @Column(name="fish_return") - private Double fishReturned; + private Double fishReturned;*/ @ManyToOne @JoinColumn(nullable=false) @@ -46,11 +46,6 @@ @Column(nullable=false,name="initial_population") private Double initialPopulation; - @ManyToOne - @JoinColumn(nullable=true) - private DayByDayDecisions dayByDayDecsion; - - public void setId(Long id) { this.id = id; } @@ -59,7 +54,7 @@ return id; } - public void setCurrentPopulation(Double currentPopulation) { + /*public void setCurrentPopulation(Double currentPopulation) { this.currentPopulation = currentPopulation; } @@ -81,7 +76,7 @@ public Double getFishReturned() { return fishReturned; - } + }*/ public void setGroup(Group group) { this.group = group; @@ -115,12 +110,4 @@ return initialPopulation; } - public void setDayByDayDecsion(DayByDayDecisions dayByDayDecsion) { - this.dayByDayDecsion = dayByDayDecsion; - } - - public DayByDayDecisions getDayByDayDecsion() { - return dayByDayDecsion; - } - } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Location.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Location.java 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Location.java 2009-11-25 23:31:53 UTC (rev 375) @@ -29,6 +29,8 @@ @Column(nullable=false,name="growth_rate") private Double growthRate; + + /*@Column(nullable=false,name="initial_population", scale=2) private Double initialPopulation; @@ -118,4 +120,6 @@ public Group getGroup() { return group; }*/ + + } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/AnsweringService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/AnsweringService.java 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/AnsweringService.java 2009-11-25 23:31:53 UTC (rev 375) @@ -37,7 +37,7 @@ Student student; public static final String getLastDay = "SELECT max(d.dayNumber) FROM DayByDayDecisions d " + - "where d.student=:student"; + "where d.student=:student and d.studentStrategy != NULL"; /** * This method is for Psychometric, Categorical and text question types. @@ -82,9 +82,9 @@ Integer lastDayNo = 0; for(int i = 0; i < studentStrategies.size(); i++ ) { - clientStudentStrategy = (StudentStrategy) studentStrategies.get(i); - if(clientStudentStrategy.getDays() != 0) - { + clientStudentStrategy = (StudentStrategy) studentStrategies.get(i); + if(clientStudentStrategy.getDays() != 0) + { studentStrategy = new StudentStrategy(); studentStrategy.setLocation(clientStudentStrategy.getLocation()); studentStrategy.setAllocationSeqNo(clientStudentStrategy.getAllocationSeqNo()); @@ -95,12 +95,12 @@ studentStrategy.setThreshold(clientStudentStrategy.getThreshold()); studentStrategy.setRepeatedDecisionNo(clientStudentStrategy.getRepeatedDecisionNo()); getStudentStrategyDao().save(studentStrategy); - + Query getLastDayForStudent = getStudentStrategyDao().getCurrentSession().createQuery(getLastDay); getLastDayForStudent.setEntity("student", studentStrategy.getStudent()); Iterator<?> lastdayIterator = getLastDayForStudent.list().iterator(); getLogger().debug("Days are : " + studentStrategy.getDays()); - + while(lastdayIterator.hasNext()) { lastDayNo = (Integer) lastdayIterator.next(); @@ -109,7 +109,7 @@ { lastDayNo = 0; } - + } //get previous day number for(int dayNo = lastDayNo+1,counter=0; counter < studentStrategy.getDays(); dayNo++,counter++) @@ -124,6 +124,8 @@ } } } + + }catch(Exception e) { e.printStackTrace(); Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/DayByDayDecisionsService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/DayByDayDecisionsService.java 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/DayByDayDecisionsService.java 2009-11-25 23:31:53 UTC (rev 375) @@ -8,24 +8,26 @@ import org.hibernate.Query; import org.springframework.transaction.annotation.Transactional; - - import edu.asu.commons.mme.dao.HibernateDayByDayDecisionsDao; import edu.asu.commons.mme.dao.HibernateDayByDayOtherStudentDecisionsDao; +import edu.asu.commons.mme.dao.HibernateDayGroupLocationDao; import edu.asu.commons.mme.dao.HibernateGameDao; import edu.asu.commons.mme.dao.HibernateGroupDao; import edu.asu.commons.mme.dao.HibernateGroupLocationDao; import edu.asu.commons.mme.dao.HibernateLocationDao; import edu.asu.commons.mme.dao.HibernateStudentDao; import edu.asu.commons.mme.dao.HibernateStudentStrategyDao; +import edu.asu.commons.mme.dao.HibernateSuspendRepetitionDao; import edu.asu.commons.mme.entity.DayByDayDecisions; import edu.asu.commons.mme.entity.DayByDayOtherStudentDecisions; +import edu.asu.commons.mme.entity.DayGroupLocation; import edu.asu.commons.mme.entity.Game; import edu.asu.commons.mme.entity.Group; import edu.asu.commons.mme.entity.GroupLocation; import edu.asu.commons.mme.entity.Location; import edu.asu.commons.mme.entity.Student; import edu.asu.commons.mme.entity.StudentStrategy; +import edu.asu.commons.mme.entity.SuspendRepetition; /*** 1) Harvesting - Find out the amount of fish per agent and calculate the current population after harvesting @@ -86,6 +88,8 @@ private HibernateGroupDao groupDao; private HibernateDayByDayOtherStudentDecisionsDao dayByDayOtherStudentDecisionsDao; private HibernateStudentStrategyDao studentStrategyDao; + private HibernateDayGroupLocationDao dayGroupLocationDao; + private HibernateSuspendRepetitionDao suspendRepetitionDao; private double totalFishLeaving; @@ -97,9 +101,38 @@ " where s.game =:game and s=ss.student and " + " dbd.studentStrategy=ss and " + " ss.repeatedDecision=:repeated_decisions"; + static final String getPreviousEarningForLocation = "select dd.earnings from DayByDayDecisions dd " + + "where dd.id = ()"; + + static final String studentStrategiesForRepeatedDecision = "SELECT ss FROM StudentStrategy ss " + + "where ss.student=:student_id" + + " and ss.repeatedDecision=:repeated_decision " + + "order by ss.repeatedDecisionNo asc"; + static final String getDayGroupLocation = "Select dgl from DayGroupLocation dgl " + + "where dgl.dayNumber=:prevdayNo and " + + "dgl.groupLocation=:groupLocation"; + /*static final String checkSuspendCondition="select sr from SuspendRepetition sr where sr.student =:student and sr.threshold < " + + "(select dd.earnings from DayByDayDecisions dd where dd.id = " + + "(SELECT max(d.id) FROM DayByDayDecisions d inner join StudentStrategy ss " + + "where d.student=:student and ss.repeatedDecision=:repeated_decision" + + "and d.studentStrategy=ss and d.location=:location))";*/ - static final String checkSuspendCondition=""; + static final String getSuspendRepetition = "select sr from SuspendRepetition sr where sr.student =:student"; + static final String checkForRepeatedDecision = "select s from StudentStrategy s " + + "where s.id = " + + "(SELECT d.studentStrategy.id FROM DayByDayDecisions d " + + "where d.dayNumber=:dayNo and d.student=:student and d.studentStrategy.id IS NOT NULL)"; + + static final String getEarningForDay = "select dd.earnings from DayByDayDecisions dd where dd.studentStrategy=:studentStrategy and dd.dayNumber=:dayNo"; + + static final String getDaybyDayDecisionForSuspendCondition = "select dd from DayByDayDecisions dd " + + "where dd.studentStrategy =: studentStrategy and dd.dayNumber<:dayNo order by dd.dayNumber desc"; + + static final String dayBydayBetweenDays = "SELECT d FROM DayByDayDecisions d where d.student=:student and d.dayNumber dayNumber >=: dayNoLow and dayNumber <=: dayNoHigh "; + + static final String prevDayEarningForSameLocation = ""; + public void setGameDao(HibernateGameDao gameDao) { this.gameDao = gameDao; } @@ -177,74 +210,17 @@ query.setEntity("game", game); - Iterator studentDecisionsIterator = query.list().iterator(); + Iterator<?> studentDecisionsIterator = query.list().iterator(); try{ while(studentDecisionsIterator.hasNext()) { DayByDayDecisions decision = (DayByDayDecisions)studentDecisionsIterator.next(); studentDecisions.add(decision); - getLogger().info("student decision is: " + decision.getId() + "location number: " + decision.getLocation().getLocationName()); + //getLogger().info("student decision is: " + decision.getId() + "location number: " + decision.getLocation().getLocationName()); } - executeStrategy(studentDecisions, game, false); + executeDayByDayDecisions(studentDecisions, game); - //find out different groups in the game - /*Query queryforGroups = studentDao.getCurrentSession().createQuery(groupsWithinGame); - - queryforGroups.setLong("current_game_id", game.getId()); - Iterator groups = queryforGroups.list().iterator(); - List<GroupLocation> groupLocations = new ArrayList<GroupLocation>(); - List<DayByDayDecisions> bay1Students = new ArrayList<DayByDayDecisions>(); - List<DayByDayDecisions> bay2Students = new ArrayList<DayByDayDecisions>(); - List<DayByDayDecisions> bay3Students = new ArrayList<DayByDayDecisions>(); - List<DayByDayDecisions> harborStudents = new ArrayList<DayByDayDecisions>(); - /** For every group find out the groupLocations objects. - * find out the student decisions for current group. - * Separate the students for each bay - * find out the population for every bay - */ - - /*getLogger().debug("number of groups in the game are: " + queryforGroups.list().size()); - while(groups.hasNext()) - { - Group currentGroup = (Group) groups.next(); - getLogger().debug("group id in executeStrategy is: " + currentGroup.getId()); - - groupLocations = groupLocationDao.findAllByProperty("group",currentGroup); - //getLogger().debug("grouplocation size is: " + groupLocations.size()); - - List<DayByDayDecisions> studentDecisionForCurrentGroup = new ArrayList<DayByDayDecisions>(); - for(DayByDayDecisions tempStudentDecision:studentDecisions) - { - if(tempStudentDecision.getStudent().getGroup().getId() == currentGroup.getId()) - { - //studentDecisionForCurrentGroup.add(tempStudentDecision); - if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay1")) - { - bay1Students.add(tempStudentDecision); - } - else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay2")) - { - bay2Students.add(tempStudentDecision); - } - else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay3")) - { - bay3Students.add(tempStudentDecision); - } - else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Harbor")) - { - harborStudents.add(tempStudentDecision); - } - - } - - } - calculateHarvest(bay1Students,"Bay1",currentGroup,game); - calculateHarvest(bay2Students,"Bay2",currentGroup,game); - calculateHarvest(bay3Students,"Bay3",currentGroup,game); - calculateNewPopulation(currentGroup); - }*/ - }catch(Exception e) { e.printStackTrace(); @@ -252,7 +228,7 @@ } //FIXME: max_fish_capacity is hard coded to 5 but actually this value should get from game object - public void calculateHarvest(List<DayByDayDecisions> bayStudents,String locationName, Group currentGroup, Game game) + public void calculateHarvest(List<DayByDayDecisions> bayStudents,String locationName, Group currentGroup, Game game, int currentDayNumber) { // TODO Auto-generated method stub double maxFishCapacity = 0.0; @@ -262,38 +238,56 @@ double fishLeaving = 0.0; Long groupId; DayByDayOtherStudentDecisions otherStudents; + DayGroupLocation dayGroupLocation = null; maxFishCapacity = game.getGameConfig().getMaxFishHarvest(); getLogger().debug("location name is : " +locationName + "bay student size is: " + bayStudents.size()); try { - Location location = locationDao.findByProperty("locationName",locationName); - Group group = groupDao.find(currentGroup.getId()); GroupLocation groupLocation = new GroupLocation(); Query query = groupLocationDao.getCurrentSession().createQuery(getgroupLocation); query.setEntity("location", location); query.setEntity("group", group); - Iterator i = query.list().iterator(); + Iterator<?> i = query.list().iterator(); while(i.hasNext()) { groupLocation = (GroupLocation)i.next(); - getLogger().debug("groupLocation id is: " + groupLocation.getId()); + // getLogger().debug("groupLocation id is: " + groupLocation.getId()); } //get current population - actualPopulation = groupLocation.getCurrentPopulation(); + if(currentDayNumber == 1) + { + actualPopulation = groupLocation.getInitialPopulation(); + dayGroupLocation = new DayGroupLocation(); + dayGroupLocation.setDayNumber(currentDayNumber); + } + else + { + Query getPopulationForPrevDay = dayGroupLocationDao.getCurrentSession().createQuery(getDayGroupLocation); + getPopulationForPrevDay.setInteger("prevdayNo", currentDayNumber-1); + getPopulationForPrevDay.setEntity("groupLocation", groupLocation); + Iterator<?> populationIterator = getPopulationForPrevDay.list().iterator(); + while(populationIterator.hasNext()) + { + dayGroupLocation = (DayGroupLocation)populationIterator.next(); + // getLogger().debug("groupLocation id is: " + groupLocation.getId()); + } + actualPopulation = dayGroupLocation.getCurrentPopulation(); + } + dayGroupLocation.setGroupLocation(groupLocation); //1)Harvesting //Find out the amount of fish per agent - amountPerAgent = maxFishCapacity * groupLocation.getCurrentPopulation() / groupLocation.getMaxCapacity(); + amountPerAgent = maxFishCapacity * actualPopulation / groupLocation.getMaxCapacity(); if((bayStudents.size() * amountPerAgent) > actualPopulation) { amountPerAgent = actualPopulation / bayStudents.size(); - getLogger().debug("In IF Loop assigning amount/agent: " + amountPerAgent); + //getLogger().debug("In IF Loop assigning amount/agent: " + amountPerAgent); } //FIXME: allocate this amountPerAgent to each student @@ -309,13 +303,13 @@ fishLeaving = actualPopulation * growth / groupLocation.getMaxCapacity(); getLogger().info("Fish leaving at " + locationName + " is: " + fishLeaving); - groupLocation.setFishLeaving(fishLeaving); + dayGroupLocation.setFishLeaving(fishLeaving); //4) Population after diffusion actualPopulation = actualPopulation + growth - fishLeaving; getLogger().info("Fish population at " + locationName + " after diffusion is: " + actualPopulation); - groupLocation.setCurrentPopulation(actualPopulation); - groupLocationDao.save(groupLocation); + dayGroupLocation.setCurrentPopulation(actualPopulation); + dayGroupLocationDao.save(dayGroupLocation); if(bayStudents.size()!= 0) { @@ -336,15 +330,15 @@ //List<Student> otherStudents = new ArrayList<Student>(); Long currentStudentId = dayByDayDecision.getStudent().getId(); - getLogger().debug("For student id is: " + currentStudentId); + //getLogger().debug("For student id is: " + currentStudentId); for(DayByDayDecisions students:tempStudentDecisions) { - getLogger().debug("temp student id is: " + students.getStudent().getId()); + // getLogger().debug("temp student id is: " + students.getStudent().getId()); if(students.getStudent().getId() != currentStudentId) { - getLogger().debug("other student id is: " + students.getStudent().getId()); + // getLogger().debug("other student id is: " + students.getStudent().getId()); otherStudents = new DayByDayOtherStudentDecisions(); otherStudents.setDayByDayDecision(dayByDayDecision); otherStudents.setOtherStudent(students.getStudent()); @@ -365,7 +359,7 @@ } - private void calculateNewPopulation(Group group) { + private void calculateNewPopulation(Group group,Integer currentDayNo) { double totalFishLeaving = 0; double totalLoss = 0.0; @@ -374,14 +368,22 @@ double maxFishCapacity = 5.0; double totalReceptivity = 0.0; double receptivity = 0.0; + DayGroupLocation dayGroupLocation = null; - getLogger().debug("group id in calculate total fish leaving is: " + group.getId()); + //getLogger().debug("group id in calculate total fish leaving is: " + group.getId()); List<GroupLocation> groupLocations = groupLocationDao.findAllByProperty("group",group); - getLogger().debug("grouplocation size is: " + groupLocations.size()); + //getLogger().debug("grouplocation size is: " + groupLocations.size()); for(GroupLocation groupLocation : groupLocations) { - totalFishLeaving = totalFishLeaving + groupLocation.getFishLeaving(); - + Query getdayGroupLocation = dayGroupLocationDao.getCurrentSession().createQuery(getDayGroupLocation); + getdayGroupLocation.setInteger("prevdayNo", currentDayNo); + getdayGroupLocation.setEntity("groupLocation", groupLocation); + Iterator<?> i = getdayGroupLocation.list().iterator(); + while(i.hasNext()) + { + dayGroupLocation = (DayGroupLocation)i.next(); + totalFishLeaving = totalFishLeaving + dayGroupLocation.getFishLeaving(); + } } getLogger().debug("total fish leaving is : " + totalFishLeaving); //setTotalFishLeaving(totalFishLeaving); @@ -407,11 +409,20 @@ { if(groupLocation.getLocation().getId()!=1) { - getLogger().debug(groupLocation.getLocation().getLocationName() + "current population : " + groupLocation.getCurrentPopulation()); - receptivity = 1 - (groupLocation.getCurrentPopulation() /groupLocation.getMaxCapacity()); - getLogger().info(groupLocation.getLocation().getLocationName() + " receptivity is: " + receptivity); - bayReceptivity.put(groupLocation.getLocation().getLocationName(), receptivity); - totalReceptivity = receptivity + totalReceptivity; + Query getdayGroupLocation = dayGroupLocationDao.getCurrentSession().createQuery(getDayGroupLocation); + getdayGroupLocation.setInteger("prevdayNo", currentDayNo); + getdayGroupLocation.setEntity("groupLocation", groupLocation); + Iterator<?> i = getdayGroupLocation.list().iterator(); + while(i.hasNext()) + { + dayGroupLocation = (DayGroupLocation)i.next(); + getLogger().debug(groupLocation.getLocation().getLocationName() + "current population : " + dayGroupLocation.getCurrentPopulation()); + receptivity = 1 - (dayGroupLocation.getCurrentPopulation() /groupLocation.getMaxCapacity()); + getLogger().info(groupLocation.getLocation().getLocationName() + " receptivity is: " + receptivity); + bayReceptivity.put(groupLocation.getLocation().getLocationName(), receptivity); + totalReceptivity = receptivity + totalReceptivity; + } + } } getLogger().info("Total receptivity is: " + totalReceptivity); @@ -428,20 +439,28 @@ { if(groupLocation.getLocation().getId()!=1) { - if(totalReceptivity == 0) + Query getdayGroupLocation = dayGroupLocationDao.getCurrentSession().createQuery(getDayGroupLocation); + getdayGroupLocation.setInteger("prevdayNo", currentDayNo); + getdayGroupLocation.setEntity("groupLocation", groupLocation); + Iterator<?> i = getdayGroupLocation.list().iterator(); + while(i.hasNext()) { - groupLocation.setFishReturned(0.0); + dayGroupLocation = (DayGroupLocation)i.next(); + if(totalReceptivity == 0) + { + dayGroupLocation.setFishReturned(0.0); + } + else + { + receptivity = bayReceptivity.get(groupLocation.getLocation().getLocationName()); + dayGroupLocation.setFishReturned(receptivity * totalReturn * receptivity / totalReceptivity); + getLogger().debug("Fish returned to " + groupLocation.getLocation().getLocationName() + "is " + dayGroupLocation.getFishReturned()); + dayGroupLocation.setCurrentPopulation(dayGroupLocation.getCurrentPopulation() + dayGroupLocation.getFishReturned()); + getLogger().debug("New population at " + groupLocation.getLocation().getLocationName() + " is: " + dayGroupLocation.getCurrentPopulation()); + } } - else - { - receptivity = bayReceptivity.get(groupLocation.getLocation().getLocationName()); - groupLocation.setFishReturned(receptivity * totalReturn * receptivity / totalReceptivity); - getLogger().debug("Fish returned to " + groupLocation.getLocation().getLocationName() + "is " + groupLocation.getFishReturned()); - groupLocation.setCurrentPopulation(groupLocation.getCurrentPopulation() + groupLocation.getFishReturned()); - getLogger().debug("New population at " + groupLocation.getLocation().getLocationName() + " is: " + groupLocation.getCurrentPopulation()); - } } - groupLocationDao.save(groupLocation); + dayGroupLocationDao.save(dayGroupLocation); } }catch(Exception e) @@ -450,7 +469,7 @@ } } - public void executeStrategy(Game game) + /*public void executeStrategy(Game game) { //get student Strategies List<DayByDayDecisions> studentDecisions = new ArrayList<DayByDayDecisions>(); @@ -463,16 +482,16 @@ studentStrategiesWithNonRepeatedDecisions.setEntity("game", game); studentStrategiesWithNonRepeatedDecisions.setBoolean("repeated_decisions", repeatedDecisions); - Iterator studentStrategyNonRepeatedIterator = studentStrategiesWithNonRepeatedDecisions.list().iterator(); + Iterator<?> studentStrategyNonRepeatedIterator = studentStrategiesWithNonRepeatedDecisions.list().iterator(); while(studentStrategyNonRepeatedIterator.hasNext()) { DayByDayDecisions dayByDayDecision = (DayByDayDecisions)studentStrategyNonRepeatedIterator.next(); studentDecisions.add(dayByDayDecision); } - executeStrategy(studentDecisions, game, repeatedDecisions); - + // executeStrategy(studentDecisions, game, repeatedDecisions); + //execute repeated decisions repeatedDecisions = true; Query studentStrategiesQueryWithRepeatedDecisions = getDao().getCurrentSession().createQuery(getStudentDayByDayDecisionsFromStrategy); @@ -480,14 +499,14 @@ studentStrategiesQueryWithRepeatedDecisions.setEntity("game", game); studentStrategiesQueryWithRepeatedDecisions.setBoolean("repeated_decisions",repeatedDecisions); - Iterator studentStrategyRepeatedIterator = studentStrategiesQueryWithRepeatedDecisions.list().iterator(); + Iterator<?> studentStrategyRepeatedIterator = studentStrategiesQueryWithRepeatedDecisions.list().iterator(); while(studentStrategyRepeatedIterator.hasNext()) { DayByDayDecisions dayByDayDecision = (DayByDayDecisions)studentStrategyRepeatedIterator.next(); studentDecisions.add(dayByDayDecision); } - executeStrategy(studentDecisions, game, repeatedDecisions); + executeStrategy(studentDecisions, game); }catch(Exception e) { @@ -497,27 +516,191 @@ } - public void executeStrategy(List<DayByDayDecisions> studentDecisions, Game game, boolean repeatedDecisions) + public void executeStrategy(List<DayByDayDecisions> studentDecisions, Game game) { - Query queryforGroups = studentDao.getCurrentSession().createQuery(groupsWithinGame); - - queryforGroups.setLong("current_game_id", game.getId()); - Iterator groups = queryforGroups.list().iterator(); List<GroupLocation> groupLocations = new ArrayList<GroupLocation>(); List<DayByDayDecisions> bay1Students = new ArrayList<DayByDayDecisions>(); List<DayByDayDecisions> bay2Students = new ArrayList<DayByDayDecisions>(); List<DayByDayDecisions> bay3Students = new ArrayList<DayByDayDecisions>(); List<DayByDayDecisions> harborStudents = new ArrayList<DayByDayDecisions>(); - /** For every group find out the groupLocations objects. + + Query queryforGroups = studentDao.getCurrentSession().createQuery(groupsWithinGame); + queryforGroups.setLong("current_game_id", game.getId()); + List<Group> groups = queryforGroups.list(); + + *//** For every group find out the groupLocations objects. * find out the student decisions for current group. * Separate the students for each bay * find out the population for every bay - */ + *//* + StudentStrategy studentStrategy; + Game currentGame = gameDao.find(game.getId()); + getLogger().debug("group size in game: " + groups.size()); + for(int currentDayNumber = 1; currentDayNumber <= currentGame.getGameConfig().getMaxDays(); currentDayNumber++ ) + { + for(Group currentGroup:groups) + { + //Group currentGroup = (Group) groups.next(); + getLogger().debug("group id in executeStrategy is: " + currentGroup.getId()); + groupLocations = groupLocationDao.findAllByProperty("group",currentGroup); + for(DayByDayDecisions tempStudentDecision:studentDecisions) + { + if(tempStudentDecision.getStudent().getGroup().getId() == currentGroup.getId()) + { + *//*** Check for the NON repeated decisions + * If they are not over execute those first + * the go for repeated decision + * + *//* + double bay1Earning = -1.0, bay2Earning = -1.0, bay3Earning = -1.0; + + getLogger().debug("student is : " + tempStudentDecision.getStudent().getId() + "day no: " + currentDayNumber ); + Query checkRepeatedDecision = getDao().getCurrentSession().createQuery(checkForRepeatedDecision); + checkRepeatedDecision.setEntity("student",tempStudentDecision.getStudent()); + checkRepeatedDecision.setInteger("dayNo",currentDayNumber); + + if(checkRepeatedDecision.list().size() == 1) + { + studentStrategy = (StudentStrategy)checkRepeatedDecision.list().get(0); + getLogger().debug("For student: " + tempStudentDecision.getStudent().getId() + "repeated decision is: " + studentStrategy.isRepeatedDecision()); + if(studentStrategy.isRepeatedDecision()) + { + if(studentStrategy.getRepeatedDecisionNo() == 1) //start of the repeated decision sequence again + { + //get all strategies for a student with repeated decision = true + + Query allStrategiesWithRepeatedDecision = studentStrategyDao.getCurrentSession().createQuery(studentStrategiesForRepeatedDecision); + allStrategiesWithRepeatedDecision.setEntity("student",tempStudentDecision.getStudent().getId()); + allStrategiesWithRepeatedDecision.setBoolean("repeated_decision", true); + //allStrategiesWithRepeatedDecision.setEntity("location_id", location.getId()); + + Iterator<?> repeatedDecisionIterator = allStrategiesWithRepeatedDecision.list().iterator(); + while(repeatedDecisionIterator.hasNext()) + { + StudentStrategy tempStudentStrategy = (StudentStrategy)repeatedDecisionIterator.next(); + getLogger().debug("Repeated Decision no is: " + tempStudentStrategy.getRepeatedDecisionNo()); + if(tempStudentStrategy != null) + { + Query queryDBD = getDao().getCurrentSession().createQuery(getDaybyDayDecisionForSuspendCondition); + queryDBD.setEntity("studentStrategy",tempStudentStrategy); + queryDBD.setInteger("dayNo",currentDayNumber); + DayByDayDecisions tempDBD = (DayByDayDecisions)queryDBD.list().get(0); + + if(tempDBD.getLocation().getLocationName().equalsIgnoreCase("Bay1")) + { + bay1Earning = tempDBD.getEarnings(); + //baycounter++; + } + + else if(tempDBD.getLocation().getLocationName().equalsIgnoreCase("Bay2")) + { + bay2Earning = tempDBD.getEarnings(); + //baycounter++; + } + + else if(tempDBD.getLocation().getLocationName().equalsIgnoreCase("Bay3")) + { + bay3Earning = tempDBD.getEarnings(); + //baycounter++; + } + } + } + SuspendRepetition suspendRepetition = null; + + Query suspendRepetitionQuery = suspendRepetitionDao.getCurrentSession().createQuery(getSuspendRepetition); + suspendRepetitionQuery.setEntity("student",tempStudentDecision.getStudent()); + if(suspendRepetitionQuery.list().size() == 1) + { + suspendRepetition = (SuspendRepetition) suspendRepetitionQuery.list().get(0); + } + + if( (bay1Earning < suspendRepetition.getThreshold()) && (bay2Earning < suspendRepetition.getThreshold()) && (bay3Earning < suspendRepetition.getThreshold()) ) + { + + //get all the day by day decisions for next day in harbor days and change the locations to harbor. + getLogger().debug("Suspend Repetition condition is succeeded for student" + tempStudentDecision.getStudent()); + Query DBDBetweenDays = getDao().getCurrentSession().createQuery(dayBydayBetweenDays); + DBDBetweenDays.setEntity("student", tempStudentDecision.getStudent()); + DBDBetweenDays.setInteger("dayNoLow", currentDayNumber); + DBDBetweenDays.setInteger("dayNoHigh", (suspendRepetition.getDaysInHarbor() + currentDayNumber) - 1); + + Iterator<?> dayByDayDecisions = DBDBetweenDays.list().iterator(); + while(dayByDayDecisions.hasNext()) + { + DayByDayDecisions tempDayByDayDecision = (DayByDayDecisions)dayByDayDecisions.next(); + tempDayByDayDecision.setLocation(locationDao.findByProperty("locationName", "Harbor")); + getDao().save(tempDayByDayDecision); + } + + } + + } + } + } + else + { + getLogger().error("Query "+ checkForRepeatedDecision + "is returning more than one parameter. Check the Query!"); + } + + //check for the threshold + + //if(tempStudentDecision.getLocation().getId() == ) + + if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Harbor")) + { + harborStudents.add(tempStudentDecision); + } + else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay1")) + { + bay1Students.add(tempStudentDecision); + } + else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay2")) + { + bay2Students.add(tempStudentDecision); + } + else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay3")) + { + bay3Students.add(tempStudentDecision); + } + } + } + + calculateHarvest(bay1Students,"Bay1",currentGroup,game,currentDayNumber); + calculateHarvest(bay2Students,"Bay2",currentGroup,game,currentDayNumber); + calculateHarvest(bay3Students,"Bay3",currentGroup,game,currentDayNumber); + calculateNewPopulation(currentGroup,currentDayNumber); + bay1Students.clear(); + bay2Students.clear(); + bay3Students.clear(); + harborStudents.clear(); + } + } + + } +*/ + public void executeDayByDayDecisions(List<DayByDayDecisions> studentDecisions, Game game) + { + + Query queryforGroups = studentDao.getCurrentSession().createQuery(groupsWithinGame); + + queryforGroups.setLong("current_game_id", game.getId()); + Iterator<?> groups = queryforGroups.list().iterator(); + List<GroupLocation> groupLocations = new ArrayList<GroupLocation>(); + List<DayByDayDecisions> bay1Students = new ArrayList<DayByDayDecisions>(); + List<DayByDayDecisions> bay2Students = new ArrayList<DayByDayDecisions>(); + List<DayByDayDecisions> bay3Students = new ArrayList<DayByDayDecisions>(); + List<DayByDayDecisions> harborStudents = new ArrayList<DayByDayDecisions>(); + /** For every group find out the groupLocations objects. + * find out the student decisions for current group. + * Separate the students for each bay + * find out the population for every bay + */ + getLogger().debug("number of groups in the game are: " + queryforGroups.list().size()); Game currentGame = gameDao.find(game.getId()); - for(int dayCounter = 0; dayCounter < currentGame.getGameConfig().getMaxDays(); dayCounter++ ) + for(int currentDayNumber = 1; currentDayNumber <= currentGame.getGameConfig().getMaxDays(); currentDayNumber++ ) { while(groups.hasNext()) { @@ -532,19 +715,8 @@ if(tempStudentDecision.getStudent().getGroup().getId() == currentGroup.getId()) { - /** - * Check suspend condition for repeated decisions before putting allocating a student to any location - * - */ - if(repeatedDecisions) + if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Harbor")) { - tempStudentDecision.getStudentStrategy().getThreshold(); - } - - - - if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Harbor")) - { harborStudents.add(tempStudentDecision); } else if(tempStudentDecision.getLocation().getLocationName().equalsIgnoreCase("Bay1")) @@ -560,19 +732,38 @@ bay3Students.add(tempStudentDecision); } } - } - - calculateHarvest(bay1Students,"Bay1",currentGroup,game); - calculateHarvest(bay2Students,"Bay2",currentGroup,game); - calculateHarvest(bay3Students,"Bay3",currentGroup,game); - calculateNewPopulation(currentGroup); - + calculateHarvest(bay1Students,"Bay1",currentGroup,game,currentDayNumber); + calculateHarvest(bay2Students,"Bay2",currentGroup,game,currentDayNumber); + calculateHarvest(bay3Students,"Bay3",currentGroup,game,currentDayNumber); + calculateNewPopulation(currentGroup,currentDayNumber); } } } + + public void setStudentStrategyDao(HibernateStudentStrategyDao studentStrategyDao) { this.studentStrategyDao = studentStrategyDao; } + + + public void setDayGroupLocationDao(HibernateDayGroupLocationDao dayGroupLocationDao) { + this.dayGroupLocationDao = dayGroupLocationDao; + } + + + public HibernateDayGroupLocationDao getDayGroupLocationDao() { + return dayGroupLocationDao; + } + + + public void setSuspendRepetitionDao(HibernateSuspendRepetitionDao suspendRepetitionDao) { + this.suspendRepetitionDao = suspendRepetitionDao; + } + + + public HibernateSuspendRepetitionDao getSuspendRepetitionDao() { + return suspendRepetitionDao; + } } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-11-25 23:31:53 UTC (rev 375) @@ -149,7 +149,7 @@ { assignGroups(game); getLogger().debug("system has assigned groups. "); - pushBlock(game); + //pushBlock(game); flag = true; } else @@ -377,10 +377,10 @@ initCapacity = maxCapacity/2; groupLocation.setInitialPopulation(initCapacity); - groupLocation.setCurrentPopulation(initCapacity); + //groupLocation.setCurrentPopulation(initCapacity); getLogger().info("initiali population set is: " + initCapacity); - groupLocation.setFishLeaving(0.0); - groupLocation.setFishReturned(0.0); + //groupLocation.setFishLeaving(0.0); + //groupLocation.setFishReturned(0.0); } if(location.getLocationName().equalsIgnoreCase("bay2")) @@ -392,10 +392,10 @@ initCapacity = maxCapacity/2; groupLocation.setInitialPopulation(initCapacity); - groupLocation.setCurrentPopulation(initCapacity); + /* groupLocation.setCurrentPopulation(initCapacity); groupLocation.setFishLeaving(0.0); groupLocation.setFishReturned(0.0); - +*/ getLogger().info("initiali population set is: " + initCapacity); } if(location.getLocationName().equalsIgnoreCase("bay3")) @@ -407,11 +407,12 @@ initCapacity = maxCapacity/2; groupLocation.setInitialPopulation(initCapacity); - groupLocation.setCurrentPopulation(initCapacity); + getLogger().info("initiali population set is: " + initCapacity); + /*groupLocation.setCurrentPopulation(initCapacity); groupLocation.setFishLeaving(0.0); groupLocation.setFishReturned(0.0); - +*/ } if(location.getLocationName().equalsIgnoreCase("harbor")) { @@ -422,13 +423,13 @@ initCapacity = maxCapacity/2; groupLocation.setInitialPopulation(initCapacity); - groupLocation.setCurrentPopulation(initCapacity); +/* groupLocation.setCurrentPopulation(initCapacity); getLogger().info("initiali population set is: " + initCapacity); groupLocation.setFishLeaving(0.0); groupLocation.setFishReturned(0.0); - +*/ } - groupLocation.setDayByDayDecsion(null); + //groupLocation.setDayByDayDecsion(null); groupLocationDao.save(groupLocation); getLogger().info("group location is created and id is: " + groupLocation.getId()); Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StrategyService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StrategyService.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StrategyService.java 2009-11-25 23:31:53 UTC (rev 375) @@ -0,0 +1,25 @@ +package edu.asu.commons.mme.service; + +import edu.asu.commons.mme.dao.HibernateDayByDayOtherStudentDecisionsDao; +import edu.asu.commons.mme.dao.HibernateGameDao; +import edu.asu.commons.mme.dao.HibernateGroupDao; +import edu.asu.commons.mme.dao.HibernateGroupLocationDao; +import edu.asu.commons.mme.dao.HibernateLocationDao; +import edu.asu.commons.mme.dao.HibernateStudentDao; +import edu.asu.commons.mme.dao.HibernateStudentStrategyDao; +import edu.asu.commons.mme.entity.StudentStrategy; + + + +public class StrategyService extends Service.Base<StudentStrategy, HibernateStudentStrategyDao> +{ + private HibernateGameDao gameDao; + private HibernateLocationDao locationDao; + private HibernateStudentDao studentDao; + private HibernateGroupLocationDao groupLocationDao; + private HibernateGroupDao groupDao; + private HibernateDayByDayOtherStudentDecisionsDao dayByDayOtherStudentDecisionsDao; + + + +} Modified: mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml =================================================================== --- mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml 2009-11-25 23:31:53 UTC (rev 375) @@ -122,6 +122,9 @@ <property name='sessionFactory' ref='sessionFactory'/> </bean> + <bean id='dayGroupLocationDao' class='edu.asu.commons.mme.dao.HibernateDayGroupLocationDao'> + <property name='sessionFactory' ref='sessionFactory'/> + </bean> <!-- spring managed service layer --> <bean id='gameService' class='edu.asu.commons.mme.service.GameService'> @@ -148,6 +151,9 @@ <property name='gameDao' ref='gameDao'/> <property name='groupLocationDao' ref='groupLocationDao'/> <property name='dayByDayOtherStudentDecisionsDao' ref='dayByDayOtherStudentDecisionsDao'/> + <property name='dayGroupLocationDao' ref='dayGroupLocationDao'/> + <property name='suspendRepetitionDao' ref='suspendRepetitionDao'/> + </bean> <bean id='answeringService' class='edu.asu.commons.mme.service.AnsweringService'> Modified: mentalmodels/trunk/src/main/webapp/WEB-INF/hibernate.cfg.xml =================================================================== --- mentalmodels/trunk/src/main/webapp/WEB-INF/hibernate.cfg.xml 2009-11-20 20:38:48 UTC (rev 374) +++ mentalmodels/trunk/src/main/webapp/WEB-INF/hibernate.cfg.xml 2009-11-25 23:31:53 UTC (rev 375) @@ -41,7 +41,9 @@ <mapping class='edu.asu.commons.mme.entity.DayByDayDecisions'/> <mapping class='edu.asu.commons.mme.entity.DayByDayOtherStudentDecisions'/> + <mapping class='edu.asu.commons.mme.entity.DayGroupLocation'/> + </session-factory> </hibernate-configuration> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |