<commit_before>/*==============================================================================
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
==============================================================================*/
package eu.delving.x3ml.engine;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import eu.delving.x3ml.X3MLEngine;
import java.util.ArrayList;
import java.util.List;
import static eu.delving.x3ml.X3MLEngine.exception;
import static eu.delving.x3ml.engine.X3ML.Additional;
import static eu.delving.x3ml.engine.X3ML.GeneratedValue;
import static eu.delving.x3ml.engine.X3ML.GeneratorElement;
import static eu.delving.x3ml.engine.X3ML.TypeElement;
import gr.forth.Utils;
import java.util.Set;
import java.util.TreeSet;

/**
 * The entity resolver creates the related model elements by calling generator
 * functions.
 * <p/>
 * Handles label nodes and additional nodes with their properties
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class EntityResolver {

    public final ModelOutput modelOutput;
    public final X3ML.EntityElement entityElement;
    public final GeneratorContext generatorContext;
    public List<LabelNode> labelNodes;
    public List<AdditionalNode> additionalNodes;
    public List<Resource> resources;
    public Literal literal;
    private boolean failed;

    EntityResolver(ModelOutput modelOutput, X3ML.EntityElement entityElement, GeneratorContext generatorContext) {
        this.modelOutput = modelOutput;
        this.entityElement = entityElement;
        this.generatorContext = generatorContext;
    }

    /*It takes as input two int numbers, representing the index of the additional node
      or the intermediate node that wants to be resolved. These indexes help us identify 
    the cases where an entity resolve is requested from the link of a mapping and therefore 
    keeping only the xapth input is not enough. We want to also keep the indexes 
    We also use the indexes of the additional or intermediate node - in cases 
    where we have "similar" nodes (with same target entity type). */
    boolean resolve(int additionalNodeIndex, int indermediateNodeIndex) {
        if (entityElement == null) {
            throw exception("Missing entity");
        }
        if (failed) {
            return false;
        }
        if (resources == null) {
            StringBuilder unique = new StringBuilder();
            Set<String> uniqueTypes=new TreeSet<String>();
            for (TypeElement typeElement : entityElement.typeElements) {
                uniqueTypes.add(typeElement.tag);
            }
            for(String str: uniqueTypes){
                unique.append("-").append(str);
            }
            String uniqueValue="";
            if(additionalNodeIndex>0 || indermediateNodeIndex>0){
                if(additionalNodeIndex>0){
                    uniqueValue=unique.toString()+"-additional-"+X3ML.RootElement.linkCounter+"-"+additionalNodeIndex;
                }else{
                    uniqueValue=unique.toString()+"-intermediate-"+X3ML.RootElement.linkCounter+"-"+indermediateNodeIndex;
                }
            }
            GeneratedValue generatedValue = entityElement.getInstance(generatorContext, uniqueValue);
            if (generatedValue == null) {
                failed = true;
                return false;
            }
            switch (generatedValue.type) {
                case URI:
                    if (resources == null) {
                        resources = new ArrayList<Resource>();
                        for (TypeElement typeElement : entityElement.typeElements) {
                            resources.add(modelOutput.createTypedResource(generatedValue.text, typeElement));
                        }
                    }
                    labelNodes = createLabelNodes(entityElement.labelGenerators);
                    additionalNodes = createAdditionalNodes(entityElement.additionals);
                    break;
                case LITERAL:
                    literal = modelOutput.createLiteral(generatedValue.text, generatedValue.language);
                    break;
                case TYPED_LITERAL:
                    if (entityElement.typeElements.size() != 1) {
                        throw new X3MLEngine.X3MLException("Expected one type in\n" + entityElement);
                    }<commit_msg>fixed issue with creation of literal in the same XPATH input (issue #33)<commit_after>/*==============================================================================
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
==============================================================================*/
package eu.delving.x3ml.engine;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import eu.delving.x3ml.X3MLEngine;
import java.util.ArrayList;
import java.util.List;
import static eu.delving.x3ml.X3MLEngine.exception;
import static eu.delving.x3ml.engine.X3ML.Additional;
import static eu.delving.x3ml.engine.X3ML.GeneratedValue;
import static eu.delving.x3ml.engine.X3ML.GeneratorElement;
import static eu.delving.x3ml.engine.X3ML.TypeElement;
import gr.forth.Utils;
import java.util.Set;
import java.util.TreeSet;

/**
 * The entity resolver creates the related model elements by calling generator
 * functions.
 * <p/>
 * Handles label nodes and additional nodes with their properties
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class EntityResolver {

    public final ModelOutput modelOutput;
    public final X3ML.EntityElement entityElement;
    public final GeneratorContext generatorContext;
    public List<LabelNode> labelNodes;
    public List<AdditionalNode> additionalNodes;
    public List<Resource> resources;
    public Literal literal;
    private boolean failed;

    EntityResolver(ModelOutput modelOutput, X3ML.EntityElement entityElement, GeneratorContext generatorContext) {
        this.modelOutput = modelOutput;
        this.entityElement = entityElement;
        this.generatorContext = generatorContext;
    }

    /*It takes as input two int numbers, representing the index of the additional node
      or the intermediate node that wants to be resolved. These indexes help us identify 
    the cases where an entity resolve is requested from the link of a mapping and therefore 
    keeping only the xapth input is not enough. We want to also keep the indexes 
    We also use the indexes of the additional or intermediate node - in cases 
    where we have "similar" nodes (with same target entity type). */
    boolean resolve(int additionalNodeIndex, int indermediateNodeIndex) {
        if (entityElement == null) {
            throw exception("Missing entity");
        }
        if (failed) {
            return false;
        }
        if (resources == null) {
            StringBuilder unique = new StringBuilder();
            Set<String> uniqueTypes=new TreeSet<String>();
            for (TypeElement typeElement : entityElement.typeElements) {
                uniqueTypes.add(typeElement.tag);
            }
            for(String str: uniqueTypes){
                unique.append("-").append(str);
            }
            String uniqueValue="";
            /*If the type is going to be used for an additional or an intermediate node then do not re-use the old one*/
            if(additionalNodeIndex>0 || indermediateNodeIndex>0){
                if(additionalNodeIndex>0){
                    uniqueValue=unique.toString()+"-additional-"+X3ML.RootElement.linkCounter+"-"+additionalNodeIndex;
                }else{
                    uniqueValue=unique.toString()+"-intermediate-"+X3ML.RootElement.linkCounter+"-"+indermediateNodeIndex;
                }
            }
            /*If the type is going to be a Literal value (i.e. a text node), then do not re-use previous instances*/
            if(unique.toString().contains("http://www.w3.org/2000/01/rdf-schema#Literal")){
                uniqueValue="http://www.w3.org/2000/01/rdf-schema#Literal";
            }
            GeneratedValue generatedValue = entityElement.getInstance(generatorContext, uniqueValue);
            if (generatedValue == null) {
                failed = true;
                return false;
            }
            switch (generatedValue.type) {
                case URI:
                    if (resources == null) {
                        resources = new ArrayList<Resource>();
                        for (TypeElement typeElement : entityElement.typeElements) {
                            resources.add(modelOutput.createTypedResource(generatedValue.text, typeElement));
                        }
                    }
                    labelNodes = createLabelNodes(entityElement.labelGenerators);
                    additionalNodes = createAdditionalNodes(entityElement.additionals);
                    break;
                case LITERAL:
                    literal = modelOutput.createLiteral(generatedValue.text, generatedValue.language);
                    break;
                case TYPED_LITERAL:
                    if (entityElement.typeElements.size() != 1) {
                        throw new X3MLEngine.X3MLException("Expected one type in\n" + entityElement);
                    }
{'fraction_non_alphanumeric': 0.10654154016008832, 'fraction_numerical': 0.006440334897414666, 'mean_word_length': 2.6501007387508393, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '133983', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2601, 'n_words': 846}