Exemple #1
0
def parseClauses(pathToFile: str) -> List[Clause]:
    '''
    Returns list of clauses parsed from the file.
    
    :param pathToFile: str 
    :return: list of Clause
    '''
    jlist = JSMU.parseClauses(tools.toBytes(pathToFile))
    return [toPython(jlist.get(idx)) for idx in range(0, jlist.size())]
Exemple #2
0
    def __init__(self, path: str):
        med = JSMU.loadMED(tools.toBytes(path), JMatching.THETA_SUBSUMPTION)

        targets = med.getTargets()
        examples = med.getExamples().toArray()

        self.med: JMED = med
        self.samples: List[Sample] = [
            Sample(Interpretation(toPython(examples[idx]).literals),
                   targets[idx] > 0.5) for idx in range(0, med.size())
        ]
    def __init__(self, atom: Atom, positive: bool = True):
        '''
        Creates and returns a new literal from the atom. The literal is negation of the atom if positive is set to False.

        :type atom: Atom
        :type positive: bool
        :rtype: Literal
        '''
        self.lit: JLiteral = JLiteral(toBytes(atom.predicate.name), not positive, toJava(atom.terms))
        self.atom: Atom = atom
        self.positive: bool = positive
def propositionalize(pathToConjunctiveFeatures: str, pathToDataset: str, subsumption="theta") -> DataFrame:
    JSystem.setProperty("ida.searchPruning.propositialization.method", "existential")
    if subsumption.lower() == "theta":
        subsumptionMode = JMatching.THETA_SUBSUMPTION
    elif subsumption.lower() == "oi":
        subsumptionMode = JMatching.OI_SUBSUMPTION
    else:
        raise ValueError('unknown subsumption type:\t{}'.format(subsumption))
    grounded = JSMU.propositionalize(toBytes(pathToConjunctiveFeatures), toBytes(pathToDataset), subsumptionMode)

    with open(pathToConjunctiveFeatures, 'r') as input:
        features = list(
            c for c in list(Clause.parse(line) for line in input.readlines() if len(line.strip()) > 0) if len(c) > 0)

    features.append("class")
    df = DataFrame(columns=features)
    for sampleIdx in range(0, grounded.size()):
        sample = grounded.get(sampleIdx)
        df = df.append({features[idx]: sample.get(idx) for idx in range(0, len(features))}
                       , ignore_index=True)
    return df
def generateMostSpecificSTRangeRestrictedClauses(predicates: Set[Predicate], s: int, t: int, constants=[],
                                                 functors=[]) -> CNF:
    '''
    Returns the most specific FOL CNF given set of predicates, constants, functors and constrains s and t.

    st-clause is such a clause which has at most s literals, each of them is composed by at most t term and predicate symbols.

    Range-restricted clause is such a clause that all of its variables in positive literals occur in some negative literals, e.g. all head's variable occure in body in case of horn clause.

    So 

    :type predicates: set of Predicate
    :type s: int,>=0
    :type t: int,>=0
    :type constants: set of Constant
    :type functors: set of Functor
    :rtype: CNF
    '''
    predicates = list(map(str, predicates))
    functions = list(map(str, constants))
    functions.extend(list(map(str, functors)))

    if len(functors) > 0:
        raise ValueError('So far, functionality for st-range restricted generation with function is not supported.')

    jfunctions = StringTransfer.create()
    for f in functions:
        jfunctions.add(toBytes(toBytes(str(f))))

    jpredicates = StringTransfer.create()
    for p in predicates:
        jpredicates.add(toBytes(str(p)))

    clauses = JSMU.STRangeRestrictedClauses(s, t, jpredicates, jfunctions, s)
    pc = [toPython(clauses.get(idx)) for idx in range(0, clauses.size()) if clauses.get(idx).countLiterals() > 0]
    return CNF(pc)
Exemple #6
0
    def __init__(self, pathToFile: str):
        '''
        Creates new dataset of clauses from path to the file. The clauses can be accessed by .clauses.

        :param pathToFile: str 
        '''
        med = JSMU.loadMED(tools.toBytes(pathToFile),
                           JMatching.THETA_SUBSUMPTION)

        targets = med.getTargets()
        examples = med.getExamples().toArray()

        self.med: JMED = med
        self.samples: List[Sample] = tuple(
            Sample(toPython(examples[idx]), targets[idx] > 0.5)
            for idx in range(0, med.size()))
    def __init__(self, functor: Functor, terms: Iterable[Term]):
        '''
        Creates and returns a new compound term which is constructed by a functor applied to list of arguments.

        :type functor: Functor
        :type terms: iterable of Term   
        :rtype: CompoundTerm
        '''
        terms = tuple(terms)
        if functor.arity != len(terms):
            raise ValueError(
                "functor's arity '{}' is different than arguments given '{}'".format(functor,
                                                                                     ', '.join(map(str, terms))))
        self.func: JFunction = JFunction.parseFunction(
            toBytes("{}({})".format(functor.name, ', '.join(map(str, terms)))))
        self.functor: Functor = functor
        self.terms: Tuple[Term] = terms
 def __init__(self, name: str):
     self.var: JVariable = name if isinstance(name, JVariable) else JVariable.construct(toBytes(str(name)))
 def parse(line: str) -> 'Clause':
     return toPython(JClause.parse(toBytes(line)))
 def __init__(self, name: str):
     self.const: JConstant = name if isinstance(name, JConstant) else JConstant.construct(toBytes(str(name)))
def featureBFS(maxLiterals: int, maxVariables: int, minSupport: int, subsumption: str,
               pathToDataset: str, minutesMax: int) -> List[Clause]:
    JSMU.setProperty("ida.logicStuff.constraints.maxLiterals", toBytes("1"))
    JSMU.setProperty("ida.logicStuff.constraints.minSupport", toBytes("1"))
    JSMU.setProperty("ida.logicStuff.constraints.learner", toBytes("none"))
    JSMU.setProperty("ida.searchPruning.runner.overallLimit", toBytes(str(minutesMax * 60)))
    JSMU.setProperty("ida.searchPruning.mining", toBytes("bfs"))
    JSMU.setProperty("ida.searchPruning.minSupport", toBytes(str(minSupport)))
    JSMU.setProperty("ida.searchPruning.maxDepth", toBytes(str(maxLiterals)))
    JSMU.setProperty("ida.searchPruning.maxVariables", toBytes(str(maxVariables)))
    JSMU.setProperty("ida.searchPruning.storeOutput", toBytes(toBytes("none")))

    # TODO !!! System.setProperty("ida.searchPruning.modeDeclaration", "molecular");
    JSMU.setProperty("ida.searchPruning.datasetSubsumption", toBytes(toBytes(subsumption)))
    JSMU.setProperty("ida.searchPruning.input", toBytes(pathToDataset))

    return toPython(JSMU.bfsFeatureMining())