/// <summary> /// Generates a knowledge structure (KStructure object) from a ranked order (RankOrder object) by applying Rule 1. /// </summary> /// /// <param name="rankOrder">RankOrder object that is used to generate a knowledge structure.</param> /// /// <returns>KStructure object, or null if error occured.</returns> public KStructure createKStructure(RankOrder rankOrder) { // Implements Rule 1: Given a set GR of problems with a rank R and a set G<R of problems of lower ranks, // a union of any subset of GR with G<R is a knowledge state. // [SC] make sure the rankOrder object is not null if (rankOrder == null) { Log(Severity.Error, "createKStructure: Null object is passed as RankOrder parameter. Returning null."); return(null); } // [SC] make sure there is at least one rank in the rank order if (rankOrder.getRankCount() == 0) { Log(Severity.Error, "createKStructure: rank order has no ranks. Returning null."); return(null); } // [SC] make sure the ranks are sorted in an ascending order rankOrder.sortAscending(); // [SC] creating knowledge states List <KState> allStates = new List <KState>(); List <PCategory> prevCategories = new List <PCategory>(); foreach (Rank rank in rankOrder.getRanks()) { // [SC] getting all unique subsets of categories in this rank List <List <PCategory> > subsets = rank.getSubsets(); // [SC] for each subset, create a knowledge state by combining with all categories of lower ranks foreach (List <PCategory> subset in subsets) { KState state = new KState(); foreach (PCategory category in prevCategories) { state.addCategory(category); } foreach (PCategory category in subset) { state.addCategory(category); } allStates.Add(state); } prevCategories.AddRange(rank.getCategories()); } // [SC] sort states by their sizes in an ascending order allStates.Sort((stateOne, stateTwo) => stateOne.getCategoryCount().CompareTo(stateTwo.getCategoryCount())); // [SC] creating an empty knowledge structure object KStructure ks = new KStructure(rankOrder); // [SC] creating 0th rank with an empty root state int rankIndex = 0; int stateCounter = 0; // [SC] used to generate an ID for each state KSRank prevRank = null; KSRank currRank = new KSRank(rankIndex); // [SC] the root rank will automatically add an empty root state ks.addRank(currRank); // [SC] adding all states in respective ranks foreach (KState state in allStates) { if (state.getCategoryCount() > rankIndex) { stateCounter = 0; prevRank = currRank; currRank = new KSRank(++rankIndex); ks.addRank(currRank); } if (currRank.addState(state)) { state.Id = KSGenerator.getStateID(rankIndex, ++stateCounter); foreach (KState prevState in prevRank.getStates()) { if (prevState.isSubsetOf(state)) { prevState.addNextState(state); state.addPrevState(prevState); } } } } return(ks); }
/// <summary> /// Expands the specified knowledge structure with new states by applying Rule 2. /// </summary> /// /// <param name="ks">Knowledge structure to be expanded with new states.</param> /// /// <returns>Expanded knowledge structure</returns> public void createExpandedKStructure(KStructure ks) { // Implements Rule 2: Given a set GR of problems with a rank R and a set GR-1 of problems with rank R-1, // a union of any subset of GR with any knowledge state KR-1 containing at least one problem from GR-1 is a state. // [SC] make sure the knowledge structure object is not null if (ks == null) { Log(Severity.Error, "createExpandedKStructure: KStructure object is null. Returning from method."); return; } // [SC] make sure the rank order of categories is available if (!ks.hasRankOrder()) { Log(Severity.Error, "createExpandedKStructure: KStructure object contains no rank order. Returning from method."); return; } // [SC] make sure the knowledge structure has ranks if (!ks.hasRanks()) { Log(Severity.Error, "createExpandedKStructure: KStructure object contains no ranks with states. Returning from method."); return; } Rank prevRank = null; foreach (Rank rank in ks.rankOrder.getRanks()) { if (prevRank != null) { // [SC] getting all unique subsets of categories in this rank List <List <PCategory> > subsets = rank.getSubsets(); // [SC] retrieve all KS ranks with minimum required state size List <KSRank> ksRanks = ks.getRanks().FindAll(rankOne => rankOne.RankIndex >= prevRank.RankIndex); if (ksRanks == null || ksRanks.Count == 0) { continue; } // [SC] this list contains all relevant states that contain any category from GR-1 List <KState> states = new List <KState>(); foreach (KSRank ksRank in ksRanks) { // [SC] From given KS rank, retrieve all states that contain at least one problem from GR-1 and add them to the common list states.AddRange(ksRank.getStates().FindAll(stateOne => stateOne.getCategories().Intersect(prevRank.getCategories()).Any())); } if (states.Count == 0) { continue; } // [SC] iterate through subsets of GR foreach (List <PCategory> subset in subsets) { foreach (KState state in states) { // [SC] if state already contains the entire subset then skip it if (state.getCategories().Intersect(subset).Count() == subset.Count) { continue; } // [SC] creating a new state KState newState = new KState(KSGenerator.EXPANDED_STATE); foreach (PCategory category in state.getCategories()) { newState.addCategory(category); } foreach (PCategory category in subset) { newState.addCategory(category); } // [SC] add the new state to the respective rank KSRank newStateRank = ks.getRanks().Find(rankOne => rankOne.RankIndex == newState.getCategoryCount()); if (newStateRank.addState(newState)) { newState.Id = KSGenerator.getStateID(newStateRank.RankIndex, newStateRank.getStateCount()); // [SC] link the new state with previous states of lower rank KSRank prevStateRank = ks.getRanks().Find(rankOne => rankOne.RankIndex == newState.getCategoryCount() - 1); if (prevStateRank != null) { foreach (KState prevState in prevStateRank.getStates()) { if (prevState.isSubsetOf(newState)) { prevState.addNextState(newState); newState.addPrevState(prevState); } } } // [SC] link the new state with next states of higher rank KSRank nextStateRank = ks.getRanks().Find(rankOne => rankOne.RankIndex == newState.getCategoryCount() + 1); if (nextStateRank != null) { foreach (KState nextState in nextStateRank.getStates()) { if (newState.isSubsetOf(nextState)) { nextState.addPrevState(newState); newState.addNextState(nextState); } } } } } } } prevRank = rank; } }