3

我想在我的iPhone應用程序中加載.clp文件。對於我使用下面的代碼如何從.clp(CLIPS)文件得到響應?

NSString *filePath = [[NSBundle mainBundle] 
          pathForResource:@"autodemo" ofType:@"clp"]; 

    environment = CreateEnvironment(); 
    char *clipsFileChar = (char *)[filePath cStringUsingEncoding:NSASCIIStringEncoding]; 
    Load(clipsFileChar); 
    Reset(); 
    Run(-1); 

    NSString *evalS = [NSString stringWithFormat:@"(find-all-facts ((?f state-list)) TRUE)"]; 
    char * evalStr = (char *)evalS; 

    DATA_OBJECT obj;// = {0,-1}; 
// obj.type = STRING; 
// obj.value = evalStr; 

    int i = Eval(evalStr, &obj); 
    NSLog(@"%d",i); 

現在,當我運行此代碼Eval(evalStr, &obj)給我每次0
我正在使用this link的autodemo.clp文件。
那麼,如何讓​​命令工作,以及如何獲得由clp文件返回的響應?

感謝,

回答

1

下面的代碼解決我的問題,希望這將有助於別人.. :)


InitializeEnvironment(); 
Clear(); 

NSString *filePath = [[NSBundle mainBundle] 
         pathForResource:@"autodemo" ofType:@"clp"]; 
char *clipsFileChar = (char *)[filePath cStringUsingEncoding:NSASCIIStringEncoding]; 

IncrementGCLocks(); 
Load(clipsFileChar); 
Reset(); 
Run(-1); 
DecrementGCLocks(); 

[self nextUIState]; 

- (void)nextUIState 
{ 
    DATA_OBJECT theDO; 
    NSString * evalS = @"(find-all-facts ((?f state-list)) TRUE)"; 
    char *evalStr = (char *)[evalS cStringUsingEncoding:NSASCIIStringEncoding]; 

    int j = EnvEval(GetCurrentEnvironment(), evalStr, &theDO); 
    NSLog(@"j = %d",j); 

    if(factDict) 
    { 
     [factDict release]; 
     factDict = nil; 

     factDict = [[NSMutableDictionary alloc] init]; 
    } 

    id value = [self objectForDataObject:&theDO]; 

    NSLog(@"%@",[value description]);  
} 

-(id) objectForDataObject: (DATA_OBJECT*) arg 
{ 
    switch(arg->type) 
    { 
     case FACT_ADDRESS: 
     { 

      DATA_OBJECT data = { 0 }; 
      struct fact* aFact = (struct fact*) arg->value; 

      if(EnvGetFactSlot(GetCurrentEnvironment(),aFact,(char*)[@"current" UTF8String],&data)) 
      { 
       [factDict setObject:[self objectForDataObject: &data] forKey:@"current"]; 
       [factDict retain]; 
      } 
      return factDict; 
     } 
     case SYMBOL: 
     { 
      NSString *str = [NSString stringWithUTF8String: ValueToString(arg->value)]; 
      if ([str isEqual: @"nil"]) return nil; 
      if ([str hasPrefix: @"<<<"] && [str hasSuffix: @">>>"]) 
      { 
       return [self dataFromSymbolString: str]; 
      } 
      return str; 
     } 
     case STRING: 
     { 
      return [NSString stringWithUTF8String: ValueToString(arg->value)]; 
     } 
     case INTEGER: 
     { 
      return [NSNumber numberWithInt: ValueToInteger(arg->value)]; 
     } 
     case FLOAT: 
     { 
      return [NSNumber numberWithDouble: ValueToDouble(arg->value)]; 
     } 
     case EXTERNAL_ADDRESS: 
     { 
      return (id) arg->value; 
     } 
     case MULTIFIELD: 
     { 
      int i, count = GetpDOLength(arg); 
      NSMutableArray *args = [NSMutableArray arrayWithCapacity: count]; 
      FIELD_PTR fptr = (FIELD_PTR) GetMFPtr(GetpValue(arg),GetpDOBegin(arg)); 
      for(i = 0; i < count; ++i, ++fptr) 
      { 
       DATA_OBJECT dobj; 
       dobj.type = fptr->type; 
       dobj.value = fptr->value; 
       [args addObject: [self objectForDataObject: &dobj]]; 
      } 
      return args; 
     } 
     default: 
      return nil; 
    } 
} 

如果你發現任何其他更好的方式(當然有),請讓我知道。 :)