本文介绍了如何从.clp(CLIPS)文件获取响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在iPhone应用程序中加载.clp文件.为此,我正在使用下面的代码

I am trying to load .clp file in my iPhone application. For that I am using below code

    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.
我正在使用此链接.
那么,如何使Eval()命令起作用以及如何获取clp文件返回的响应?

now when I run this code Eval(evalStr, &obj) gives me 0 every time.
I am using autodemo.clp file from this link.
So, how to make Eval() command work and how do I get response returned by clp file?

谢谢

推荐答案

下面的代码解决了我的问题,希望对其他人有所帮助..:)

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. :)

这篇关于如何从.clp(CLIPS)文件获取响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 00:52