NWFAvatar {{ private Config config; /** Constructor to be called during registration where you supply config */ public NWFAvatar(Config cfg){ config = cfg; } /** proposing random unique claims which are not in the forbidden list */ public List propose(List forbiddenClaims){ List claims = List.create(); SCGConfig scg_cfg = config.getScgCfg(); int maxProposals = scg_cfg.getMaxProposals() - 1; Claim claim = null; for (int i = 0; i < maxProposals; i++) { claim = generateRandomClaim(); while (forbiddenClaims.contains(claim) || claims.contains(claim)) { claim = generateRandomClaim(); } claims = claims.append(claim); } return claims; } /** * Generates random claim */ private Claim generateRandomClaim() { Random rand = new Random(); NWFConfig nwf_cfg = (NWFConfig)config.getDomainConfig(); int maxN = nwf_cfg.getMaxN(); // c1, c2, c3 are in [1, maxN] int c1 = rand.nextInt(maxN) + 1; int c2 = rand.nextInt(maxN) + 1; int c3 = rand.nextInt(maxN) + 1; String inpt = "instance " + "\"s\" successors (\"1\" c " + c1 + " f 0) " + "\"1\" successors (\"t\" c " + c2 + " f 0) " + "\"2\" successors (\"t\" c " + c3 + " f 0) " + "\"t\" successors () " + "source \"s\" " + "sink \"t\""; NWFInstance singleton = null; try { singleton = NWFInstance.parse(inpt); } catch (nwf.ParseException e) { e.printStackTrace(); } NWFInstanceSet instanceSet = new NWFInstanceSet(singleton); // To Change: The protocol instance must be one of the // allowed protocols mentioned in SCGConfig Cons protocolsAllowed = config.getScgCfg().getProtocols(); ProtocolI protocol = generateRandomAllowedProtocol(protocolsAllowed ); return new Claim(instanceSet, protocol, 0.5, 0.5); } /** generates a random protocol instance from the given protocolsAllowed */ private ProtocolI generateRandomAllowedProtocol(Cons protocolsAllowed){ Random rand = new Random(); FullyQualifiedClassName randProtocol = protocolsAllowed.lookup(rand.nextInt(protocolsAllowed.length())); ProtocolI protocol = null; try{ Class protocolClass = Class.forName(randProtocol.print().trim()); Method instance = protocolClass.getMethod("parse", String.class); protocol = (ProtocolI) instance.invoke(null, randProtocol.print().trim()); }catch(Exception ex){ ex.printStackTrace(); } return protocol; } /** Random oppose method - randomly agrees, refutes or strengthens */ public List oppose(List claimsToBeOpposed){ return claimsToBeOpposed.map(new List.Map() { public OpposeAction map(Claim claim){ Random rand = new Random(); int randOppose = rand.nextInt(3); if(randOppose == 0) return new Agreement(); else if(randOppose == 1){ double q = claim.getQuality(); SCGConfig scg_cfg = config.getScgCfg(); if (claim.getProtocol() instanceof ForAllExists){ if (q > scg_cfg.getMinStrengthening()) return new Strengthening(claim.getQuality() - scg_cfg.getMinStrengthening()); else return new Refuting(); }else{ if (q < scg_cfg.getMinStrengthening()) { return new Strengthening(claim.getQuality() + scg_cfg.getMinStrengthening()); } else { return new Refuting(); } } }else return new Refuting(); } }); } /** providing instance method - provides symmetric Instance of given instanceSet */ public InstanceI provide(Claim claimToBeProvided){ NWFInstanceSet nwfInstanceSet = (NWFInstanceSet)claimToBeProvided.getInstanceSet(); NWFInstance nwfInstance = new NWFInstance(nwfInstanceSet.getSingleton().getG(), nwfInstanceSet.getSingleton().getSource(), nwfInstanceSet.getSingleton().getSink()); return nwfInstance; } /** */ public SolutionI solve(SolveRequest solveRequest){ NWFInstance nwfInstance = (NWFInstance)solveRequest.getInstance(); NWFSolution solution = solve(nwfInstance); return solution; } /** * solve function */ private NWFSolution solve(NWFInstance nwfInstance) { return new NWFSolution(nwfInstance.getG(), nwfInstance.getSource(), nwfInstance.getSink()); } }}