public static IList <Object> generateFacts(IRule rule, Rete.Rete engine) { List <Object> facts = new List <Object>(); if (rule != null) { ICondition[] conditions = rule.Conditions; for (int idx = 0; idx < conditions.Length; idx++) { ICondition c = conditions[idx]; if (c is ObjectCondition) { ObjectCondition oc = (ObjectCondition)c; Deftemplate tpl = (Deftemplate)engine.findTemplate(oc.TemplateName); if (tpl.ClassName != null) { Object data = generateJavaFacts(oc, tpl, engine); facts.Add(data); } else { IFact data = generateDeffact(oc, tpl, engine); facts.Add(data); } } else if (c is TestCondition) { } } } return(facts); }
/// <summary> Method will compile exists quantifier /// </summary> public override void compile(ICondition condition, int position, Rule.IRule util, bool alphaMemory) { ExistCondition cond = (ExistCondition)condition; ObjectCondition oc = (ObjectCondition)cond; conditionCompiler.compile(oc, position, util, alphaMemory); }
public override void compileSingleCE(Rule.IRule rule) { ICondition[] conds = rule.Conditions; ObjectCondition oc = (ObjectCondition)conds[0]; if (oc.Negated) { // the ObjectCondition is negated, so we need to // handle it appropriate. This means we need to // Add a LIANode to _IntialFact and attach a NOTNode // to the LIANode. ObjectTypeNode otn = (ObjectTypeNode)ruleCompiler.Inputnodes.Get(ruleCompiler.Engine.InitFact); LIANode lianode = ruleCompiler.findLIANode(otn); NotJoin njoin = new NotJoin(ruleCompiler.Engine.nextNodeId()); njoin.Bindings = new Binding[0]; lianode.addSuccessorNode(njoin, ruleCompiler.Engine, ruleCompiler.Memory); // Add the join to the rule object rule.addJoinNode(njoin); oc.LastNode.addSuccessorNode(njoin, ruleCompiler.Engine, ruleCompiler.Memory); } else if (oc.Nodes.Count == 0) { // this means the rule has a binding, but no conditions ObjectTypeNode otn = ruleCompiler.findObjectTypeNode(oc.TemplateName); LIANode lianode = new LIANode(ruleCompiler.Engine.nextNodeId()); otn.addSuccessorNode(lianode, ruleCompiler.Engine, ruleCompiler.Memory); rule.Conditions[0].addNode(lianode); } }
/// <summary> /// </summary> /// <param name="">cond /// </param> /// <param name="">templ /// </param> /// <param name="">engine /// </param> /// <returns> /// /// </returns> public static IFact generateDeffact(ObjectCondition cond, Deftemplate templ, Rete.Rete engine) { List <object> list = new List <Object>(); IConstraint[] cnstr = cond.Constraints; for (int idx = 0; idx < cnstr.Length; idx++) { IConstraint cn = cnstr[idx]; if (cn is LiteralConstraint) { Slot s = new Slot(cn.Name, cn.Value); list.Add(s); } else if (cn is PredicateConstraint) { PredicateConstraint pc = (PredicateConstraint)cn; Object val = generatePredicateValue(pc); Slot s = new Slot(cn.Name, val); list.Add(s); } else if (cn is BoundConstraint) { // for now we do the simple thing and just set // any bound slots to 1 Slot s = new Slot(cn.Name, 1); list.Add(s); } } IFact f = templ.createFact(list, engine.nextFactId()); return(f); }
/// <summary> /// </summary> /// <param name="">cond /// </param> /// <param name="">templ /// </param> /// <param name="">engine /// </param> /// <returns> /// /// </returns> public static IFact generateDeffact(ObjectCondition cond, Deftemplate templ, Rete.Rete engine) { List<object> list = new List<Object>(); IConstraint[] cnstr = cond.Constraints; for (int idx = 0; idx < cnstr.Length; idx++) { IConstraint cn = cnstr[idx]; if (cn is LiteralConstraint) { Slot s = new Slot(cn.Name, cn.Value); list.Add(s); } else if (cn is PredicateConstraint) { PredicateConstraint pc = (PredicateConstraint) cn; Object val = generatePredicateValue(pc); Slot s = new Slot(cn.Name, val); list.Add(s); } else if (cn is BoundConstraint) { // for now we do the simple thing and just set // any bound slots to 1 Slot s = new Slot(cn.Name, 1); list.Add(s); } } IFact f = templ.createFact(list, engine.nextFactId()); return f; }
public override void compileFirstJoin(ICondition condition, Rule.IRule rule) { ObjectCondition cond = (ObjectCondition)condition; ObjectTypeNode otn = ruleCompiler.findObjectTypeNode(cond.TemplateName); // the LeftInputAdapterNode is the first node to propogate to // the first joinNode of the rule LIANode node = new LIANode(ruleCompiler.Engine.nextNodeId()); // if the condition doesn't have any nodes, we want to Add it to // the objectType node if one doesn't already exist. // otherwise we Add it to the last AlphaNode if (cond.Nodes.Count == 0) { // try to find the existing LIANode for the given ObjectTypeNode // if we don't do this, we end up with multiple LIANodes // descending directly from the ObjectTypeNode LIANode existingLIANode = ruleCompiler.findLIANode(otn); if (existingLIANode == null) { otn.addSuccessorNode(node, ruleCompiler.Engine, ruleCompiler.Memory); cond.addNode(node); } else { existingLIANode.incrementUseCount(); cond.addNode(existingLIANode); } } else { // Add the LeftInputAdapterNode to the last alphaNode // In the case of node sharing, the LIANode could be the last // alphaNode, so we have to check and only Add the node to // the condition if it isn't a LIANode BaseAlpha old = (BaseAlpha)cond.LastNode; //if the last node of condition has a LIANode successor, //the LIANode should be shared with the new CE followed by another CE. // Houzhanbin,10/16/2007 BaseNode[] successors = (BaseNode[])old.SuccessorNodes; for (int i = 0; i < successors.Length; i++) { if (successors[i] is LIANode) { cond.addNode(successors[i]); return; } } if (!(old is LIANode)) { old.addSuccessorNode(node, ruleCompiler.Engine, ruleCompiler.Memory); cond.addNode(node); } } }
/// <summary> method compiles ObjectConditions, which include NOTCE /// </summary> public override BaseJoin compileJoin(ICondition condition, int position, Rule.IRule rule) { Binding[] binds = getBindings(condition, rule, position); ObjectCondition oc = (ObjectCondition)condition; BaseJoin joinNode = null; //deal with the CE which is not NOT CE. if (!oc.Negated) { if (binds.Length > 0 && oc.HasPredicateJoin) { joinNode = new PredicateBNode(ruleCompiler.Engine.nextNodeId()); } else if (binds.Length > 0 && oc.HasNotEqual) { joinNode = new HashedNotEqBNode(ruleCompiler.Engine.nextNodeId()); } else if (binds.Length > 0) { joinNode = new HashedEqBNode(ruleCompiler.Engine.nextNodeId()); } else if (binds.Length == 0) { joinNode = new ZJBetaNode(ruleCompiler.Engine.nextNodeId()); } } //deal with the CE which is NOT CE. if (oc.Negated) { if (binds.Length > 0 && oc.HasPredicateJoin) { joinNode = new NotJoin(ruleCompiler.Engine.nextNodeId()); } else if (oc.HasNotEqual) { joinNode = new HashedNotEqNJoin(ruleCompiler.Engine.nextNodeId()); } else { joinNode = new HashedEqNJoin(ruleCompiler.Engine.nextNodeId()); } } if (joinNode != null) { joinNode.Bindings = binds; } return(joinNode); }
public bool CanEdit(int workspaceId, int artifactTypeId) { var query = new Query(); var objectTypeCondition = new ObjectCondition(PermissionFieldNames.ArtifactType, ObjectConditionEnum.EqualTo, artifactTypeId); var editPermissionTypeCondition = new TextCondition(PermissionFieldNames.PermissionType, TextConditionEnum.EqualTo, PermissionType.Edit.Name); query.Condition = new CompositeCondition(objectTypeCondition, CompositeConditionEnum.And, editPermissionTypeCondition).ToQueryString(); var permissions = this.permissionManager.QueryAsync(workspaceId, query, 1).Result; if (permissions.Success) { var permission = permissions.Results.FirstOrDefault().Artifact; var result = this.permissionManager.GetPermissionSelectedAsync(workspaceId, new List <PermissionRef>() { permission }).Result; return(result.FirstOrDefault()?.Selected ?? false); } return(false); }
/// <summary> The method uses Defclass, Class, Deftemplate and Rete to create a new /// instance of the java object. Once the instance is created, the method /// uses Defclass to look up the write method and calls it with the /// appropriate value. /// </summary> /// <param name="">cond /// </param> /// <param name="">templ /// </param> /// <param name="">engine /// </param> /// <returns> /// /// </returns> public static Object generateJavaFacts(ObjectCondition cond, Deftemplate templ, Rete.Rete engine) { try { Type theclz = Type.GetType(templ.ClassName); Defclass dfc = engine.findDefclass(theclz); Object data = CreateNewInstance(theclz); IConstraint[] cnstr = cond.Constraints; for (int idx = 0; idx < cnstr.Length; idx++) { IConstraint cn = cnstr[idx]; if (cn is LiteralConstraint) { MethodInfo meth = dfc.getWriteMethod(cn.Name); meth.Invoke(data, (Object[])new Object[] { cn.Value }); } } // for now the method doesn't inspect the bindings // later on it needs to be added return(data); } catch (UnauthorizedAccessException e) { return(null); } catch (ArgumentException e) { return(null); } catch (TargetInvocationException e) { return(null); } catch (Exception e) { return(null); } }
/// <summary> Remove a rule from this module /// </summary> public virtual void removeRule(Rule.IRule rl, Rete engine, IWorkingMemory mem) { rules.Remove(rl.Name); // we should iterate over the nodes of the rule and Remove // them if they are not shared ICondition[] cnds = rl.Conditions; // first Remove the alpha nodes for (int idx = 0; idx < cnds.Length; idx++) { ICondition cnd = cnds[idx]; if (cnd is ObjectCondition) { ObjectCondition oc = (ObjectCondition)cnd; String templ = oc.TemplateName; Deftemplate temp = (Deftemplate)deftemplates.Get(templ); ObjectTypeNode otn = mem.RuleCompiler.getObjectTypeNode(temp); removeAlphaNodes(oc.Nodes, otn); } } // now Remove the betaNodes, since the engine currently // doesn't share the betaNodes, we can just Remove it IList bjl = rl.Joins; for (int idx = 0; idx < bjl.Count; idx++) { BaseJoin bjoin = (BaseJoin)bjl[idx]; ICondition cnd = cnds[idx + 1]; if (cnd is ObjectCondition) { ObjectCondition oc = (ObjectCondition)cnd; String templ = oc.TemplateName; Deftemplate temp = (Deftemplate)deftemplates.Get(templ); ObjectTypeNode otn = mem.RuleCompiler.getObjectTypeNode(temp); otn.removeNode(bjoin); } } }
/// <summary> The first step is to connect the exist join to the parent on the left side. /// The second step is to connect it to the parent on the right. For the right /// side, if the objectCondition doesn't have any nodes, we attach it to the /// objectType node. /// </summary> public void connectJoinNode(ICondition previousCondition, ICondition condition, BaseJoin previousJoinNode, BaseJoin joinNode) { if (previousJoinNode != null) { ruleCompiler.attachJoinNode(previousJoinNode, (BaseJoin)joinNode); } else { ruleCompiler.attachJoinNode(previousCondition.LastNode, (BaseJoin)joinNode); } // Current we have to Add the ExistJoin for the right side, which should be either // an alphaNode or the objectTypeNode ObjectCondition oc = getObjectCondition(condition); ObjectTypeNode otn = ruleCompiler.findObjectTypeNode(oc.TemplateName); if (oc.Nodes.Count > 0) { ruleCompiler.attachJoinNode(oc.LastNode, (BaseJoin)joinNode); } else { otn.addSuccessorNode(joinNode, ruleCompiler.Engine, ruleCompiler.Engine.WorkingMemory); } }
/// <summary> Compile a single ObjectCondition and create the alphaNodes and/or Bindings /// </summary> public override void compile(ICondition condition, int position, Rule.IRule util, bool alphaMemory) { ObjectCondition cond = (ObjectCondition)condition; ObjectTypeNode otn = ruleCompiler.findObjectTypeNode(cond.TemplateName); if (otn != null) { BaseAlpha2 first = null; BaseAlpha2 previous = null; BaseAlpha2 current = null; ITemplate templ = cond.Deftemplate; IConstraint[] constrs = cond.Constraints; for (int idx = 0; idx < constrs.Length; idx++) { IConstraint cnstr = constrs[idx]; if (cnstr is LiteralConstraint) { current = ruleCompiler.compileConstraint((LiteralConstraint)cnstr, templ, util); } else if (cnstr is AndLiteralConstraint) { current = ruleCompiler.compileConstraint((AndLiteralConstraint)cnstr, templ, util); } else if (cnstr is OrLiteralConstraint) { current = ruleCompiler.compileConstraint((OrLiteralConstraint)cnstr, templ, util); } else if (cnstr is BoundConstraint) { ruleCompiler.compileConstraint((BoundConstraint)cnstr, templ, util, position); } else if (cnstr is PredicateConstraint) { current = ruleCompiler.compileConstraint((PredicateConstraint)cnstr, templ, util, position); } // we Add the node to the previous if (first == null) { first = current; previous = current; } else if (current != previous) { try { previous.addSuccessorNode(current, ruleCompiler.Engine, ruleCompiler.Memory); // now set the previous to current previous = current; } catch (AssertException e) { // send an event } } } if (first != null) { attachAlphaNode(otn, first, cond); } } }
/// <summary> the paramList should be clean and /// other codes surrounding this method in subclass may be removed into this method. /// Houzhanbin 10/16/2007 /// </summary> /// <param name="">condition /// </param> /// <param name="">rule /// </param> /// <param name="">Constraints /// </param> /// <param name="">position /// </param> /// <param name="">hasNotEqual /// </param> /// <param name="">hasPredicateJoin /// </param> /// <returns> /// /// </returns> internal Binding[] getBindings(ICondition condition, Rule.IRule rule, int position) { ObjectCondition oc = getObjectCondition(condition); IList Constraints = oc.BindConstraints; Deftemplate tmpl = oc.Deftemplate; Binding[] binds = new Binding[Constraints.Count]; for (int idz = 0; idz < Constraints.Count; idz++) { Object cst = Constraints[idz]; if (cst is BoundConstraint) { BoundConstraint bc = (BoundConstraint)cst; Binding cpy = rule.copyBinding(bc.VariableName); if (cpy.LeftRow >= position) { binds = new Binding[0]; break; } else { binds[idz] = cpy; int rinx = tmpl.getColumnIndex(bc.Name); // we increment the count to make sure the // template isn't removed if it is being used tmpl.incrementColumnUseCount(bc.Name); binds[idz].RightIndex = rinx; binds[idz].Negated = bc.Negated; if (bc.Negated) { oc.HasNotEqual = true; } } } else if (cst is PredicateConstraint) { PredicateConstraint pc = (PredicateConstraint)cst; if (pc.Value is BoundParam) { oc.HasPredicateJoin = true; BoundParam bpm = (BoundParam)pc.Value; String var = bpm.VariableName; int op = ConversionUtils.getOperatorCode(pc.FunctionName); // check and make sure the function isn't user defined if (op != Constants.USERDEFINED) { // if the first binding in the function is from the object type // we reverse the operator if (pc.Parameters[0] != bpm) { op = ConversionUtils.getOppositeOperatorCode(op); } binds[idz] = rule.copyPredicateBinding(var, op); ((Binding2)binds[idz]).RightVariable = pc.VariableName; } else { binds[idz] = rule.copyPredicateBinding(var, op); } binds[idz].PredJoin = true; int rinx = tmpl.getColumnIndex(pc.Name); // we increment the count to make sure the // template isn't removed if it is being used tmpl.incrementColumnUseCount(pc.Name); binds[idz].RightIndex = rinx; } else if (pc.Value is FunctionParam) { // this means there is a nested function } } } return(binds); }
/// <summary> The method uses Defclass, Class, Deftemplate and Rete to create a new /// instance of the java object. Once the instance is created, the method /// uses Defclass to look up the write method and calls it with the /// appropriate value. /// </summary> /// <param name="">cond /// </param> /// <param name="">templ /// </param> /// <param name="">engine /// </param> /// <returns> /// /// </returns> public static Object generateJavaFacts(ObjectCondition cond, Deftemplate templ, Rete.Rete engine) { try { Type theclz = Type.GetType(templ.ClassName); Defclass dfc = engine.findDefclass(theclz); Object data = CreateNewInstance(theclz); IConstraint[] cnstr = cond.Constraints; for (int idx = 0; idx < cnstr.Length; idx++) { IConstraint cn = cnstr[idx]; if (cn is LiteralConstraint) { MethodInfo meth = dfc.getWriteMethod(cn.Name); meth.Invoke(data, (Object[]) new Object[] {cn.Value}); } } // for now the method doesn't inspect the bindings // later on it needs to be added return data; } catch (UnauthorizedAccessException e) { return null; } catch (ArgumentException e) { return null; } catch (TargetInvocationException e) { return null; } catch (Exception e) { return null; } }