/** * Get the inputable nodes of the circuit * * @return an hashset of the inputable nodes of the circuit */ public HashSet<Inputable> getInputables() { HashSet<Inputable> inputs = new HashSet<Inputable>(); for (iterNodes = getNodesIterator(); iterNodes.hasNext(); ) { Node n = iterNodes.next(); if (n.getCategoryID() == Node.INPUT) inputs.add((Inputable) n); } return inputs; }
/** * Get the outputable nodes of the circuit * * @return an hashset of the outputable nodes of the circuit */ public HashSet<Outputable> getOutputables() { HashSet<Outputable> outputs = new HashSet<Outputable>(); for (iterNodes = getNodesIterator(); iterNodes.hasNext(); ) { Node n = iterNodes.next(); if (n.getCategoryID() == Node.OUTPUT) outputs.add((Outputable) n); } return outputs; }
public HashSet getLeafOperations() { HashSet result = new HashSet(); Object[] ops = operations.values().toArray(); for (int i = 0; i < ops.length; i++) { PDMOperation op = (PDMOperation) ops[i]; if ((op.getInputElements().isEmpty())) { result.add(op); } } return result; }
/** * Returns a HashSet with the operations that have data element 'data' as output element. * * @param data PDMDataElement * @return HashSet */ public HashSet getOperationsWithOutputElement(PDMDataElement data) { HashSet opso = new HashSet(); Object[] ops = operations.values().toArray(); for (int i = 0; i < ops.length; i++) { PDMOperation op = (PDMOperation) ops[i]; HashMap outputs = op.getOutputElements(); if (outputs.containsValue(data)) { opso.add(op); } } return opso; }
public static PetriNet convert(ConfigurableEPC baseEPC) { HashMap<EPCFunction, Transition> functionActivityMapping; HashMap<EPCConnector, Place> xorconnectorChoiceMapping; // HV: Initialize the mappings. functionActivityMapping = new HashMap<EPCFunction, Transition>(); xorconnectorChoiceMapping = new HashMap<EPCConnector, Place>(); // Check to use the weights if necessary // HV: Add both mappings. On completion, these will be filledd. PetriNet petrinet = EPCToPetriNetConverter.convert( baseEPC, new HashMap(), functionActivityMapping, xorconnectorChoiceMapping); HashSet visible = new HashSet(); // HV: The next block is taken care of by the functionActivityMapping // below. /* * Iterator it = petrinet.getTransitions().iterator(); while * (it.hasNext()) { Transition t = (Transition) it.next(); if (t.object * instanceof EPCFunction) { // if (t.getLogEvent() != null) { // Add * transitions with LogEvent (i.e. referring to functions) * visible.add(t); } } */ // HV: Prevent the places mapped onto from being reduced. visible.addAll(functionActivityMapping.values()); visible.addAll(xorconnectorChoiceMapping.values()); Message.add(visible.toString(), Message.DEBUG); Iterator it = petrinet.getPlaces().iterator(); while (it.hasNext()) { Place p = (Place) it.next(); if (p.inDegree() * p.outDegree() == 0) { // Add Initial and final places to visible, i.e. places that // refer to in and output events visible.add(p); } } // Reduce the PetriNet with Murata rules, while keeping the visible ones PetriNetReduction pnred = new PetriNetReduction(); pnred.setNonReducableNodes(visible); HashMap pnMap = new HashMap(); // Used to map pre-reduction nodes to // post-reduction nodes. PetriNet reduced = pnred.reduce(petrinet, pnMap); if (reduced != petrinet) { // Update both mappings from pre-reduction nodes to post-reduction // nodes. HashMap<EPCFunction, Transition> newFunctionActivityMapping = new HashMap<EPCFunction, Transition>(); for (EPCFunction function : functionActivityMapping.keySet()) { Transition transition = (Transition) functionActivityMapping.get(function); if (pnMap.keySet().contains(transition)) { newFunctionActivityMapping.put(function, (Transition) pnMap.get(transition)); } } functionActivityMapping = newFunctionActivityMapping; HashMap<EPCConnector, Place> newXorconnectorChoiceMapping = new HashMap<EPCConnector, Place>(); for (EPCConnector connector : xorconnectorChoiceMapping.keySet()) { Place place = (Place) xorconnectorChoiceMapping.get(connector); if (pnMap.keySet().contains(place)) { newXorconnectorChoiceMapping.put(connector, (Place) pnMap.get(place)); } } xorconnectorChoiceMapping = newXorconnectorChoiceMapping; } reduced.makeClusters(); // filter the \nunknown:normal ArrayList<Transition> alTrans = reduced.getVisibleTasks(); for (int i = 0; i < alTrans.size(); i++) { Transition t = alTrans.get(i); String id = t.getIdentifier(); int idx = id.indexOf("\\nunknown:normal"); if (idx > 0) { id = id.substring(0, idx); } // �˴������ֵ��ѯ�滻���е�label String mappedId = htDict.get(id); if (mappedId != null) { t.setIdentifier(mappedId); } else { t.setIdentifier(id); } } return reduced; }
@Override public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException { HashSet<ScoredAnnotation> res; try { res = new HashSet<ScoredAnnotation>(); lastTime = Calendar.getInstance().getTimeInMillis(); URL wikiApi = new URL(url); String parameters = "references=true&repeatMode=all&minProbability=0.0&source=" + URLEncoder.encode(text, "UTF-8"); HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection(); slConnection.setRequestProperty("accept", "text/xml"); slConnection.setDoOutput(true); slConnection.setDoInput(true); slConnection.setRequestMethod("POST"); slConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); slConnection.setRequestProperty("charset", "utf-8"); slConnection.setRequestProperty( "Content-Length", "" + Integer.toString(parameters.getBytes().length)); slConnection.setUseCaches(false); DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream()); wr.writeBytes(parameters); wr.flush(); wr.close(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(slConnection.getInputStream()); /* URL wikiApi = new URL(url+"?references=true&repeatMode=all&minProbability=0.0&source="+URLEncoder.encode(text, "UTF-8")); URLConnection wikiConnection = wikiApi.openConnection(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(wikiConnection.getInputStream()); */ lastTime = Calendar.getInstance().getTimeInMillis() - lastTime; XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression idExpr = xpath.compile("//detectedTopic/@id"); XPathExpression weightExpr = xpath.compile("//detectedTopic/@weight"); XPathExpression referenceExpr = xpath.compile("//detectedTopic/references"); NodeList ids = (NodeList) idExpr.evaluate(doc, XPathConstants.NODESET); NodeList weights = (NodeList) weightExpr.evaluate(doc, XPathConstants.NODESET); NodeList references = (NodeList) referenceExpr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < weights.getLength(); i++) { if (weights.item(i).getNodeType() != Node.TEXT_NODE) { int id = Integer.parseInt(ids.item(i).getNodeValue()); float weight = Float.parseFloat(weights.item(i).getNodeValue()); // System.out.println("ID="+ids.item(i).getNodeValue()+" weight="+weight); XPathExpression startExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@start"); XPathExpression endExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@end"); NodeList starts = (NodeList) startExpr.evaluate(references.item(i), XPathConstants.NODESET); NodeList ends = (NodeList) endExpr.evaluate(references.item(i), XPathConstants.NODESET); for (int j = 0; j < starts.getLength(); j++) { int start = Integer.parseInt(starts.item(j).getNodeValue()); int end = Integer.parseInt(ends.item(j).getNodeValue()); int len = end - start; res.add(new ScoredAnnotation(start, len, id, weight)); } } } } catch (Exception e) { e.printStackTrace(); throw new AnnotationException( "An error occurred while querying Wikipedia Miner API. Message: " + e.getMessage()); } return res; }
public HashSet calculateNextStates( PDMState state, PDMStateSpace statespace, boolean root, boolean failure, int numStates, int breadth) { HashSet result = new HashSet(); HashSet data = state.dataElements; HashSet exec1 = state.executedOperations; HashSet failed = state.failedOperations; HashSet execOps = calculateExecutableOperations(data, exec1, failed, root); Iterator it = execOps.iterator(); int b = 0; int bLimit = 0; if (!failure) { bLimit = breadth; } else { bLimit = breadth / 2; } // for each executable operation a new state is created while (it.hasNext() && i < numStates && b < bLimit) { if (!failure) { PDMOperation op = (PDMOperation) it.next(); PDMDataElement d = op.getOutputElement(); // First, add the state with the operation 'op' succesfully // executed HashSet ins2 = (HashSet) data.clone(); // NB: is it necessary to // clear this clone // again? ins2.add(d); HashSet exec2 = (HashSet) exec1.clone(); exec2.add(op); PDMState s = checkIfStateExists(statespace, ins2, exec2, failed); // Check whether the new state already exists // If so, then another link to this state is made if (!(s == null)) { PDMState st = s; PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), 1.0); statespace.addEdge(edge); } // If not, a new state is created and linked to the current // state else { String name = "state" + i; // int num = checkStatusOfState(statespace, ) PDMState st = new PDMState(statespace, name, ins2, exec2, failed); statespace.addState(st); result.add(st); PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), 1.0); statespace.addEdge(edge); i++; b++; } } // Then, if failure of operations is considered, add the state with // the failed operations 'op'. if (failure) { PDMOperation op = (PDMOperation) it.next(); PDMDataElement d = op.getOutputElement(); // First, add the state with the operation 'op' succesfully // executed HashSet ins2 = (HashSet) data.clone(); // NB: is it necessary to // clear this clone // again? ins2.add(d); HashSet exec2 = (HashSet) exec1.clone(); exec2.add(op); PDMState s = checkIfStateExists(statespace, ins2, exec2, failed); // Check whether the new state already exists // If so, then another link to this state is made if (!(s == null)) { PDMState st = s; double prob = 1.0 - (op.getFailureProbability()); PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), prob); statespace.addEdge(edge); } // If not, a new state is created and linked to the current // state else { String name = "state" + i; // int num = checkStatusOfState(statespace, ) PDMState st = new PDMState(statespace, name, ins2, exec2, failed); statespace.addState(st); result.add(st); double prob = 1.0 - (op.getFailureProbability()); PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), prob); statespace.addEdge(edge); i++; b++; } HashSet failed2 = (HashSet) failed.clone(); failed2.add(op); PDMState s2 = checkIfStateExists(statespace, data, exec1, failed2); if (!(s2 == null)) { PDMState st = s2; PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), op.getFailureProbability()); statespace.addEdge(edge); } // If not, a new state is created and linked to the current // state else { String name = "state" + i; PDMState st = new PDMState(statespace, name, data, exec1, failed2); statespace.addState(st); result.add(st); PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), op.getFailureProbability()); statespace.addEdge(edge); i++; b++; } // failed2.clear(); } } return result; }
public PDMStateSpace calculateSimpleStateSpace( boolean root, boolean failure, boolean input, boolean colored, int numStates, int breadth) { PDMStateSpace result = new PDMStateSpace(this, colored); HashSet states = new HashSet(); int j = (operations.size() + 1); if (!input) { HashSet empty = new HashSet(); PDMState st = new PDMState(result, "state" + i, empty, empty, empty); result.addState(st); states.add(st); i++; } else { // Start with the complete set of input data elements available HashSet empty = new HashSet(); String name = new String("state" + i); HashSet ins = new HashSet(); // this hashSet contains the input // elements to the process (input // elements of PDM) HashSet execOps = new HashSet(); // Fill the hashSet with the leaf elements HashMap leafs = getLeafElements(); Object[] leafElts = leafs.values().toArray(); for (int i = 0; i < leafElts.length; i++) { PDMDataElement d = (PDMDataElement) leafElts[i]; ins.add(d); } HashSet leafOps = getLeafOperations(); Iterator it = leafOps.iterator(); while (it.hasNext()) { PDMOperation op = (PDMOperation) it.next(); execOps.add(op); } PDMState start = new PDMState(result, name, ins, execOps, empty); // start // state // of // the // statespace result.addState(start); i++; states.add(start); } while (!states.isEmpty()) { HashSet states2 = (HashSet) states.clone(); Iterator it = states2.iterator(); while (it.hasNext()) { PDMState state = (PDMState) it.next(); HashSet nextStates = calculateNextStates(state, result, root, failure, numStates, breadth); Iterator it2 = nextStates.iterator(); // Add the new states to iterator while (it2.hasNext()) { PDMState st = (PDMState) it2.next(); states.add(st); } states.remove(state); } } i = 0; j = 0; Message.add("<PDMMDPStateSpace>", Message.TEST); Message.add("<NumberOfStates = " + result.getNumberOfStates() + " >", Message.TEST); Message.add("</PDMMDPStateSpace>", Message.TEST); return result; }
public HashSet calculateExecutableOperations( HashSet dataElts, HashSet executed, HashSet failed, boolean root) { HashSet result = new HashSet(); HashSet enabledOperations = new HashSet(); if (root) { // Calculate the enabled operations (i.e. those operation of which // all input elements are in the set of available elements) Object[] ops = operations.values().toArray(); for (int i = 0; i < ops.length; i++) { PDMOperation op = (PDMOperation) ops[i]; HashMap inputs = op.getInputElements(); Object[] ins = inputs.values().toArray(); boolean enabled = true; int k = 0; while (enabled && k < ins.length) { PDMDataElement d = (PDMDataElement) ins[k]; if (!(dataElts.contains(d))) { enabled = false; } k++; } if (enabled) { enabledOperations.add(op); // System.out.println("Enabled operation: "+ op.getID()); } } } else if (!(dataElts.contains(this.getRootElement()))) { // Calculate the enabled operations (i.e. those operation of which // all input elements are in the set of available elements) Object[] ops = operations.values().toArray(); for (int i = 0; i < ops.length; i++) { PDMOperation op = (PDMOperation) ops[i]; HashMap inputs = op.getInputElements(); Object[] ins = inputs.values().toArray(); boolean enabled = true; int k = 0; while (enabled && k < ins.length) { PDMDataElement d = (PDMDataElement) ins[k]; if (!(dataElts.contains(d))) { enabled = false; } k++; } if (enabled) { enabledOperations.add(op); } } } // remove already executed operations Iterator exIt = executed.iterator(); while (exIt.hasNext()) { PDMOperation op = (PDMOperation) exIt.next(); enabledOperations.remove(op); } // remove already failed operations Iterator fIt = failed.iterator(); while (fIt.hasNext()) { PDMOperation op = (PDMOperation) fIt.next(); enabledOperations.remove(op); } result = enabledOperations; return result; }