public static CodeTerm Less(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 2); try { CodeValue argValue0 = arguments[0] as CodeValue; CodeValue argValue1 = arguments[1] as CodeValue; if (argValue0 != null && argValue1 != null) { IComparable lhs = argValue0.Object as IComparable; IComparable rhs = argValue1.Object as IComparable; if (lhs != null && rhs != null) { return new CodeValueBoolean(lhs.CompareTo(rhs) < 0); } } throw new ArgumentException("Arguments are not comparable."); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm Substring(CodeTerm[] arguments) { Debug.Assert(arguments.Length >= 2); Debug.Assert(arguments.Length <= 3); try { var argValue0 = (CodeValue) arguments[0]; var argValue1 = (CodeValue) arguments[1]; var value = Convert.ToString(argValue0.Object); var index = Convert.ToInt32(argValue1.Object); if (arguments.Length == 2) { return new CodeValueString(value.Substring(index)); } // arguments.Length == 3 var argValue2 = (CodeValue) arguments[2]; var length = Convert.ToInt32(argValue2.Object); return new CodeValueString(value.Substring(index, length)); } catch (Exception ex) { return new CodeValueException(ex); } }
public static void Write(CodeTerm codeTerm, int indentation, TextWriter wtr) { var codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { Write(codeCompoundTerm, indentation, wtr); return; } var codeVariable = codeTerm as CodeVariable; if (codeVariable != null) { Write(codeVariable, indentation, wtr); return; } var codeValue = codeTerm as CodeValue; if (codeValue != null) { Write(codeValue, indentation, wtr); return; } }
private void Get(CodeCompoundTerm codeCompoundTerm, WamInstructionRegister sourceRegister) { WamInstructionRegister[] childrenRegisters = new WamInstructionRegister[codeCompoundTerm.Children.Count]; InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.GetStructure, sourceRegister, Functor.Create(codeCompoundTerm.Functor))); for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeVariable) { string variableName = child.AsCodeVariable.Name; WamInstructionRegister variableRegister = GetRegisterAssignment(variableName); if (variableRegister.IsUnused) { variableRegister = GetNextPermanentRegister(variableName); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, variableRegister)); } else { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyBoundVariable, variableRegister)); } } else if (child.IsCodeCompoundTerm) { childrenRegisters[idx] = GetNextTemporaryRegister(); InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyUnboundVariable, childrenRegisters[idx])); } else if (child.IsCodeValue) { InstructionStreamBuilder.Write(new WamInstruction(WamInstructionOpCodes.UnifyValue, WamValue.Create(child.AsCodeValue))); } else { throw new InvalidOperationException("Unsupported codeTerm type."); } } // Build substructures. // for (int idx = 0; idx < codeCompoundTerm.Children.Count; ++idx) { CodeTerm child = codeCompoundTerm.Children[idx]; if (child.IsCodeList) { child = ConvertCodeList(child.AsCodeList); } if (child.IsCodeCompoundTerm) { Get(child, childrenRegisters[idx]); } } }
private void Get(CodeTerm codeTerm, WamInstructionRegister sourceRegister) { if (codeTerm.IsCodeList) { codeTerm = ConvertCodeList(codeTerm.AsCodeList); } if (codeTerm.IsCodeVariable) { Get(codeTerm.AsCodeVariable, sourceRegister); return; } if (codeTerm.IsCodeCompoundTerm) { Get(codeTerm.AsCodeCompoundTerm, sourceRegister); return; } if (codeTerm.IsCodeValue) { Get(codeTerm.AsCodeValue, sourceRegister); return; } throw new InvalidOperationException("Unsupported codeTerm type."); }
private void Put(CodeTerm codeTerm, WamInstructionRegister targetRegister, LibraryList libraries) { if (codeTerm.IsCodeList) { codeTerm = ConvertCodeList(codeTerm.AsCodeList); } if (codeTerm.IsCodeVariable) { Put(codeTerm.AsCodeVariable, targetRegister); return; } if (codeTerm.IsCodeCompoundTerm) { Put(codeTerm.AsCodeCompoundTerm, targetRegister, libraries); return; } if (codeTerm.IsCodeValue) { Put(codeTerm.AsCodeValue, targetRegister); return; } throw new InvalidOperationException("Unsupported codeTerm type."); }
public static CodeTerm Unequal(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 2); try { CodeValue argValue0 = arguments[0] as CodeValue; CodeValue argValue1 = arguments[1] as CodeValue; if (argValue0 != null && argValue1 != null) { IComparable lhs = argValue0.Object as IComparable; IComparable rhs = argValue1.Object as IComparable; if (lhs != null && rhs != null) { return new CodeValueBoolean(lhs.CompareTo(rhs) != 0); } } return new CodeValueBoolean(arguments[0] != arguments[1]); } catch (Exception ex) { return new CodeValueException(ex); } }
public static Schedule Create(CodeTerm codeTerm) { if (codeTerm == null) throw new ArgumentNullException("codeTerm"); Schedule result = new Schedule(); result.ProcessResults(codeTerm); return result; }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a work period. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <returns>The <see cref="ScheduleShift"/> specified by <paramref name="codeTerm"/>.</returns> /// <remarks> /// Work periods are compound terms of the form <code>workPeriod(day,shift)</code>. /// </remarks> private ScheduleShift ProcessWorkPeriod(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodFunctor) { throw new ArgumentException("Functor workPeriod not specified.", "codeTerm"); } string day = ProcessDay(codeCompoundTerm.Children[0]); string shift = ProcessShift(codeCompoundTerm.Children[1]); ScheduleDay scheduleDay; switch (day) { case "monday": scheduleDay = Monday; break; case "tuesday": scheduleDay = Tuesday; break; case "wednesday": scheduleDay = Wednesday; break; case "thursday": scheduleDay = Thursday; break; case "friday": scheduleDay = Friday; break; default: throw new ArgumentException(string.Format("Unknown day {0}.", day), "codeTerm"); } ScheduleShift scheduleShift; switch (shift) { case "first": scheduleShift = scheduleDay.First; break; case "second": scheduleShift = scheduleDay.Second; break; case "third": scheduleShift = scheduleDay.Third; break; default: throw new ArgumentException(string.Format("Unknown shift {0}.", shift), "codeTerm"); } return(scheduleShift); }
public static Schedule Create(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException(nameof(codeTerm)); } var result = new Schedule(); result.ProcessResults(codeTerm); return(result); }
private CodeTerm ConvertCodeList(CodeList codeList) { CodeTerm result = codeList.Tail; for (int index = codeList.Head.Count - 1; index >= 0; --index) { result = new CodeCompoundTerm( CodeFunctor.ListFunctor, new CodeTerm[] { codeList.Head[index], result }); } return(result); }
public static CodeTerm Construct(CodeTerm value) { CodeValueType codeValueObjectType = value as CodeValueType; if (codeValueObjectType == null) { return null; } ConstructorInfo constructorInfo = codeValueObjectType.Value.GetConstructor(new Type[] { }); object objectInstance = constructorInfo.Invoke(new object[] { }); return new CodeValueObject(objectInstance); }
public static CodeTerm Decrement(CodeTerm[] arguments) { try { Operand operand; GetOperand(arguments, out operand); return CodeValue.Create(operand.Decrement()); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm ToBoolean(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { CodeValue argValue0 = (CodeValue)arguments[0]; return new CodeValueBoolean(Convert.ToBoolean(argValue0.Object)); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm Construct(CodeTerm value) { CodeValueType codeValueObjectType = value as CodeValueType; if (codeValueObjectType == null) { return(null); } ConstructorInfo constructorInfo = codeValueObjectType.Value.GetConstructor(new Type[] { }); object objectInstance = constructorInfo.Invoke(new object[] { }); return(new CodeValueObject(objectInstance)); }
public static CodeTerm ToInteger(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { var argValue0 = (CodeValue)arguments[0]; return new CodeValueInteger(Convert.ToInt32(argValue0.Object)); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm Subtract(CodeTerm[] arguments) { try { Operand lhs; Operand rhs; GetOperands(arguments, out lhs, out rhs); return CodeValue.Create(lhs.Subtract(rhs)); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm GetType(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { CodeValue argValue0 = (CodeValue)arguments[0]; string typeName = Convert.ToString(argValue0.Object); return new CodeValueType(Type.GetType(typeName)); } catch (Exception ex) { return new CodeValueException(ex); } }
private void ProcessOptimizePragma(CodeTerm pragmaArgument) { CodeValue pragmaValue = pragmaArgument.AsCodeValue; if (pragmaValue != null) { if (pragmaValue.Object.Equals(true)) { IsOptimized = true; } if (pragmaValue.Object.Equals(false)) { IsOptimized = false; } } }
public static CodeTerm StringLength(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { var argValue0 = (CodeValue)arguments[0]; var value = Convert.ToString(argValue0.Object); return new CodeValueInteger(value.Length); } catch (Exception ex) { return new CodeValueException(ex); } }
void ProcessOptimizePragma(CodeTerm pragmaArgument) { var pragmaValue = pragmaArgument.AsCodeValue; if (pragmaValue == null) { return; } if (pragmaValue.Object.Equals(true)) { IsOptimized = true; } if (pragmaValue.Object.Equals(false)) { IsOptimized = false; } }
public static WamReferenceTarget Create(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } var codeValue = codeTerm as CodeValue; if (codeValue != null) { return WamValue.Create(codeValue); } var codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { return WamCompoundTerm.Create(codeCompoundTerm); } throw new ArgumentException("Invalid CodeTerm type."); }
public static CodeTerm StringContains(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 2); try { CodeValue argValue0 = (CodeValue)arguments[0]; CodeValue argValue1 = (CodeValue)arguments[1]; string value = Convert.ToString(argValue0.Object); string substring = Convert.ToString(argValue1.Object); return new CodeValueBoolean(value.Contains(substring)); } catch (Exception ex) { return new CodeValueException(ex); } }
ExecutionResults OnLibraryCallFunction(WamInstruction instruction, Function function) { var functionArguments = new CodeTerm[instruction.Functor.Arity]; for (int index = 0; index < instruction.Functor.Arity; ++index) { functionArguments[index] = Evaluate(ArgumentRegisters[index]).GetCodeTerm(); } CodeTerm functionResult; try { functionResult = function.FunctionDelegate(functionArguments); } catch { // Backtrack on exception. // return(ExecutionResults.Backtrack); } try { var functionResultValue = (CodeValue)functionResult; if (Convert.ToBoolean(functionResultValue.Object)) { InstructionPointer = InstructionPointer.GetNext(); return(ExecutionResults.None); } else { // Result converts to false. // return(ExecutionResults.Backtrack); } } catch { // Result cannot be converted to a boolean. // return(ExecutionResults.Backtrack); } }
public static bool Assert(WamMachine machine, WamReferenceTarget[] arguments) { Debug.Assert(arguments.Length == 1); WamReferenceTarget expression = machine.Evaluate(arguments[0]); CodeTerm codeTerm = expression.GetCodeTerm(); if (codeTerm != null) { CodeValue codeValue = codeTerm as CodeValue; if (codeValue != null) { return(Convert.ToBoolean(codeValue.Object)); } } return(false); }
public static CodeTerm TypeOf(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { var argValue0 = (CodeValue) arguments[0]; var value = argValue0.Object; if (value == null) { throw new ArgumentNullException("value"); } return new CodeValueType(value.GetType()); } catch (Exception ex) { return new CodeValueException(ex); } }
protected override CodeTerm GetCodeTermBase(WamDeferenceTypes dereferenceType, WamReferenceTargetMapping mapping) { if (Functor == Functor.ListFunctor) { List <CodeTerm> head = new List <CodeTerm>(); CodeTerm tail = null; CodeTerm codeTermHead = Children[0].GetCodeTerm(dereferenceType, mapping); CodeTerm codeTermTail = Children[1].GetCodeTerm(dereferenceType, mapping); head.Add(codeTermHead); if (codeTermTail.IsCodeList) { head.AddRange(codeTermTail.AsCodeList.Head); tail = codeTermTail.AsCodeList.Tail; } else { tail = codeTermTail; } return(new CodeList(head, tail)); } else { if (Functor.Arity == 0) { return(new CodeCompoundTerm(new CodeFunctor(Functor.Name))); } else { List <CodeTerm> children = new List <CodeTerm>(); foreach (WamReferenceTarget child in Children) { children.Add(child.GetCodeTerm(dereferenceType, mapping)); } return(new CodeCompoundTerm(new CodeFunctor(Functor.Name, Functor.Arity), children)); } } }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a person. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeAtom"/>.</param> /// <returns>The person specified by <paramref name="codeTerm"/>.</returns> private string ProcessPerson(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor.Arity != 0) { throw new ArgumentException("Non-zero functor arity specified.", "codeTerm"); } return(codeCompoundTerm.Functor.Name); }
public static WamReferenceTarget Create(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } var codeValue = codeTerm as CodeValue; if (codeValue != null) { return(WamValue.Create(codeValue)); } var codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { return(WamCompoundTerm.Create(codeCompoundTerm)); } throw new ArgumentException("Invalid CodeTerm type."); }
public static CodeTerm StringReplace(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 3); try { CodeValue argValue0 = (CodeValue)arguments[0]; CodeValue argValue1 = (CodeValue)arguments[1]; CodeValue argValue2 = (CodeValue)arguments[2]; string value = Convert.ToString(argValue0.Object); string oldValue = Convert.ToString(argValue1.Object); string newValue = Convert.ToString(argValue2.Object); return new CodeValueString(value.Replace(oldValue, newValue)); } catch (Exception ex) { return new CodeValueException(ex); } }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a day. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeAtom"/>.</param> /// <returns>The day specified by <paramref name="codeTerm"/>.</returns> private string ProcessDay(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor.Arity != 0) { throw new ArgumentException("Non-zero functor arity specified.", "codeTerm"); } return codeCompoundTerm.Functor.Name; }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a shift. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeAtom"/>.</param> /// <returns>The shift specified by <paramref name="codeTerm"/>.</returns> string ProcessShift(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException(nameof(codeTerm)); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", nameof(codeTerm)); } var codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor.Arity != 0) { throw new ArgumentException("Non-zero functor arity specified.", nameof(codeTerm)); } return(codeCompoundTerm.Functor.Name); }
private void ProcessPragma(CodeSentence codeSentence) { if (codeSentence.Body.Count != 0) { return; } CodeCompoundTerm pragma = codeSentence.Head; CodeCompoundTerm pragmaName = pragma.Children[0].AsCodeCompoundTerm; CodeTerm pragmaArgument = pragma.Children[1]; if (pragmaName != null && pragmaArgument != null) { if (pragmaName.Functor == PragmaOptimizeFunctor) { ProcessOptimizePragma(pragmaArgument); } } }
public Schedule Execute() { PrologMachine machine = Machine; if (!machine.CanRunToSuccess) { return(null); } ExecutionResults executionResults = machine.RunToSuccess(); if (executionResults != ExecutionResults.Success) { return(null); } CodeTerm codeTerm = machine.QueryResults.Variables[0].CodeTerm; Schedule schedule = Schedule.Create(codeTerm); return(schedule); }
/// <summary> /// Adds the work period assignment specified by <see cref="CodeTerm"/> to the schedule. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <remarks> /// Work periods assignment are compound terms of the form <code>workPeriodAssignment(person,workPeriod(day,shift))</code>. /// </remarks> private void ProcessWorkPeriodAssignment(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } CodeCompoundTerm codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodAssignmentFunctor) { throw new ArgumentException("Functor workPeriodAssignment not specified.", "codeTerm"); } string person = ProcessPerson(codeCompoundTerm.Children[0]); ScheduleShift scheduleShift = ProcessWorkPeriod(codeCompoundTerm.Children[1]); scheduleShift.Name = person; }
public static void Write(CodeTerm codeTerm, int indentation, TextWriter wtr) { CodeCompoundTerm codeCompoundTerm = codeTerm as CodeCompoundTerm; if (codeCompoundTerm != null) { Write(codeCompoundTerm, indentation, wtr); return; } CodeVariable codeVariable = codeTerm as CodeVariable; if (codeVariable != null) { Write(codeVariable, indentation, wtr); return; } CodeValue codeValue = codeTerm as CodeValue; if (codeValue != null) { Write(codeValue, indentation, wtr); return; } }
/// <summary> /// Adds the work period assignments specified by <see cref="CodeTerm"/> to the schedule. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that represents a list of <see cref="CodeCompoundTerm"/> work period assignments.</param> void ProcessResults(CodeTerm codeTerm) { Debug.Assert(codeTerm != null); // If the list is nil, there are no results to process. // if (codeTerm.IsCodeCompoundTerm && codeTerm.AsCodeCompoundTerm.Functor == CodeFunctor.NilFunctor) { return; } if (!codeTerm.IsCodeList) { throw new ArgumentException("CodeList not specified.", "codeTerm"); } var codeList = codeTerm.AsCodeList; foreach (var item in codeList.Head) { ProcessWorkPeriodAssignment(item); } }
/// <summary> /// Adds the work period assignments specified by <see cref="CodeTerm"/> to the schedule. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that represents a list of <see cref="CodeCompoundTerm"/> work period assignments.</param> private void ProcessResults(CodeTerm codeTerm) { Debug.Assert(codeTerm != null); // If the list is nil, there are no results to process. // if (codeTerm.IsCodeCompoundTerm && codeTerm.AsCodeCompoundTerm.Functor == CodeFunctor.NilFunctor) { return; } if (!codeTerm.IsCodeList) { throw new ArgumentException("CodeList not specified.", "codeTerm"); } CodeList codeList = codeTerm.AsCodeList; foreach (CodeTerm item in codeList.Head) { ProcessWorkPeriodAssignment(item); } }
public static CodeTerm IsType(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 2); try { var argValue0 = (CodeValue)arguments[0]; var value = argValue0.Object; var argValue1 = (CodeValueType)arguments[1]; var type = argValue1.Value; return new CodeValueBoolean(type.IsInstanceOfType(value)); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm IsNull(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { var argValue0 = (CodeValue)arguments[0]; var value = argValue0.Object; return new CodeValueBoolean(value == null); } catch (Exception ex) { return new CodeValueException(ex); } }
public static CodeTerm IsEmpty(CodeTerm[] arguments) { Debug.Assert(arguments.Length == 1); try { var argValue0 = (CodeValue)arguments[0]; var value = Convert.ToString(argValue0.Object); return new CodeValueBoolean(string.IsNullOrEmpty(value)); } catch (Exception ex) { return new CodeValueException(ex); } }
internal WamReferenceTarget Evaluate(WamReferenceTarget wamReferenceTarget) { if (wamReferenceTarget == null) { throw new ArgumentNullException("wamReferenceTarget"); } var wamCompoundTerm = wamReferenceTarget.Dereference() as WamCompoundTerm; if (wamCompoundTerm == null) { return(wamReferenceTarget); } if (!Program.Libraries.Contains(wamCompoundTerm.Functor)) { return(wamReferenceTarget); } var method = Program.Libraries[wamCompoundTerm.Functor]; if (method == null || method.CanEvaluate == false) { return(wamReferenceTarget); } var arguments = new WamReferenceTarget[method.Functor.Arity]; for (var index = 0; index < method.Functor.Arity; ++index) { arguments[index] = Evaluate(wamCompoundTerm.Children[index]); } var function = method as Function; if (function != null) { var codeTerms = new CodeTerm[method.Functor.Arity]; for (var index = 0; index < method.Functor.Arity; ++index) { codeTerms[index] = arguments[index].GetCodeTerm(); } CodeTerm result; try { result = function.FunctionDelegate(codeTerms) ?? new CodeValueObject(null); } catch (Exception ex) { result = new CodeValueException(ex); } return(WamValue.Create(result)); } var predicate = method as Predicate; if (predicate != null) { bool result; try { result = predicate.PredicateDelegate(this, arguments); } catch { result = false; } return(WamValueBoolean.Create(result)); } return(wamReferenceTarget); }
private ExecutionResults OnLibraryCallFunction(WamInstruction instruction, Function function) { CodeTerm[] functionArguments = new CodeTerm[instruction.Functor.Arity]; for (int index = 0; index < instruction.Functor.Arity; ++index) { functionArguments[index] = Evaluate(ArgumentRegisters[index]).GetCodeTerm(); } CodeTerm functionResult; try { functionResult = function.FunctionDelegate(functionArguments); } catch { // Backtrack on exception. // return ExecutionResults.Backtrack; } try { CodeValue functionResultValue = (CodeValue)functionResult; if (Convert.ToBoolean(functionResultValue.Object)) { InstructionPointer = InstructionPointer.GetNext(); return ExecutionResults.None; } else { // Result converts to false. // return ExecutionResults.Backtrack; } } catch { // Result cannot be converted to a boolean. // return ExecutionResults.Backtrack; } }
private static void GetOperands(CodeTerm[] arguments, out Operand lhs, out Operand rhs) { Debug.Assert(arguments.Length == 2); CodeValue argValue0 = (CodeValue)arguments[0]; CodeValue argValue1 = (CodeValue)arguments[1]; lhs = Operand.Create(argValue0.Object); rhs = Operand.Create(argValue1.Object); }
private static void GetOperand(CodeTerm[] arguments, out Operand operand) { Debug.Assert(arguments.Length == 1); CodeValue argValue = (CodeValue)arguments[0]; operand = Operand.Create(argValue.Object); }
void ProcessOptimizePragma(CodeTerm pragmaArgument) { var pragmaValue = pragmaArgument.AsCodeValue; if (pragmaValue == null) return; if (pragmaValue.Object.Equals(true)) { IsOptimized = true; } if (pragmaValue.Object.Equals(false)) { IsOptimized = false; } }
/// <summary> /// Adds the work period assignment specified by <see cref="CodeTerm"/> to the schedule. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <remarks> /// Work periods assignment are compound terms of the form <code>workPeriodAssignment(person,workPeriod(day,shift))</code>. /// </remarks> void ProcessWorkPeriodAssignment(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } var codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodAssignmentFunctor) { throw new ArgumentException("Functor workPeriodAssignment not specified.", "codeTerm"); } var person = ProcessPerson(codeCompoundTerm.Children[0]); var scheduleShift = ProcessWorkPeriod(codeCompoundTerm.Children[1]); scheduleShift.Name = person; }
internal WamReferenceTarget Evaluate(WamReferenceTarget wamReferenceTarget) { if (wamReferenceTarget == null) { throw new ArgumentNullException("wamReferenceTarget"); } WamCompoundTerm wamCompoundTerm = wamReferenceTarget.Dereference() as WamCompoundTerm; if (wamCompoundTerm == null) { return wamReferenceTarget; } if (!Program.Libraries.Contains(wamCompoundTerm.Functor)) { return wamReferenceTarget; } LibraryMethod method = Program.Libraries[wamCompoundTerm.Functor]; if (method == null || method.CanEvaluate == false) { return wamReferenceTarget; } WamReferenceTarget[] arguments = new WamReferenceTarget[method.Functor.Arity]; for (int index = 0; index < method.Functor.Arity; ++index) { arguments[index] = Evaluate(wamCompoundTerm.Children[index]); } Function function = method as Function; if (function != null) { CodeTerm[] codeTerms = new CodeTerm[method.Functor.Arity]; for (int index = 0; index < method.Functor.Arity; ++index) { codeTerms[index] = arguments[index].GetCodeTerm(); } CodeTerm result; try { result = function.FunctionDelegate(codeTerms); if (result == null) { result = new CodeValueObject(null); } } catch (Exception ex) { result = new CodeValueException(ex); } return WamValue.Create(result); } Predicate predicate = method as Predicate; if (predicate != null) { bool result; try { result = predicate.PredicateDelegate(this, arguments); } catch { result = false; } return WamValueBoolean.Create(result); } return wamReferenceTarget; }
/// <summary> /// Processes a <see cref="CodeTerm"/> that specifies a work period. /// </summary> /// <param name="codeTerm">A <see cref="CodeTerm"/> that references an <see cref="CodeCompoundTerm"/>.</param> /// <returns>The <see cref="ScheduleShift"/> specified by <paramref name="codeTerm"/>.</returns> /// <remarks> /// Work periods are compound terms of the form <code>workPeriod(day,shift)</code>. /// </remarks> ScheduleShift ProcessWorkPeriod(CodeTerm codeTerm) { if (codeTerm == null) { throw new ArgumentNullException("codeTerm"); } if (!codeTerm.IsCodeCompoundTerm) { throw new ArgumentException("CodeCompoundTerm not specified.", "codeTerm"); } var codeCompoundTerm = codeTerm.AsCodeCompoundTerm; if (codeCompoundTerm.Functor != _workPeriodFunctor) { throw new ArgumentException("Functor workPeriod not specified.", "codeTerm"); } var day = ProcessDay(codeCompoundTerm.Children[0]); var shift = ProcessShift(codeCompoundTerm.Children[1]); ScheduleDay scheduleDay; switch (day) { case "monday": scheduleDay = Monday; break; case "tuesday": scheduleDay = Tuesday; break; case "wednesday": scheduleDay = Wednesday; break; case "thursday": scheduleDay = Thursday; break; case "friday": scheduleDay = Friday; break; default: throw new ArgumentException(string.Format("Unknown day {0}.", day), "codeTerm"); } ScheduleShift scheduleShift; switch (shift) { case "first": scheduleShift = scheduleDay.First; break; case "second": scheduleShift = scheduleDay.Second; break; case "third": scheduleShift = scheduleDay.Third; break; default: throw new ArgumentException(string.Format("Unknown shift {0}.", shift), "codeTerm"); } return scheduleShift; }