private void initState() { int numNodes = state.getNodeMap().size(); int numSlots = numNodes * slotsPerNode; this.legend = new Legend(null, null, 0); int latSides = (int) Math.ceil(Math.pow(numNodes, 1 / 3.0)); log.info("Lattice of " + numNodes + " requires " + latSides + " per side"); this.lattice = new Lattice<Lattice<Void>>(latSides); List<String> nodeNames = new ArrayList<String>(); List<Lattice<Void>> nodeLatti = new ArrayList<Lattice<Void>>(); for (GridNode node : state.getNodeMap().values()) { Lattice<Void> nodeLattice = new Lattice<Void>(nodeLatSides); List<String> slotNames = new ArrayList<String>(); List<Void> slots = new ArrayList<Void>(); for (int i = 0; i < slotsPerNode; i++) { slotNames.add("" + i); slots.add(null); } nodeLattice.addItems(slotNames, slots); nodeNames.add(node.getShortName()); nodeLatti.add(nodeLattice); // log.warn("Adding lattice for node "+node.getShortName()); } lattice.addItems(nodeNames, nodeLatti); // Initialize actors from the grid state for (GridNode node : state.getNodeMap().values()) { int s = 0; for (GridJob job : node.getSlots()) { if (job == null) continue; String slotName = s + ""; JobActor jobActor = createJobActor(job); jobActor.pos = getLatticePos(node.getShortName(), slotName); log.info( "Starting job {} on slot: {}", job.getFullJobId(), node.getShortName() + "#" + slotName); addJobActor(job.getFullJobId(), jobActor); s++; } } }
/** * Internal routine used when loading Lattices from .LAT files * * @param lattice * @param tokens */ static void load(Lattice lattice, StringTokenizer tokens) { String id = tokens.nextToken(); String label = tokens.nextToken(); long beginTime = Long.parseLong(tokens.nextToken()); long endTime = Long.parseLong(tokens.nextToken()); Word word = new Word(label, new Pronunciation[0], label.startsWith("<") || label.startsWith("[")); lattice.addNode(id, word, beginTime, endTime); }
private PVector getLatticePos(String nodeName, String slotName) { PVector nodeLoc = lattice.getLocation(nodeName); Lattice<Void> slotLat = lattice.getContents(nodeName); if (slotLat == null) { log.warn("No slot lattice for node " + nodeName); return new PVector(0, 0, 0); } // Node position within the lattice, natural coordinates float nodeOffset = nodeSize + 2 * nodePadding; PVector nodePos = nodeLoc.get(); nodePos.mult(nodeOffset); nodePos.add(nodePadding, nodePadding, nodePadding); PVector slotLoc = slotLat.getLocation(slotName); if (slotLoc == null) { log.warn("No slot location for slot " + slotName + " in node " + nodeName); return nodePos.get(); } // TODO: cache these calculations // Slot position within the node, natural coordinates float slotOffset = jobSize + 2 * jobPadding; PVector slotPos = slotLoc.get(); slotPos.mult(slotOffset); slotPos.add(jobPadding, jobPadding, jobPadding); // Combine node and slot position, natural coordinates PVector pos = slotPos.get(); pos.add(nodePos); // Convert into central coordinate system float latticeWidth = lattice.size * nodeOffset; float t = latticeWidth / 2; pos.sub(t, t, t); return pos; }
/** * Transform the current state into the new state. We iterate over all entries in the state vector * that need updating and transform them according to the defined transformation method. * * @param nodesToUpdate Nodes which must be considered in this transformation step. */ private void transformState(final Set<GraphNode> nodesToUpdate) { final StateVector<GraphNode, LatticeElement> newState = new StateVector<GraphNode, LatticeElement>(); final Set<GraphNode> newNodesToUpdate = new LinkedHashSet<GraphNode>(); for (final GraphNode node : nodesToUpdate) { final List<IInfluencingState<LatticeElement, ObjectType>> influencingStates = getStates(walker.getInfluencing(node)); final LatticeElement combinedState = lattice.combine(influencingStates); final LatticeElement transformedState = transformationList.transform(node, state.getState(node), combinedState); newState.setState(node, transformedState); if (debugger != null) { debugger.updatedState(node, influencingStates, transformedState); } // State has changed since the last iteration => We need another iteration with the // nodes that are influenced by this state change. if (!newState.getState(node).equals(state.getState(node))) { newNodesToUpdate.addAll(walker.getInfluenced(node)); } if (newState.getState(node).lessThan(state.getState(node))) { throw new IllegalStateException("Non-monotone transformation detected"); } } updateCurrentState(newState); if (debugger != null) { debugger.updatedState(state); } nodesToUpdate.clear(); nodesToUpdate.addAll(newNodesToUpdate); }
/** Use example for Context and ConceptLattice classes * */ public static void ExampleIS(String name) { try { // load an IS from the "ISrules.txt" file String nameIS = name + ".txt"; ImplicationalSystem base = new ImplicationalSystem(inputDir + nameIS); // create the directory to save files File f = new File(outputDir + name); f.mkdir(); // create the Readme file name = name + File.separator + name; BufferedWriter file = new BufferedWriter(new FileWriter(outputDir + name + "Readme.txt")); String log = "EXAMPLE FOR IS AND CONCEPTLATTICE CLASSES\n"; log += "-----------------------------------------\n"; log += "-> Initial set of rules (" + base.sizeRules() + " rules):\n" + base + "\n"; System.out.println(log); file.write(log); // computes the precedence graph of the IS DGraph prec = base.precedenceGraph(); String namePrecGraph = name + "PrecedenceGraph.dot"; prec.save(outputDir + namePrecGraph); log = "Precedence graph of IS saved in " + namePrecGraph + "\n"; System.out.println(log + prec.toString()); file.write(log); // some IS transformation log = "-> Some IS transformations: \n"; base.makeUnary(); log += "-> Unary equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n"; base.makeLeftMinimal(); log += "Left minimal equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n"; base.makeRightMaximal(); log += "Right maximal equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n"; base.makeCompact(); log += "Compact equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n"; System.out.println(log); file.write(log); // computes and prints the closed set lattice of the initial rules with NextClosure ConceptLattice CLNC = base.closedSetLattice(false); String nameCLNC = name + "ClosedSetLatticeNextClosure.dot"; CLNC.save(outputDir + nameCLNC); log = "-> Closed set lattice of IS (generated by Next Closure algorithm) saved in " + nameCLNC + "\n"; System.out.println(log + CLNC.toString()); file.write(log); // computes and prints the closed set lattice of the initial rules with Bordat ConceptLattice CLBordat = base.closedSetLattice(true); String nameCLBordat = name + "ClosedSetLatticeBordat.dot"; CLBordat.save(outputDir + nameCLBordat); log = "-> Closed set lattice of IS (generated by Bordat's algorithm) saved in " + nameCLBordat + "\n"; System.out.println(log + CLBordat.toString()); file.write(log); // computes dependance graph, minimal generators and canonical direct basis log = "-> Components generated while Bordat's algorithm computes the lattice:\n"; DGraph ODG = CLBordat.getDependencyGraph(); String nameODG = name + "DependanceGraphOfClosedSetLattice.dot"; ODG.save(outputDir + nameODG); log += "Dependance graph of closed set lattice saved in " + nameODG + "\n"; System.out.println(log + ODG.toString()); file.write(log); TreeSet MinGen = CLBordat.getMinimalGenerators(); log = "Minimal generators of closed set lattice : " + MinGen + "\n"; ImplicationalSystem CLBCD = CLBordat.getCanonicalDirectBasis(); String nameCLBCD = name + "CanonicalDirectBasisOfClosedSetLattice.txt"; CLBCD.save(outputDir + nameCLBCD); log += "Canonical direct basis of closed set lattice saved in " + nameCLBCD + ": \n" + CLBCD.toString(); System.out.println(log); file.write(log); // computes the canonical basis and the closed set lattice of the basis base.makeCanonicalBasis(); String nameBC = name + "CanonicalBasis.txt"; base.save(outputDir + nameBC); log = "Canonical basis (" + base.sizeRules() + " rules) saved in " + nameBC + ": \n" + base; ConceptLattice CLBC = base.closedSetLattice(true); String nameCLBC = name + "ClosedSetLatticeOfCanonicalBasis.dot"; CLBC.save(outputDir + nameCLBC); log += "Closed set lattice of the canonical basis saved in " + nameCLBC + "\n"; System.out.println(log + CLBC.toString()); file.write(log); // BIJECTION log = "--- BIJECTION --- \n"; log += "Concept lattice of initial IS (" + nameCLBordat + ") isomorphic to\n"; log += "Concept lattice of the canonical basis of initial IC (" + nameCLBC + ")\n"; log += "-----------------\n"; // computes the canonical directe basis base.makeCanonicalDirectBasis(); String nameBCD = name + "CanonicalDirectBasis.txt"; base.save(outputDir + nameBC); log = "-> Canonical direct basis (" + base.sizeRules() + " rules) saved in " + nameBCD + ": \n" + base; System.out.println(log); file.write(log); // BIJECTION log = "--- BIJECTION --- \n"; log += "Canonical direct basis of initial IS (" + nameBCD + ") isomorphic to\n"; log += "Canonical direct basis of the concept lattice of initial IC (" + nameCLBCD + ")\n"; log += "-----------------\n"; // computes the closed set lattice of the canonical direct basis ConceptLattice BCDCL = base.closedSetLattice(true); String nameBCDCL = name + "ClosedSetLatticeOfCanonicalDirectBasis.dot"; BCDCL.save(outputDir + nameCLBCD); log += "-> Closed set lattice of the canonical direct basis saved in " + nameBCDCL + "\n"; System.out.println(log + BCDCL.toString()); file.write(log); // BIJECTION log = "--- BIJECTION --- \n"; log += "Closed set lattice of initial IS (" + nameCLBordat + ") isomorphic to\n"; log += "Closed set lattice of the canonical direct basis of initial IC (" + nameBCDCL + ")\n"; log += "-----------------\n"; System.out.println(log); file.write(log); // computes and prints the join reduction of the closed set lattice Lattice L = CLBordat.getJoinReduction(); String nameCLJoinReduced = name + "LatticeJoinReduction.dot"; L.save(outputDir + nameCLJoinReduced); log = "-> Join reduction of the concept lattice saved in " + nameCLJoinReduced + "\n"; System.out.println(log + L.toString()); file.write(log); // computes the table of irreducible nodes of the reduced lattice Context T = L.getTable(); String nameTable = name + "TableOfReducedLattice.txt"; T.save(outputDir + nameTable); log = "-> Irreducibles table saved in " + nameTable + ":\n " + T; System.out.println(log); file.write(log); // computes the concept lattice of the table ConceptLattice CLTable = T.conceptLattice(false); String nameCLTable = name + "ConceptLatticeOfTable.dot"; CLTable.save(outputDir + nameCLTable); log = "Concept lattice of the table saved in " + nameCLTable + "\n"; System.out.println(log + CLTable.toString()); file.write(log); // BIJECTION log = "--- BIJECTION --- \n"; log += "Concept lattice of the canonical direct basis of initial IC (" + nameCLBCD + ") is isomorphic to \n"; log += "is isomorphic to concept lattice of its irreducibles table (" + nameCLTable + ")\n"; log += "-----------------\n"; System.out.println(log); file.write(log); file.close(); } catch (Exception e) { } }
/** Use example for DAGraph and Lattice classes * */ public static void ExampleDAGraph() { try { String name = "DAGraph"; // create the directory to save files File f = new File(outputDir + name); f.mkdir(); // create the Readme file name = name + File.separator + name; BufferedWriter file = new BufferedWriter(new FileWriter(outputDir + name + "Readme.txt")); String log = "EXAMPLE FOR DAGRAPH AND LATTICE CLASSES\n"; log += "-----------------------------------------\n"; System.out.println(log); file.write(log); // randomly generates a directed graph of 10 nodes DAGraph G = DAGraph.random(10); String nameGraph = name + ".dot"; G.save(outputDir + nameGraph); log = "-> Randomly generated DAGraph saved in " + nameGraph + "\n"; System.out.println(log + G.toString()); file.write(log); // verify if the dagraph is acyclic log = nameGraph + " acyclic? " + G.isAcyclic() + "\n"; System.out.println(log); file.write(log); // computes and print the transitive reduction of the dagraph G.transitiveReduction(); String nameTR = name + "TransitiveReduction.dot"; G.save(outputDir + nameTR); log = "-> Transitive reduction saved in " + nameTR + "\n"; System.out.println(log + G.toString()); file.write(log); // computes and print the ideal and the filter of the first node Node n = G.getNodes().first(); DAGraph ideal = G.ideal(n); String nameIdeal = name + "Ideal.dot"; ideal.save(outputDir + nameIdeal); log = "-> Minorants of " + n + " : " + G.minorants(n) + "\n saved as a dagraph in " + nameIdeal + "\n"; System.out.println(log); file.write(log); DAGraph filter = G.filter(n); String nameFilter = name + "Filter.dot"; filter.save(outputDir + nameFilter); log = "-> Majorants of " + n + " : " + G.majorants(n) + "\n saved as a dagraph in " + nameFilter + "\n"; System.out.println(log); file.write(log); // computes and print the ideals lattice of the dagraph ConceptLattice CSL = ConceptLattice.idealLattice(G); String nameIdealsLattice = name + "IdealsLattice.dot"; CSL.save(outputDir + nameIdealsLattice); log = "-> Ideal lattice saved in " + nameIdealsLattice + "\n"; System.out.println(log + CSL.toString()); file.write(log); // check if the ideals lattice is a lattice log = "-> Check if the ideal lattice is a lattice ? " + CSL.isLattice() + "\n"; System.out.println(log); file.write(log); // print the irreducibles elements of the ideal lattice log = "-> Join irreducibles of ideal lattice: " + CSL.joinIrreducibles() + "\n"; log += "Meet irreducibles of ideal lattice: " + CSL.meetIrreducibles() + "\n"; System.out.println(log); file.write(log); // reduces the ideal lattice by replacing each join irreducible node by one element Lattice L = CSL.getJoinReduction(); String nameReducedLattice = name + "ReducedLattice.dot"; L.save(outputDir + nameReducedLattice); log = "-> Reduced ideal lattice saved in " + nameReducedLattice + "\n"; System.out.println(log + L.toString()); file.write(log); // print the irreducibles elements of the reduces ideal lattice log = "-> Join irreducibles of reduced ideal lattice: " + L.joinIrreducibles() + "\n"; log += "Meet irreducibles of reduced ideal lattice: " + L.meetIrreducibles() + "\n"; System.out.println(log); file.write(log); // computes the table of the reduced lattice Context T = L.getTable(); String nameTable = name + "IrrTable.txt"; T.save(outputDir + nameTable); log = "-> Irreducibles table of the reduced ideal lattice saved in " + nameTable + ":\n " + T.toString(); System.out.println(log); file.write(log); // compute the subgraph of join irreducible nodes DAGraph JIrr = L.joinIrreduciblesSubgraph(); String nameIrrSG = name + "IrrSubgraph.dot"; JIrr.save(outputDir + nameIrrSG); log = "-> Join irreducibles subgraph saved in " + nameIrrSG + "\n"; System.out.println(log + JIrr.toString()); file.write(log); // BIJECTION log = "--- BIJECTION --- \n"; log += "Initial random DAGraph (" + nameGraph + ") isomorphic to\n"; log += "Join irreducible subgraph of its ideal lattice (" + nameIrrSG + ")\n"; System.out.println(log); file.write(log); file.close(); } catch (Exception e) { } }