How to get response from .clp(CLIPS) file?

回眸只為那壹抹淺笑 提交于 2019-12-06 00:58:45

below code solved my problem, hope it will help to someone else.. :)


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;
    }
}

If you find out any other and better way(ofcourse there is), please let me know. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!