0

我有一个 .rule 文件、一个 .bat 文件和一个 .jar 文件。

.rule 的内容是

exLiving="Living Existence Sensor";
dvLight="Light";
LivingLamp1="LivingLamp1";
LivingLamp2="LivingLamp2";
LivingLamp3="LivingLamp3";
LivFan="LivingFan";
clock="Example";
curtain="Curtain";
camera="SurveillanceCamera";
component="MusicPlayer";
fan="Fan";

ruleset(0) {
    if( !exLiving.mahmoud && !exLiving.Alice ){
        dvLight.SetPower(0.0);
        LivingLamp1.SetPower(1.0);
        LivingLamp2.SetPower(1.0);
        LivingLamp3.SetPower(1.0);
        LivFan.SetPower(0.0);
        clock.SetStatus(true);
        curtain.SetStatus(false);

        camera.SetPower(false);

        component.SetPower(false);

        fan.SetStatus(false);
    }   


if( exLiving.mahmoud && exLiving.Alice ){
        dvLight.SetPower(1.0);
        LivingLamp1.SetPower(0.0);
        LivingLamp2.SetPower(1.0);
        LivingLamp3.SetPower(0.0);
        LivFan.SetPower(0.0);
        clock.SetStatus(true);
        curtain.SetStatus(true);

        camera.SetPower(false);

        component.SetPower(true);

        fan.SetStatus(true);



}
    if( exLiving.mahmoud && !exLiving.Alice ){
        dvLight.SetPower(0.0);
        LivingLamp1.SetPower(1.0);
        LivingLamp2.SetPower(0.0);
        LivingLamp3.SetPower(0.0);
        LivFan.SetPower(0.0);
        clock.SetStatus(true);
        curtain.SetStatus(true);

        camera.SetPower(false);

        component.SetPower(true);

        fan.setPower(true);
    }
    if( !exLiving.mahmoud && exLiving.Alice ){
        dvLight.SetPower(0.0);
        LivingLamp1.SetPower(1.0);
        LivingLamp2.SetPower(0.0);
        LivingLamp3.SetPower(0.0);
        LivFan.SetPower(0.0);
        clock.SetStatus(true);
        curtain.SetStatus(true);

        camera.SetPower(false);

        component.SetPower(true);

        fan.setPower(true);



}


}

.bat 的内容是

@ECHO OFF

set bin=./bin
set classes=%bin%/cadel.jar;%bin%/clinkx.jar;%bin%/clink170b_ns.jar;%bin%/ns.jar
set main=org.itolab.morihit.cadel2.Cadel2
set rule=data/default

start /min "cadel" java -classpath %classes% %main% %rule%

我必须运行这个 bat 文件,它会在 .jar 的 main 方法中读取规则文件的内容。

当我提取 .jar 文件时,我发现

