private SimpleFBObject GetSimpleObjectPredicatesAndCVTs(string subjectMid, out Dictionary <string, FBObject> nodesInGraph, int maxPerPredicate = int.MaxValue, bool followCVT = true) { SimpleFBObject myself = new SimpleFBObject(); myself.Mid = subjectMid; myself.Name = GetName(subjectMid); Dictionary <string, FBObject> existingNodes = new Dictionary <string, FBObject>(); existingNodes[subjectMid] = myself; myself.Objects = GetPredicatesAndNamedObjectsIncludingCVTs(existingNodes, subjectMid, maxPerPredicate, followCVT); nodesInGraph = existingNodes; return(myself); }
private void FilterToSinglePredicate(SimpleFBObject node, string predicate) { PredicateAndObjects predicateAndObject = null; foreach (PredicateAndObjects p in node.Objects) { if (p.Predicate == predicate) { predicateAndObject = p; break; } } if (predicateAndObject == null) { // Didn't find the predicate node.Objects = new PredicateAndObjects[0]; } node.Objects = new PredicateAndObjects[1] { predicateAndObject }; }
public SimpleFBObject GetSimpleObjectFilteredPredicateAndObjects(string subjectMid, string predicate) { try { logger.Log("GetSimpleObjectFilteredPredicateAndObjects called for subj=" + subjectMid + ", pred=" + predicate); Dictionary <string, FBObject> nodesInGraph; SimpleFBObject initial = GetSimpleObjectPredicatesAndCVTs(subjectMid, out nodesInGraph, int.MaxValue, true); string[] predicateParts = predicate.Split(' '); if (predicateParts.Length < 1 || predicateParts.Length > 2) { return(null); } FilterToSinglePredicate(initial, predicateParts[0]); if (initial.Objects.Length == 0) { return(initial); // Doesn't contain the desired predicate } PredicateAndObjects predicateAndObjects = initial.Objects[0]; if (predicateParts.Length == 2) { foreach (FBObject fbo in predicateAndObjects.Objects) { if (fbo is CVTFBObject) { foreach (PredicateAndObjects poi in (((CVTFBObject)fbo).Objects)) { if (poi.Predicate == predicateParts[1]) { foreach (FBObject fboObj in poi.Objects) { if (fboObj is SimpleFBObject) { SimpleFBObject fboAnswer = (SimpleFBObject)fboObj; if (fboAnswer.Objects == null) { // We need to expand the objects for this node PredicateAndObjects[] resultsForObj = GetPredicatesAndNamedObjectsIncludingCVTs(nodesInGraph, fboAnswer.Mid, int.MaxValue, false); fboAnswer.Objects = resultsForObj; } } } } } } } } else { foreach (FBObject fbo in predicateAndObjects.Objects) { if (fbo is SimpleFBObject) { SimpleFBObject fboAnswer = (SimpleFBObject)fbo; if (fboAnswer.Objects == null) { // We need to expand the objects for this node PredicateAndObjects[] resultsForObj = GetPredicatesAndNamedObjectsIncludingCVTs( nodesInGraph, fboAnswer.Mid, int.MaxValue, false); fboAnswer.Objects = resultsForObj; } } } } return(initial); } catch (Exception e) { logger.LogException("GetSimpleObjectFilteredPredicateAndObjects failed", e); return(null); } }
/// <summary> /// /// </summary> /// <param name="existingNodes"></param> /// <param name="subjectMid"></param> /// <param name="maxPerPredicate"></param> /// <param name="followCVT"></param> /// <returns>Predicates and objects hanging off of subjectMid. We guarantee that each predicate appears only once in the array</returns> private PredicateAndObjects[] GetPredicatesAndNamedObjectsIncludingCVTs( Dictionary <string, FBObject> existingNodes, string subjectMid, int maxPerPredicate = int.MaxValue, bool followCVT = true) { List <Tuple <string, string> > results = GetPredicateObjectPairsForSubject(subjectMid); Dictionary <string, List <FBObject> > predicatesToReturn = new Dictionary <string, List <FBObject> >(); Dictionary <string, int> predicateCountDict = new Dictionary <string, int>(); foreach (Tuple <string, string> pair in results) { string predicate = pair.Item1; string obj = pair.Item2; // Check if the obj type is legit FBNodeType legitObjType, objType; objType = IsCVT(obj) ? FBNodeType.CVT : (IsEntity(obj) ? FBNodeType.Entity : FBNodeType.Value); if (predObjTypeTable.TryGetValue(predicate, out legitObjType) && objType != legitObjType) { continue; } // Check if obj is a ghost MID using the pre-compiled ghost MID table if (objType != FBNodeType.Value && setGhostMid.Contains(obj)) { continue; } // Check if obj is a ghost MID if (1) it's not in the cvtNodes and (2) it does not have an entity name // This may happen because we do not index some tuples because of the predicates are excluded. if (IsEntity(obj) && !IsCVT(obj) && string.IsNullOrEmpty(GetName(obj))) { continue; } // Skip this predicate if we have added it maxPerPredicate times int predicateCount; predicateCountDict.TryGetValue(predicate, out predicateCount); // sets predicateCount to 0 if not in the dictionary if (predicateCount >= maxPerPredicate) { continue; // Skip any more predicates, we've reached our max } predicateCountDict[predicate] = predicateCount + 1; // Get the list of answers we're returning for this predicate List <FBObject> predicateObjects; if (!predicatesToReturn.TryGetValue(predicate, out predicateObjects)) { predicateObjects = new List <FBObject>(); predicatesToReturn[predicate] = predicateObjects; } if (objType == FBNodeType.Entity) { FBObject fbObject; if (!existingNodes.TryGetValue(obj, out fbObject)) { SimpleFBObject simpleFBObject = new SimpleFBObject(); simpleFBObject.Mid = obj; simpleFBObject.Name = GetName(obj); existingNodes[obj] = simpleFBObject; fbObject = simpleFBObject; } predicateObjects.Add(fbObject); } else if (objType == FBNodeType.Value) { ValueFBObject fbObject = new ValueFBObject(); fbObject.Value = obj; predicateObjects.Add(fbObject); } else if (followCVT) // (objType == FBNodeType.CVT) { FBObject fbObject; if (!existingNodes.TryGetValue(obj, out fbObject)) { CVTFBObject cvtFBObject = new CVTFBObject(); cvtFBObject.Mid = obj; cvtFBObject.Objects = GetPredicatesAndNamedObjectsIncludingCVTs(existingNodes, obj, maxPerPredicate, false /* don't follow CVT nodes from this CVT node */); existingNodes[obj] = cvtFBObject; fbObject = cvtFBObject; } predicateObjects.Add(fbObject); } } // Convert to the return type (arrays instead of lists and dictionaries) return (predicatesToReturn.Select( pair => new PredicateAndObjects() { Predicate = pair.Key, Objects = pair.Value.ToArray() }).ToArray()); }