/*   1:    */ package org.itolab.morihit.cadel2;
/*   2:    */ 
/*   3:    */ import java.io.FileNotFoundException;
/*   4:    */ import java.io.PrintStream;
import java.util.Arrays;
/*   5:    */ import java.util.HashMap;
/*   6:    */ import java.util.List;
/*   7:    */ import org.itolab.morihit.clinkx.UPnPControlPoint;
/*   8:    */ import org.itolab.morihit.clinkx.UPnPDeviceChangeListener;
/*   9:    */ import org.itolab.morihit.clinkx.UPnPRemoteAction;
/*  10:    */ import org.itolab.morihit.clinkx.UPnPRemoteActionArgument;
/*  11:    */ import org.itolab.morihit.clinkx.UPnPRemoteDevice;
/*  12:    */ import org.itolab.morihit.clinkx.UPnPRemoteService;
/*  13:    */ import org.itolab.morihit.clinkx.UPnPRemoteStateVariable;
/*  14:    */ 
/*  15:    */ public class Cadel2
/*  16:    */   implements UPnPDeviceChangeListener
/*  17:    */ {
/*  18:    */   private final UPnPControlPoint controlPoint;
/*  19:    */   private final HashMap<UPnPRemoteStateVariable, StateVariable> subscribedStateVariableList;
/*  20:    */   private final boolean noSubscription;
/*  21:    */   private DeviceList deviceList;
/*  22:    */   private Rule[] ruleList;
/*  23:    */   
/*  24:    */   private static void usage()
/*  25:    */   {
/*  26: 16 */     System.out.println("usage: java Cadel2 [-n] [-v[level]] <rule-file-name>");
/*  27: 17 */     System.out.println("\t-n: don't subscribe state variables");
/*  28: 18 */     System.out.println("\t-v: set verbose level");
/*  29: 19 */     System.exit(1);
/*  30:    */   }
/*  31:    */   
/*  32:    */   public static void main(String[] args)
/*  33:    */   {

                System.out.println("MAIN OF CADEL "+args);
                System.out.println(Arrays.toString(args));


/*  34: 23 */     if (args.length < 1) {
/*  35: 23 */       usage();
/*  36:    */     }

/*  37: 25 */     boolean noSubscription = false;
/*  38: 26 */     for (int i = 0; i < args.length - 1; i++) {
/*  39: 27 */       if (args[i].startsWith("-v"))
/*  40:    */       {
/*  41: 28 */         String level = args[i].substring("-v".length());
/*  42: 29 */         if (level.length() == 0) {
/*  43: 29 */           Debug.setReportLevel(1);
/*  44:    */         } else {
/*  45:    */           try
/*  46:    */           {
/*  47: 32 */             Debug.setReportLevel(Integer.parseInt(level));
/*  48:    */           }
/*  49:    */           catch (NumberFormatException e)
/*  50:    */           {
/*  51: 34 */             usage();
/*  52:    */           }
/*  53:    */         }
/*  54:    */       }
/*  55: 37 */       else if (args[i].equals("-n"))
/*  56:    */       {
/*  57: 38 */         noSubscription = true;
/*  58:    */       }
/*  59:    */       else
/*  60:    */       {
/*  61: 39 */         usage();
/*  62:    */       }
/*  63:    */     }
/*  64: 42 */     String fileName = args[(args.length - 1)];
/*  65:    */     
/*  66: 44 */     Cadel2 cadel2 = new Cadel2(noSubscription);
/*  67:    */     try
/*  68:    */     {
/*  69: 47 */       String parserError = cadel2.parse(fileName);
/*  70: 48 */       if (parserError != null)
/*  71:    */       {
/*  72: 49 */         System.out.println(parserError);
/*  73: 50 */         System.exit(1);
/*  74:    */       }
/*  75:    */     }
/*  76:    */     catch (FileNotFoundException e)
/*  77:    */     {
/*  78: 53 */       e.printStackTrace();
/*  79: 54 */       System.exit(1);
/*  80:    */     }
/*  81: 57 */     cadel2.start();
/*  82:    */   }
/*  83:    */   
/*  84:    */   Cadel2()
/*  85:    */   {
/*  86: 61 */     this(false);
/*  87:    */   }
/*  88:    */   
/*  89:    */   Cadel2(boolean noSubscription)
/*  90:    */   {
/*  91: 65 */     this.noSubscription = noSubscription;
/*  92: 66 */     this.controlPoint = new UPnPControlPoint();
/*  93: 67 */     this.controlPoint.setDeviceChangeListener(this);
/*  94: 68 */     this.subscribedStateVariableList = new HashMap();
/*  95:    */   }
/*  96:    */   
/*  97:    */   String parse(String fileName)
/*  98:    */     throws FileNotFoundException
/*  99:    */   {
/* 100: 72 */     Scanner scanner = new Scanner(fileName);
/* 101: 73 */     Parser parser = new Parser(scanner);
/* 102: 74 */     Module module = parser.parse();
/* 103: 75 */     if (module != null)
/* 104:    */     {
/* 105: 76 */       this.deviceList = module.getDeviceList();
/* 106: 77 */       this.ruleList = module.getRuleList();
/* 107:    */     }
/* 108: 79 */     return parser.getError();
/* 109:    */   }
/* 110:    */   
/* 111:    */   private void fireRules()
/* 112:    */   {
/* 113: 83 */     HashMap<String, ActionStatement> actionList = new HashMap();
/* 114:    */     
/* 115: 85 */     String debugMsg = "";
/* 116: 86 */     if (Debug.getReportLevel() >= 3) {
/* 117: 86 */       Debug.startTimer();
/* 118:    */     }
/* 119: 88 */     int i = 0;
/* 120: 88 */     for (int n = this.ruleList.length; i < n; i++) {
/* 121:    */       try
/* 122:    */       {
/* 123: 90 */         if (this.ruleList[i].evaluate())
/* 124:    */         {
/* 125: 91 */           ActionStatementList list = this.ruleList[i].getActionStatementList();
/* 126: 92 */           int j = 0;
/* 127: 92 */           for (int m = list.size(); j < m; j++)
/* 128:    */           {
/* 129: 93 */             ActionStatement actionStatement = list.get(j);
/* 130: 94 */             String actionName = actionStatement.getName();
/* 131: 95 */             ActionStatement currentActionStatement = (ActionStatement)actionList.get(actionName);
/* 132: 96 */             if (currentActionStatement == null) {
/* 133: 96 */               actionList.put(actionName, actionStatement);
/* 134: 98 */             } else if (actionStatement.getPriority() > currentActionStatement.getPriority()) {
/* 135: 99 */               actionList.put(actionName, actionStatement);
/* 136:    */             }
/* 137:    */           }
/* 138:    */         }
/* 139:    */       }
/* 140:    */       catch (IllegalArgumentException e)
/* 141:    */       {
/* 142:104 */         Debug.warning(e.getMessage());
/* 143:105 */         Debug.warning("error occurred while processing the rule (line=" + this.ruleList[i].getLineNum() + ")");
/* 144:    */       }
/* 145:    */     }
/* 146:109 */     if (Debug.getReportLevel() >= 3)
/* 147:    */     {
/* 148:110 */       debugMsg = debugMsg + "   RuleProcessingTime [ms]: check=" + Debug.checkTimer();
/* 149:111 */       Debug.startTimer();
/* 150:    */     }
/* 151:114 */     for (ActionStatement actionStatement : actionList.values()) {
/* 152:115 */       actionStatement.invokeAction();
/* 153:    */     }
/* 154:118 */     if (Debug.getReportLevel() >= 3)
/* 155:    */     {
/* 156:119 */       debugMsg = debugMsg + ", actionInvocation=" + Debug.checkTimer();
/* 157:120 */       Debug.report3(debugMsg);
/* 158:    */     }
/* 159:    */   }
/* 160:    */   
/* 161:    */   public void start()
/* 162:    */   {
/* 163:125 */     this.controlPoint.start();
/* 164:    */     
/* 165:127 */     Runtime.getRuntime().addShutdownHook(new Thread()
/* 166:    */     {
/* 167:    */       public void run()
/* 168:    */       {
/* 169:129 */         Cadel2.this.controlPoint.stop();
/* 170:    */       }
/* 171:    */     });
/* 172:    */   }
/* 173:    */   
/* 174:    */   void subscribeStateVariable(UPnPRemoteDevice remoteDevice, StateVariable var)
/* 175:    */   {
/* 176:135 */     if (!this.noSubscription)
/* 177:    */     {
/* 178:136 */       String varName = var.getName();
/* 179:137 */       UPnPRemoteStateVariable remoteStateVariable = remoteDevice.getRemoteStateVariable(varName);
/* 180:138 */       if ((remoteStateVariable != null) && (remoteStateVariable.subscribe()))
/* 181:    */       {
/* 182:139 */         this.subscribedStateVariableList.put(remoteStateVariable, var);
/* 183:140 */         Debug.report0("Subscribed: \"" + remoteDevice.getFriendlyName() + "\"." + varName);
/* 184:    */       }
/* 185:    */       else
/* 186:    */       {
/* 187:141 */         Debug.warning("failed to subscribe: \"" + remoteDevice.getFriendlyName() + "\"." + varName);
/* 188:    */       }
/* 189:    */     }
/* 190:    */   }
/* 191:    */   
/* 192:    */   void unsubscribeStateVariable(UPnPRemoteDevice remoteDevice, StateVariable var)
/* 193:    */   {
/* 194:146 */     if (!this.noSubscription)
/* 195:    */     {
/* 196:147 */       String varName = var.getName();
/* 197:148 */       UPnPRemoteStateVariable remoteStateVariable = remoteDevice.getRemoteStateVariable(varName);
/* 198:149 */       if (remoteStateVariable != null)
/* 199:    */       {
/* 200:150 */         var = (StateVariable)this.subscribedStateVariableList.remove(remoteStateVariable);
/* 201:151 */         if (var != null) {
/* 202:151 */           var.update(null);
/* 203:    */         }
/* 204:    */       }
/* 205:    */     }
/* 206:    */   }
/* 207:    */   
/* 208:    */   void setupRemoteAction(UPnPRemoteDevice remoteDevice, Action action)
/* 209:    */   {
/* 210:157 */     String actionName = action.getName();
/* 211:158 */     UPnPRemoteAction remoteAction = remoteDevice.getRemoteAction(actionName);
/* 212:159 */     if (remoteAction != null) {
/* 213:159 */       action.setRemoteAction(remoteAction);
/* 214:    */     } else {
/* 215:160 */       Debug.warning("no such action: \"" + remoteDevice.getFriendlyName() + "\"." + actionName);
/* 216:    */     }
/* 217:    */   }
/* 218:    */   
/* 219:    */   void resetRemoteAction(UPnPRemoteDevice remoteDevice, Action action)
/* 220:    */   {
/* 221:164 */     String actionName = action.getName();
/* 222:165 */     UPnPRemoteAction remoteAction = remoteDevice.getRemoteAction(actionName);
/* 223:166 */     if (remoteAction != null) {
/* 224:166 */       action.resetRemoteAction();
/* 225:    */     }
/* 226:    */   }
/* 227:    */   
/* 228:    */   public void deviceAdded(UPnPRemoteDevice remoteDevice)
/* 229:    */   {
/* 230:170 */     String deviceName = remoteDevice.getFriendlyName();
/* 231:    */     
/* 232:172 */     Device device = this.deviceList.get(deviceName);
/* 233:173 */     if (device == null)
/* 234:    */     {
/* 235:174 */       device = new Device(deviceName);
/* 236:175 */       this.deviceList.put(deviceName, device);
/* 237:    */     }
/* 238:178 */     if (!device.isAlive())
/* 239:    */     {
/* 240:179 */       if (Debug.getReportLevel() >= 1)
/* 241:    */       {
/* 242:180 */         Debug.report1("DeviceDiscovered: \"" + deviceName + "\"");
/* 243:181 */         for (UPnPRemoteService remoteService : remoteDevice.getRemoteServiceList())
/* 244:    */         {
/* 245:182 */           for (UPnPRemoteStateVariable remoteVariable : remoteService.getRemoteStateVariableList()) {
/* 246:183 */             Debug.report1("   StateVariable: " + remoteVariable.getName() + " (" + remoteVariable.getDataType() + ")");
/* 247:    */           }
/* 248:186 */           for (UPnPRemoteAction remoteAction : remoteService.getRemoteActionList())
/* 249:    */           {
/* 250:187 */             List<UPnPRemoteActionArgument> list = remoteAction.getRemoteActionInputArgumentList();
/* 251:188 */             StringBuffer argTypes = new StringBuffer();
/* 252:189 */             if (list.size() > 0)
/* 253:    */             {
/* 254:190 */               argTypes.append("(");
/* 255:191 */               for (UPnPRemoteActionArgument arg : list) {
/* 256:192 */                 argTypes.append(arg.getDataType() + ",");
/* 257:    */               }
/* 258:193 */               argTypes.deleteCharAt(argTypes.length() - 1);
/* 259:194 */               argTypes.append(")");
/* 260:    */             }
/* 261:196 */             Debug.report1("   Action: " + remoteAction.getName() + " " + argTypes);
/* 262:    */           }
/* 263:    */         }
/* 264:    */       }
/* 265:200 */       device.up(this, remoteDevice);
/* 266:201 */       fireRules();
/* 267:    */     }
/* 268:    */   }
/* 269:    */   
/* 270:    */   public void deviceRemoved(UPnPRemoteDevice remoteDevice)
/* 271:    */   {
/* 272:206 */     String deviceName = remoteDevice.getFriendlyName();
/* 273:207 */     Device device = this.deviceList.get(deviceName);
/* 274:208 */     if ((device.isAlive()) && (remoteDevice.getUDN().equals(device.getUDN())))
/* 275:    */     {
/* 276:209 */       device.down(this, remoteDevice);
/* 277:210 */       Debug.report0("DeviceDisappeared: \"" + deviceName + "\"");
/* 278:    */     }
/* 279:    */   }
/* 280:    */   
/* 281:    */   public void deviceStateChanged(UPnPRemoteStateVariable remoteVariable)
/* 282:    */   {
/* 283:215 */     StateVariable var = (StateVariable)this.subscribedStateVariableList.get(remoteVariable);
/* 284:216 */     Object value = remoteVariable.getValue();
/* 285:217 */     if (var != null)
/* 286:    */     {
/* 287:218 */       if (Debug.getReportLevel() >= 2) {
/* 288:219 */         Debug.report2("StateVariableChanged: \"" + var.getDeviceName() + "\"." + var.getName() + ": " + value);
/* 289:    */       }
/* 290:220 */       var.update(value);
/* 291:221 */       fireRules();
/* 292:    */     }
/* 293:    */   }
/* 294:    */ }

我的问题是

它如何从 .rule 中获取 String 并作为参数传递给 main 方法?

谢谢

4

1 回答 1

0

内容不直接作为参数传递给 main 方法。程序接收文件名作为参数。

String fileName = args[(args.length - 1)];

文件名应该是您将收到的最后一个参数。该程序在其 usage() 方法中指定了这一点:

用法:java Cadel2 [-n] [-v[level]]<rule-file-name>

该程序稍后将读取 .rule 文件的内容,然后对其进行解析以提取规则集。关于它发生的地方的一些提示:

取自主要方法:

String parserError = cadel2.parse(fileName);

在 Cadel2 类中:

Scanner scanner = new Scanner(fileName);
Parser parser = new Parser(scanner);
Module module = parser.parse();

要将文件名作为参数传递,您可以查看 .bat 文件以了解有关该部分的一些提示。

于 2013-11-13T17:44:01.513 回答